Skip to content
Open
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
18 changes: 8 additions & 10 deletions lib/internal/repl/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const {
const {
kContextId,
getREPLResourceName,
globalBuiltins,
getGlobalBuiltins,
getReplBuiltinLibs,
fixReplRequire,
} = require('internal/repl/utils');
Expand Down Expand Up @@ -61,13 +61,6 @@ const {
getOwnNonIndexProperties,
} = internalBinding('util');

const {
isIdentifierStart,
isIdentifierChar,
parse: acornParse,
} = require('internal/deps/acorn/acorn/dist/acorn');
const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk');

const importRE = /\bimport\s*\(\s*['"`](([\w@./:-]+\/)?(?:[\w@./:-]*))(?![^'"`])$/;
const requireRE = /\brequire\s*\(\s*['"`](([\w@./:-]+\/)?(?:[\w@./:-]*))(?![^'"`])$/;
const fsAutoCompleteRE = /fs(?:\.promises)?\.\s*[a-z][a-zA-Z]+\(\s*["'](.*)/;
Expand All @@ -87,6 +80,8 @@ function isIdentifier(str) {
if (str === '') {
return false;
}
const { isIdentifierStart, isIdentifierChar } =
require('internal/deps/acorn/acorn/dist/acorn');
const first = StringPrototypeCodePointAt(str, 0);
if (!isIdentifierStart(first)) {
return false;
Expand Down Expand Up @@ -374,8 +369,8 @@ function complete(line, callback) {
if (!this.useGlobal) {
// When the context is not `global`, builtins are not own
// properties of it.
// `globalBuiltins` is a `SafeSet`, not an Array-like.
ArrayPrototypePush(contextOwnNames, ...globalBuiltins);
// `getGlobalBuiltins()` is a `SafeSet`, not an Array-like.
ArrayPrototypePush(contextOwnNames, ...getGlobalBuiltins());
}
ArrayPrototypePush(completionGroups, contextOwnNames);
if (filter !== '') addCommonWords(completionGroups);
Expand All @@ -387,6 +382,7 @@ function complete(line, callback) {
// so in order to make it correct we add an identifier to its end (e.g. `obj.foo.x`)
const parsableCompleteTarget = completeTarget.endsWith('.') ? `${completeTarget}x` : completeTarget;

const { parse: acornParse } = require('internal/deps/acorn/acorn/dist/acorn');
let completeTargetAst;
try {
completeTargetAst = acornParse(
Expand Down Expand Up @@ -551,6 +547,7 @@ function findExpressionCompleteTarget(code) {
return !result ? result : `${result}.`;
}

const { parse: acornParse } = require('internal/deps/acorn/acorn/dist/acorn');
let ast;
try {
ast = acornParse(code, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
Expand Down Expand Up @@ -625,6 +622,7 @@ function findExpressionCompleteTarget(code) {

// Walk the AST for the current block of code, and check whether it contains any
// statement or expression type that would potentially have side effects if evaluated.
const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk');
let isAllowed = true;
const disallow = () => isAllowed = false;
acornWalk.simple(lastBodyStatement, {
Expand Down
16 changes: 10 additions & 6 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ const {
Symbol,
} = primordials;

const { tokTypes: tt, Parser: AcornParser } =
require('internal/deps/acorn/acorn/dist/acorn');

const { sendInspectorCommand } = require('internal/util/inspector');
const { getLazy } = require('internal/util');

const {
ERR_INSPECTOR_NOT_AVAILABLE,
Expand Down Expand Up @@ -80,6 +78,9 @@ function isRecoverableError(e, code) {
isRecoverableError(e, `(${code}`))
return true;

const { tokTypes: tt, Parser: AcornParser } =
require('internal/deps/acorn/acorn/dist/acorn');

let recoverable = false;

// Determine if the point of any error raised is at the end of the input.
Expand Down Expand Up @@ -756,6 +757,8 @@ function setupReverseSearch(repl) {
const startsWithBraceRegExp = /^\s*{/;
const endsWithSemicolonRegExp = /;\s*$/;
function isValidSyntax(input) {
const { Parser: AcornParser } =
require('internal/deps/acorn/acorn/dist/acorn');
try {
AcornParser.parse(input, {
ecmaVersion: 'latest',
Expand Down Expand Up @@ -815,8 +818,9 @@ function getREPLResourceName() {
return `REPL${nextREPLResourceNumber++}`;
}

const globalBuiltins =
new SafeSet(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)'));
// Creating a new context is expensive, so only do it on first use.
const getGlobalBuiltins = getLazy(() =>
new SafeSet(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)')));

let _builtinLibs = ArrayPrototypeFilter(
CJSModule.builtinModules,
Expand Down Expand Up @@ -848,7 +852,7 @@ module.exports = {
isValidSyntax,
kContextId,
getREPLResourceName,
globalBuiltins,
getGlobalBuiltins,
getReplBuiltinLibs,
setReplBuiltinLibs,
fixReplRequire,
Expand Down
10 changes: 4 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ const {
makeRequireFunction,
addBuiltinLibsToObject,
} = require('internal/modules/helpers');
const {
parse: acornParse,
} = require('internal/deps/acorn/acorn/dist/acorn');
const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk');
const {
decorateErrorStack,
isError,
Expand Down Expand Up @@ -153,7 +149,7 @@ const {
isValidSyntax,
kContextId,
getREPLResourceName,
globalBuiltins,
getGlobalBuiltins,
getReplBuiltinLibs,
setReplBuiltinLibs,
fixReplRequire,
Expand Down Expand Up @@ -249,6 +245,8 @@ writer.options = { ...inspect.defaultOptions, showProxy: true };

// Converts static import statement to dynamic import statement
const toDynamicImport = (codeLine) => {
const { parse: acornParse } = require('internal/deps/acorn/acorn/dist/acorn');
const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk');
let dynamicImportStatement = '';
const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
acornWalk.ancestor(ast, {
Expand Down Expand Up @@ -1139,7 +1137,7 @@ class REPLServer extends Interface {
});
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => {
// Only set properties that do not already exist as a global builtin.
if (!globalBuiltins.has(name)) {
if (!getGlobalBuiltins().has(name)) {
ObjectDefineProperty(context, name,
{
__proto__: null,
Expand Down
Loading