Skip to content

repl: fix autocomplete when useGlobal is false #30883

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 13 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const { setImmediate } = require('timers');
// Lazy-loaded.
let processTopLevelAwait;

const globalBuiltins =
new Set(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)'));

const parentModule = module;
const replMap = new WeakMap();
const domainSet = new WeakSet();
Expand Down Expand Up @@ -935,8 +938,8 @@ REPLServer.prototype.createContext = function() {
context = vm.createContext();
});
for (const name of ObjectGetOwnPropertyNames(global)) {
// Only set properties on the context that do not exist as primordial.
if (!(name in primordials)) {
// Only set properties that do not already exist as a global builtin.
if (!globalBuiltins.has(name)) {
ObjectDefineProperty(context, name,
ObjectGetOwnPropertyDescriptor(global, name));
}
Expand Down Expand Up @@ -1283,8 +1286,14 @@ function complete(line, callback) {
completionGroups.push(
filteredOwnPropertyNames.call(this, contextProto));
}
completionGroups.push(
filteredOwnPropertyNames.call(this, this.context));
const contextOwnNames =
filteredOwnPropertyNames.call(this, this.context);
if (!this.useGlobal) {
// When the context is not `global`, builtins are not own
// properties of it.
contextOwnNames.push(...globalBuiltins);
}
completionGroups.push(contextOwnNames);
if (filter !== '') addCommonWords(completionGroups);
completionGroupsLoaded();
} else {
Expand Down