repl: Enable tab completion for global properties · nodejs/node@c39f6c0 · GitHub
Skip to content

Commit c39f6c0

Browse files
lanceFishrock123
authored andcommitted
repl: Enable tab completion for global properties
When `useGlobal` is false, tab completion in the repl does not enumerate global properties. Instead of just setting these properties blindly on the global context, e.g. context[prop] = global[prop] Use `Object.defineProperty` and the property descriptor found on `global` for the new property in `context`. Also addresses a previously unnoticed issue where `console` is writable when `useGlobal` is false. If the binary has been built with `./configure --without-intl` then the `Intl` builtin type will not be available in a repl runtime. Check for this in the test. Fixes: #7353 PR-URL: #7369 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 84dd526 commit c39f6c0

4 files changed

Lines changed: 69 additions & 10 deletions

File tree

lib/repl.js

Lines changed: 24 additions & 10 deletions

test/parallel/test-repl-console.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ assert(r.context.console);
1818

1919
// ensure that the repl console instance is not the global one
2020
assert.notStrictEqual(r.context.console, console);
21+
22+
// ensure that the repl console instance does not have a setter
23+
assert.throws(() => r.context.console = 'foo', TypeError);

test/parallel/test-repl-context.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const repl = require('repl');
5+
6+
// Create a dummy stream that does nothing
7+
const stream = new common.ArrayStream();
8+
9+
// Test when useGlobal is false
10+
testContext(repl.start({
11+
input: stream,
12+
output: stream,
13+
useGlobal: false
14+
}));
15+
16+
function testContext(repl) {
17+
const context = repl.createContext();
18+
// ensure that the repl context gets its own "console" instance
19+
assert(context.console instanceof require('console').Console);
20+
21+
// ensure that the repl's global property is the context
22+
assert(context.global === context);
23+
24+
// ensure that the repl console instance does not have a setter
25+
assert.throws(() => context.console = 'foo');
26+
}

test/parallel/test-repl-tab-complete.js

Lines changed: 16 additions & 0 deletions

0 commit comments

Comments
 (0)