Skip to content

Commit

Permalink
repl: fix tab completion for object properties with special char
Browse files Browse the repository at this point in the history
The old RegExp will pass property names like `"hello world!"`
when filtering the results of tab complete. This change is to
fix it.

Fixes: #21201
  • Loading branch information
starkwang committed Jul 2, 2018
1 parent 484c6c3 commit 4443b61
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const {
makeRequireFunction,
addBuiltinLibsToObject
} = require('internal/modules/cjs/helpers');
const {
isIdentifierStart,
isIdentifierChar
} = require('internal/deps/acorn/dist/acorn');
const internalUtil = require('internal/util');
const { isTypedArray } = require('internal/util/types');
const util = require('util');
Expand Down Expand Up @@ -900,9 +904,25 @@ const requireRE = /\brequire\s*\(['"](([\w@./-]+\/)?(?:[\w@./-]*))/;
const simpleExpressionRE =
/(?:[a-zA-Z_$](?:\w|\$)*\.)*[a-zA-Z_$](?:\w|\$)*\.?$/;

function intFilter(item) {
// filters out anything not starting with A-Z, a-z, $ or _
return /^[A-Za-z_$]/.test(item);
function isIdentifier(str) {
if (str === '') {
return false;
}
const first = str.codePointAt(0);
if (!isIdentifierStart(first)) {
return false;
}
const firstLen = first > 0xffff ? 2 : 1;
for (var i = firstLen; i < str.length; i += 1) {
const cp = str.codePointAt(i);
if (!isIdentifierChar(cp)) {
return false;
}
if (cp > 0xffff) {
i += 1;
}
}
return true;
}

const ARRAY_LENGTH_THRESHOLD = 1e6;
Expand Down Expand Up @@ -932,7 +952,7 @@ function filteredOwnPropertyNames(obj) {

return fakeProperties;
}
return Object.getOwnPropertyNames(obj).filter(intFilter);
return Object.getOwnPropertyNames(obj).filter(isIdentifier);
}

function getGlobalLexicalScopeNames(contextId) {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ putIn.run(['.clear']);
testMe.complete('.b', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [['break'], 'b']);
}));
putIn.run(['.clear']);
putIn.run(['var obj = {"hello, world!": "some string", "key": 123}']);
testMe.complete('obj.', common.mustCall((error, data) => {
assert.strictEqual(data[0].includes('obj.hello, world!'), false);
assert(data[0].includes('obj.key'));
}));

// tab completion for large buffer
const warningRegEx = new RegExp(
Expand Down

0 comments on commit 4443b61

Please sign in to comment.