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: assign underscore fix #3737

Closed
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
4 changes: 4 additions & 0 deletions doc/api/repl.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ The special variable `_` (underscore) contains the result of the last expression
4
```

*NOTE*: Explicitly assigning a value to `_` in the REPL can produce unexpected
results. Also, attempting to create `_` as a `const` variable in the REPL will
fail.

The REPL provides access to any variables in the global scope. You can expose
a variable to the REPL explicitly by assigning it to the `context` object
associated with each `REPLServer`. For example:
Expand Down
6 changes: 6 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,12 @@ REPLServer.prototype.createContext = function() {
});
});

// Refer: https://github.com/nodejs/node/pull/3729#issuecomment-155460861
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of referring to an issue, how about just explaining why we're doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding here as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to @cjihrig's comment. The reference to the issue is unnecessary, I think.

// The REPL stores the result of the last evaluated expression in context's _.
// But, in the REPL, if _ is defined as a const, then it will break the REPL.
// So, we define _ first, so that later redefiniitions will fail.
context._ = undefined;

return context;
};

Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ function error_test() {
});

send_expect([
// Ref: https://github.com/nodejs/node/pull/3729#issuecomment-155460861
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, adding it.

// REPL stores the result of the last evaluated expression in _.
// This test makes sure that _ can not be redefined in REPL.
{ client: client_unix, send: 'const _ = 1',
expect: /^TypeError: Identifier '_' has already been declared/ },
// `_` should still be assignable
{ client: client_unix, send: '_ = 1\n_',
expect: `1\n${prompt_unix}1\n${prompt_unix}` },
// Uncaught error throws and prints out
{ client: client_unix, send: 'throw new Error(\'test error\');',
expect: /^Error: test error/ },
Expand Down