Skip to content

Commit

Permalink
repl: Remove magic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
princejwesley committed Nov 7, 2016
1 parent b315e24 commit 24c9e46
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 39 deletions.
11 changes: 4 additions & 7 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 13 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 @@ -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')
Expand All @@ -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}`;
Expand All @@ -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
Expand Down Expand Up @@ -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);
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

0 comments on commit 24c9e46

Please sign in to comment.