Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: Remove magic mode #7850

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,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`.
Expand Down Expand Up @@ -427,9 +425,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

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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);
Expand Down
29 changes: 12 additions & 17 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -238,7 +234,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')
Expand All @@ -247,7 +243,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}`;
Expand All @@ -258,17 +254,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
Expand Down Expand Up @@ -553,6 +543,11 @@ function REPLServer(prompt,
self.displayPrompt(true);
});

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);
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-repl-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', `
Expand Down
16 changes: 3 additions & 13 deletions test/parallel/test-repl-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-repl-underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function testMagicMode() {
`);

assertOutput(r.output, [
'magic mode is deprecated. Switched to sloppy mode',
'undefined',
'10',
'10',
Expand Down