From bcfa8852b2a94d74d8d545c7c1fc605c4c0ae10d Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 10 Nov 2015 21:47:15 +0530 Subject: [PATCH 1/3] repl: pre-define `_` in REPL's context As `_` is not defined in REPL's context, when it is defined as `const`, it breaks REPL, as it tries to store the result of the last evaluated expression in `_`. This patch makes sure that `_` is pre-defined in REPL's context, so that if users define it again, they will get error. Refer: https://github.com/nodejs/node/pull/3729 Refer: https://github.com/nodejs/node/issues/3704 --- lib/repl.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/repl.js b/lib/repl.js index 7f913c0801738e..01f1f5b552430a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -562,6 +562,12 @@ REPLServer.prototype.createContext = function() { }); }); + // Refer: https://github.com/nodejs/node/pull/3729#issuecomment-155460861 + // 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; }; From c2a37317b1412d6fa433fa71bda15d3024871344 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 10 Nov 2015 21:51:01 +0530 Subject: [PATCH 2/3] test: make sure `_` cannot be defined in REPL If the `_` is redefined as `const` in REPL, it will break the REPL, as REPL will store the result of the last evaluated expression in `_`. This patch has a test to make sure that the REPL doesn't allow redefining `_` as `const`, also still assiging values to `_` is permitted. Refer: https://github.com/nodejs/node/pull/3729 Refer: https://github.com/nodejs/node/issues/3704 --- test/parallel/test-repl.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 539878a9475585..76ac9fced32ccd 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -94,6 +94,14 @@ function error_test() { }); send_expect([ + // Ref: https://github.com/nodejs/node/pull/3729#issuecomment-155460861 + // 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/ }, From 48d79a4e93a9a9a29e968ba8fd272ad114ea06b4 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 10 Nov 2015 15:01:13 +0530 Subject: [PATCH 3/3] doc: warn about reassigning _ in REPL When users assign a value to `_` in REPL, it is prone to unexpected results or messing up with REPL's internals. For example, https://github.com/nodejs/node/issues/3704. This patch issues a warning about the same. --- doc/api/repl.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/api/repl.markdown b/doc/api/repl.markdown index e9bf877e8ac06c..30b65269f5a3c3 100644 --- a/doc/api/repl.markdown +++ b/doc/api/repl.markdown @@ -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: