-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: Assignment of _ allowed with warning #5535
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6cc2713
repl: Assignment of _ allowed with warning
lance 1dd5e2a
repl: Reset underscore assignment on .clear
lance ca199e2
doc: Document `_` behavior in REPL.
lance 4062e62
repl: Adjust docs for `_` usage, and tighten test.
lance 428cef6
repl: Test `_` behavior in `REPL_MODE_MAGIC` mode.
lance 1471d13
repl: Change warning text to a complete sentence.
lance File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const repl = require('repl'); | ||
const stream = require('stream'); | ||
|
||
testSloppyMode(); | ||
testStrictMode(); | ||
testResetContext(); | ||
testMagicMode(); | ||
|
||
function testSloppyMode() { | ||
const r = initRepl(repl.REPL_MODE_SLOPPY); | ||
|
||
// cannot use `let` in sloppy mode | ||
r.write(`_; // initial value undefined | ||
var x = 10; // evaluates to undefined | ||
_; // still undefined | ||
y = 10; // evaluates to 10 | ||
_; // 10 from last eval | ||
_ = 20; // explicitly set to 20 | ||
_; // 20 from user input | ||
_ = 30; // make sure we can set it twice and no prompt | ||
_; // 30 from user input | ||
y = 40; // make sure eval doesn't change _ | ||
_; // remains 30 from user input | ||
`); | ||
|
||
assertOutput(r.output, [ | ||
'undefined', | ||
'undefined', | ||
'undefined', | ||
'10', | ||
'10', | ||
'Expression assignment to _ now disabled.', | ||
'20', | ||
'20', | ||
'30', | ||
'30', | ||
'40', | ||
'30' | ||
]); | ||
} | ||
|
||
function testStrictMode() { | ||
const r = initRepl(repl.REPL_MODE_STRICT); | ||
|
||
r.write(`_; // initial value undefined | ||
var x = 10; // evaluates to undefined | ||
_; // still undefined | ||
let _ = 20; // use 'let' only in strict mode - evals to undefined | ||
_; // 20 from user input | ||
_ = 30; // make sure we can set it twice and no prompt | ||
_; // 30 from user input | ||
var y = 40; // make sure eval doesn't change _ | ||
_; // remains 30 from user input | ||
function f() { let _ = 50; } // undefined | ||
f(); // undefined | ||
_; // remains 30 from user input | ||
`); | ||
|
||
assertOutput(r.output, [ | ||
'undefined', | ||
'undefined', | ||
'undefined', | ||
'undefined', | ||
'20', | ||
'30', | ||
'30', | ||
'undefined', | ||
'30', | ||
'undefined', | ||
'undefined', | ||
'30' | ||
]); | ||
} | ||
|
||
function testMagicMode() { | ||
const r = initRepl(repl.REPL_MODE_MAGIC); | ||
|
||
r.write(`_; // initial value undefined | ||
x = 10; // | ||
_; // last eval - 10 | ||
let _ = 20; // undefined | ||
_; // 20 from user input | ||
_ = 30; // make sure we can set it twice and no prompt | ||
_; // 30 from user input | ||
var y = 40; // make sure eval doesn't change _ | ||
_; // remains 30 from user input | ||
function f() { let _ = 50; return _; } // undefined | ||
f(); // 50 | ||
_; // remains 30 from user input | ||
`); | ||
|
||
assertOutput(r.output, [ | ||
'undefined', | ||
'10', | ||
'10', | ||
'undefined', | ||
'20', | ||
'30', | ||
'30', | ||
'undefined', | ||
'30', | ||
'undefined', | ||
'50', | ||
'30' | ||
]); | ||
} | ||
|
||
function testResetContext() { | ||
const r = initRepl(repl.REPL_MODE_SLOPPY); | ||
|
||
r.write(`_ = 10; // explicitly set to 10 | ||
_; // 10 from user input | ||
.clear // Clearing context... | ||
_; // remains 10 | ||
x = 20; // but behavior reverts to last eval | ||
_; // expect 20 | ||
`); | ||
|
||
assertOutput(r.output, [ | ||
'Expression assignment to _ now disabled.', | ||
'10', | ||
'10', | ||
'Clearing context...', | ||
'10', | ||
'20', | ||
'20' | ||
]); | ||
} | ||
|
||
function initRepl(mode) { | ||
const inputStream = new stream.PassThrough(); | ||
const outputStream = new stream.PassThrough(); | ||
outputStream.accum = ''; | ||
|
||
outputStream.on('data', (data) => { | ||
outputStream.accum += data; | ||
}); | ||
|
||
return repl.start({ | ||
input: inputStream, | ||
output: outputStream, | ||
useColors: false, | ||
terminal: false, | ||
prompt: '', | ||
replMode: mode | ||
}); | ||
} | ||
|
||
function assertOutput(output, expected) { | ||
const lines = output.accum.trim().split('\n'); | ||
assert.deepStrictEqual(lines, expected); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Care to explain why
expression assignment to _ now disabled
is not printed in strict/magic modes? I think I'm missing something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's complicated in this case. The behavior of let is such that the setter on an object is not called in a scenario like this. Because of the way
runInContext
andrunInThisContext
work, the behavior is going to be something similar to this: https://gist.github.com/lance/2b2a6a8286b4bb52ddcf. If you run this script, the output is:Notice that a "Setting 'val'..." message is not printed for when a
let
statement is used.** EDIT **
I have updated the gist with a bunch of comments and examples showing what happens with
let
,this
, andvar
. The behavior in the REPL is, I believe, consistent with what you should expect.