diff --git a/doc/api/repl.md b/doc/api/repl.md index b18488f4218174..99b8c0dbb5d958 100644 --- a/doc/api/repl.md +++ b/doc/api/repl.md @@ -400,13 +400,11 @@ added: v0.1.91 * `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: + all JavaScript commands in strict mode or 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. * `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`. @@ -442,9 +440,8 @@ environment variables: REPL history. Whitespace will be trimmed from the value. - `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. + - `NODE_REPL_MODE` - `sloppy` or `strict` mode. Defaults + to `sloppy`. ### Persistent History diff --git a/lib/internal/repl.js b/lib/internal/repl.js index 7782d6b84bb8fe..be0201e3045c3d 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -44,7 +44,7 @@ function createRepl(env, opts, cb) { }[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 7a735371ffd66a..021078eb336d55 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() { @@ -239,7 +235,7 @@ function REPLServer(prompt, eval_ = eval_ || defaultEval; function defaultEval(code, context, file, cb) { - var err, result, retry = false, input = code, wrappedErr; + var err, result, input = code, wrappedErr; // first, create the Script object to check the syntax if (code === '\n') @@ -248,7 +244,7 @@ function REPLServer(prompt, while (true) { try { if (!/^\s*$/.test(code) && - (self.replMode === exports.REPL_MODE_STRICT || retry)) { + (self.replMode === exports.REPL_MODE_STRICT)) { // "void 0" keeps the repl from returning "use strict" as the // result value for let/const statements. code = `'use strict'; void 0;\n${code}`; @@ -259,17 +255,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 @@ -637,6 +627,12 @@ function REPLServer(prompt, } }; + if (self.replMode === exports.REPL_MODE_MAGIC) { + self.outputStream.write( + 'magic mode is deprecated. Switched to sloppy mode\n'); + self.replMode = exports.REPL_MODE_SLOPPY; + } + self.displayPrompt(); } inherits(REPLServer, Interface); diff --git a/test/parallel/test-repl-mode.js b/test/parallel/test-repl-mode.js index 08b296c2c341a4..2732b9be39f115 100644 --- a/test/parallel/test-repl-mode.js +++ b/test/parallel/test-repl-mode.js @@ -50,10 +50,14 @@ function testStrictMode() { function testAutoMode() { var cli = initRepl(repl.REPL_MODE_MAGIC); + assert.equal(cli.output.accumulator.join(''), + 'magic mode is deprecated. Switched to sloppy mode\n> '); + cli.output.accumulator.length = 0; + cli.input.emit('data', ` x = 3 `.trim() + '\n'); - assert.equal(cli.output.accumulator.join(''), '> 3\n> '); + assert.equal(cli.output.accumulator.join(''), '3\n> '); cli.output.accumulator.length = 0; cli.input.emit('data', ` diff --git a/test/parallel/test-repl-options.js b/test/parallel/test-repl-options.js index 70244802dd0f45..a4360f1b77eec1 100644 --- a/test/parallel/test-repl-options.js +++ b/test/parallel/test-repl-options.js @@ -46,7 +46,8 @@ var r2 = repl.start({ ignoreUndefined: true, eval: evaler, writer: writer, - replMode: repl.REPL_MODE_STRICT + replMode: repl.REPL_MODE_STRICT, + historySize: 50 }); assert.equal(r2.input, stream); assert.equal(r2.output, stream); @@ -65,18 +66,7 @@ assert.equal(r2.rli.output, stream); assert.equal(r2.rli.input, r2.inputStream); assert.equal(r2.rli.output, r2.outputStream); assert.equal(r2.rli.terminal, false); - -// testing out "magic" replMode -var r3 = repl.start({ - input: stream, - output: stream, - writer: writer, - replMode: repl.REPL_MODE_MAGIC, - historySize: 50 -}); - -assert.equal(r3.replMode, repl.REPL_MODE_MAGIC); -assert.equal(r3.historySize, 50); +assert.equal(r2.historySize, 50); // Verify that defaults are used when no arguments are provided const r4 = repl.start(); diff --git a/test/parallel/test-repl-underscore.js b/test/parallel/test-repl-underscore.js index 97fc508ad9eee4..dd6713bf572d95 100644 --- a/test/parallel/test-repl-underscore.js +++ b/test/parallel/test-repl-underscore.js @@ -94,6 +94,7 @@ function testMagicMode() { `); assertOutput(r.output, [ + 'magic mode is deprecated. Switched to sloppy mode', 'undefined', '10', '10',