-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
repl: Enable tab completion for global properties when useGlobal is false #7369
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
Changes from 1 commit
dcaf20b
12902dd
b4d8de7
0991efe
b1981b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
When `useGlobal` is false, all of the properties on `global` are copied to the new context (excluding `console` and `global`). Since `Object.getOwnPropertyNames()` returns all properties, not just enumerable ones, builtin properties on `global` are returned. We need to filter those out.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,16 @@ const debug = util.debuglog('repl'); | |
| const parentModule = module; | ||
| const replMap = new WeakMap(); | ||
|
|
||
| const GLOBAL_OBJECT_PROPERTIES = ['NaN', 'Infinity', 'undefined', | ||
| 'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', | ||
| 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', | ||
| 'Object', 'Function', 'Array', 'String', 'Boolean', 'Number', | ||
| 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', | ||
| 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', | ||
| 'Math', 'JSON']; | ||
| const GLOBAL_OBJECT_PROPERTY_MAP = {}; | ||
| GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p); | ||
|
|
||
| try { | ||
| // hack for require.resolve("./relative") to work properly. | ||
| module.filename = path.resolve('repl'); | ||
|
|
@@ -582,19 +592,20 @@ REPLServer.prototype.createContext = function() { | |
| context = global; | ||
| } else { | ||
| context = vm.createContext(); | ||
| context.global = context; | ||
| context.global.global = context; | ||
| const _console = new Console(this.outputStream); | ||
| Object.getOwnPropertyNames(global).forEach((name) => { | ||
| if (name === 'console') { | ||
| Object.defineProperty(context, name, { | ||
| configurable: true, | ||
| enumerable: true, | ||
| value: _console | ||
| }); | ||
| } | ||
| else { | ||
| Object.defineProperty(context, name, | ||
| Object.getOwnPropertyDescriptor(global, name)); | ||
| } | ||
| Object.defineProperty(context, 'console', { | ||
| configurable: true, | ||
| enumerable: true, | ||
| get: () => _console | ||
| }); | ||
| Object.getOwnPropertyNames(global).filter((name) => { | ||
| if (name === 'console' || name === 'global') return false; | ||
| return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has false positives for entries from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, However, I'm not seeing the false positives from And Do I misunderstand your comment @addaleax?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lance sorry for not following up here… Yes, I’m not thinking about tab completion here, I’m just wondering if these properties should or should not be copied to the new context. |
||
| }).forEach((name) => { | ||
| Object.defineProperty(context, name, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lance
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't figure out the best way to test this programmatically, but the CLI behavior tests manually as expected. I think this is the case because there is no builtin shorthand for |
||
| Object.getOwnPropertyDescriptor(global, name)); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -1062,13 +1073,7 @@ REPLServer.prototype.memory = function memory(cmd) { | |
| function addStandardGlobals(completionGroups, filter) { | ||
| // Global object properties | ||
| // (http://www.ecma-international.org/publications/standards/Ecma-262.htm) | ||
| completionGroups.push(['NaN', 'Infinity', 'undefined', | ||
| 'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', | ||
| 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', | ||
| 'Object', 'Function', 'Array', 'String', 'Boolean', 'Number', | ||
| 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', | ||
| 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', | ||
| 'Math', 'JSON']); | ||
| completionGroups.push(GLOBAL_OBJECT_PROPERTIES); | ||
| // Common keywords. Exclude for completion on the empty string, b/c | ||
| // they just get in the way. | ||
| if (filter) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const repl = require('repl'); | ||
|
|
||
| // Create a dummy stream that does nothing | ||
| const stream = new common.ArrayStream(); | ||
|
|
||
| // Test when useGlobal is false | ||
| testContext(repl.start({ | ||
| input: stream, | ||
| output: stream, | ||
| useGlobal: false | ||
| })); | ||
|
|
||
| // Test when useGlobal is true | ||
| repl.start({ | ||
| input: stream, | ||
| output: stream | ||
| }); | ||
|
|
||
| function testContext(repl) { | ||
| const context = repl.createContext(); | ||
| // ensure that the repl context gets its own "console" instance | ||
| assert(context.console instanceof require('console').Console); | ||
|
|
||
| // ensure that the repl's global property is the context | ||
| assert(context.global === context); | ||
| assert(context.global.global === context); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I’m missing something, but the second line is redundant, right?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, to be honest, I'm not sure why the original
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
|
|
||
| // ensure that the repl console instance does not have a setter | ||
| assert.throws(() => context.console = 'foo'); | ||
| } | ||
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.
I really wish we had a better way of tracking these... ah well.