diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2020-05-29 20:24:06 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-29 12:24:06 +0200 |
commit | 2668637e9bad75bef016e7f8a5f481b3c6221891 (patch) | |
tree | 5c3d2801e7de03e14f6896db402941dc3f2846d2 /cli/js | |
parent | 958f21e7abc36f0a5abaa381ed8d7f94c723f3fb (diff) |
fix: REPL evaluates in strict mode (#5565)
Since everything that Deno loads is treated as an ES Module,
it means that all code is treated as "use strict" except for
when using the REPL. This PR changes that so code in the
REPL is also always evaluated with "use strict". There are
also a couple other places where we load code as scripts
which should also use "use strict" just in case.
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/repl.ts | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/cli/js/repl.ts b/cli/js/repl.ts index 79273ed33..f38324d11 100644 --- a/cli/js/repl.ts +++ b/cli/js/repl.ts @@ -49,11 +49,18 @@ let lastThrownError: Value = undefined; // Returns true if code is consumed (no error/irrecoverable error). // Returns false if error is recoverable function evaluate(code: string): boolean { - const [result, errInfo] = core.evalContext(code); + // each evalContext is a separate function body, and we want strict mode to + // work, so we should ensure that the code starts with "use strict" + const [result, errInfo] = core.evalContext(`"use strict";\n\n${code}`); if (!errInfo) { - lastEvalResult = result; + // when a function is eval'ed with just "use strict" sometimes the result + // is "use strict" which should be discarded + lastEvalResult = + typeof result === "string" && result === "use strict" + ? undefined + : result; if (!isCloseCalled()) { - replLog(result); + replLog(lastEvalResult); } } else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) { // Recoverable compiler error |