-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
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 all commits
dcaf20b
12902dd
b4d8de7
0991efe
b1981b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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,10 +592,20 @@ REPLServer.prototype.createContext = function() { | |
context = global; | ||
} else { | ||
context = vm.createContext(); | ||
for (var i in global) context[i] = global[i]; | ||
context.console = new Console(this.outputStream); | ||
context.global = context; | ||
context.global.global = context; | ||
const _console = new Console(this.outputStream); | ||
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; | ||
|
||
}).forEach((name) => { | ||
Object.defineProperty(context, name, | ||
|
||
Object.getOwnPropertyDescriptor(global, name)); | ||
}); | ||
} | ||
|
||
const module = new Module('<repl>'); | ||
|
@@ -1052,13 +1072,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,26 @@ | ||
'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 | ||
})); | ||
|
||
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); | ||
|
||
// 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.