File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -728,9 +728,7 @@ REPLServer.prototype.createContext = function() {
728728
729729 Object . defineProperty ( context , '_' , {
730730 configurable : true ,
731- get : ( ) => {
732- return this . last ;
733- } ,
731+ get : ( ) => this . last ,
734732 set : ( value ) => {
735733 this . last = value ;
736734 if ( ! this . underscoreAssigned ) {
@@ -1273,9 +1271,10 @@ function defineDefaultCommands(repl) {
12731271 help : 'Print this help message' ,
12741272 action : function ( ) {
12751273 const names = Object . keys ( this . commands ) . sort ( ) ;
1276- const longestNameLength = names . reduce ( ( max , name ) => {
1277- return Math . max ( max , name . length ) ;
1278- } , 0 ) ;
1274+ const longestNameLength = names . reduce (
1275+ ( max , name ) => Math . max ( max , name . length ) ,
1276+ 0
1277+ ) ;
12791278 names . forEach ( ( name ) => {
12801279 const cmd = this . commands [ name ] ;
12811280 const spaces = ' ' . repeat ( longestNameLength - name . length + 3 ) ;
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const stream = require('stream');
88testSloppyMode ( ) ;
99testStrictMode ( ) ;
1010testResetContext ( ) ;
11+ testResetContextGlobal ( ) ;
1112testMagicMode ( ) ;
1213
1314function testSloppyMode ( ) {
@@ -131,7 +132,28 @@ function testResetContext() {
131132 ] ) ;
132133}
133134
134- function initRepl ( mode ) {
135+ function testResetContextGlobal ( ) {
136+ const r = initRepl ( repl . REPL_MODE_STRICT , true ) ;
137+
138+ r . write ( `_ = 10; // explicitly set to 10
139+ _; // 10 from user input
140+ .clear // No output because useGlobal is true
141+ _; // remains 10
142+ ` ) ;
143+
144+ assertOutput ( r . output , [
145+ 'Expression assignment to _ now disabled.' ,
146+ '10' ,
147+ '10' ,
148+ '10' ,
149+ ] ) ;
150+
151+ // delete globals leaked by REPL when `useGlobal` is `true`
152+ delete global . module ;
153+ delete global . require ;
154+ }
155+
156+ function initRepl ( mode , useGlobal ) {
135157 const inputStream = new stream . PassThrough ( ) ;
136158 const outputStream = new stream . PassThrough ( ) ;
137159 outputStream . accum = '' ;
@@ -146,7 +168,8 @@ function initRepl(mode) {
146168 useColors : false ,
147169 terminal : false ,
148170 prompt : '' ,
149- replMode : mode
171+ replMode : mode ,
172+ useGlobal : useGlobal
150173 } ) ;
151174}
152175
You can’t perform that action at this time.
0 commit comments