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: Enable tab completion for global properties when useGlobal is false #7369

Closed
wants to merge 5 commits into from
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
34 changes: 24 additions & 10 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const debug = util.debuglog('repl');
const parentModule = module;
const replMap = new WeakMap();

const GLOBAL_OBJECT_PROPERTIES = ['NaN', 'Infinity', 'undefined',
Copy link
Member

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.

'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');
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

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

This has false positives for entries from Object.prototype, e.g. for name === 'constructor'. I’m not sure if that’s actually relevant, but this line should be read as equivalent to GLOBAL_OBJECT_PROPERTIES.indexOf(name) === -1, right?

Copy link
Member Author

@lance lance Jun 24, 2016

Choose a reason for hiding this comment

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

Yes, GLOBAL_OBJECT_PROPERTY_MAP is just a map used for convenience and to avoid having to scan the array each time for a lookup on name. So, GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined is essentially equivalent to GLOBAL_OBJECT_PROPERTIES.indexOf(name) === -1.

However, I'm not seeing the false positives from Object.prototype that you mention. A tab completion does exist for constructor, but that is the case for node version 6.2.2 as well.

~ ❯❯❯ which node
/Users/lanceball/.nvm/versions/node/v6.2.2/bin/node
~ ❯❯❯ node
> con
const        continue

console

constructor

> con

And constructor doesn't appear in global's own property names.

> Object.getOwnPropertyNames(global).filter((name) => name === 'constructor');
[] 

Do I misunderstand your comment @addaleax?

Copy link
Member

Choose a reason for hiding this comment

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

@lance sorry for not following up here…

Yes, global has no “own” constructor property unless set by user code, but GLOBAL_OBJECT_PROPERTY_MAP['constructor'] will be truthy whereas GLOBAL_OBJECT_PROPERTIES.indexOf('constructor') === -1 will not, so these are in fact not equivalent.

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,
Copy link
Contributor

Choose a reason for hiding this comment

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

@lance instanceof test for this filtered list may fail!. e.g Int16Array. Can you confirm that? Adding those missing properties to GLOBAL_OBJECT_PROPERTIES will solve this issue.

Copy link
Member Author

@lance lance Jun 23, 2016

Choose a reason for hiding this comment

The 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.

~/s/node git:repl-useglobal-false-tab-completion ❯❯❯ ./node test-use-global-auto-complete.js
> const x = new Int16Array()
undefined
> x instanceof Int16Array
true
>

I think this is the case because there is no builtin shorthand for Int16Array and the like. For builtin objects that have a shorthand representation such as [] or /\w+/ that representation is an instanceofthe builtin language object no matter what. But there is no builtin shorthand for Int16Array.

Object.getOwnPropertyDescriptor(global, name));
});
}

const module = new Module('<repl>');
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-repl-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ assert(r.context.console);

// ensure that the repl console instance is not the global one
assert.notStrictEqual(r.context.console, console);

// ensure that the repl console instance does not have a setter
assert.throws(() => r.context.console = 'foo', TypeError);
26 changes: 26 additions & 0 deletions test/parallel/test-repl-context.js
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');
}
16 changes: 16 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,19 @@ putIn.run(['.clear']);
testMe.complete('.b', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [['break'], 'b']);
}));

const testNonGlobal = repl.start({
input: putIn,
output: putIn,
useGlobal: false
});

const builtins = [['Infinity', '', 'Int16Array', 'Int32Array',
'Int8Array'], 'I'];

if (typeof Intl === 'object') {
builtins[0].push('Intl');
}
testNonGlobal.complete('I', common.mustCall((error, data) => {
assert.deepStrictEqual(data, builtins);
}));