From 6b1d020670946259ab171a8b22d0df293dbc4f3e Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Mon, 27 Feb 2017 17:57:44 -0800 Subject: [PATCH 1/2] repl: remove magic mode semantics --- lib/repl.js | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 60b68dc05b4c6a..3db04b7718ab20 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -78,10 +78,6 @@ exports.writer = util.inspect; exports._builtinLibs = internalModule.builtinLibs; -const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, const, function, ' + - 'class) not yet supported outside strict mode'; - - class LineParser { constructor() { @@ -266,7 +262,6 @@ function REPLServer(prompt, code = code.replace(/\n$/, ''); code = preprocess(code); - var retry = false; var input = code; var err, result, wrappedErr; // first, create the Script object to check the syntax @@ -277,9 +272,9 @@ function REPLServer(prompt, while (true) { try { if (!/^\s*$/.test(code) && - (self.replMode === exports.REPL_MODE_STRICT || retry)) { - // "void 0" keeps the repl from returning "use strict" as the - // result value for let/const statements. + self.replMode === exports.REPL_MODE_STRICT) { + // "void 0" keeps the repl from returning "use strict" as the result + // value for statements and declarations that don't return a value. code = `'use strict'; void 0;\n${code}`; } var script = vm.createScript(code, { @@ -288,17 +283,11 @@ function REPLServer(prompt, }); } catch (e) { debug('parse error %j', code, e); - if (self.replMode === exports.REPL_MODE_MAGIC && - e.message === BLOCK_SCOPED_ERROR && - !retry || self.wrappedCmd) { - if (self.wrappedCmd) { - self.wrappedCmd = false; - // unwrap and try again - code = `${input.substring(1, input.length - 2)}\n`; - wrappedErr = e; - } else { - retry = true; - } + if (self.wrappedCmd) { + self.wrappedCmd = false; + // unwrap and try again + code = `${input.substring(1, input.length - 2)}\n`; + wrappedErr = e; continue; } // preserve original error for wrapped command From 44b640eb7db1393903cf439eb48031e6e5e59b4b Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Mon, 27 Feb 2017 18:45:53 -0800 Subject: [PATCH 2/2] repl: docs-only deprecation of magic mode --- doc/api/deprecations.md | 14 ++++++++++++++ doc/api/repl.md | 15 ++++++++------- lib/internal/repl.js | 5 ++--- lib/repl.js | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 71f846b8b613eb..f4743e0db21dab 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -534,6 +534,20 @@ deprecated. Please use `ServerResponse.prototype.writeHead()` instead. *Note*: The `ServerResponse.prototype.writeHeader()` method was never documented as an officially supported API. + +### DEP00XX: repl.REPL_MODE_MAGIC and NODE_REPL_MODE=magic + +Type: Documentation-only + +The `repl` module's `REPL_MODE_MAGIC` constant, used for `replMode` option, has +been deprecated. Its behavior has been functionally identical to that of +`REPL_MODE_SLOPPY` since Node.js v6.0.0, when V8 5.0 was imported. Please use +`REPL_MODE_SLOPPY` instead. + +The `NODE_REPL_MODE` environment variable is used to set the underlying +`replMode` of an interactive `node` session. Its default value, `magic`, is +similarly deprecated in favor of `sloppy`. + [alloc]: buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding [alloc_unsafe_size]: buffer.html#buffer_class_method_buffer_allocunsafe_size [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size diff --git a/doc/api/repl.md b/doc/api/repl.md index f4d85e11c96b3b..19b00b85159b84 100644 --- a/doc/api/repl.md +++ b/doc/api/repl.md @@ -408,14 +408,15 @@ changes: command before writing to `output`. Defaults to [`util.inspect()`][]. * `completer` {Function} An optional function used for custom Tab auto completion. See [`readline.InterfaceCompleter`][] for an example. - * `replMode` - A flag that specifies whether the default evaluator executes - all JavaScript commands in strict mode, default mode, or a hybrid mode - ("magic" mode.) Acceptable values are: + * `replMode` {symbol} A flag that specifies whether the default evaluator + executes all JavaScript commands in strict mode or default (sloppy) mode. + Acceptable values are: * `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode. * `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to prefacing every repl statement with `'use strict'`. - * `repl.REPL_MODE_MAGIC` - attempt to evaluates expressions in default - mode. If expressions fail to parse, re-try in strict mode. + * `repl.REPL_MODE_MAGIC` - This value is **deprecated**, since enhanced + spec compliance in V8 has rendered magic mode unnecessary. It is now + equivalent to `repl.REPL_MODE_SLOPPY` (documented above). * `breakEvalOnSigint` - Stop evaluating the current piece of code when `SIGINT` is received, i.e. `Ctrl+C` is pressed. This cannot be used together with a custom `eval` function. Defaults to `false`. @@ -461,8 +462,8 @@ environment variables: - `NODE_REPL_HISTORY_SIZE` - Defaults to `1000`. Controls how many lines of history will be persisted if history is available. Must be a positive number. - `NODE_REPL_MODE` - May be any of `sloppy`, `strict`, or `magic`. Defaults - to `magic`, which will automatically run "strict mode only" statements in - strict mode. + to `sloppy`, which will allow non-strict mode code to be run. `magic` is + **deprecated** and treated as an alias of `sloppy`. ### Persistent History diff --git a/lib/internal/repl.js b/lib/internal/repl.js index 7782d6b84bb8fe..d81f56f5c96d33 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -39,12 +39,11 @@ function createRepl(env, opts, cb) { opts.replMode = { 'strict': REPL.REPL_MODE_STRICT, - 'sloppy': REPL.REPL_MODE_SLOPPY, - 'magic': REPL.REPL_MODE_MAGIC + 'sloppy': REPL.REPL_MODE_SLOPPY }[String(env.NODE_REPL_MODE).toLowerCase().trim()]; if (opts.replMode === undefined) { - opts.replMode = REPL.REPL_MODE_MAGIC; + opts.replMode = REPL.REPL_MODE_SLOPPY; } const historySize = Number(env.NODE_REPL_HISTORY_SIZE); diff --git a/lib/repl.js b/lib/repl.js index 3db04b7718ab20..7d50cc6bf5201c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -633,7 +633,7 @@ exports.REPLServer = REPLServer; exports.REPL_MODE_SLOPPY = Symbol('repl-sloppy'); exports.REPL_MODE_STRICT = Symbol('repl-strict'); -exports.REPL_MODE_MAGIC = Symbol('repl-magic'); +exports.REPL_MODE_MAGIC = exports.REPL_MODE_SLOPPY; // prompt is a string to print on each line for the prompt, // source is a stream to use for I/O, defaulting to stdin/stdout.