From 3bb9b4ede740a377e19a5af21c608eeca570929a Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Sat, 27 May 2023 07:17:38 -0700 Subject: [PATCH 01/33] repl: display dynamic import variant in static import error messages Enhance the REPL message for static import error message. ``` > import {foo, bar} from 'moo'; import {foo, bar} from 'moo'; ^^^^^^ Uncaught: SyntaxError: .* dynamic import: const {foo,bar} = await import('moo'); ``` --- lib/repl.js | 113 +++++++++++++++++++++++-------------- test/parallel/test-repl.js | 36 +++++++++++- 2 files changed, 106 insertions(+), 43 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index df038901d7834a..368d442c3b6e8d 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -58,6 +58,7 @@ const { ArrayPrototypeSome, ArrayPrototypeSort, ArrayPrototypeSplice, + ArrayPrototypeToString, ArrayPrototypeUnshift, Boolean, Error, @@ -104,7 +105,12 @@ const { const { isIdentifierStart, isIdentifierChar, + parse: acornParse, } = require('internal/deps/acorn/acorn/dist/acorn'); + + +const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk'); + const { decorateErrorStack, isError, @@ -223,6 +229,29 @@ module.paths = CJSModule._nodeModulePaths(module.filename); const writer = (obj) => inspect(obj, writer.options); writer.options = { ...inspect.defaultOptions, showProxy: true }; +// Converts static import statement to dynamic import statement +const toDynamicImport = (codeLine) => { + let dynamicImportStatement = ''; + let moduleName = ''; + const toCamelCase = (str) => str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase()); + const ast = acornParse(codeLine, { sourceType: 'module', ecmaVersion: 'latest' }); + acornWalk.ancestor(ast, { + ImportDeclaration: (node) => { + const importedModules = node.source.value; + const importedSpecifiers = node.specifiers.map((specifier) => specifier.local.name); + if (importedSpecifiers.length > 1) { + moduleName = `{${importedSpecifiers.join(',')}}`; + } else { + const formattedSpecifiers = importedSpecifiers.length ? ArrayPrototypeToString(importedSpecifiers) : ''; + moduleName = toCamelCase(formattedSpecifiers || importedModules); + } + dynamicImportStatement += `const ${moduleName} = await import('${importedModules}');`; + }, + }); + return dynamicImportStatement; +}; + + function REPLServer(prompt, stream, eval_, @@ -283,13 +312,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.input, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.input, set: pendingDeprecation ? deprecate((val) => this.input = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.input = val, enumerable: false, @@ -300,13 +329,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.output, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.output, set: pendingDeprecation ? deprecate((val) => this.output = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.output = val, enumerable: false, @@ -344,9 +373,9 @@ function REPLServer(prompt, // instance and that could trigger the `MaxListenersExceededWarning`. process.prependListener('newListener', (event, listener) => { if (event === 'uncaughtException' && - process.domain && - listener.name !== 'domainUncaughtExceptionClear' && - domainSet.has(process.domain)) { + process.domain && + listener.name !== 'domainUncaughtExceptionClear' && + domainSet.has(process.domain)) { // Throw an error so that the event will not be added and the current // domain takes over. That way the user is notified about the error // and the current code evaluation is stopped, just as any other code @@ -363,8 +392,8 @@ function REPLServer(prompt, const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; const sep = '\u0000\u0000\u0000'; const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)$`); + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)$`); eval_ = eval_ || defaultEval; @@ -417,7 +446,7 @@ function REPLServer(prompt, // an expression. Note that if the above condition changes, // lib/internal/repl/utils.js needs to be changed to match. if (RegExpPrototypeExec(/^\s*{/, code) !== null && - RegExpPrototypeExec(/;\s*$/, code) === null) { + RegExpPrototypeExec(/;\s*$/, code) === null) { code = `(${StringPrototypeTrim(code)})\n`; wrappedCmd = true; } @@ -492,7 +521,7 @@ function REPLServer(prompt, while (true) { try { if (self.replMode === module.exports.REPL_MODE_STRICT && - RegExpPrototypeExec(/^\s*$/, code) === null) { + RegExpPrototypeExec(/^\s*$/, code) === null) { // "void 0" keeps the repl from returning "use strict" as the result // value for statements and declarations that don't return a value. code = `'use strict'; void 0;\n${code}`; @@ -684,7 +713,7 @@ function REPLServer(prompt, 'module'; if (StringPrototypeIncludes(e.message, importErrorStr)) { e.message = 'Cannot use import statement inside the Node.js ' + - 'REPL, alternatively use dynamic import'; + 'REPL, alternatively use dynamic import: ' + toDynamicImport(self.lines.at(-1)); e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( /SyntaxError:.*\n/, e.stack, @@ -712,7 +741,7 @@ function REPLServer(prompt, } if (options[kStandaloneREPL] && - process.listenerCount('uncaughtException') !== 0) { + process.listenerCount('uncaughtException') !== 0) { process.nextTick(() => { process.emit('uncaughtException', e); self.clearBufferedCommand(); @@ -729,7 +758,7 @@ function REPLServer(prompt, errStack = ''; ArrayPrototypeForEach(lines, (line) => { if (!matched && - RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { + RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { errStack += writer.options.breakLength >= line.length ? `Uncaught ${line}` : `Uncaught:\n${line}`; @@ -875,8 +904,8 @@ function REPLServer(prompt, // display next prompt and return. if (trimmedCmd) { if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && - StringPrototypeCharAt(trimmedCmd, 1) !== '.' && - NumberIsNaN(NumberParseFloat(trimmedCmd))) { + StringPrototypeCharAt(trimmedCmd, 1) !== '.' && + NumberIsNaN(NumberParseFloat(trimmedCmd))) { const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); const keyword = matches && matches[1]; const rest = matches && matches[2]; @@ -901,10 +930,10 @@ function REPLServer(prompt, ReflectApply(_memory, self, [cmd]); if (e && !self[kBufferedCommandSymbol] && - StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { + StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { self.output.write('npm should be run outside of the ' + - 'Node.js REPL, in your normal shell.\n' + - '(Press Ctrl+D to exit.)\n'); + 'Node.js REPL, in your normal shell.\n' + + '(Press Ctrl+D to exit.)\n'); self.displayPrompt(); return; } @@ -929,11 +958,11 @@ function REPLServer(prompt, // If we got any output - print it (if no error) if (!e && - // When an invalid REPL command is used, error message is printed - // immediately. We don't have to print anything else. So, only when - // the second argument to this function is there, print it. - arguments.length === 2 && - (!self.ignoreUndefined || ret !== undefined)) { + // When an invalid REPL command is used, error message is printed + // immediately. We don't have to print anything else. So, only when + // the second argument to this function is there, print it. + arguments.length === 2 && + (!self.ignoreUndefined || ret !== undefined)) { if (!self.underscoreAssigned) { self.last = ret; } @@ -984,7 +1013,7 @@ function REPLServer(prompt, if (!self.editorMode || !self.terminal) { // Before exiting, make sure to clear the line. if (key.ctrl && key.name === 'd' && - self.cursor === 0 && self.line.length === 0) { + self.cursor === 0 && self.line.length === 0) { self.clearLine(); } clearPreview(key); @@ -1181,7 +1210,7 @@ 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*["'](.*)/; const simpleExpressionRE = - /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; + /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; const versionedFileNamesRe = /-\d+\.\d+/; function isIdentifier(str) { @@ -1337,7 +1366,7 @@ function complete(line, callback) { const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; ArrayPrototypeForEach(dirents, (dirent) => { if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null || - dirent.name === '.npm') { + dirent.name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1345,7 +1374,7 @@ function complete(line, callback) { const base = StringPrototypeSlice(dirent.name, 0, -extension.length); if (!dirent.isDirectory()) { if (StringPrototypeIncludes(extensions, extension) && - (!subdir || base !== 'index')) { + (!subdir || base !== 'index')) { ArrayPrototypePush(group, `${subdir}${base}`); } return; @@ -1398,7 +1427,7 @@ function complete(line, callback) { ArrayPrototypeForEach(dirents, (dirent) => { const { name } = dirent; if (RegExpPrototypeExec(versionedFileNamesRe, name) !== null || - name === '.npm') { + name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1431,20 +1460,20 @@ function complete(line, callback) { ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); } else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null && - this.allowBlockingCompletions) { + this.allowBlockingCompletions) { ({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match)); - // Handle variable member lookup. - // We support simple chained expressions like the following (no function - // calls, etc.). That is for simplicity and also because we *eval* that - // leading expression so for safety (see WARNING above) don't want to - // eval function calls. - // - // foo.bar<|> # completions for 'foo' with filter 'bar' - // spam.eggs.<|> # completions for 'spam.eggs' with filter '' - // foo<|> # all scope vars with filter 'foo' - // foo.<|> # completions for 'foo' with filter '' + // Handle variable member lookup. + // We support simple chained expressions like the following (no function + // calls, etc.). That is for simplicity and also because we *eval* that + // leading expression so for safety (see WARNING above) don't want to + // eval function calls. + // + // foo.bar<|> # completions for 'foo' with filter 'bar' + // spam.eggs.<|> # completions for 'spam.eggs' with filter '' + // foo<|> # all scope vars with filter 'foo' + // foo.<|> # completions for 'foo' with filter '' } else if (line.length === 0 || - RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { + RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { const { 0: match } = RegExpPrototypeExec(simpleExpressionRE, line) || ['']; if (line.length !== 0 && !match) { completionGroupsLoaded(); @@ -1495,7 +1524,7 @@ function complete(line, callback) { try { let p; if ((typeof obj === 'object' && obj !== null) || - typeof obj === 'function') { + typeof obj === 'function') { memberGroups.push(filteredOwnPropertyNames(obj)); p = ObjectGetPrototypeOf(obj); } else { diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 4981816151f55b..4fc6ebb6fc4b54 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -818,7 +818,41 @@ const tcpTests = [ kArrow, '', 'Uncaught:', - /^SyntaxError: .* dynamic import/, + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: const comeOn = await import(\'fhqwhgads\');', + ] + }, + { + send: 'import { export1, export2 } from "module-name"', + expect: [ + kSource, + kArrow, + '', + 'Uncaught:', + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: const {export1,export2} = await import(\'module-name\');', + ] + }, + { + send: 'import * as name from "module-name";', + expect: [ + kSource, + kArrow, + '', + 'Uncaught:', + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: const name = await import(\'module-name\');', + ] + }, + { + send: 'import "module-name";', + expect: [ + kSource, + kArrow, + '', + 'Uncaught:', + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: const moduleName = await import(\'module-name\');', ] }, ]; From 653dd2b971a72ac9c6c8ab6901f356cb6294503b Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Tue, 30 May 2023 21:38:27 -0700 Subject: [PATCH 02/33] fix: handle edge cases for toDynamicImport --- lib/repl.js | 16 +++++++++------- test/parallel/test-repl.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 368d442c3b6e8d..82990f66949189 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -58,7 +58,6 @@ const { ArrayPrototypeSome, ArrayPrototypeSort, ArrayPrototypeSplice, - ArrayPrototypeToString, ArrayPrototypeUnshift, Boolean, Error, @@ -233,17 +232,21 @@ writer.options = { ...inspect.defaultOptions, showProxy: true }; const toDynamicImport = (codeLine) => { let dynamicImportStatement = ''; let moduleName = ''; - const toCamelCase = (str) => str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase()); const ast = acornParse(codeLine, { sourceType: 'module', ecmaVersion: 'latest' }); + const toCamelCase = (str) => str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase()); acornWalk.ancestor(ast, { - ImportDeclaration: (node) => { + ImportDeclaration(node) { const importedModules = node.source.value; - const importedSpecifiers = node.specifiers.map((specifier) => specifier.local.name); + const importedSpecifiers = node.specifiers.map((specifier) => { + if (specifier.local.name === specifier?.imported?.name) { + return specifier.local.name; + } + return `${specifier?.imported?.name ? specifier.imported.name + ':' : ''}${specifier.local.name}`; + }); if (importedSpecifiers.length > 1) { moduleName = `{${importedSpecifiers.join(',')}}`; } else { - const formattedSpecifiers = importedSpecifiers.length ? ArrayPrototypeToString(importedSpecifiers) : ''; - moduleName = toCamelCase(formattedSpecifiers || importedModules); + moduleName = toCamelCase(importedSpecifiers.length ? importedSpecifiers.toString() : importedModules); } dynamicImportStatement += `const ${moduleName} = await import('${importedModules}');`; }, @@ -251,7 +254,6 @@ const toDynamicImport = (codeLine) => { return dynamicImportStatement; }; - function REPLServer(prompt, stream, eval_, diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 4fc6ebb6fc4b54..eab5f603e92af9 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -855,6 +855,17 @@ alternatively use dynamic import: const name = await import(\'module-name\');', alternatively use dynamic import: const moduleName = await import(\'module-name\');', ] }, + { + send: 'import { export1 as localName1, export2 } from "bar";', + expect: [ + kSource, + kArrow, + '', + 'Uncaught:', + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: const {export1:localName1,export2} = await import("bar");', + ] + }, ]; (async function() { From 65aff1f9ac6b19ccd61bb22d744a560073202809 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 12:23:31 -0700 Subject: [PATCH 03/33] use primordials Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 82990f66949189..6b7b51954491d9 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -233,7 +233,7 @@ const toDynamicImport = (codeLine) => { let dynamicImportStatement = ''; let moduleName = ''; const ast = acornParse(codeLine, { sourceType: 'module', ecmaVersion: 'latest' }); - const toCamelCase = (str) => str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase()); + const toCamelCase = (str) => RegexpPrototypeSymbolReplace(/[-_](\w)/g, str, (_, c) => StringPrototypeToUpperCase(c)); acornWalk.ancestor(ast, { ImportDeclaration(node) { const importedModules = node.source.value; From d2891e17813a57c51ee16faec84bb468d902de62 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 12:23:51 -0700 Subject: [PATCH 04/33] __proto__ to null Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 6b7b51954491d9..70c29d284eb872 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -232,7 +232,7 @@ writer.options = { ...inspect.defaultOptions, showProxy: true }; const toDynamicImport = (codeLine) => { let dynamicImportStatement = ''; let moduleName = ''; - const ast = acornParse(codeLine, { sourceType: 'module', ecmaVersion: 'latest' }); + const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' }); const toCamelCase = (str) => RegexpPrototypeSymbolReplace(/[-_](\w)/g, str, (_, c) => StringPrototypeToUpperCase(c)); acornWalk.ancestor(ast, { ImportDeclaration(node) { From 075295892dd800b7a4dfb7212e23541894599556 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 12:25:00 -0700 Subject: [PATCH 05/33] use primordials Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 70c29d284eb872..bed65a4c7a4b39 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -237,7 +237,7 @@ const toDynamicImport = (codeLine) => { acornWalk.ancestor(ast, { ImportDeclaration(node) { const importedModules = node.source.value; - const importedSpecifiers = node.specifiers.map((specifier) => { + const importedSpecifiers = ArrayPrototypeMap(node.specifiers, (specifier) => { if (specifier.local.name === specifier?.imported?.name) { return specifier.local.name; } From 183761477fb1ef990f0a500ba4aa43729fd8c271 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 12:25:20 -0700 Subject: [PATCH 06/33] use primordials Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index bed65a4c7a4b39..25f9fa23536a67 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -244,7 +244,7 @@ const toDynamicImport = (codeLine) => { return `${specifier?.imported?.name ? specifier.imported.name + ':' : ''}${specifier.local.name}`; }); if (importedSpecifiers.length > 1) { - moduleName = `{${importedSpecifiers.join(',')}}`; + moduleName = `{${ArrayPrototypeJoin(importedSpecifiers, ',')}}`; } else { moduleName = toCamelCase(importedSpecifiers.length ? importedSpecifiers.toString() : importedModules); } From 16ea5fca691e256d6520492ef0ccb013d1bc5035 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 12:25:43 -0700 Subject: [PATCH 07/33] use primordials Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 25f9fa23536a67..0313a6c9fd358c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -246,7 +246,7 @@ const toDynamicImport = (codeLine) => { if (importedSpecifiers.length > 1) { moduleName = `{${ArrayPrototypeJoin(importedSpecifiers, ',')}}`; } else { - moduleName = toCamelCase(importedSpecifiers.length ? importedSpecifiers.toString() : importedModules); + moduleName = toCamelCase(importedSpecifiers.length ? `${ArrayPrototypeJoin(importedSpecifiers, '')}` : importedModules); } dynamicImportStatement += `const ${moduleName} = await import('${importedModules}');`; }, From b1b6a8abf6a356a41bea449ece8d1a5f41f471b6 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 12:27:22 -0700 Subject: [PATCH 08/33] fix: quotes in test --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index eab5f603e92af9..844c9e7ad5137f 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -863,7 +863,7 @@ alternatively use dynamic import: const moduleName = await import(\'module-name\ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const {export1:localName1,export2} = await import("bar");', +alternatively use dynamic import: const {export1:localName1,export2} = await import(\'bar\');', ] }, ]; From 3f08ac7c1ee20024f9d9a9da7cbe33aae5a2e29b Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Wed, 31 May 2023 14:35:12 -0700 Subject: [PATCH 09/33] lint: space fix --- lib/repl.js | 87 ++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 0313a6c9fd358c..e1ac127f932cf9 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -106,10 +106,7 @@ const { isIdentifierChar, parse: acornParse, } = require('internal/deps/acorn/acorn/dist/acorn'); - - const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk'); - const { decorateErrorStack, isError, @@ -314,13 +311,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.input, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.input, set: pendingDeprecation ? deprecate((val) => this.input = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.input = val, enumerable: false, @@ -331,13 +328,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.output, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.output, set: pendingDeprecation ? deprecate((val) => this.output = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.output = val, enumerable: false, @@ -375,9 +372,9 @@ function REPLServer(prompt, // instance and that could trigger the `MaxListenersExceededWarning`. process.prependListener('newListener', (event, listener) => { if (event === 'uncaughtException' && - process.domain && - listener.name !== 'domainUncaughtExceptionClear' && - domainSet.has(process.domain)) { + process.domain && + listener.name !== 'domainUncaughtExceptionClear' && + domainSet.has(process.domain)) { // Throw an error so that the event will not be added and the current // domain takes over. That way the user is notified about the error // and the current code evaluation is stopped, just as any other code @@ -394,8 +391,8 @@ function REPLServer(prompt, const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; const sep = '\u0000\u0000\u0000'; const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)$`); + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)$`); eval_ = eval_ || defaultEval; @@ -448,7 +445,7 @@ function REPLServer(prompt, // an expression. Note that if the above condition changes, // lib/internal/repl/utils.js needs to be changed to match. if (RegExpPrototypeExec(/^\s*{/, code) !== null && - RegExpPrototypeExec(/;\s*$/, code) === null) { + RegExpPrototypeExec(/;\s*$/, code) === null) { code = `(${StringPrototypeTrim(code)})\n`; wrappedCmd = true; } @@ -523,7 +520,7 @@ function REPLServer(prompt, while (true) { try { if (self.replMode === module.exports.REPL_MODE_STRICT && - RegExpPrototypeExec(/^\s*$/, code) === null) { + RegExpPrototypeExec(/^\s*$/, code) === null) { // "void 0" keeps the repl from returning "use strict" as the result // value for statements and declarations that don't return a value. code = `'use strict'; void 0;\n${code}`; @@ -715,7 +712,7 @@ function REPLServer(prompt, 'module'; if (StringPrototypeIncludes(e.message, importErrorStr)) { e.message = 'Cannot use import statement inside the Node.js ' + - 'REPL, alternatively use dynamic import: ' + toDynamicImport(self.lines.at(-1)); + 'REPL, alternatively use dynamic import'; e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( /SyntaxError:.*\n/, e.stack, @@ -743,7 +740,7 @@ function REPLServer(prompt, } if (options[kStandaloneREPL] && - process.listenerCount('uncaughtException') !== 0) { + process.listenerCount('uncaughtException') !== 0) { process.nextTick(() => { process.emit('uncaughtException', e); self.clearBufferedCommand(); @@ -760,7 +757,7 @@ function REPLServer(prompt, errStack = ''; ArrayPrototypeForEach(lines, (line) => { if (!matched && - RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { + RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { errStack += writer.options.breakLength >= line.length ? `Uncaught ${line}` : `Uncaught:\n${line}`; @@ -906,8 +903,8 @@ function REPLServer(prompt, // display next prompt and return. if (trimmedCmd) { if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && - StringPrototypeCharAt(trimmedCmd, 1) !== '.' && - NumberIsNaN(NumberParseFloat(trimmedCmd))) { + StringPrototypeCharAt(trimmedCmd, 1) !== '.' && + NumberIsNaN(NumberParseFloat(trimmedCmd))) { const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); const keyword = matches && matches[1]; const rest = matches && matches[2]; @@ -932,10 +929,10 @@ function REPLServer(prompt, ReflectApply(_memory, self, [cmd]); if (e && !self[kBufferedCommandSymbol] && - StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { + StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { self.output.write('npm should be run outside of the ' + - 'Node.js REPL, in your normal shell.\n' + - '(Press Ctrl+D to exit.)\n'); + 'Node.js REPL, in your normal shell.\n' + + '(Press Ctrl+D to exit.)\n'); self.displayPrompt(); return; } @@ -960,11 +957,11 @@ function REPLServer(prompt, // If we got any output - print it (if no error) if (!e && - // When an invalid REPL command is used, error message is printed - // immediately. We don't have to print anything else. So, only when - // the second argument to this function is there, print it. - arguments.length === 2 && - (!self.ignoreUndefined || ret !== undefined)) { + // When an invalid REPL command is used, error message is printed + // immediately. We don't have to print anything else. So, only when + // the second argument to this function is there, print it. + arguments.length === 2 && + (!self.ignoreUndefined || ret !== undefined)) { if (!self.underscoreAssigned) { self.last = ret; } @@ -1015,7 +1012,7 @@ function REPLServer(prompt, if (!self.editorMode || !self.terminal) { // Before exiting, make sure to clear the line. if (key.ctrl && key.name === 'd' && - self.cursor === 0 && self.line.length === 0) { + self.cursor === 0 && self.line.length === 0) { self.clearLine(); } clearPreview(key); @@ -1212,7 +1209,7 @@ 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*["'](.*)/; const simpleExpressionRE = - /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; + /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; const versionedFileNamesRe = /-\d+\.\d+/; function isIdentifier(str) { @@ -1368,7 +1365,7 @@ function complete(line, callback) { const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; ArrayPrototypeForEach(dirents, (dirent) => { if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null || - dirent.name === '.npm') { + dirent.name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1376,7 +1373,7 @@ function complete(line, callback) { const base = StringPrototypeSlice(dirent.name, 0, -extension.length); if (!dirent.isDirectory()) { if (StringPrototypeIncludes(extensions, extension) && - (!subdir || base !== 'index')) { + (!subdir || base !== 'index')) { ArrayPrototypePush(group, `${subdir}${base}`); } return; @@ -1429,7 +1426,7 @@ function complete(line, callback) { ArrayPrototypeForEach(dirents, (dirent) => { const { name } = dirent; if (RegExpPrototypeExec(versionedFileNamesRe, name) !== null || - name === '.npm') { + name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1462,20 +1459,20 @@ function complete(line, callback) { ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); } else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null && - this.allowBlockingCompletions) { + this.allowBlockingCompletions) { ({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match)); - // Handle variable member lookup. - // We support simple chained expressions like the following (no function - // calls, etc.). That is for simplicity and also because we *eval* that - // leading expression so for safety (see WARNING above) don't want to - // eval function calls. - // - // foo.bar<|> # completions for 'foo' with filter 'bar' - // spam.eggs.<|> # completions for 'spam.eggs' with filter '' - // foo<|> # all scope vars with filter 'foo' - // foo.<|> # completions for 'foo' with filter '' + // Handle variable member lookup. + // We support simple chained expressions like the following (no function + // calls, etc.). That is for simplicity and also because we *eval* that + // leading expression so for safety (see WARNING above) don't want to + // eval function calls. + // + // foo.bar<|> # completions for 'foo' with filter 'bar' + // spam.eggs.<|> # completions for 'spam.eggs' with filter '' + // foo<|> # all scope vars with filter 'foo' + // foo.<|> # completions for 'foo' with filter '' } else if (line.length === 0 || - RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { + RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { const { 0: match } = RegExpPrototypeExec(simpleExpressionRE, line) || ['']; if (line.length !== 0 && !match) { completionGroupsLoaded(); @@ -1526,7 +1523,7 @@ function complete(line, callback) { try { let p; if ((typeof obj === 'object' && obj !== null) || - typeof obj === 'function') { + typeof obj === 'function') { memberGroups.push(filteredOwnPropertyNames(obj)); p = ObjectGetPrototypeOf(obj); } else { From 3b569cfbd04d65e7cb9655b09d2b8df5ca48237a Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 13:34:28 -0700 Subject: [PATCH 10/33] feat: remove const declaration --- lib/repl.js | 1568 ++++++++++++++++++------------------ test/parallel/test-repl.js | 21 +- 2 files changed, 797 insertions(+), 792 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index e1ac127f932cf9..513e3b1fe25559 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -94,50 +94,50 @@ const { SyntaxError, SyntaxErrorPrototype, globalThis, -} = primordials; +}=primordials; -const { BuiltinModule } = require('internal/bootstrap/realm'); +const {BuiltinModule}=require('internal/bootstrap/realm'); const { makeRequireFunction, addBuiltinLibsToObject, -} = require('internal/modules/helpers'); +}=require('internal/modules/helpers'); const { isIdentifierStart, isIdentifierChar, parse: acornParse, -} = require('internal/deps/acorn/acorn/dist/acorn'); -const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk'); +}=require('internal/deps/acorn/acorn/dist/acorn'); +const acornWalk=require('internal/deps/acorn/acorn-walk/dist/walk'); const { decorateErrorStack, isError, deprecate, SideEffectFreeRegExpPrototypeSymbolReplace, SideEffectFreeRegExpPrototypeSymbolSplit, -} = require('internal/util'); -const { inspect } = require('internal/util/inspect'); -const vm = require('vm'); -const path = require('path'); -const fs = require('fs'); -const { Interface } = require('readline'); +}=require('internal/util'); +const {inspect}=require('internal/util/inspect'); +const vm=require('vm'); +const path=require('path'); +const fs=require('fs'); +const {Interface}=require('readline'); const { commonPrefix, -} = require('internal/readline/utils'); -const { Console } = require('console'); -const { shouldColorize } = require('internal/util/colors'); -const CJSModule = require('internal/modules/cjs/loader').Module; -let _builtinLibs = ArrayPrototypeFilter( +}=require('internal/readline/utils'); +const {Console}=require('console'); +const {shouldColorize}=require('internal/util/colors'); +const CJSModule=require('internal/modules/cjs/loader').Module; +let _builtinLibs=ArrayPrototypeFilter( CJSModule.builtinModules, - (e) => !StringPrototypeStartsWith(e, '_'), + (e) => !StringPrototypeStartsWith(e,'_'), ); -const nodeSchemeBuiltinLibs = ArrayPrototypeMap( - _builtinLibs, (lib) => `node:${lib}`); +const nodeSchemeBuiltinLibs=ArrayPrototypeMap( + _builtinLibs,(lib) => `node:${lib}`); ArrayPrototypeForEach( BuiltinModule.getSchemeOnlyModuleNames(), - (lib) => ArrayPrototypePush(nodeSchemeBuiltinLibs, `node:${lib}`), + (lib) => ArrayPrototypePush(nodeSchemeBuiltinLibs,`node:${lib}`), ); -const domain = require('domain'); -let debug = require('internal/util/debuglog').debuglog('repl', (fn) => { - debug = fn; +const domain=require('domain'); +let debug=require('internal/util/debuglog').debuglog('repl',(fn) => { + debug=fn; }); const { codes: { @@ -148,17 +148,17 @@ const { }, isErrorStackTraceLimitWritable, overrideStackTrace, -} = require('internal/errors'); -const { sendInspectorCommand } = require('internal/util/inspector'); -const { getOptionValue } = require('internal/options'); +}=require('internal/errors'); +const {sendInspectorCommand}=require('internal/util/inspector'); +const {getOptionValue}=require('internal/options'); const { validateFunction, validateObject, -} = require('internal/validators'); -const experimentalREPLAwait = getOptionValue( +}=require('internal/validators'); +const experimentalREPLAwait=getOptionValue( '--experimental-repl-await', ); -const pendingDeprecation = getOptionValue('--pending-deprecation'); +const pendingDeprecation=getOptionValue('--pending-deprecation'); const { REPL_MODE_SLOPPY, REPL_MODE_STRICT, @@ -166,25 +166,25 @@ const { kStandaloneREPL, setupPreview, setupReverseSearch, -} = require('internal/repl/utils'); +}=require('internal/repl/utils'); const { constants: { ALL_PROPERTIES, SKIP_SYMBOLS, }, getOwnNonIndexProperties, -} = internalBinding('util'); +}=internalBinding('util'); const { startSigintWatchdog, stopSigintWatchdog, -} = internalBinding('contextify'); +}=internalBinding('contextify'); -const history = require('internal/repl/history'); +const history=require('internal/repl/history'); const { extensionFormatMap, -} = require('internal/modules/esm/formats'); +}=require('internal/modules/esm/formats'); -let nextREPLResourceNumber = 1; +let nextREPLResourceNumber=1; // This prevents v8 code cache from getting confused and using a different // cache from a resource of the same name function getREPLResourceName() { @@ -194,187 +194,181 @@ function getREPLResourceName() { // Lazy-loaded. let processTopLevelAwait; -const globalBuiltins = +const globalBuiltins= new SafeSet(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)')); -const parentModule = module; -const domainSet = new SafeWeakSet(); +const parentModule=module; +const domainSet=new SafeWeakSet(); -const kBufferedCommandSymbol = Symbol('bufferedCommand'); -const kContextId = Symbol('contextId'); +const kBufferedCommandSymbol=Symbol('bufferedCommand'); +const kContextId=Symbol('contextId'); -let addedNewListener = false; +let addedNewListener=false; try { // Hack for require.resolve("./relative") to work properly. - module.filename = path.resolve('repl'); + module.filename=path.resolve('repl'); } catch { // path.resolve('repl') fails when the current working directory has been // deleted. Fall back to the directory name of the (absolute) executable // path. It's not really correct but what are the alternatives? - const dirname = path.dirname(process.execPath); - module.filename = path.resolve(dirname, 'repl'); + const dirname=path.dirname(process.execPath); + module.filename=path.resolve(dirname,'repl'); } // Hack for repl require to work properly with node_modules folders -module.paths = CJSModule._nodeModulePaths(module.filename); +module.paths=CJSModule._nodeModulePaths(module.filename); // This is the default "writer" value, if none is passed in the REPL options, // and it can be overridden by custom print functions, such as `probe` or // `eyes.js`. -const writer = (obj) => inspect(obj, writer.options); -writer.options = { ...inspect.defaultOptions, showProxy: true }; +const writer=(obj) => inspect(obj,writer.options); +writer.options={...inspect.defaultOptions,showProxy: true}; // Converts static import statement to dynamic import statement -const toDynamicImport = (codeLine) => { - let dynamicImportStatement = ''; - let moduleName = ''; - const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' }); - const toCamelCase = (str) => RegexpPrototypeSymbolReplace(/[-_](\w)/g, str, (_, c) => StringPrototypeToUpperCase(c)); - acornWalk.ancestor(ast, { +const toDynamicImport=(codeLine) => { + let dynamicImportStatement=''; + let moduleName=''; + const ast=acornParse(codeLine,{__proto__: null,sourceType: 'module',ecmaVersion: 'latest'}); + acornWalk.ancestor(ast,{ ImportDeclaration(node) { - const importedModules = node.source.value; - const importedSpecifiers = ArrayPrototypeMap(node.specifiers, (specifier) => { - if (specifier.local.name === specifier?.imported?.name) { + const importedModules=node.source.value; + const importedSpecifiers=ArrayPrototypeMap(node.specifiers,(specifier) => { + if(specifier.local.name===specifier?.imported?.name) { return specifier.local.name; } - return `${specifier?.imported?.name ? specifier.imported.name + ':' : ''}${specifier.local.name}`; + return `${specifier?.imported?.name? specifier.imported.name+':':''}${specifier.local.name}`; }); - if (importedSpecifiers.length > 1) { - moduleName = `{${ArrayPrototypeJoin(importedSpecifiers, ',')}}`; - } else { - moduleName = toCamelCase(importedSpecifiers.length ? `${ArrayPrototypeJoin(importedSpecifiers, '')}` : importedModules); - } - dynamicImportStatement += `const ${moduleName} = await import('${importedModules}');`; + dynamicImportStatement+=`const ${moduleName} = await import('${importedModules}');`; }, }); return dynamicImportStatement; }; function REPLServer(prompt, - stream, - eval_, - useGlobal, - ignoreUndefined, - replMode) { - if (!(this instanceof REPLServer)) { + stream, + eval_, + useGlobal, + ignoreUndefined, + replMode) { + if(!(this instanceof REPLServer)) { return new REPLServer(prompt, - stream, - eval_, - useGlobal, - ignoreUndefined, - replMode); + stream, + eval_, + useGlobal, + ignoreUndefined, + replMode); } let options; - if (prompt !== null && typeof prompt === 'object') { + if(prompt!==null&&typeof prompt==='object') { // An options object was given. - options = { ...prompt }; - stream = options.stream || options.socket; - eval_ = options.eval; - useGlobal = options.useGlobal; - ignoreUndefined = options.ignoreUndefined; - prompt = options.prompt; - replMode = options.replMode; + options={...prompt}; + stream=options.stream||options.socket; + eval_=options.eval; + useGlobal=options.useGlobal; + ignoreUndefined=options.ignoreUndefined; + prompt=options.prompt; + replMode=options.replMode; } else { - options = {}; + options={}; } - if (!options.input && !options.output) { + if(!options.input&&!options.output) { // Legacy API, passing a 'stream'/'socket' option. - if (!stream) { + if(!stream) { // Use stdin and stdout as the default streams if none were given. - stream = process; + stream=process; } // We're given a duplex readable/writable Stream, like a `net.Socket` // or a custom object with 2 streams, or the `process` object. - options.input = stream.stdin || stream; - options.output = stream.stdout || stream; + options.input=stream.stdin||stream; + options.output=stream.stdout||stream; } - if (options.terminal === undefined) { - options.terminal = options.output.isTTY; + if(options.terminal===undefined) { + options.terminal=options.output.isTTY; } - options.terminal = !!options.terminal; + options.terminal=!!options.terminal; - if (options.terminal && options.useColors === undefined) { + if(options.terminal&&options.useColors===undefined) { // If possible, check if stdout supports colors or not. - options.useColors = shouldColorize(options.output) || process.env.NODE_DISABLE_COLORS === undefined; + options.useColors=shouldColorize(options.output)||process.env.NODE_DISABLE_COLORS===undefined; } // TODO(devsnek): Add a test case for custom eval functions. - const preview = options.terminal && - (options.preview !== undefined ? !!options.preview : !eval_); + const preview=options.terminal&& + (options.preview!==undefined? !!options.preview:!eval_); - ObjectDefineProperty(this, 'inputStream', { + ObjectDefineProperty(this,'inputStream',{ __proto__: null, - get: pendingDeprecation ? + get: pendingDeprecation? deprecate(() => this.input, - 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', - 'DEP0141') : + 'repl.inputStream and repl.outputStream are deprecated. '+ + 'Use repl.input and repl.output instead', + 'DEP0141'): () => this.input, - set: pendingDeprecation ? - deprecate((val) => this.input = val, - 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', - 'DEP0141') : - (val) => this.input = val, + set: pendingDeprecation? + deprecate((val) => this.input=val, + 'repl.inputStream and repl.outputStream are deprecated. '+ + 'Use repl.input and repl.output instead', + 'DEP0141'): + (val) => this.input=val, enumerable: false, configurable: true, }); - ObjectDefineProperty(this, 'outputStream', { + ObjectDefineProperty(this,'outputStream',{ __proto__: null, - get: pendingDeprecation ? + get: pendingDeprecation? deprecate(() => this.output, - 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', - 'DEP0141') : + 'repl.inputStream and repl.outputStream are deprecated. '+ + 'Use repl.input and repl.output instead', + 'DEP0141'): () => this.output, - set: pendingDeprecation ? - deprecate((val) => this.output = val, - 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', - 'DEP0141') : - (val) => this.output = val, + set: pendingDeprecation? + deprecate((val) => this.output=val, + 'repl.inputStream and repl.outputStream are deprecated. '+ + 'Use repl.input and repl.output instead', + 'DEP0141'): + (val) => this.output=val, enumerable: false, configurable: true, }); - this.allowBlockingCompletions = !!options.allowBlockingCompletions; - this.useColors = !!options.useColors; - this._domain = options.domain || domain.create(); - this.useGlobal = !!useGlobal; - this.ignoreUndefined = !!ignoreUndefined; - this.replMode = replMode || module.exports.REPL_MODE_SLOPPY; - this.underscoreAssigned = false; - this.last = undefined; - this.underscoreErrAssigned = false; - this.lastError = undefined; - this.breakEvalOnSigint = !!options.breakEvalOnSigint; - this.editorMode = false; + this.allowBlockingCompletions=!!options.allowBlockingCompletions; + this.useColors=!!options.useColors; + this._domain=options.domain||domain.create(); + this.useGlobal=!!useGlobal; + this.ignoreUndefined=!!ignoreUndefined; + this.replMode=replMode||module.exports.REPL_MODE_SLOPPY; + this.underscoreAssigned=false; + this.last=undefined; + this.underscoreErrAssigned=false; + this.lastError=undefined; + this.breakEvalOnSigint=!!options.breakEvalOnSigint; + this.editorMode=false; // Context id for use with the inspector protocol. - this[kContextId] = undefined; + this[kContextId]=undefined; - if (this.breakEvalOnSigint && eval_) { + if(this.breakEvalOnSigint&&eval_) { // Allowing this would not reflect user expectations. // breakEvalOnSigint affects only the behavior of the default eval(). throw new ERR_INVALID_REPL_EVAL_CONFIG(); } - if (options[kStandaloneREPL]) { + if(options[kStandaloneREPL]) { // It is possible to introspect the running REPL accessing this variable // from inside the REPL. This is useful for anyone working on the REPL. - module.exports.repl = this; - } else if (!addedNewListener) { + module.exports.repl=this; + } else if(!addedNewListener) { // Add this listener only once and use a WeakSet that contains the REPLs // domains. Otherwise we'd have to add a single listener to each REPL // instance and that could trigger the `MaxListenersExceededWarning`. - process.prependListener('newListener', (event, listener) => { - if (event === 'uncaughtException' && - process.domain && - listener.name !== 'domainUncaughtExceptionClear' && - domainSet.has(process.domain)) { + process.prependListener('newListener',(event,listener) => { + if(event==='uncaughtException'&& + process.domain&& + listener.name!=='domainUncaughtExceptionClear'&& + domainSet.has(process.domain)) { // Throw an error so that the event will not be added and the current // domain takes over. That way the user is notified about the error // and the current code evaluation is stopped, just as any other code @@ -383,172 +377,172 @@ function REPLServer(prompt, 'Listeners for `uncaughtException` cannot be used in the REPL'); } }); - addedNewListener = true; + addedNewListener=true; } domainSet.add(this._domain); - const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; - const sep = '\u0000\u0000\u0000'; - const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)$`); + const savedRegExMatches=['','','','','','','','','','']; + const sep='\u0000\u0000\u0000'; + const regExMatcher=new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)`+ + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)`+ + `${sep}(.*)$`); - eval_ = eval_ || defaultEval; + eval_=eval_||defaultEval; - const self = this; + const self=this; // Pause taking in new input, and store the keys in a buffer. - const pausedBuffer = []; - let paused = false; + const pausedBuffer=[]; + let paused=false; function pause() { - paused = true; + paused=true; } function unpause() { - if (!paused) return; - paused = false; + if(!paused) return; + paused=false; let entry; - const tmpCompletionEnabled = self.isCompletionEnabled; - while ((entry = ArrayPrototypeShift(pausedBuffer)) !== undefined) { - const { 0: type, 1: payload, 2: isCompletionEnabled } = entry; - switch (type) { + const tmpCompletionEnabled=self.isCompletionEnabled; + while((entry=ArrayPrototypeShift(pausedBuffer))!==undefined) { + const {0: type,1: payload,2: isCompletionEnabled}=entry; + switch(type) { case 'key': { - const { 0: d, 1: key } = payload; - self.isCompletionEnabled = isCompletionEnabled; - self._ttyWrite(d, key); + const {0: d,1: key}=payload; + self.isCompletionEnabled=isCompletionEnabled; + self._ttyWrite(d,key); break; } case 'close': self.emit('exit'); break; } - if (paused) { + if(paused) { break; } } - self.isCompletionEnabled = tmpCompletionEnabled; + self.isCompletionEnabled=tmpCompletionEnabled; } - function defaultEval(code, context, file, cb) { - const asyncESM = require('internal/process/esm_loader'); + function defaultEval(code,context,file,cb) { + const asyncESM=require('internal/process/esm_loader'); - let result, script, wrappedErr; - let err = null; - let wrappedCmd = false; - let awaitPromise = false; - const input = code; + let result,script,wrappedErr; + let err=null; + let wrappedCmd=false; + let awaitPromise=false; + const input=code; // It's confusing for `{ a : 1 }` to be interpreted as a block // statement rather than an object literal. So, we first try // to wrap it in parentheses, so that it will be interpreted as // an expression. Note that if the above condition changes, // lib/internal/repl/utils.js needs to be changed to match. - if (RegExpPrototypeExec(/^\s*{/, code) !== null && - RegExpPrototypeExec(/;\s*$/, code) === null) { - code = `(${StringPrototypeTrim(code)})\n`; - wrappedCmd = true; + if(RegExpPrototypeExec(/^\s*{/,code)!==null&& + RegExpPrototypeExec(/;\s*$/,code)===null) { + code=`(${StringPrototypeTrim(code)})\n`; + wrappedCmd=true; } // `experimentalREPLAwait` is set to true by default. // Shall be false in case `--no-experimental-repl-await` flag is used. - if (experimentalREPLAwait && StringPrototypeIncludes(code, 'await')) { - if (processTopLevelAwait === undefined) { - ({ processTopLevelAwait } = require('internal/repl/await')); + if(experimentalREPLAwait&&StringPrototypeIncludes(code,'await')) { + if(processTopLevelAwait===undefined) { + ({processTopLevelAwait}=require('internal/repl/await')); } try { - const potentialWrappedCode = processTopLevelAwait(code); - if (potentialWrappedCode !== null) { - code = potentialWrappedCode; - wrappedCmd = true; - awaitPromise = true; + const potentialWrappedCode=processTopLevelAwait(code); + if(potentialWrappedCode!==null) { + code=potentialWrappedCode; + wrappedCmd=true; + awaitPromise=true; } - } catch (e) { - let recoverableError = false; - if (e.name === 'SyntaxError') { + } catch(e) { + let recoverableError=false; + if(e.name==='SyntaxError') { let parentURL; try { - const { pathToFileURL } = require('url'); + const {pathToFileURL}=require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; + parentURL=pathToFileURL(path.join(process.cwd(),'repl')).href; } catch { // Continue regardless of error. } // Remove all "await"s and attempt running the script // in order to detect if error is truly non recoverable - const fallbackCode = SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g, code, ''); + const fallbackCode=SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g,code,''); try { - vm.createScript(fallbackCode, { + vm.createScript(fallbackCode,{ filename: file, displayErrors: true, - importModuleDynamically: (specifier, _, importAssertions) => { - return asyncESM.esmLoader.import(specifier, parentURL, - importAssertions); + importModuleDynamically: (specifier,_,importAssertions) => { + return asyncESM.esmLoader.import(specifier,parentURL, + importAssertions); }, }); - } catch (fallbackError) { - if (isRecoverableError(fallbackError, fallbackCode)) { - recoverableError = true; - err = new Recoverable(e); + } catch(fallbackError) { + if(isRecoverableError(fallbackError,fallbackCode)) { + recoverableError=true; + err=new Recoverable(e); } } } - if (!recoverableError) { + if(!recoverableError) { decorateErrorStack(e); - err = e; + err=e; } } } // First, create the Script object to check the syntax - if (code === '\n') + if(code==='\n') return cb(null); - if (err === null) { + if(err===null) { let parentURL; try { - const { pathToFileURL } = require('url'); + const {pathToFileURL}=require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; + parentURL=pathToFileURL(path.join(process.cwd(),'repl')).href; } catch { // Continue regardless of error. } - while (true) { + while(true) { try { - if (self.replMode === module.exports.REPL_MODE_STRICT && - RegExpPrototypeExec(/^\s*$/, code) === null) { + if(self.replMode===module.exports.REPL_MODE_STRICT&& + RegExpPrototypeExec(/^\s*$/,code)===null) { // "void 0" keeps the repl from returning "use strict" as the result // value for statements and declarations that don't return a value. - code = `'use strict'; void 0;\n${code}`; + code=`'use strict'; void 0;\n${code}`; } - script = vm.createScript(code, { + script=vm.createScript(code,{ filename: file, displayErrors: true, - importModuleDynamically: (specifier, _, importAssertions) => { - return asyncESM.esmLoader.import(specifier, parentURL, - importAssertions); + importModuleDynamically: (specifier,_,importAssertions) => { + return asyncESM.esmLoader.import(specifier,parentURL, + importAssertions); }, }); - } catch (e) { - debug('parse error %j', code, e); - if (wrappedCmd) { + } catch(e) { + debug('parse error %j',code,e); + if(wrappedCmd) { // Unwrap and try again - wrappedCmd = false; - awaitPromise = false; - code = input; - wrappedErr = e; + wrappedCmd=false; + awaitPromise=false; + code=input; + wrappedErr=e; continue; } // Preserve original error for wrapped command - const error = wrappedErr || e; - if (isRecoverableError(error, code)) - err = new Recoverable(error); + const error=wrappedErr||e; + if(isRecoverableError(error,code)) + err=new Recoverable(error); else - err = error; + err=error; } break; } @@ -557,94 +551,94 @@ function REPLServer(prompt, // This will set the values from `savedRegExMatches` to corresponding // predefined RegExp properties `RegExp.$1`, `RegExp.$2` ... `RegExp.$9` RegExpPrototypeExec(regExMatcher, - ArrayPrototypeJoin(savedRegExMatches, sep)); + ArrayPrototypeJoin(savedRegExMatches,sep)); - let finished = false; - function finishExecution(err, result) { - if (finished) return; - finished = true; + let finished=false; + function finishExecution(err,result) { + if(finished) return; + finished=true; // After executing the current expression, store the values of RegExp // predefined properties back in `savedRegExMatches` - for (let idx = 1; idx < savedRegExMatches.length; idx += 1) { - savedRegExMatches[idx] = RegExp[`$${idx}`]; + for(let idx=1;idx { - sigintListener = () => { - const tmp = Error.stackTraceLimit; - if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0; - const err = new ERR_SCRIPT_EXECUTION_INTERRUPTED(); - if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = tmp; + let promise=result; + if(self.breakEvalOnSigint) { + const interrupt=new Promise((resolve,reject) => { + sigintListener=() => { + const tmp=Error.stackTraceLimit; + if(isErrorStackTraceLimitWritable()) Error.stackTraceLimit=0; + const err=new ERR_SCRIPT_EXECUTION_INTERRUPTED(); + if(isErrorStackTraceLimitWritable()) Error.stackTraceLimit=tmp; reject(err); }; prioritizedSigintQueue.add(sigintListener); }); - promise = SafePromiseRace([promise, interrupt]); + promise=SafePromiseRace([promise,interrupt]); } (async () => { try { - const result = (await promise)?.value; - finishExecution(null, result); - } catch (err) { - if (err && process.domain) { + const result=(await promise)?.value; + finishExecution(null,result); + } catch(err) { + if(err&&process.domain) { debug('not recoverable, send to domain'); - process.domain.emit('error', err); + process.domain.emit('error',err); process.domain.exit(); return; } @@ -658,138 +652,138 @@ function REPLServer(prompt, } } - if (!awaitPromise || err) { - finishExecution(err, result); + if(!awaitPromise||err) { + finishExecution(err,result); } } - self.eval = self._domain.bind(eval_); + self.eval=self._domain.bind(eval_); - self._domain.on('error', function debugDomainError(e) { + self._domain.on('error',function debugDomainError(e) { debug('domain error'); - let errStack = ''; + let errStack=''; - if (typeof e === 'object' && e !== null) { - overrideStackTrace.set(e, (error, stackFrames) => { + if(typeof e==='object'&&e!==null) { + overrideStackTrace.set(e,(error,stackFrames) => { let frames; - if (typeof stackFrames === 'object') { + if(typeof stackFrames==='object') { // Search from the bottom of the call stack to // find the first frame with a null function name - const idx = ArrayPrototypeFindIndex( + const idx=ArrayPrototypeFindIndex( ArrayPrototypeReverse(stackFrames), - (frame) => frame.getFunctionName() === null, + (frame) => frame.getFunctionName()===null, ); // If found, get rid of it and everything below it - frames = ArrayPrototypeSplice(stackFrames, idx + 1); + frames=ArrayPrototypeSplice(stackFrames,idx+1); } else { - frames = stackFrames; + frames=stackFrames; } // FIXME(devsnek): this is inconsistent with the checks // that the real prepareStackTrace dispatch uses in // lib/internal/errors.js. - if (typeof Error.prepareStackTrace === 'function') { - return Error.prepareStackTrace(error, frames); + if(typeof Error.prepareStackTrace==='function') { + return Error.prepareStackTrace(error,frames); } - ArrayPrototypePush(frames, error); - return ArrayPrototypeJoin(ArrayPrototypeReverse(frames), '\n at '); + ArrayPrototypePush(frames,error); + return ArrayPrototypeJoin(ArrayPrototypeReverse(frames),'\n at '); }); decorateErrorStack(e); - if (e.domainThrown) { + if(e.domainThrown) { delete e.domain; delete e.domainThrown; } - if (isError(e)) { - if (e.stack) { - if (e.name === 'SyntaxError') { + if(isError(e)) { + if(e.stack) { + if(e.name==='SyntaxError') { // Remove stack trace. - e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( + e.stack=SideEffectFreeRegExpPrototypeSymbolReplace( /^\s+at\s.*\n?/gm, - SideEffectFreeRegExpPrototypeSymbolReplace(/^REPL\d+:\d+\r?\n/, e.stack, ''), + SideEffectFreeRegExpPrototypeSymbolReplace(/^REPL\d+:\d+\r?\n/,e.stack,''), ''); - const importErrorStr = 'Cannot use import statement outside a ' + + const importErrorStr='Cannot use import statement outside a '+ 'module'; - if (StringPrototypeIncludes(e.message, importErrorStr)) { - e.message = 'Cannot use import statement inside the Node.js ' + - 'REPL, alternatively use dynamic import'; - e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( + if(StringPrototypeIncludes(e.message,importErrorStr)) { + e.message='Cannot use import statement inside the Node.js '+ + 'REPL, alternatively use dynamic import: ' + toDynamicImport(self.lines.at(-1)); + e.stack=SideEffectFreeRegExpPrototypeSymbolReplace( /SyntaxError:.*\n/, e.stack, `SyntaxError: ${e.message}\n`); } - } else if (self.replMode === module.exports.REPL_MODE_STRICT) { - e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( + } else if(self.replMode===module.exports.REPL_MODE_STRICT) { + e.stack=SideEffectFreeRegExpPrototypeSymbolReplace( /(\s+at\s+REPL\d+:)(\d+)/, e.stack, - (_, pre, line) => pre + (line - 1), + (_,pre,line) => pre+(line-1), ); } } - errStack = self.writer(e); + errStack=self.writer(e); // Remove one line error braces to keep the old style in place. - if (errStack[0] === '[' && errStack[errStack.length - 1] === ']') { - errStack = StringPrototypeSlice(errStack, 1, -1); + if(errStack[0]==='['&&errStack[errStack.length-1]===']') { + errStack=StringPrototypeSlice(errStack,1,-1); } } } - if (!self.underscoreErrAssigned) { - self.lastError = e; + if(!self.underscoreErrAssigned) { + self.lastError=e; } - if (options[kStandaloneREPL] && - process.listenerCount('uncaughtException') !== 0) { + if(options[kStandaloneREPL]&& + process.listenerCount('uncaughtException')!==0) { process.nextTick(() => { - process.emit('uncaughtException', e); + process.emit('uncaughtException',e); self.clearBufferedCommand(); - self.lines.level = []; + self.lines.level=[]; self.displayPrompt(); }); } else { - if (errStack === '') { - errStack = self.writer(e); + if(errStack==='') { + errStack=self.writer(e); } - const lines = SideEffectFreeRegExpPrototypeSymbolSplit(/(?<=\n)/, errStack); - let matched = false; - - errStack = ''; - ArrayPrototypeForEach(lines, (line) => { - if (!matched && - RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { - errStack += writer.options.breakLength >= line.length ? - `Uncaught ${line}` : + const lines=SideEffectFreeRegExpPrototypeSymbolSplit(/(?<=\n)/,errStack); + let matched=false; + + errStack=''; + ArrayPrototypeForEach(lines,(line) => { + if(!matched&& + RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/,line)!==null) { + errStack+=writer.options.breakLength>=line.length? + `Uncaught ${line}`: `Uncaught:\n${line}`; - matched = true; + matched=true; } else { - errStack += line; + errStack+=line; } }); - if (!matched) { - const ln = lines.length === 1 ? ' ' : ':\n'; - errStack = `Uncaught${ln}${errStack}`; + if(!matched) { + const ln=lines.length===1? ' ':':\n'; + errStack=`Uncaught${ln}${errStack}`; } // Normalize line endings. - errStack += StringPrototypeEndsWith(errStack, '\n') ? '' : '\n'; + errStack+=StringPrototypeEndsWith(errStack,'\n')? '':'\n'; self.output.write(errStack); self.clearBufferedCommand(); - self.lines.level = []; + self.lines.level=[]; self.displayPrompt(); } }); self.clearBufferedCommand(); - function completer(text, cb) { - ReflectApply(complete, self, - [text, self.editorMode ? self.completeOnEditorMode(cb) : cb]); + function completer(text,cb) { + ReflectApply(complete,self, + [text,self.editorMode? self.completeOnEditorMode(cb):cb]); } - ReflectApply(Interface, this, [{ + ReflectApply(Interface,this,[{ input: options.input, output: options.output, - completer: options.completer || completer, + completer: options.completer||completer, terminal: options.terminal, historySize: options.historySize, prompt, @@ -797,25 +791,25 @@ function REPLServer(prompt, self.resetContext(); - this.commands = { __proto__: null }; + this.commands={__proto__: null}; defineDefaultCommands(this); // Figure out which "writer" function to use - self.writer = options.writer || module.exports.writer; + self.writer=options.writer||module.exports.writer; - if (self.writer === writer) { + if(self.writer===writer) { // Conditionally turn on ANSI coloring. - writer.options.colors = self.useColors; + writer.options.colors=self.useColors; - if (options[kStandaloneREPL]) { - ObjectDefineProperty(inspect, 'replDefaults', { + if(options[kStandaloneREPL]) { + ObjectDefineProperty(inspect,'replDefaults',{ __proto__: null, get() { return writer.options; }, set(options) { - validateObject(options, 'options'); - return ObjectAssign(writer.options, options); + validateObject(options,'options'); + return ObjectAssign(writer.options,options); }, enumerable: true, configurable: true, @@ -823,95 +817,95 @@ function REPLServer(prompt, } } - function _parseREPLKeyword(keyword, rest) { - const cmd = this.commands[keyword]; - if (cmd) { - ReflectApply(cmd.action, this, [rest]); + function _parseREPLKeyword(keyword,rest) { + const cmd=this.commands[keyword]; + if(cmd) { + ReflectApply(cmd.action,this,[rest]); return true; } return false; } - self.on('close', function emitExit() { - if (paused) { - ArrayPrototypePush(pausedBuffer, ['close']); + self.on('close',function emitExit() { + if(paused) { + ArrayPrototypePush(pausedBuffer,['close']); return; } self.emit('exit'); }); - let sawSIGINT = false; - let sawCtrlD = false; - const prioritizedSigintQueue = new SafeSet(); - self.on('SIGINT', function onSigInt() { - if (prioritizedSigintQueue.size > 0) { - for (const task of prioritizedSigintQueue) { + let sawSIGINT=false; + let sawCtrlD=false; + const prioritizedSigintQueue=new SafeSet(); + self.on('SIGINT',function onSigInt() { + if(prioritizedSigintQueue.size>0) { + for(const task of prioritizedSigintQueue) { task(); } return; } - const empty = self.line.length === 0; + const empty=self.line.length===0; self.clearLine(); _turnOffEditorMode(self); - const cmd = self[kBufferedCommandSymbol]; - if (!(cmd && cmd.length > 0) && empty) { - if (sawSIGINT) { + const cmd=self[kBufferedCommandSymbol]; + if(!(cmd&&cmd.length>0)&&empty) { + if(sawSIGINT) { self.close(); - sawSIGINT = false; + sawSIGINT=false; return; } self.output.write( '(To exit, press Ctrl+C again or Ctrl+D or type .exit)\n', ); - sawSIGINT = true; + sawSIGINT=true; } else { - sawSIGINT = false; + sawSIGINT=false; } self.clearBufferedCommand(); - self.lines.level = []; + self.lines.level=[]; self.displayPrompt(); }); - self.on('line', function onLine(cmd) { - debug('line %j', cmd); - cmd = cmd || ''; - sawSIGINT = false; + self.on('line',function onLine(cmd) { + debug('line %j',cmd); + cmd=cmd||''; + sawSIGINT=false; - if (self.editorMode) { - self[kBufferedCommandSymbol] += cmd + '\n'; + if(self.editorMode) { + self[kBufferedCommandSymbol]+=cmd+'\n'; // code alignment - const matches = self._sawKeyPress ? - RegExpPrototypeExec(/^\s+/, cmd) : null; - if (matches) { - const prefix = matches[0]; + const matches=self._sawKeyPress? + RegExpPrototypeExec(/^\s+/,cmd):null; + if(matches) { + const prefix=matches[0]; self.write(prefix); - self.line = prefix; - self.cursor = prefix.length; + self.line=prefix; + self.cursor=prefix.length; } - ReflectApply(_memory, self, [cmd]); + ReflectApply(_memory,self,[cmd]); return; } // Check REPL keywords and empty lines against a trimmed line input. - const trimmedCmd = StringPrototypeTrim(cmd); + const trimmedCmd=StringPrototypeTrim(cmd); // Check to see if a REPL keyword was used. If it returns true, // display next prompt and return. - if (trimmedCmd) { - if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && - StringPrototypeCharAt(trimmedCmd, 1) !== '.' && - NumberIsNaN(NumberParseFloat(trimmedCmd))) { - const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); - const keyword = matches && matches[1]; - const rest = matches && matches[2]; - if (ReflectApply(_parseREPLKeyword, self, [keyword, rest]) === true) { + if(trimmedCmd) { + if(StringPrototypeCharAt(trimmedCmd,0)==='.'&& + StringPrototypeCharAt(trimmedCmd,1)!=='.'&& + NumberIsNaN(NumberParseFloat(trimmedCmd))) { + const matches=RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/,trimmedCmd); + const keyword=matches&&matches[1]; + const rest=matches&&matches[2]; + if(ReflectApply(_parseREPLKeyword,self,[keyword,rest])===true) { return; } - if (!self[kBufferedCommandSymbol]) { + if(!self[kBufferedCommandSymbol]) { self.output.write('Invalid REPL keyword\n'); finish(null); return; @@ -919,65 +913,65 @@ function REPLServer(prompt, } } - const evalCmd = self[kBufferedCommandSymbol] + cmd + '\n'; + const evalCmd=self[kBufferedCommandSymbol]+cmd+'\n'; - debug('eval %j', evalCmd); - self.eval(evalCmd, self.context, getREPLResourceName(), finish); + debug('eval %j',evalCmd); + self.eval(evalCmd,self.context,getREPLResourceName(),finish); - function finish(e, ret) { - debug('finish', e, ret); - ReflectApply(_memory, self, [cmd]); + function finish(e,ret) { + debug('finish',e,ret); + ReflectApply(_memory,self,[cmd]); - if (e && !self[kBufferedCommandSymbol] && - StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { - self.output.write('npm should be run outside of the ' + - 'Node.js REPL, in your normal shell.\n' + - '(Press Ctrl+D to exit.)\n'); + if(e&&!self[kBufferedCommandSymbol]&& + StringPrototypeStartsWith(StringPrototypeTrim(cmd),'npm ')) { + self.output.write('npm should be run outside of the '+ + 'Node.js REPL, in your normal shell.\n'+ + '(Press Ctrl+D to exit.)\n'); self.displayPrompt(); return; } // If error was SyntaxError and not JSON.parse error - if (e) { - if (e instanceof Recoverable && !sawCtrlD) { + if(e) { + if(e instanceof Recoverable&&!sawCtrlD) { // Start buffering data like that: // { // ... x: 1 // ... } - self[kBufferedCommandSymbol] += cmd + '\n'; + self[kBufferedCommandSymbol]+=cmd+'\n'; self.displayPrompt(); return; } - self._domain.emit('error', e.err || e); + self._domain.emit('error',e.err||e); } // Clear buffer if no SyntaxErrors self.clearBufferedCommand(); - sawCtrlD = false; + sawCtrlD=false; // If we got any output - print it (if no error) - if (!e && - // When an invalid REPL command is used, error message is printed - // immediately. We don't have to print anything else. So, only when - // the second argument to this function is there, print it. - arguments.length === 2 && - (!self.ignoreUndefined || ret !== undefined)) { - if (!self.underscoreAssigned) { - self.last = ret; + if(!e&& + // When an invalid REPL command is used, error message is printed + // immediately. We don't have to print anything else. So, only when + // the second argument to this function is there, print it. + arguments.length===2&& + (!self.ignoreUndefined||ret!==undefined)) { + if(!self.underscoreAssigned) { + self.last=ret; } - self.output.write(self.writer(ret) + '\n'); + self.output.write(self.writer(ret)+'\n'); } // Display prompt again (unless we already did by emitting the 'error' // event on the domain instance). - if (!e) { + if(!e) { self.displayPrompt(); } } }); - self.on('SIGCONT', function onSigCont() { - if (self.editorMode) { + self.on('SIGCONT',function onSigCont() { + if(self.editorMode) { self.output.write(`${self._initialPrompt}.editor\n`); self.output.write( '// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)\n'); @@ -988,12 +982,12 @@ function REPLServer(prompt, } }); - const { reverseSearch } = setupReverseSearch(this); + const {reverseSearch}=setupReverseSearch(this); const { clearPreview, showPreview, - } = setupPreview( + }=setupPreview( this, kContextId, kBufferedCommandSymbol, @@ -1001,123 +995,123 @@ function REPLServer(prompt, ); // Wrap readline tty to enable editor mode and pausing. - const ttyWrite = FunctionPrototypeBind(self._ttyWrite, self); - self._ttyWrite = (d, key) => { - key = key || {}; - if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) { + const ttyWrite=FunctionPrototypeBind(self._ttyWrite,self); + self._ttyWrite=(d,key) => { + key=key||{}; + if(paused&&!(self.breakEvalOnSigint&&key.ctrl&&key.name==='c')) { ArrayPrototypePush(pausedBuffer, - ['key', [d, key], self.isCompletionEnabled]); + ['key',[d,key],self.isCompletionEnabled]); return; } - if (!self.editorMode || !self.terminal) { + if(!self.editorMode||!self.terminal) { // Before exiting, make sure to clear the line. - if (key.ctrl && key.name === 'd' && - self.cursor === 0 && self.line.length === 0) { + if(key.ctrl&&key.name==='d'&& + self.cursor===0&&self.line.length===0) { self.clearLine(); } clearPreview(key); - if (!reverseSearch(d, key)) { - ttyWrite(d, key); - const showCompletionPreview = key.name !== 'escape'; + if(!reverseSearch(d,key)) { + ttyWrite(d,key); + const showCompletionPreview=key.name!=='escape'; showPreview(showCompletionPreview); } return; } // Editor mode - if (key.ctrl && !key.shift) { - switch (key.name) { + if(key.ctrl&&!key.shift) { + switch(key.name) { // TODO(BridgeAR): There should not be a special mode necessary for full // multiline support. case 'd': // End editor mode _turnOffEditorMode(self); - sawCtrlD = true; - ttyWrite(d, { name: 'return' }); + sawCtrlD=true; + ttyWrite(d,{name: 'return'}); break; case 'n': // Override next history item case 'p': // Override previous history item break; default: - ttyWrite(d, key); + ttyWrite(d,key); } } else { - switch (key.name) { + switch(key.name) { case 'up': // Override previous history item case 'down': // Override next history item break; case 'tab': // Prevent double tab behavior - self._previousKey = null; - ttyWrite(d, key); + self._previousKey=null; + ttyWrite(d,key); break; default: - ttyWrite(d, key); + ttyWrite(d,key); } } }; self.displayPrompt(); } -ObjectSetPrototypeOf(REPLServer.prototype, Interface.prototype); -ObjectSetPrototypeOf(REPLServer, Interface); +ObjectSetPrototypeOf(REPLServer.prototype,Interface.prototype); +ObjectSetPrototypeOf(REPLServer,Interface); // Prompt is a string to print on each line for the prompt, // source is a stream to use for I/O, defaulting to stdin/stdout. -function start(prompt, source, eval_, useGlobal, ignoreUndefined, replMode) { +function start(prompt,source,eval_,useGlobal,ignoreUndefined,replMode) { return new REPLServer( - prompt, source, eval_, useGlobal, ignoreUndefined, replMode); + prompt,source,eval_,useGlobal,ignoreUndefined,replMode); } -REPLServer.prototype.setupHistory = function setupHistory(historyFile, cb) { - history(this, historyFile, cb); +REPLServer.prototype.setupHistory=function setupHistory(historyFile,cb) { + history(this,historyFile,cb); }; -REPLServer.prototype.clearBufferedCommand = function clearBufferedCommand() { - this[kBufferedCommandSymbol] = ''; +REPLServer.prototype.clearBufferedCommand=function clearBufferedCommand() { + this[kBufferedCommandSymbol]=''; }; -REPLServer.prototype.close = function close() { - if (this.terminal && this._flushing && !this._closingOnFlush) { - this._closingOnFlush = true; - this.once('flushHistory', () => - ReflectApply(Interface.prototype.close, this, []), +REPLServer.prototype.close=function close() { + if(this.terminal&&this._flushing&&!this._closingOnFlush) { + this._closingOnFlush=true; + this.once('flushHistory',() => + ReflectApply(Interface.prototype.close,this,[]), ); return; } process.nextTick(() => - ReflectApply(Interface.prototype.close, this, []), + ReflectApply(Interface.prototype.close,this,[]), ); }; -REPLServer.prototype.createContext = function() { +REPLServer.prototype.createContext=function() { let context; - if (this.useGlobal) { - context = globalThis; + if(this.useGlobal) { + context=globalThis; } else { sendInspectorCommand((session) => { session.post('Runtime.enable'); - session.once('Runtime.executionContextCreated', ({ params }) => { - this[kContextId] = params.context.id; + session.once('Runtime.executionContextCreated',({params}) => { + this[kContextId]=params.context.id; }); - context = vm.createContext(); + context=vm.createContext(); session.post('Runtime.disable'); - }, () => { - context = vm.createContext(); + },() => { + context=vm.createContext(); }); - ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => { + ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis),(name) => { // Only set properties that do not already exist as a global builtin. - if (!globalBuiltins.has(name)) { - ObjectDefineProperty(context, name, - { - __proto__: null, - ...ObjectGetOwnPropertyDescriptor(globalThis, name), - }); + if(!globalBuiltins.has(name)) { + ObjectDefineProperty(context,name, + { + __proto__: null, + ...ObjectGetOwnPropertyDescriptor(globalThis,name), + }); } }); - context.global = context; - const _console = new Console(this.output); - ObjectDefineProperty(context, 'console', { + context.global=context; + const _console=new Console(this.output); + ObjectDefineProperty(context,'console',{ __proto__: null, configurable: true, writable: true, @@ -1125,56 +1119,56 @@ REPLServer.prototype.createContext = function() { }); } - const replModule = new CJSModule(''); - replModule.paths = CJSModule._resolveLookupPaths('', parentModule); + const replModule=new CJSModule(''); + replModule.paths=CJSModule._resolveLookupPaths('',parentModule); - ObjectDefineProperty(context, 'module', { + ObjectDefineProperty(context,'module',{ __proto__: null, configurable: true, writable: true, value: replModule, }); - ObjectDefineProperty(context, 'require', { + ObjectDefineProperty(context,'require',{ __proto__: null, configurable: true, writable: true, value: makeRequireFunction(replModule), }); - addBuiltinLibsToObject(context, ''); + addBuiltinLibsToObject(context,''); return context; }; -REPLServer.prototype.resetContext = function() { - this.context = this.createContext(); - this.underscoreAssigned = false; - this.underscoreErrAssigned = false; +REPLServer.prototype.resetContext=function() { + this.context=this.createContext(); + this.underscoreAssigned=false; + this.underscoreErrAssigned=false; // TODO(BridgeAR): Deprecate the lines. - this.lines = []; - this.lines.level = []; + this.lines=[]; + this.lines.level=[]; - ObjectDefineProperty(this.context, '_', { + ObjectDefineProperty(this.context,'_',{ __proto__: null, configurable: true, get: () => this.last, set: (value) => { - this.last = value; - if (!this.underscoreAssigned) { - this.underscoreAssigned = true; + this.last=value; + if(!this.underscoreAssigned) { + this.underscoreAssigned=true; this.output.write('Expression assignment to _ now disabled.\n'); } }, }); - ObjectDefineProperty(this.context, '_error', { + ObjectDefineProperty(this.context,'_error',{ __proto__: null, configurable: true, get: () => this.lastError, set: (value) => { - this.lastError = value; - if (!this.underscoreErrAssigned) { - this.underscoreErrAssigned = true; + this.lastError=value; + if(!this.underscoreErrAssigned) { + this.underscoreErrAssigned=true; this.output.write( 'Expression assignment to _error now disabled.\n'); } @@ -1182,128 +1176,128 @@ REPLServer.prototype.resetContext = function() { }); // Allow REPL extensions to extend the new context - this.emit('reset', this.context); + this.emit('reset',this.context); }; -REPLServer.prototype.displayPrompt = function(preserveCursor) { - let prompt = this._initialPrompt; - if (this[kBufferedCommandSymbol].length) { - prompt = '...'; - const len = this.lines.level.length ? this.lines.level.length - 1 : 0; - const levelInd = StringPrototypeRepeat('..', len); - prompt += levelInd + ' '; +REPLServer.prototype.displayPrompt=function(preserveCursor) { + let prompt=this._initialPrompt; + if(this[kBufferedCommandSymbol].length) { + prompt='...'; + const len=this.lines.level.length? this.lines.level.length-1:0; + const levelInd=StringPrototypeRepeat('..',len); + prompt+=levelInd+' '; } // Do not overwrite `_initialPrompt` here - ReflectApply(Interface.prototype.setPrompt, this, [prompt]); + ReflectApply(Interface.prototype.setPrompt,this,[prompt]); this.prompt(preserveCursor); }; // When invoked as an API method, overwrite _initialPrompt -REPLServer.prototype.setPrompt = function setPrompt(prompt) { - this._initialPrompt = prompt; - ReflectApply(Interface.prototype.setPrompt, this, [prompt]); +REPLServer.prototype.setPrompt=function setPrompt(prompt) { + this._initialPrompt=prompt; + ReflectApply(Interface.prototype.setPrompt,this,[prompt]); }; -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*["'](.*)/; -const simpleExpressionRE = - /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; -const versionedFileNamesRe = /-\d+\.\d+/; +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*["'](.*)/; +const simpleExpressionRE= + /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; +const versionedFileNamesRe=/-\d+\.\d+/; function isIdentifier(str) { - if (str === '') { + if(str==='') { return false; } - const first = StringPrototypeCodePointAt(str, 0); - if (!isIdentifierStart(first)) { + const first=StringPrototypeCodePointAt(str,0); + if(!isIdentifierStart(first)) { return false; } - const firstLen = first > 0xffff ? 2 : 1; - for (let i = firstLen; i < str.length; i += 1) { - const cp = StringPrototypeCodePointAt(str, i); - if (!isIdentifierChar(cp)) { + const firstLen=first>0xffff? 2:1; + for(let i=firstLen;i 0xffff) { - i += 1; + if(cp>0xffff) { + i+=1; } } return true; } function isNotLegacyObjectPrototypeMethod(str) { - return isIdentifier(str) && - str !== '__defineGetter__' && - str !== '__defineSetter__' && - str !== '__lookupGetter__' && - str !== '__lookupSetter__'; + return isIdentifier(str)&& + str!=='__defineGetter__'&& + str!=='__defineSetter__'&& + str!=='__lookupGetter__'&& + str!=='__lookupSetter__'; } function filteredOwnPropertyNames(obj) { - if (!obj) return []; + if(!obj) return []; // `Object.prototype` is the only non-contrived object that fulfills // `Object.getPrototypeOf(X) === null && // Object.getPrototypeOf(Object.getPrototypeOf(X.constructor)) === X`. - let isObjectPrototype = false; - if (ObjectGetPrototypeOf(obj) === null) { - const ctorDescriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor'); - if (ctorDescriptor && ctorDescriptor.value) { - const ctorProto = ObjectGetPrototypeOf(ctorDescriptor.value); - isObjectPrototype = ctorProto && ObjectGetPrototypeOf(ctorProto) === obj; + let isObjectPrototype=false; + if(ObjectGetPrototypeOf(obj)===null) { + const ctorDescriptor=ObjectGetOwnPropertyDescriptor(obj,'constructor'); + if(ctorDescriptor&&ctorDescriptor.value) { + const ctorProto=ObjectGetPrototypeOf(ctorDescriptor.value); + isObjectPrototype=ctorProto&&ObjectGetPrototypeOf(ctorProto)===obj; } } - const filter = ALL_PROPERTIES | SKIP_SYMBOLS; + const filter=ALL_PROPERTIES|SKIP_SYMBOLS; return ArrayPrototypeFilter( - getOwnNonIndexProperties(obj, filter), - isObjectPrototype ? isNotLegacyObjectPrototypeMethod : isIdentifier); + getOwnNonIndexProperties(obj,filter), + isObjectPrototype? isNotLegacyObjectPrototypeMethod:isIdentifier); } function getGlobalLexicalScopeNames(contextId) { return sendInspectorCommand((session) => { - let names = []; - session.post('Runtime.globalLexicalScopeNames', { + let names=[]; + session.post('Runtime.globalLexicalScopeNames',{ executionContextId: contextId, - }, (error, result) => { - if (!error) names = result.names; + },(error,result) => { + if(!error) names=result.names; }); return names; - }, () => []); + },() => []); } -REPLServer.prototype.complete = function() { - ReflectApply(this.completer, this, arguments); +REPLServer.prototype.complete=function() { + ReflectApply(this.completer,this,arguments); }; function gracefulReaddir(...args) { try { - return ReflectApply(fs.readdirSync, null, args); + return ReflectApply(fs.readdirSync,null,args); } catch { // Continue regardless of error. } } function completeFSFunctions(match) { - let baseName = ''; - let filePath = match[1]; - let fileList = gracefulReaddir(filePath, { withFileTypes: true }); - - if (!fileList) { - baseName = path.basename(filePath); - filePath = path.dirname(filePath); - fileList = gracefulReaddir(filePath, { withFileTypes: true }) || []; + let baseName=''; + let filePath=match[1]; + let fileList=gracefulReaddir(filePath,{withFileTypes: true}); + + if(!fileList) { + baseName=path.basename(filePath); + filePath=path.dirname(filePath); + fileList=gracefulReaddir(filePath,{withFileTypes: true})||[]; } - const completions = ArrayPrototypeMap( + const completions=ArrayPrototypeMap( ArrayPrototypeFilter( fileList, - (dirent) => StringPrototypeStartsWith(dirent.name, baseName), + (dirent) => StringPrototypeStartsWith(dirent.name,baseName), ), (d) => d.name, ); - return [[completions], baseName]; + return [[completions],baseName]; } // Provide a list of completions for the given leading text. This is @@ -1316,224 +1310,224 @@ function completeFSFunctions(match) { // // Warning: This eval's code like "foo.bar.baz", so it will run property // getter code. -function complete(line, callback) { +function complete(line,callback) { // List of completion lists, one for each inheritance "level" - let completionGroups = []; - let completeOn, group; + let completionGroups=[]; + let completeOn,group; // Ignore right whitespace. It could change the outcome. - line = StringPrototypeTrimLeft(line); + line=StringPrototypeTrimLeft(line); - let filter = ''; + let filter=''; let match; // REPL commands (e.g. ".break"). - if ((match = RegExpPrototypeExec(/^\s*\.(\w*)$/, line)) !== null) { - ArrayPrototypePush(completionGroups, ObjectKeys(this.commands)); - completeOn = match[1]; - if (completeOn.length) { - filter = completeOn; + if((match=RegExpPrototypeExec(/^\s*\.(\w*)$/,line))!==null) { + ArrayPrototypePush(completionGroups,ObjectKeys(this.commands)); + completeOn=match[1]; + if(completeOn.length) { + filter=completeOn; } - } else if ((match = RegExpPrototypeExec(requireRE, line)) !== null) { + } else if((match=RegExpPrototypeExec(requireRE,line))!==null) { // require('...') - completeOn = match[1]; - filter = completeOn; - if (this.allowBlockingCompletions) { - const subdir = match[2] || ''; - const extensions = ObjectKeys(this.context.require.extensions); - const indexes = ArrayPrototypeMap(extensions, - (extension) => `index${extension}`); - ArrayPrototypePush(indexes, 'package.json', 'index'); - - group = []; - let paths = []; - - if (completeOn === '.') { - group = ['./', '../']; - } else if (completeOn === '..') { - group = ['../']; - } else if (RegExpPrototypeExec(/^\.\.?\//, completeOn) !== null) { - paths = [process.cwd()]; + completeOn=match[1]; + filter=completeOn; + if(this.allowBlockingCompletions) { + const subdir=match[2]||''; + const extensions=ObjectKeys(this.context.require.extensions); + const indexes=ArrayPrototypeMap(extensions, + (extension) => `index${extension}`); + ArrayPrototypePush(indexes,'package.json','index'); + + group=[]; + let paths=[]; + + if(completeOn==='.') { + group=['./','../']; + } else if(completeOn==='..') { + group=['../']; + } else if(RegExpPrototypeExec(/^\.\.?\//,completeOn)!==null) { + paths=[process.cwd()]; } else { - paths = []; - ArrayPrototypePushApply(paths, module.paths); - ArrayPrototypePushApply(paths, CJSModule.globalPaths); + paths=[]; + ArrayPrototypePushApply(paths,module.paths); + ArrayPrototypePushApply(paths,CJSModule.globalPaths); } - ArrayPrototypeForEach(paths, (dir) => { - dir = path.resolve(dir, subdir); - const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; - ArrayPrototypeForEach(dirents, (dirent) => { - if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null || - dirent.name === '.npm') { + ArrayPrototypeForEach(paths,(dir) => { + dir=path.resolve(dir,subdir); + const dirents=gracefulReaddir(dir,{withFileTypes: true})||[]; + ArrayPrototypeForEach(dirents,(dirent) => { + if(RegExpPrototypeExec(versionedFileNamesRe,dirent.name)!==null|| + dirent.name==='.npm') { // Exclude versioned names that 'npm' installs. return; } - const extension = path.extname(dirent.name); - const base = StringPrototypeSlice(dirent.name, 0, -extension.length); - if (!dirent.isDirectory()) { - if (StringPrototypeIncludes(extensions, extension) && - (!subdir || base !== 'index')) { - ArrayPrototypePush(group, `${subdir}${base}`); + const extension=path.extname(dirent.name); + const base=StringPrototypeSlice(dirent.name,0,-extension.length); + if(!dirent.isDirectory()) { + if(StringPrototypeIncludes(extensions,extension)&& + (!subdir||base!=='index')) { + ArrayPrototypePush(group,`${subdir}${base}`); } return; } - ArrayPrototypePush(group, `${subdir}${dirent.name}/`); - const absolute = path.resolve(dir, dirent.name); - if (ArrayPrototypeSome( - gracefulReaddir(absolute) || [], - (subfile) => ArrayPrototypeIncludes(indexes, subfile), + ArrayPrototypePush(group,`${subdir}${dirent.name}/`); + const absolute=path.resolve(dir,dirent.name); + if(ArrayPrototypeSome( + gracefulReaddir(absolute)||[], + (subfile) => ArrayPrototypeIncludes(indexes,subfile), )) { - ArrayPrototypePush(group, `${subdir}${dirent.name}`); + ArrayPrototypePush(group,`${subdir}${dirent.name}`); } }); }); - if (group.length) { - ArrayPrototypePush(completionGroups, group); + if(group.length) { + ArrayPrototypePush(completionGroups,group); } } - ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); - } else if ((match = RegExpPrototypeExec(importRE, line)) !== null) { + ArrayPrototypePush(completionGroups,_builtinLibs,nodeSchemeBuiltinLibs); + } else if((match=RegExpPrototypeExec(importRE,line))!==null) { // import('...') - completeOn = match[1]; - filter = completeOn; - if (this.allowBlockingCompletions) { - const subdir = match[2] || ''; + completeOn=match[1]; + filter=completeOn; + if(this.allowBlockingCompletions) { + const subdir=match[2]||''; // File extensions that can be imported: - const extensions = ObjectKeys(extensionFormatMap); + const extensions=ObjectKeys(extensionFormatMap); // Only used when loading bare module specifiers from `node_modules`: - const indexes = ArrayPrototypeMap(extensions, (ext) => `index${ext}`); - ArrayPrototypePush(indexes, 'package.json'); - - group = []; - let paths = []; - if (completeOn === '.') { - group = ['./', '../']; - } else if (completeOn === '..') { - group = ['../']; - } else if (RegExpPrototypeExec(/^\.\.?\//, completeOn) !== null) { - paths = [process.cwd()]; + const indexes=ArrayPrototypeMap(extensions,(ext) => `index${ext}`); + ArrayPrototypePush(indexes,'package.json'); + + group=[]; + let paths=[]; + if(completeOn==='.') { + group=['./','../']; + } else if(completeOn==='..') { + group=['../']; + } else if(RegExpPrototypeExec(/^\.\.?\//,completeOn)!==null) { + paths=[process.cwd()]; } else { - paths = ArrayPrototypeSlice(module.paths); + paths=ArrayPrototypeSlice(module.paths); } - ArrayPrototypeForEach(paths, (dir) => { - dir = path.resolve(dir, subdir); - const isInNodeModules = path.basename(dir) === 'node_modules'; - const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; - ArrayPrototypeForEach(dirents, (dirent) => { - const { name } = dirent; - if (RegExpPrototypeExec(versionedFileNamesRe, name) !== null || - name === '.npm') { + ArrayPrototypeForEach(paths,(dir) => { + dir=path.resolve(dir,subdir); + const isInNodeModules=path.basename(dir)==='node_modules'; + const dirents=gracefulReaddir(dir,{withFileTypes: true})||[]; + ArrayPrototypeForEach(dirents,(dirent) => { + const {name}=dirent; + if(RegExpPrototypeExec(versionedFileNamesRe,name)!==null|| + name==='.npm') { // Exclude versioned names that 'npm' installs. return; } - if (!dirent.isDirectory()) { - const extension = path.extname(name); - if (StringPrototypeIncludes(extensions, extension)) { - ArrayPrototypePush(group, `${subdir}${name}`); + if(!dirent.isDirectory()) { + const extension=path.extname(name); + if(StringPrototypeIncludes(extensions,extension)) { + ArrayPrototypePush(group,`${subdir}${name}`); } return; } - ArrayPrototypePush(group, `${subdir}${name}/`); - if (!subdir && isInNodeModules) { - const absolute = path.resolve(dir, name); - const subfiles = gracefulReaddir(absolute) || []; - if (ArrayPrototypeSome(subfiles, (subfile) => { - return ArrayPrototypeIncludes(indexes, subfile); + ArrayPrototypePush(group,`${subdir}${name}/`); + if(!subdir&&isInNodeModules) { + const absolute=path.resolve(dir,name); + const subfiles=gracefulReaddir(absolute)||[]; + if(ArrayPrototypeSome(subfiles,(subfile) => { + return ArrayPrototypeIncludes(indexes,subfile); })) { - ArrayPrototypePush(group, `${subdir}${name}`); + ArrayPrototypePush(group,`${subdir}${name}`); } } }); }); - if (group.length) { - ArrayPrototypePush(completionGroups, group); + if(group.length) { + ArrayPrototypePush(completionGroups,group); } } - ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); - } else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null && - this.allowBlockingCompletions) { - ({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match)); - // Handle variable member lookup. - // We support simple chained expressions like the following (no function - // calls, etc.). That is for simplicity and also because we *eval* that - // leading expression so for safety (see WARNING above) don't want to - // eval function calls. - // - // foo.bar<|> # completions for 'foo' with filter 'bar' - // spam.eggs.<|> # completions for 'spam.eggs' with filter '' - // foo<|> # all scope vars with filter 'foo' - // foo.<|> # completions for 'foo' with filter '' - } else if (line.length === 0 || - RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { - const { 0: match } = RegExpPrototypeExec(simpleExpressionRE, line) || ['']; - if (line.length !== 0 && !match) { + ArrayPrototypePush(completionGroups,_builtinLibs,nodeSchemeBuiltinLibs); + } else if((match=RegExpPrototypeExec(fsAutoCompleteRE,line))!==null&& + this.allowBlockingCompletions) { + ({0: completionGroups,1: completeOn}=completeFSFunctions(match)); + // Handle variable member lookup. + // We support simple chained expressions like the following (no function + // calls, etc.). That is for simplicity and also because we *eval* that + // leading expression so for safety (see WARNING above) don't want to + // eval function calls. + // + // foo.bar<|> # completions for 'foo' with filter 'bar' + // spam.eggs.<|> # completions for 'spam.eggs' with filter '' + // foo<|> # all scope vars with filter 'foo' + // foo.<|> # completions for 'foo' with filter '' + } else if(line.length===0|| + RegExpPrototypeExec(/\w|\.|\$/,line[line.length-1])!==null) { + const {0: match}=RegExpPrototypeExec(simpleExpressionRE,line)||['']; + if(line.length!==0&&!match) { completionGroupsLoaded(); return; } - let expr = ''; - completeOn = match; - if (StringPrototypeEndsWith(line, '.')) { - expr = StringPrototypeSlice(match, 0, -1); - } else if (line.length !== 0) { - const bits = StringPrototypeSplit(match, '.'); - filter = ArrayPrototypePop(bits); - expr = ArrayPrototypeJoin(bits, '.'); + let expr=''; + completeOn=match; + if(StringPrototypeEndsWith(line,'.')) { + expr=StringPrototypeSlice(match,0,-1); + } else if(line.length!==0) { + const bits=StringPrototypeSplit(match,'.'); + filter=ArrayPrototypePop(bits); + expr=ArrayPrototypeJoin(bits,'.'); } // Resolve expr and get its completions. - if (!expr) { + if(!expr) { // Get global vars synchronously ArrayPrototypePush(completionGroups, - getGlobalLexicalScopeNames(this[kContextId])); - let contextProto = this.context; - while ((contextProto = ObjectGetPrototypeOf(contextProto)) !== null) { + getGlobalLexicalScopeNames(this[kContextId])); + let contextProto=this.context; + while((contextProto=ObjectGetPrototypeOf(contextProto))!==null) { ArrayPrototypePush(completionGroups, - filteredOwnPropertyNames(contextProto)); + filteredOwnPropertyNames(contextProto)); } - const contextOwnNames = filteredOwnPropertyNames(this.context); - if (!this.useGlobal) { + const contextOwnNames=filteredOwnPropertyNames(this.context); + 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); + ArrayPrototypePush(contextOwnNames,...globalBuiltins); } - ArrayPrototypePush(completionGroups, contextOwnNames); - if (filter !== '') addCommonWords(completionGroups); + ArrayPrototypePush(completionGroups,contextOwnNames); + if(filter!=='') addCommonWords(completionGroups); completionGroupsLoaded(); return; } - let chaining = '.'; - if (StringPrototypeEndsWith(expr, '?')) { - expr = StringPrototypeSlice(expr, 0, -1); - chaining = '?.'; + let chaining='.'; + if(StringPrototypeEndsWith(expr,'?')) { + expr=StringPrototypeSlice(expr,0,-1); + chaining='?.'; } - const memberGroups = []; - const evalExpr = `try { ${expr} } catch {}`; - this.eval(evalExpr, this.context, getREPLResourceName(), (e, obj) => { + const memberGroups=[]; + const evalExpr=`try { ${expr} } catch {}`; + this.eval(evalExpr,this.context,getREPLResourceName(),(e,obj) => { try { let p; - if ((typeof obj === 'object' && obj !== null) || - typeof obj === 'function') { + if((typeof obj==='object'&&obj!==null)|| + typeof obj==='function') { memberGroups.push(filteredOwnPropertyNames(obj)); - p = ObjectGetPrototypeOf(obj); + p=ObjectGetPrototypeOf(obj); } else { - p = obj.constructor ? obj.constructor.prototype : null; + p=obj.constructor? obj.constructor.prototype:null; } // Circular refs possible? Let's guard against that. - let sentinel = 5; - while (p !== null && sentinel-- !== 0) { + let sentinel=5; + while(p!==null&&sentinel--!==0) { memberGroups.push(filteredOwnPropertyNames(p)); - p = ObjectGetPrototypeOf(p); + p=ObjectGetPrototypeOf(p); } } catch { // Maybe a Proxy object without `getOwnPropertyNames` trap. @@ -1542,15 +1536,15 @@ function complete(line, callback) { // https://github.com/nodejs/node/issues/2119 } - if (memberGroups.length) { - expr += chaining; - ArrayPrototypeForEach(memberGroups, (group) => { + if(memberGroups.length) { + expr+=chaining; + ArrayPrototypeForEach(memberGroups,(group) => { ArrayPrototypePush(completionGroups, - ArrayPrototypeMap(group, - (member) => `${expr}${member}`)); + ArrayPrototypeMap(group, + (member) => `${expr}${member}`)); }); - if (filter) { - filter = `${expr}${filter}`; + if(filter) { + filter=`${expr}${filter}`; } } @@ -1565,11 +1559,11 @@ function complete(line, callback) { // Useful for async autocompletion function completionGroupsLoaded() { // Filter, sort (within each group), uniq and merge the completion groups. - if (completionGroups.length && filter) { - const newCompletionGroups = []; - const lowerCaseFilter = StringPrototypeToLocaleLowerCase(filter); - ArrayPrototypeForEach(completionGroups, (group) => { - const filteredGroup = ArrayPrototypeFilter(group, (str) => { + if(completionGroups.length&&filter) { + const newCompletionGroups=[]; + const lowerCaseFilter=StringPrototypeToLocaleLowerCase(filter); + ArrayPrototypeForEach(completionGroups,(group) => { + const filteredGroup=ArrayPrototypeFilter(group,(str) => { // Filter is always case-insensitive following chromium autocomplete // behavior. return StringPrototypeStartsWith( @@ -1577,105 +1571,105 @@ function complete(line, callback) { lowerCaseFilter, ); }); - if (filteredGroup.length) { - ArrayPrototypePush(newCompletionGroups, filteredGroup); + if(filteredGroup.length) { + ArrayPrototypePush(newCompletionGroups,filteredGroup); } }); - completionGroups = newCompletionGroups; + completionGroups=newCompletionGroups; } - const completions = []; + const completions=[]; // Unique completions across all groups. - const uniqueSet = new SafeSet(); + const uniqueSet=new SafeSet(); uniqueSet.add(''); // Completion group 0 is the "closest" (least far up the inheritance // chain) so we put its completions last: to be closest in the REPL. - ArrayPrototypeForEach(completionGroups, (group) => { - ArrayPrototypeSort(group, (a, b) => (b > a ? 1 : -1)); - const setSize = uniqueSet.size; - ArrayPrototypeForEach(group, (entry) => { - if (!uniqueSet.has(entry)) { - ArrayPrototypeUnshift(completions, entry); + ArrayPrototypeForEach(completionGroups,(group) => { + ArrayPrototypeSort(group,(a,b) => (b>a? 1:-1)); + const setSize=uniqueSet.size; + ArrayPrototypeForEach(group,(entry) => { + if(!uniqueSet.has(entry)) { + ArrayPrototypeUnshift(completions,entry); uniqueSet.add(entry); } }); // Add a separator between groups. - if (uniqueSet.size !== setSize) { - ArrayPrototypeUnshift(completions, ''); + if(uniqueSet.size!==setSize) { + ArrayPrototypeUnshift(completions,''); } }); // Remove obsolete group entry, if present. - if (completions[0] === '') { + if(completions[0]==='') { ArrayPrototypeShift(completions); } - callback(null, [completions, completeOn]); + callback(null,[completions,completeOn]); } } -REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => { - if (err) return callback(err); +REPLServer.prototype.completeOnEditorMode=(callback) => (err,results) => { + if(err) return callback(err); - const { 0: completions, 1: completeOn = '' } = results; - let result = ArrayPrototypeFilter(completions, Boolean); + const {0: completions,1: completeOn=''}=results; + let result=ArrayPrototypeFilter(completions,Boolean); - if (completeOn && result.length !== 0) { - result = [commonPrefix(result)]; + if(completeOn&&result.length!==0) { + result=[commonPrefix(result)]; } - callback(null, [result, completeOn]); + callback(null,[result,completeOn]); }; -REPLServer.prototype.defineCommand = function(keyword, cmd) { - if (typeof cmd === 'function') { - cmd = { action: cmd }; +REPLServer.prototype.defineCommand=function(keyword,cmd) { + if(typeof cmd==='function') { + cmd={action: cmd}; } else { - validateFunction(cmd.action, 'cmd.action'); + validateFunction(cmd.action,'cmd.action'); } - this.commands[keyword] = cmd; + this.commands[keyword]=cmd; }; // TODO(BridgeAR): This should be replaced with acorn to build an AST. The // language became more complex and using a simple approach like this is not // sufficient anymore. function _memory(cmd) { - const self = this; - self.lines = self.lines || []; - self.lines.level = self.lines.level || []; + const self=this; + self.lines=self.lines||[]; + self.lines.level=self.lines.level||[]; // Save the line so I can do magic later - if (cmd) { - const len = self.lines.level.length ? self.lines.level.length - 1 : 0; - ArrayPrototypePush(self.lines, StringPrototypeRepeat(' ', len) + cmd); + if(cmd) { + const len=self.lines.level.length? self.lines.level.length-1:0; + ArrayPrototypePush(self.lines,StringPrototypeRepeat(' ',len)+cmd); } else { // I don't want to not change the format too much... - ArrayPrototypePush(self.lines, ''); + ArrayPrototypePush(self.lines,''); } - if (!cmd) { - self.lines.level = []; + if(!cmd) { + self.lines.level=[]; return; } // I need to know "depth." // Because I can not tell the difference between a } that // closes an object literal and a } that closes a function - const countMatches = (regex, str) => { - let count = 0; - while (RegExpPrototypeExec(regex, str) !== null) count++; + const countMatches=(regex,str) => { + let count=0; + while(RegExpPrototypeExec(regex,str)!==null) count++; return count; }; // Going down is { and ( e.g. function() { // going up is } and ) - const dw = countMatches(/[{(]/g, cmd); - const up = countMatches(/[})]/g, cmd); - let depth = dw.length - up.length; + const dw=countMatches(/[{(]/g,cmd); + const up=countMatches(/[})]/g,cmd); + let depth=dw.length-up.length; - if (depth) { + if(depth) { (function workIt() { - if (depth > 0) { + if(depth>0) { // Going... down. // Push the line#, depth count, and if the line is a function. // Since JS only has functional scope I only need to remove @@ -1683,23 +1677,23 @@ function _memory(cmd) { // "function() // {" but nothing should break, only tab completion for local // scope will not work for this function. - ArrayPrototypePush(self.lines.level, { - line: self.lines.length - 1, + ArrayPrototypePush(self.lines.level,{ + line: self.lines.length-1, depth: depth, }); - } else if (depth < 0) { + } else if(depth<0) { // Going... up. - const curr = ArrayPrototypePop(self.lines.level); - if (curr) { - const tmp = curr.depth + depth; - if (tmp < 0) { + const curr=ArrayPrototypePop(self.lines.level); + if(curr) { + const tmp=curr.depth+depth; + if(tmp<0) { // More to go, recurse - depth += curr.depth; + depth+=curr.depth; workIt(); - } else if (tmp > 0) { + } else if(tmp>0) { // Remove and push back - curr.depth += depth; - ArrayPrototypePush(self.lines.level, curr); + curr.depth+=depth; + ArrayPrototypePush(self.lines.level,curr); } } } @@ -1710,27 +1704,27 @@ function _memory(cmd) { function addCommonWords(completionGroups) { // Only words which do not yet exist as global property should be added to // this list. - ArrayPrototypePush(completionGroups, [ - 'async', 'await', 'break', 'case', 'catch', 'const', 'continue', - 'debugger', 'default', 'delete', 'do', 'else', 'export', 'false', - 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let', - 'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try', - 'typeof', 'var', 'void', 'while', 'with', 'yield', + ArrayPrototypePush(completionGroups,[ + 'async','await','break','case','catch','const','continue', + 'debugger','default','delete','do','else','export','false', + 'finally','for','function','if','import','in','instanceof','let', + 'new','null','return','switch','this','throw','true','try', + 'typeof','var','void','while','with','yield', ]); } function _turnOnEditorMode(repl) { - repl.editorMode = true; - ReflectApply(Interface.prototype.setPrompt, repl, ['']); + repl.editorMode=true; + ReflectApply(Interface.prototype.setPrompt,repl,['']); } function _turnOffEditorMode(repl) { - repl.editorMode = false; + repl.editorMode=false; repl.setPrompt(repl._initialPrompt); } function defineDefaultCommands(repl) { - repl.defineCommand('break', { + repl.defineCommand('break',{ help: 'Sometimes you get stuck, this gets you out', action: function() { this.clearBufferedCommand(); @@ -1739,16 +1733,16 @@ function defineDefaultCommands(repl) { }); let clearMessage; - if (repl.useGlobal) { - clearMessage = 'Alias for .break'; + if(repl.useGlobal) { + clearMessage='Alias for .break'; } else { - clearMessage = 'Break, and also clear the local context'; + clearMessage='Break, and also clear the local context'; } - repl.defineCommand('clear', { + repl.defineCommand('clear',{ help: clearMessage, action: function() { this.clearBufferedCommand(); - if (!this.useGlobal) { + if(!this.useGlobal) { this.output.write('Clearing context...\n'); this.resetContext(); } @@ -1756,38 +1750,38 @@ function defineDefaultCommands(repl) { }, }); - repl.defineCommand('exit', { + repl.defineCommand('exit',{ help: 'Exit the REPL', action: function() { this.close(); }, }); - repl.defineCommand('help', { + repl.defineCommand('help',{ help: 'Print this help message', action: function() { - const names = ArrayPrototypeSort(ObjectKeys(this.commands)); - const longestNameLength = MathMaxApply( - ArrayPrototypeMap(names, (name) => name.length), + const names=ArrayPrototypeSort(ObjectKeys(this.commands)); + const longestNameLength=MathMaxApply( + ArrayPrototypeMap(names,(name) => name.length), ); - ArrayPrototypeForEach(names, (name) => { - const cmd = this.commands[name]; - const spaces = - StringPrototypeRepeat(' ', longestNameLength - name.length + 3); - const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; + ArrayPrototypeForEach(names,(name) => { + const cmd=this.commands[name]; + const spaces= + StringPrototypeRepeat(' ',longestNameLength-name.length+3); + const line=`.${name}${cmd.help? spaces+cmd.help:''}\n`; this.output.write(line); }); - this.output.write('\nPress Ctrl+C to abort current expression, ' + + this.output.write('\nPress Ctrl+C to abort current expression, '+ 'Ctrl+D to exit the REPL\n'); this.displayPrompt(); }, }); - repl.defineCommand('save', { + repl.defineCommand('save',{ help: 'Save all evaluated commands in this REPL session to a file', action: function(file) { try { - fs.writeFileSync(file, ArrayPrototypeJoin(this.lines, '\n')); + fs.writeFileSync(file,ArrayPrototypeJoin(this.lines,'\n')); this.output.write(`Session saved to: ${file}\n`); } catch { this.output.write(`Failed to save: ${file}\n`); @@ -1796,14 +1790,14 @@ function defineDefaultCommands(repl) { }, }); - repl.defineCommand('load', { + repl.defineCommand('load',{ help: 'Load JS from a file into the REPL session', action: function(file) { try { - const stats = fs.statSync(file); - if (stats && stats.isFile()) { + const stats=fs.statSync(file); + if(stats&&stats.isFile()) { _turnOnEditorMode(this); - const data = fs.readFileSync(file, 'utf8'); + const data=fs.readFileSync(file,'utf8'); this.write(data); _turnOffEditorMode(this); this.write('\n'); @@ -1818,8 +1812,8 @@ function defineDefaultCommands(repl) { this.displayPrompt(); }, }); - if (repl.terminal) { - repl.defineCommand('editor', { + if(repl.terminal) { + repl.defineCommand('editor',{ help: 'Enter editor mode', action() { _turnOnEditorMode(this); @@ -1831,12 +1825,12 @@ function defineDefaultCommands(repl) { } function Recoverable(err) { - this.err = err; + this.err=err; } -ObjectSetPrototypeOf(Recoverable.prototype, SyntaxErrorPrototype); -ObjectSetPrototypeOf(Recoverable, SyntaxError); +ObjectSetPrototypeOf(Recoverable.prototype,SyntaxErrorPrototype); +ObjectSetPrototypeOf(Recoverable,SyntaxError); -module.exports = { +module.exports={ start, writer, REPLServer, @@ -1845,26 +1839,26 @@ module.exports = { Recoverable, }; -ObjectDefineProperty(module.exports, 'builtinModules', { +ObjectDefineProperty(module.exports,'builtinModules',{ __proto__: null, get: () => _builtinLibs, - set: (val) => _builtinLibs = val, + set: (val) => _builtinLibs=val, enumerable: true, configurable: true, }); -ObjectDefineProperty(module.exports, '_builtinLibs', { +ObjectDefineProperty(module.exports,'_builtinLibs',{ __proto__: null, - get: pendingDeprecation ? deprecate( + get: pendingDeprecation? deprecate( () => _builtinLibs, 'repl._builtinLibs is deprecated. Check module.builtinModules instead', 'DEP0142', - ) : () => _builtinLibs, - set: pendingDeprecation ? deprecate( - (val) => _builtinLibs = val, + ):() => _builtinLibs, + set: pendingDeprecation? deprecate( + (val) => _builtinLibs=val, 'repl._builtinLibs is deprecated. Check module.builtinModules instead', 'DEP0142', - ) : (val) => _builtinLibs = val, + ):(val) => _builtinLibs=val, enumerable: false, configurable: true, }); diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 844c9e7ad5137f..e1313d11974b30 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -819,7 +819,7 @@ const tcpTests = [ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const comeOn = await import(\'fhqwhgads\');', +alternatively use dynamic import: await import(\'fhqwhgads\');', ] }, { @@ -830,7 +830,7 @@ alternatively use dynamic import: const comeOn = await import(\'fhqwhgads\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const {export1,export2} = await import(\'module-name\');', +alternatively use dynamic import: await import(\'module-name\');', ] }, { @@ -841,7 +841,7 @@ alternatively use dynamic import: const {export1,export2} = await import(\'modul '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const name = await import(\'module-name\');', +alternatively use dynamic import: await import(\'module-name\');', ] }, { @@ -852,7 +852,7 @@ alternatively use dynamic import: const name = await import(\'module-name\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const moduleName = await import(\'module-name\');', +alternatively use dynamic import: await import(\'module-name\');', ] }, { @@ -863,7 +863,18 @@ alternatively use dynamic import: const moduleName = await import(\'module-name\ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const {export1:localName1,export2} = await import(\'bar\');', +alternatively use dynamic import: await import(\'bar\');', + ] + }, + { + send: 'import {default as alias} from "bar";', + expect: [ + kSource, + kArrow, + '', + 'Uncaught:', + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: await import(\'bar\');', ] }, ]; From 708665d1fefe2ca1b383741e1a2f117b0ab6d855 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 13:46:18 -0700 Subject: [PATCH 11/33] fix: whitespaces --- lib/repl.js | 1540 +++++++++++++++++++++++++-------------------------- 1 file changed, 769 insertions(+), 771 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 513e3b1fe25559..32f3a82c4d9d1a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -94,50 +94,48 @@ const { SyntaxError, SyntaxErrorPrototype, globalThis, -}=primordials; +} = primordials; -const {BuiltinModule}=require('internal/bootstrap/realm'); +const { BuiltinModule } = require('internal/bootstrap/realm'); const { makeRequireFunction, addBuiltinLibsToObject, -}=require('internal/modules/helpers'); +} = require('internal/modules/helpers'); const { isIdentifierStart, isIdentifierChar, - parse: acornParse, -}=require('internal/deps/acorn/acorn/dist/acorn'); -const acornWalk=require('internal/deps/acorn/acorn-walk/dist/walk'); +} = require('internal/deps/acorn/acorn/dist/acorn'); const { decorateErrorStack, isError, deprecate, SideEffectFreeRegExpPrototypeSymbolReplace, SideEffectFreeRegExpPrototypeSymbolSplit, -}=require('internal/util'); -const {inspect}=require('internal/util/inspect'); -const vm=require('vm'); -const path=require('path'); -const fs=require('fs'); -const {Interface}=require('readline'); +} = require('internal/util'); +const { inspect } = require('internal/util/inspect'); +const vm = require('vm'); +const path = require('path'); +const fs = require('fs'); +const { Interface } = require('readline'); const { commonPrefix, -}=require('internal/readline/utils'); -const {Console}=require('console'); -const {shouldColorize}=require('internal/util/colors'); -const CJSModule=require('internal/modules/cjs/loader').Module; -let _builtinLibs=ArrayPrototypeFilter( +} = require('internal/readline/utils'); +const { Console } = require('console'); +const { shouldColorize } = require('internal/util/colors'); +const CJSModule = require('internal/modules/cjs/loader').Module; +let _builtinLibs = ArrayPrototypeFilter( CJSModule.builtinModules, - (e) => !StringPrototypeStartsWith(e,'_'), + (e) => !StringPrototypeStartsWith(e, '_'), ); -const nodeSchemeBuiltinLibs=ArrayPrototypeMap( - _builtinLibs,(lib) => `node:${lib}`); +const nodeSchemeBuiltinLibs = ArrayPrototypeMap( + _builtinLibs, (lib) => `node:${lib}`); ArrayPrototypeForEach( BuiltinModule.getSchemeOnlyModuleNames(), - (lib) => ArrayPrototypePush(nodeSchemeBuiltinLibs,`node:${lib}`), + (lib) => ArrayPrototypePush(nodeSchemeBuiltinLibs, `node:${lib}`), ); -const domain=require('domain'); -let debug=require('internal/util/debuglog').debuglog('repl',(fn) => { - debug=fn; +const domain = require('domain'); +let debug = require('internal/util/debuglog').debuglog('repl', (fn) => { + debug = fn; }); const { codes: { @@ -148,17 +146,17 @@ const { }, isErrorStackTraceLimitWritable, overrideStackTrace, -}=require('internal/errors'); -const {sendInspectorCommand}=require('internal/util/inspector'); -const {getOptionValue}=require('internal/options'); +} = require('internal/errors'); +const { sendInspectorCommand } = require('internal/util/inspector'); +const { getOptionValue } = require('internal/options'); const { validateFunction, validateObject, -}=require('internal/validators'); -const experimentalREPLAwait=getOptionValue( +} = require('internal/validators'); +const experimentalREPLAwait = getOptionValue( '--experimental-repl-await', ); -const pendingDeprecation=getOptionValue('--pending-deprecation'); +const pendingDeprecation = getOptionValue('--pending-deprecation'); const { REPL_MODE_SLOPPY, REPL_MODE_STRICT, @@ -166,25 +164,25 @@ const { kStandaloneREPL, setupPreview, setupReverseSearch, -}=require('internal/repl/utils'); +} = require('internal/repl/utils'); const { constants: { ALL_PROPERTIES, SKIP_SYMBOLS, }, getOwnNonIndexProperties, -}=internalBinding('util'); +} = internalBinding('util'); const { startSigintWatchdog, stopSigintWatchdog, -}=internalBinding('contextify'); +} = internalBinding('contextify'); -const history=require('internal/repl/history'); +const history = require('internal/repl/history'); const { extensionFormatMap, -}=require('internal/modules/esm/formats'); +} = require('internal/modules/esm/formats'); -let nextREPLResourceNumber=1; +let nextREPLResourceNumber = 1; // This prevents v8 code cache from getting confused and using a different // cache from a resource of the same name function getREPLResourceName() { @@ -194,36 +192,36 @@ function getREPLResourceName() { // Lazy-loaded. let processTopLevelAwait; -const globalBuiltins= +const globalBuiltins = new SafeSet(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)')); -const parentModule=module; -const domainSet=new SafeWeakSet(); +const parentModule = module; +const domainSet = new SafeWeakSet(); -const kBufferedCommandSymbol=Symbol('bufferedCommand'); -const kContextId=Symbol('contextId'); +const kBufferedCommandSymbol = Symbol('bufferedCommand'); +const kContextId = Symbol('contextId'); -let addedNewListener=false; +let addedNewListener = false; try { // Hack for require.resolve("./relative") to work properly. - module.filename=path.resolve('repl'); + module.filename = path.resolve('repl'); } catch { // path.resolve('repl') fails when the current working directory has been // deleted. Fall back to the directory name of the (absolute) executable // path. It's not really correct but what are the alternatives? - const dirname=path.dirname(process.execPath); - module.filename=path.resolve(dirname,'repl'); + const dirname = path.dirname(process.execPath); + module.filename = path.resolve(dirname, 'repl'); } // Hack for repl require to work properly with node_modules folders -module.paths=CJSModule._nodeModulePaths(module.filename); +module.paths = CJSModule._nodeModulePaths(module.filename); // This is the default "writer" value, if none is passed in the REPL options, // and it can be overridden by custom print functions, such as `probe` or // `eyes.js`. -const writer=(obj) => inspect(obj,writer.options); -writer.options={...inspect.defaultOptions,showProxy: true}; +const writer = (obj) => inspect(obj, writer.options); +writer.options = { ...inspect.defaultOptions, showProxy: true }; // Converts static import statement to dynamic import statement const toDynamicImport=(codeLine) => { @@ -246,129 +244,129 @@ const toDynamicImport=(codeLine) => { }; function REPLServer(prompt, - stream, - eval_, - useGlobal, - ignoreUndefined, - replMode) { - if(!(this instanceof REPLServer)) { + stream, + eval_, + useGlobal, + ignoreUndefined, + replMode) { + if (!(this instanceof REPLServer)) { return new REPLServer(prompt, - stream, - eval_, - useGlobal, - ignoreUndefined, - replMode); + stream, + eval_, + useGlobal, + ignoreUndefined, + replMode); } let options; - if(prompt!==null&&typeof prompt==='object') { + if (prompt !== null && typeof prompt === 'object') { // An options object was given. - options={...prompt}; - stream=options.stream||options.socket; - eval_=options.eval; - useGlobal=options.useGlobal; - ignoreUndefined=options.ignoreUndefined; - prompt=options.prompt; - replMode=options.replMode; + options = { ...prompt }; + stream = options.stream || options.socket; + eval_ = options.eval; + useGlobal = options.useGlobal; + ignoreUndefined = options.ignoreUndefined; + prompt = options.prompt; + replMode = options.replMode; } else { - options={}; + options = {}; } - if(!options.input&&!options.output) { + if (!options.input && !options.output) { // Legacy API, passing a 'stream'/'socket' option. - if(!stream) { + if (!stream) { // Use stdin and stdout as the default streams if none were given. - stream=process; + stream = process; } // We're given a duplex readable/writable Stream, like a `net.Socket` // or a custom object with 2 streams, or the `process` object. - options.input=stream.stdin||stream; - options.output=stream.stdout||stream; + options.input = stream.stdin || stream; + options.output = stream.stdout || stream; } - if(options.terminal===undefined) { - options.terminal=options.output.isTTY; + if (options.terminal === undefined) { + options.terminal = options.output.isTTY; } - options.terminal=!!options.terminal; + options.terminal = !!options.terminal; - if(options.terminal&&options.useColors===undefined) { + if (options.terminal && options.useColors === undefined) { // If possible, check if stdout supports colors or not. - options.useColors=shouldColorize(options.output)||process.env.NODE_DISABLE_COLORS===undefined; + options.useColors = shouldColorize(options.output) || process.env.NODE_DISABLE_COLORS === undefined; } // TODO(devsnek): Add a test case for custom eval functions. - const preview=options.terminal&& - (options.preview!==undefined? !!options.preview:!eval_); + const preview = options.terminal && + (options.preview !== undefined ? !!options.preview : !eval_); - ObjectDefineProperty(this,'inputStream',{ + ObjectDefineProperty(this, 'inputStream', { __proto__: null, - get: pendingDeprecation? + get: pendingDeprecation ? deprecate(() => this.input, - 'repl.inputStream and repl.outputStream are deprecated. '+ - 'Use repl.input and repl.output instead', - 'DEP0141'): + 'repl.inputStream and repl.outputStream are deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0141') : () => this.input, - set: pendingDeprecation? - deprecate((val) => this.input=val, - 'repl.inputStream and repl.outputStream are deprecated. '+ - 'Use repl.input and repl.output instead', - 'DEP0141'): - (val) => this.input=val, + set: pendingDeprecation ? + deprecate((val) => this.input = val, + 'repl.inputStream and repl.outputStream are deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0141') : + (val) => this.input = val, enumerable: false, configurable: true, }); - ObjectDefineProperty(this,'outputStream',{ + ObjectDefineProperty(this, 'outputStream', { __proto__: null, - get: pendingDeprecation? + get: pendingDeprecation ? deprecate(() => this.output, - 'repl.inputStream and repl.outputStream are deprecated. '+ - 'Use repl.input and repl.output instead', - 'DEP0141'): + 'repl.inputStream and repl.outputStream are deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0141') : () => this.output, - set: pendingDeprecation? - deprecate((val) => this.output=val, - 'repl.inputStream and repl.outputStream are deprecated. '+ - 'Use repl.input and repl.output instead', - 'DEP0141'): - (val) => this.output=val, + set: pendingDeprecation ? + deprecate((val) => this.output = val, + 'repl.inputStream and repl.outputStream are deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0141') : + (val) => this.output = val, enumerable: false, configurable: true, }); - this.allowBlockingCompletions=!!options.allowBlockingCompletions; - this.useColors=!!options.useColors; - this._domain=options.domain||domain.create(); - this.useGlobal=!!useGlobal; - this.ignoreUndefined=!!ignoreUndefined; - this.replMode=replMode||module.exports.REPL_MODE_SLOPPY; - this.underscoreAssigned=false; - this.last=undefined; - this.underscoreErrAssigned=false; - this.lastError=undefined; - this.breakEvalOnSigint=!!options.breakEvalOnSigint; - this.editorMode=false; + this.allowBlockingCompletions = !!options.allowBlockingCompletions; + this.useColors = !!options.useColors; + this._domain = options.domain || domain.create(); + this.useGlobal = !!useGlobal; + this.ignoreUndefined = !!ignoreUndefined; + this.replMode = replMode || module.exports.REPL_MODE_SLOPPY; + this.underscoreAssigned = false; + this.last = undefined; + this.underscoreErrAssigned = false; + this.lastError = undefined; + this.breakEvalOnSigint = !!options.breakEvalOnSigint; + this.editorMode = false; // Context id for use with the inspector protocol. - this[kContextId]=undefined; + this[kContextId] = undefined; - if(this.breakEvalOnSigint&&eval_) { + if (this.breakEvalOnSigint && eval_) { // Allowing this would not reflect user expectations. // breakEvalOnSigint affects only the behavior of the default eval(). throw new ERR_INVALID_REPL_EVAL_CONFIG(); } - if(options[kStandaloneREPL]) { + if (options[kStandaloneREPL]) { // It is possible to introspect the running REPL accessing this variable // from inside the REPL. This is useful for anyone working on the REPL. - module.exports.repl=this; - } else if(!addedNewListener) { + module.exports.repl = this; + } else if (!addedNewListener) { // Add this listener only once and use a WeakSet that contains the REPLs // domains. Otherwise we'd have to add a single listener to each REPL // instance and that could trigger the `MaxListenersExceededWarning`. - process.prependListener('newListener',(event,listener) => { - if(event==='uncaughtException'&& - process.domain&& - listener.name!=='domainUncaughtExceptionClear'&& - domainSet.has(process.domain)) { + process.prependListener('newListener', (event, listener) => { + if (event === 'uncaughtException' && + process.domain && + listener.name !== 'domainUncaughtExceptionClear' && + domainSet.has(process.domain)) { // Throw an error so that the event will not be added and the current // domain takes over. That way the user is notified about the error // and the current code evaluation is stopped, just as any other code @@ -377,172 +375,172 @@ function REPLServer(prompt, 'Listeners for `uncaughtException` cannot be used in the REPL'); } }); - addedNewListener=true; + addedNewListener = true; } domainSet.add(this._domain); - const savedRegExMatches=['','','','','','','','','','']; - const sep='\u0000\u0000\u0000'; - const regExMatcher=new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)`+ - `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)`+ - `${sep}(.*)$`); + const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; + const sep = '\u0000\u0000\u0000'; + const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)$`); - eval_=eval_||defaultEval; + eval_ = eval_ || defaultEval; - const self=this; + const self = this; // Pause taking in new input, and store the keys in a buffer. - const pausedBuffer=[]; - let paused=false; + const pausedBuffer = []; + let paused = false; function pause() { - paused=true; + paused = true; } function unpause() { - if(!paused) return; - paused=false; + if (!paused) return; + paused = false; let entry; - const tmpCompletionEnabled=self.isCompletionEnabled; - while((entry=ArrayPrototypeShift(pausedBuffer))!==undefined) { - const {0: type,1: payload,2: isCompletionEnabled}=entry; - switch(type) { + const tmpCompletionEnabled = self.isCompletionEnabled; + while ((entry = ArrayPrototypeShift(pausedBuffer)) !== undefined) { + const { 0: type, 1: payload, 2: isCompletionEnabled } = entry; + switch (type) { case 'key': { - const {0: d,1: key}=payload; - self.isCompletionEnabled=isCompletionEnabled; - self._ttyWrite(d,key); + const { 0: d, 1: key } = payload; + self.isCompletionEnabled = isCompletionEnabled; + self._ttyWrite(d, key); break; } case 'close': self.emit('exit'); break; } - if(paused) { + if (paused) { break; } } - self.isCompletionEnabled=tmpCompletionEnabled; + self.isCompletionEnabled = tmpCompletionEnabled; } - function defaultEval(code,context,file,cb) { - const asyncESM=require('internal/process/esm_loader'); + function defaultEval(code, context, file, cb) { + const asyncESM = require('internal/process/esm_loader'); - let result,script,wrappedErr; - let err=null; - let wrappedCmd=false; - let awaitPromise=false; - const input=code; + let result, script, wrappedErr; + let err = null; + let wrappedCmd = false; + let awaitPromise = false; + const input = code; // It's confusing for `{ a : 1 }` to be interpreted as a block // statement rather than an object literal. So, we first try // to wrap it in parentheses, so that it will be interpreted as // an expression. Note that if the above condition changes, // lib/internal/repl/utils.js needs to be changed to match. - if(RegExpPrototypeExec(/^\s*{/,code)!==null&& - RegExpPrototypeExec(/;\s*$/,code)===null) { - code=`(${StringPrototypeTrim(code)})\n`; - wrappedCmd=true; + if (RegExpPrototypeExec(/^\s*{/, code) !== null && + RegExpPrototypeExec(/;\s*$/, code) === null) { + code = `(${StringPrototypeTrim(code)})\n`; + wrappedCmd = true; } // `experimentalREPLAwait` is set to true by default. // Shall be false in case `--no-experimental-repl-await` flag is used. - if(experimentalREPLAwait&&StringPrototypeIncludes(code,'await')) { - if(processTopLevelAwait===undefined) { - ({processTopLevelAwait}=require('internal/repl/await')); + if (experimentalREPLAwait && StringPrototypeIncludes(code, 'await')) { + if (processTopLevelAwait === undefined) { + ({ processTopLevelAwait } = require('internal/repl/await')); } try { - const potentialWrappedCode=processTopLevelAwait(code); - if(potentialWrappedCode!==null) { - code=potentialWrappedCode; - wrappedCmd=true; - awaitPromise=true; + const potentialWrappedCode = processTopLevelAwait(code); + if (potentialWrappedCode !== null) { + code = potentialWrappedCode; + wrappedCmd = true; + awaitPromise = true; } - } catch(e) { - let recoverableError=false; - if(e.name==='SyntaxError') { + } catch (e) { + let recoverableError = false; + if (e.name === 'SyntaxError') { let parentURL; try { - const {pathToFileURL}=require('url'); + const { pathToFileURL } = require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentURL=pathToFileURL(path.join(process.cwd(),'repl')).href; + parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; } catch { // Continue regardless of error. } // Remove all "await"s and attempt running the script // in order to detect if error is truly non recoverable - const fallbackCode=SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g,code,''); + const fallbackCode = SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g, code, ''); try { - vm.createScript(fallbackCode,{ + vm.createScript(fallbackCode, { filename: file, displayErrors: true, - importModuleDynamically: (specifier,_,importAssertions) => { - return asyncESM.esmLoader.import(specifier,parentURL, - importAssertions); + importModuleDynamically: (specifier, _, importAssertions) => { + return asyncESM.esmLoader.import(specifier, parentURL, + importAssertions); }, }); - } catch(fallbackError) { - if(isRecoverableError(fallbackError,fallbackCode)) { - recoverableError=true; - err=new Recoverable(e); + } catch (fallbackError) { + if (isRecoverableError(fallbackError, fallbackCode)) { + recoverableError = true; + err = new Recoverable(e); } } } - if(!recoverableError) { + if (!recoverableError) { decorateErrorStack(e); - err=e; + err = e; } } } // First, create the Script object to check the syntax - if(code==='\n') + if (code === '\n') return cb(null); - if(err===null) { + if (err === null) { let parentURL; try { - const {pathToFileURL}=require('url'); + const { pathToFileURL } = require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentURL=pathToFileURL(path.join(process.cwd(),'repl')).href; + parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; } catch { // Continue regardless of error. } - while(true) { + while (true) { try { - if(self.replMode===module.exports.REPL_MODE_STRICT&& - RegExpPrototypeExec(/^\s*$/,code)===null) { + if (self.replMode === module.exports.REPL_MODE_STRICT && + RegExpPrototypeExec(/^\s*$/, code) === null) { // "void 0" keeps the repl from returning "use strict" as the result // value for statements and declarations that don't return a value. - code=`'use strict'; void 0;\n${code}`; + code = `'use strict'; void 0;\n${code}`; } - script=vm.createScript(code,{ + script = vm.createScript(code, { filename: file, displayErrors: true, - importModuleDynamically: (specifier,_,importAssertions) => { - return asyncESM.esmLoader.import(specifier,parentURL, - importAssertions); + importModuleDynamically: (specifier, _, importAssertions) => { + return asyncESM.esmLoader.import(specifier, parentURL, + importAssertions); }, }); - } catch(e) { - debug('parse error %j',code,e); - if(wrappedCmd) { + } catch (e) { + debug('parse error %j', code, e); + if (wrappedCmd) { // Unwrap and try again - wrappedCmd=false; - awaitPromise=false; - code=input; - wrappedErr=e; + wrappedCmd = false; + awaitPromise = false; + code = input; + wrappedErr = e; continue; } // Preserve original error for wrapped command - const error=wrappedErr||e; - if(isRecoverableError(error,code)) - err=new Recoverable(error); + const error = wrappedErr || e; + if (isRecoverableError(error, code)) + err = new Recoverable(error); else - err=error; + err = error; } break; } @@ -551,94 +549,94 @@ function REPLServer(prompt, // This will set the values from `savedRegExMatches` to corresponding // predefined RegExp properties `RegExp.$1`, `RegExp.$2` ... `RegExp.$9` RegExpPrototypeExec(regExMatcher, - ArrayPrototypeJoin(savedRegExMatches,sep)); + ArrayPrototypeJoin(savedRegExMatches, sep)); - let finished=false; - function finishExecution(err,result) { - if(finished) return; - finished=true; + let finished = false; + function finishExecution(err, result) { + if (finished) return; + finished = true; // After executing the current expression, store the values of RegExp // predefined properties back in `savedRegExMatches` - for(let idx=1;idx { - sigintListener=() => { - const tmp=Error.stackTraceLimit; - if(isErrorStackTraceLimitWritable()) Error.stackTraceLimit=0; - const err=new ERR_SCRIPT_EXECUTION_INTERRUPTED(); - if(isErrorStackTraceLimitWritable()) Error.stackTraceLimit=tmp; + let promise = result; + if (self.breakEvalOnSigint) { + const interrupt = new Promise((resolve, reject) => { + sigintListener = () => { + const tmp = Error.stackTraceLimit; + if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0; + const err = new ERR_SCRIPT_EXECUTION_INTERRUPTED(); + if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = tmp; reject(err); }; prioritizedSigintQueue.add(sigintListener); }); - promise=SafePromiseRace([promise,interrupt]); + promise = SafePromiseRace([promise, interrupt]); } (async () => { try { - const result=(await promise)?.value; - finishExecution(null,result); - } catch(err) { - if(err&&process.domain) { + const result = (await promise)?.value; + finishExecution(null, result); + } catch (err) { + if (err && process.domain) { debug('not recoverable, send to domain'); - process.domain.emit('error',err); + process.domain.emit('error', err); process.domain.exit(); return; } @@ -652,138 +650,138 @@ function REPLServer(prompt, } } - if(!awaitPromise||err) { - finishExecution(err,result); + if (!awaitPromise || err) { + finishExecution(err, result); } } - self.eval=self._domain.bind(eval_); + self.eval = self._domain.bind(eval_); - self._domain.on('error',function debugDomainError(e) { + self._domain.on('error', function debugDomainError(e) { debug('domain error'); - let errStack=''; + let errStack = ''; - if(typeof e==='object'&&e!==null) { - overrideStackTrace.set(e,(error,stackFrames) => { + if (typeof e === 'object' && e !== null) { + overrideStackTrace.set(e, (error, stackFrames) => { let frames; - if(typeof stackFrames==='object') { + if (typeof stackFrames === 'object') { // Search from the bottom of the call stack to // find the first frame with a null function name - const idx=ArrayPrototypeFindIndex( + const idx = ArrayPrototypeFindIndex( ArrayPrototypeReverse(stackFrames), - (frame) => frame.getFunctionName()===null, + (frame) => frame.getFunctionName() === null, ); // If found, get rid of it and everything below it - frames=ArrayPrototypeSplice(stackFrames,idx+1); + frames = ArrayPrototypeSplice(stackFrames, idx + 1); } else { - frames=stackFrames; + frames = stackFrames; } // FIXME(devsnek): this is inconsistent with the checks // that the real prepareStackTrace dispatch uses in // lib/internal/errors.js. - if(typeof Error.prepareStackTrace==='function') { - return Error.prepareStackTrace(error,frames); + if (typeof Error.prepareStackTrace === 'function') { + return Error.prepareStackTrace(error, frames); } - ArrayPrototypePush(frames,error); - return ArrayPrototypeJoin(ArrayPrototypeReverse(frames),'\n at '); + ArrayPrototypePush(frames, error); + return ArrayPrototypeJoin(ArrayPrototypeReverse(frames), '\n at '); }); decorateErrorStack(e); - if(e.domainThrown) { + if (e.domainThrown) { delete e.domain; delete e.domainThrown; } - if(isError(e)) { - if(e.stack) { - if(e.name==='SyntaxError') { + if (isError(e)) { + if (e.stack) { + if (e.name === 'SyntaxError') { // Remove stack trace. - e.stack=SideEffectFreeRegExpPrototypeSymbolReplace( + e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( /^\s+at\s.*\n?/gm, - SideEffectFreeRegExpPrototypeSymbolReplace(/^REPL\d+:\d+\r?\n/,e.stack,''), + SideEffectFreeRegExpPrototypeSymbolReplace(/^REPL\d+:\d+\r?\n/, e.stack, ''), ''); - const importErrorStr='Cannot use import statement outside a '+ + const importErrorStr = 'Cannot use import statement outside a ' + 'module'; - if(StringPrototypeIncludes(e.message,importErrorStr)) { - e.message='Cannot use import statement inside the Node.js '+ + if (StringPrototypeIncludes(e.message, importErrorStr)) { + e.message = 'Cannot use import statement inside the Node.js ' + 'REPL, alternatively use dynamic import: ' + toDynamicImport(self.lines.at(-1)); - e.stack=SideEffectFreeRegExpPrototypeSymbolReplace( + e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( /SyntaxError:.*\n/, e.stack, `SyntaxError: ${e.message}\n`); } - } else if(self.replMode===module.exports.REPL_MODE_STRICT) { - e.stack=SideEffectFreeRegExpPrototypeSymbolReplace( + } else if (self.replMode === module.exports.REPL_MODE_STRICT) { + e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( /(\s+at\s+REPL\d+:)(\d+)/, e.stack, - (_,pre,line) => pre+(line-1), + (_, pre, line) => pre + (line - 1), ); } } - errStack=self.writer(e); + errStack = self.writer(e); // Remove one line error braces to keep the old style in place. - if(errStack[0]==='['&&errStack[errStack.length-1]===']') { - errStack=StringPrototypeSlice(errStack,1,-1); + if (errStack[0] === '[' && errStack[errStack.length - 1] === ']') { + errStack = StringPrototypeSlice(errStack, 1, -1); } } } - if(!self.underscoreErrAssigned) { - self.lastError=e; + if (!self.underscoreErrAssigned) { + self.lastError = e; } - if(options[kStandaloneREPL]&& - process.listenerCount('uncaughtException')!==0) { + if (options[kStandaloneREPL] && + process.listenerCount('uncaughtException') !== 0) { process.nextTick(() => { - process.emit('uncaughtException',e); + process.emit('uncaughtException', e); self.clearBufferedCommand(); - self.lines.level=[]; + self.lines.level = []; self.displayPrompt(); }); } else { - if(errStack==='') { - errStack=self.writer(e); + if (errStack === '') { + errStack = self.writer(e); } - const lines=SideEffectFreeRegExpPrototypeSymbolSplit(/(?<=\n)/,errStack); - let matched=false; - - errStack=''; - ArrayPrototypeForEach(lines,(line) => { - if(!matched&& - RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/,line)!==null) { - errStack+=writer.options.breakLength>=line.length? - `Uncaught ${line}`: + const lines = SideEffectFreeRegExpPrototypeSymbolSplit(/(?<=\n)/, errStack); + let matched = false; + + errStack = ''; + ArrayPrototypeForEach(lines, (line) => { + if (!matched && + RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { + errStack += writer.options.breakLength >= line.length ? + `Uncaught ${line}` : `Uncaught:\n${line}`; - matched=true; + matched = true; } else { - errStack+=line; + errStack += line; } }); - if(!matched) { - const ln=lines.length===1? ' ':':\n'; - errStack=`Uncaught${ln}${errStack}`; + if (!matched) { + const ln = lines.length === 1 ? ' ' : ':\n'; + errStack = `Uncaught${ln}${errStack}`; } // Normalize line endings. - errStack+=StringPrototypeEndsWith(errStack,'\n')? '':'\n'; + errStack += StringPrototypeEndsWith(errStack, '\n') ? '' : '\n'; self.output.write(errStack); self.clearBufferedCommand(); - self.lines.level=[]; + self.lines.level = []; self.displayPrompt(); } }); self.clearBufferedCommand(); - function completer(text,cb) { - ReflectApply(complete,self, - [text,self.editorMode? self.completeOnEditorMode(cb):cb]); + function completer(text, cb) { + ReflectApply(complete, self, + [text, self.editorMode ? self.completeOnEditorMode(cb) : cb]); } - ReflectApply(Interface,this,[{ + ReflectApply(Interface, this, [{ input: options.input, output: options.output, - completer: options.completer||completer, + completer: options.completer || completer, terminal: options.terminal, historySize: options.historySize, prompt, @@ -791,25 +789,25 @@ function REPLServer(prompt, self.resetContext(); - this.commands={__proto__: null}; + this.commands = { __proto__: null }; defineDefaultCommands(this); // Figure out which "writer" function to use - self.writer=options.writer||module.exports.writer; + self.writer = options.writer || module.exports.writer; - if(self.writer===writer) { + if (self.writer === writer) { // Conditionally turn on ANSI coloring. - writer.options.colors=self.useColors; + writer.options.colors = self.useColors; - if(options[kStandaloneREPL]) { - ObjectDefineProperty(inspect,'replDefaults',{ + if (options[kStandaloneREPL]) { + ObjectDefineProperty(inspect, 'replDefaults', { __proto__: null, get() { return writer.options; }, set(options) { - validateObject(options,'options'); - return ObjectAssign(writer.options,options); + validateObject(options, 'options'); + return ObjectAssign(writer.options, options); }, enumerable: true, configurable: true, @@ -817,95 +815,95 @@ function REPLServer(prompt, } } - function _parseREPLKeyword(keyword,rest) { - const cmd=this.commands[keyword]; - if(cmd) { - ReflectApply(cmd.action,this,[rest]); + function _parseREPLKeyword(keyword, rest) { + const cmd = this.commands[keyword]; + if (cmd) { + ReflectApply(cmd.action, this, [rest]); return true; } return false; } - self.on('close',function emitExit() { - if(paused) { - ArrayPrototypePush(pausedBuffer,['close']); + self.on('close', function emitExit() { + if (paused) { + ArrayPrototypePush(pausedBuffer, ['close']); return; } self.emit('exit'); }); - let sawSIGINT=false; - let sawCtrlD=false; - const prioritizedSigintQueue=new SafeSet(); - self.on('SIGINT',function onSigInt() { - if(prioritizedSigintQueue.size>0) { - for(const task of prioritizedSigintQueue) { + let sawSIGINT = false; + let sawCtrlD = false; + const prioritizedSigintQueue = new SafeSet(); + self.on('SIGINT', function onSigInt() { + if (prioritizedSigintQueue.size > 0) { + for (const task of prioritizedSigintQueue) { task(); } return; } - const empty=self.line.length===0; + const empty = self.line.length === 0; self.clearLine(); _turnOffEditorMode(self); - const cmd=self[kBufferedCommandSymbol]; - if(!(cmd&&cmd.length>0)&&empty) { - if(sawSIGINT) { + const cmd = self[kBufferedCommandSymbol]; + if (!(cmd && cmd.length > 0) && empty) { + if (sawSIGINT) { self.close(); - sawSIGINT=false; + sawSIGINT = false; return; } self.output.write( '(To exit, press Ctrl+C again or Ctrl+D or type .exit)\n', ); - sawSIGINT=true; + sawSIGINT = true; } else { - sawSIGINT=false; + sawSIGINT = false; } self.clearBufferedCommand(); - self.lines.level=[]; + self.lines.level = []; self.displayPrompt(); }); - self.on('line',function onLine(cmd) { - debug('line %j',cmd); - cmd=cmd||''; - sawSIGINT=false; + self.on('line', function onLine(cmd) { + debug('line %j', cmd); + cmd = cmd || ''; + sawSIGINT = false; - if(self.editorMode) { - self[kBufferedCommandSymbol]+=cmd+'\n'; + if (self.editorMode) { + self[kBufferedCommandSymbol] += cmd + '\n'; // code alignment - const matches=self._sawKeyPress? - RegExpPrototypeExec(/^\s+/,cmd):null; - if(matches) { - const prefix=matches[0]; + const matches = self._sawKeyPress ? + RegExpPrototypeExec(/^\s+/, cmd) : null; + if (matches) { + const prefix = matches[0]; self.write(prefix); - self.line=prefix; - self.cursor=prefix.length; + self.line = prefix; + self.cursor = prefix.length; } - ReflectApply(_memory,self,[cmd]); + ReflectApply(_memory, self, [cmd]); return; } // Check REPL keywords and empty lines against a trimmed line input. - const trimmedCmd=StringPrototypeTrim(cmd); + const trimmedCmd = StringPrototypeTrim(cmd); // Check to see if a REPL keyword was used. If it returns true, // display next prompt and return. - if(trimmedCmd) { - if(StringPrototypeCharAt(trimmedCmd,0)==='.'&& - StringPrototypeCharAt(trimmedCmd,1)!=='.'&& - NumberIsNaN(NumberParseFloat(trimmedCmd))) { - const matches=RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/,trimmedCmd); - const keyword=matches&&matches[1]; - const rest=matches&&matches[2]; - if(ReflectApply(_parseREPLKeyword,self,[keyword,rest])===true) { + if (trimmedCmd) { + if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && + StringPrototypeCharAt(trimmedCmd, 1) !== '.' && + NumberIsNaN(NumberParseFloat(trimmedCmd))) { + const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); + const keyword = matches && matches[1]; + const rest = matches && matches[2]; + if (ReflectApply(_parseREPLKeyword, self, [keyword, rest]) === true) { return; } - if(!self[kBufferedCommandSymbol]) { + if (!self[kBufferedCommandSymbol]) { self.output.write('Invalid REPL keyword\n'); finish(null); return; @@ -913,65 +911,65 @@ function REPLServer(prompt, } } - const evalCmd=self[kBufferedCommandSymbol]+cmd+'\n'; + const evalCmd = self[kBufferedCommandSymbol] + cmd + '\n'; - debug('eval %j',evalCmd); - self.eval(evalCmd,self.context,getREPLResourceName(),finish); + debug('eval %j', evalCmd); + self.eval(evalCmd, self.context, getREPLResourceName(), finish); - function finish(e,ret) { - debug('finish',e,ret); - ReflectApply(_memory,self,[cmd]); + function finish(e, ret) { + debug('finish', e, ret); + ReflectApply(_memory, self, [cmd]); - if(e&&!self[kBufferedCommandSymbol]&& - StringPrototypeStartsWith(StringPrototypeTrim(cmd),'npm ')) { - self.output.write('npm should be run outside of the '+ - 'Node.js REPL, in your normal shell.\n'+ - '(Press Ctrl+D to exit.)\n'); + if (e && !self[kBufferedCommandSymbol] && + StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { + self.output.write('npm should be run outside of the ' + + 'Node.js REPL, in your normal shell.\n' + + '(Press Ctrl+D to exit.)\n'); self.displayPrompt(); return; } // If error was SyntaxError and not JSON.parse error - if(e) { - if(e instanceof Recoverable&&!sawCtrlD) { + if (e) { + if (e instanceof Recoverable && !sawCtrlD) { // Start buffering data like that: // { // ... x: 1 // ... } - self[kBufferedCommandSymbol]+=cmd+'\n'; + self[kBufferedCommandSymbol] += cmd + '\n'; self.displayPrompt(); return; } - self._domain.emit('error',e.err||e); + self._domain.emit('error', e.err || e); } // Clear buffer if no SyntaxErrors self.clearBufferedCommand(); - sawCtrlD=false; + sawCtrlD = false; // If we got any output - print it (if no error) - if(!e&& - // When an invalid REPL command is used, error message is printed - // immediately. We don't have to print anything else. So, only when - // the second argument to this function is there, print it. - arguments.length===2&& - (!self.ignoreUndefined||ret!==undefined)) { - if(!self.underscoreAssigned) { - self.last=ret; + if (!e && + // When an invalid REPL command is used, error message is printed + // immediately. We don't have to print anything else. So, only when + // the second argument to this function is there, print it. + arguments.length === 2 && + (!self.ignoreUndefined || ret !== undefined)) { + if (!self.underscoreAssigned) { + self.last = ret; } - self.output.write(self.writer(ret)+'\n'); + self.output.write(self.writer(ret) + '\n'); } // Display prompt again (unless we already did by emitting the 'error' // event on the domain instance). - if(!e) { + if (!e) { self.displayPrompt(); } } }); - self.on('SIGCONT',function onSigCont() { - if(self.editorMode) { + self.on('SIGCONT', function onSigCont() { + if (self.editorMode) { self.output.write(`${self._initialPrompt}.editor\n`); self.output.write( '// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)\n'); @@ -982,12 +980,12 @@ function REPLServer(prompt, } }); - const {reverseSearch}=setupReverseSearch(this); + const { reverseSearch } = setupReverseSearch(this); const { clearPreview, showPreview, - }=setupPreview( + } = setupPreview( this, kContextId, kBufferedCommandSymbol, @@ -995,123 +993,123 @@ function REPLServer(prompt, ); // Wrap readline tty to enable editor mode and pausing. - const ttyWrite=FunctionPrototypeBind(self._ttyWrite,self); - self._ttyWrite=(d,key) => { - key=key||{}; - if(paused&&!(self.breakEvalOnSigint&&key.ctrl&&key.name==='c')) { + const ttyWrite = FunctionPrototypeBind(self._ttyWrite, self); + self._ttyWrite = (d, key) => { + key = key || {}; + if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) { ArrayPrototypePush(pausedBuffer, - ['key',[d,key],self.isCompletionEnabled]); + ['key', [d, key], self.isCompletionEnabled]); return; } - if(!self.editorMode||!self.terminal) { + if (!self.editorMode || !self.terminal) { // Before exiting, make sure to clear the line. - if(key.ctrl&&key.name==='d'&& - self.cursor===0&&self.line.length===0) { + if (key.ctrl && key.name === 'd' && + self.cursor === 0 && self.line.length === 0) { self.clearLine(); } clearPreview(key); - if(!reverseSearch(d,key)) { - ttyWrite(d,key); - const showCompletionPreview=key.name!=='escape'; + if (!reverseSearch(d, key)) { + ttyWrite(d, key); + const showCompletionPreview = key.name !== 'escape'; showPreview(showCompletionPreview); } return; } // Editor mode - if(key.ctrl&&!key.shift) { - switch(key.name) { + if (key.ctrl && !key.shift) { + switch (key.name) { // TODO(BridgeAR): There should not be a special mode necessary for full // multiline support. case 'd': // End editor mode _turnOffEditorMode(self); - sawCtrlD=true; - ttyWrite(d,{name: 'return'}); + sawCtrlD = true; + ttyWrite(d, { name: 'return' }); break; case 'n': // Override next history item case 'p': // Override previous history item break; default: - ttyWrite(d,key); + ttyWrite(d, key); } } else { - switch(key.name) { + switch (key.name) { case 'up': // Override previous history item case 'down': // Override next history item break; case 'tab': // Prevent double tab behavior - self._previousKey=null; - ttyWrite(d,key); + self._previousKey = null; + ttyWrite(d, key); break; default: - ttyWrite(d,key); + ttyWrite(d, key); } } }; self.displayPrompt(); } -ObjectSetPrototypeOf(REPLServer.prototype,Interface.prototype); -ObjectSetPrototypeOf(REPLServer,Interface); +ObjectSetPrototypeOf(REPLServer.prototype, Interface.prototype); +ObjectSetPrototypeOf(REPLServer, Interface); // Prompt is a string to print on each line for the prompt, // source is a stream to use for I/O, defaulting to stdin/stdout. -function start(prompt,source,eval_,useGlobal,ignoreUndefined,replMode) { +function start(prompt, source, eval_, useGlobal, ignoreUndefined, replMode) { return new REPLServer( - prompt,source,eval_,useGlobal,ignoreUndefined,replMode); + prompt, source, eval_, useGlobal, ignoreUndefined, replMode); } -REPLServer.prototype.setupHistory=function setupHistory(historyFile,cb) { - history(this,historyFile,cb); +REPLServer.prototype.setupHistory = function setupHistory(historyFile, cb) { + history(this, historyFile, cb); }; -REPLServer.prototype.clearBufferedCommand=function clearBufferedCommand() { - this[kBufferedCommandSymbol]=''; +REPLServer.prototype.clearBufferedCommand = function clearBufferedCommand() { + this[kBufferedCommandSymbol] = ''; }; -REPLServer.prototype.close=function close() { - if(this.terminal&&this._flushing&&!this._closingOnFlush) { - this._closingOnFlush=true; - this.once('flushHistory',() => - ReflectApply(Interface.prototype.close,this,[]), +REPLServer.prototype.close = function close() { + if (this.terminal && this._flushing && !this._closingOnFlush) { + this._closingOnFlush = true; + this.once('flushHistory', () => + ReflectApply(Interface.prototype.close, this, []), ); return; } process.nextTick(() => - ReflectApply(Interface.prototype.close,this,[]), + ReflectApply(Interface.prototype.close, this, []), ); }; -REPLServer.prototype.createContext=function() { +REPLServer.prototype.createContext = function() { let context; - if(this.useGlobal) { - context=globalThis; + if (this.useGlobal) { + context = globalThis; } else { sendInspectorCommand((session) => { session.post('Runtime.enable'); - session.once('Runtime.executionContextCreated',({params}) => { - this[kContextId]=params.context.id; + session.once('Runtime.executionContextCreated', ({ params }) => { + this[kContextId] = params.context.id; }); - context=vm.createContext(); + context = vm.createContext(); session.post('Runtime.disable'); - },() => { - context=vm.createContext(); + }, () => { + context = vm.createContext(); }); - ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis),(name) => { + ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => { // Only set properties that do not already exist as a global builtin. - if(!globalBuiltins.has(name)) { - ObjectDefineProperty(context,name, - { - __proto__: null, - ...ObjectGetOwnPropertyDescriptor(globalThis,name), - }); + if (!globalBuiltins.has(name)) { + ObjectDefineProperty(context, name, + { + __proto__: null, + ...ObjectGetOwnPropertyDescriptor(globalThis, name), + }); } }); - context.global=context; - const _console=new Console(this.output); - ObjectDefineProperty(context,'console',{ + context.global = context; + const _console = new Console(this.output); + ObjectDefineProperty(context, 'console', { __proto__: null, configurable: true, writable: true, @@ -1119,56 +1117,56 @@ REPLServer.prototype.createContext=function() { }); } - const replModule=new CJSModule(''); - replModule.paths=CJSModule._resolveLookupPaths('',parentModule); + const replModule = new CJSModule(''); + replModule.paths = CJSModule._resolveLookupPaths('', parentModule); - ObjectDefineProperty(context,'module',{ + ObjectDefineProperty(context, 'module', { __proto__: null, configurable: true, writable: true, value: replModule, }); - ObjectDefineProperty(context,'require',{ + ObjectDefineProperty(context, 'require', { __proto__: null, configurable: true, writable: true, value: makeRequireFunction(replModule), }); - addBuiltinLibsToObject(context,''); + addBuiltinLibsToObject(context, ''); return context; }; -REPLServer.prototype.resetContext=function() { - this.context=this.createContext(); - this.underscoreAssigned=false; - this.underscoreErrAssigned=false; +REPLServer.prototype.resetContext = function() { + this.context = this.createContext(); + this.underscoreAssigned = false; + this.underscoreErrAssigned = false; // TODO(BridgeAR): Deprecate the lines. - this.lines=[]; - this.lines.level=[]; + this.lines = []; + this.lines.level = []; - ObjectDefineProperty(this.context,'_',{ + ObjectDefineProperty(this.context, '_', { __proto__: null, configurable: true, get: () => this.last, set: (value) => { - this.last=value; - if(!this.underscoreAssigned) { - this.underscoreAssigned=true; + this.last = value; + if (!this.underscoreAssigned) { + this.underscoreAssigned = true; this.output.write('Expression assignment to _ now disabled.\n'); } }, }); - ObjectDefineProperty(this.context,'_error',{ + ObjectDefineProperty(this.context, '_error', { __proto__: null, configurable: true, get: () => this.lastError, set: (value) => { - this.lastError=value; - if(!this.underscoreErrAssigned) { - this.underscoreErrAssigned=true; + this.lastError = value; + if (!this.underscoreErrAssigned) { + this.underscoreErrAssigned = true; this.output.write( 'Expression assignment to _error now disabled.\n'); } @@ -1176,128 +1174,128 @@ REPLServer.prototype.resetContext=function() { }); // Allow REPL extensions to extend the new context - this.emit('reset',this.context); + this.emit('reset', this.context); }; -REPLServer.prototype.displayPrompt=function(preserveCursor) { - let prompt=this._initialPrompt; - if(this[kBufferedCommandSymbol].length) { - prompt='...'; - const len=this.lines.level.length? this.lines.level.length-1:0; - const levelInd=StringPrototypeRepeat('..',len); - prompt+=levelInd+' '; +REPLServer.prototype.displayPrompt = function(preserveCursor) { + let prompt = this._initialPrompt; + if (this[kBufferedCommandSymbol].length) { + prompt = '...'; + const len = this.lines.level.length ? this.lines.level.length - 1 : 0; + const levelInd = StringPrototypeRepeat('..', len); + prompt += levelInd + ' '; } // Do not overwrite `_initialPrompt` here - ReflectApply(Interface.prototype.setPrompt,this,[prompt]); + ReflectApply(Interface.prototype.setPrompt, this, [prompt]); this.prompt(preserveCursor); }; // When invoked as an API method, overwrite _initialPrompt -REPLServer.prototype.setPrompt=function setPrompt(prompt) { - this._initialPrompt=prompt; - ReflectApply(Interface.prototype.setPrompt,this,[prompt]); +REPLServer.prototype.setPrompt = function setPrompt(prompt) { + this._initialPrompt = prompt; + ReflectApply(Interface.prototype.setPrompt, this, [prompt]); }; -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*["'](.*)/; -const simpleExpressionRE= - /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; -const versionedFileNamesRe=/-\d+\.\d+/; +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*["'](.*)/; +const simpleExpressionRE = + /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; +const versionedFileNamesRe = /-\d+\.\d+/; function isIdentifier(str) { - if(str==='') { + if (str === '') { return false; } - const first=StringPrototypeCodePointAt(str,0); - if(!isIdentifierStart(first)) { + const first = StringPrototypeCodePointAt(str, 0); + if (!isIdentifierStart(first)) { return false; } - const firstLen=first>0xffff? 2:1; - for(let i=firstLen;i 0xffff ? 2 : 1; + for (let i = firstLen; i < str.length; i += 1) { + const cp = StringPrototypeCodePointAt(str, i); + if (!isIdentifierChar(cp)) { return false; } - if(cp>0xffff) { - i+=1; + if (cp > 0xffff) { + i += 1; } } return true; } function isNotLegacyObjectPrototypeMethod(str) { - return isIdentifier(str)&& - str!=='__defineGetter__'&& - str!=='__defineSetter__'&& - str!=='__lookupGetter__'&& - str!=='__lookupSetter__'; + return isIdentifier(str) && + str !== '__defineGetter__' && + str !== '__defineSetter__' && + str !== '__lookupGetter__' && + str !== '__lookupSetter__'; } function filteredOwnPropertyNames(obj) { - if(!obj) return []; + if (!obj) return []; // `Object.prototype` is the only non-contrived object that fulfills // `Object.getPrototypeOf(X) === null && // Object.getPrototypeOf(Object.getPrototypeOf(X.constructor)) === X`. - let isObjectPrototype=false; - if(ObjectGetPrototypeOf(obj)===null) { - const ctorDescriptor=ObjectGetOwnPropertyDescriptor(obj,'constructor'); - if(ctorDescriptor&&ctorDescriptor.value) { - const ctorProto=ObjectGetPrototypeOf(ctorDescriptor.value); - isObjectPrototype=ctorProto&&ObjectGetPrototypeOf(ctorProto)===obj; + let isObjectPrototype = false; + if (ObjectGetPrototypeOf(obj) === null) { + const ctorDescriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor'); + if (ctorDescriptor && ctorDescriptor.value) { + const ctorProto = ObjectGetPrototypeOf(ctorDescriptor.value); + isObjectPrototype = ctorProto && ObjectGetPrototypeOf(ctorProto) === obj; } } - const filter=ALL_PROPERTIES|SKIP_SYMBOLS; + const filter = ALL_PROPERTIES | SKIP_SYMBOLS; return ArrayPrototypeFilter( - getOwnNonIndexProperties(obj,filter), - isObjectPrototype? isNotLegacyObjectPrototypeMethod:isIdentifier); + getOwnNonIndexProperties(obj, filter), + isObjectPrototype ? isNotLegacyObjectPrototypeMethod : isIdentifier); } function getGlobalLexicalScopeNames(contextId) { return sendInspectorCommand((session) => { - let names=[]; - session.post('Runtime.globalLexicalScopeNames',{ + let names = []; + session.post('Runtime.globalLexicalScopeNames', { executionContextId: contextId, - },(error,result) => { - if(!error) names=result.names; + }, (error, result) => { + if (!error) names = result.names; }); return names; - },() => []); + }, () => []); } -REPLServer.prototype.complete=function() { - ReflectApply(this.completer,this,arguments); +REPLServer.prototype.complete = function() { + ReflectApply(this.completer, this, arguments); }; function gracefulReaddir(...args) { try { - return ReflectApply(fs.readdirSync,null,args); + return ReflectApply(fs.readdirSync, null, args); } catch { // Continue regardless of error. } } function completeFSFunctions(match) { - let baseName=''; - let filePath=match[1]; - let fileList=gracefulReaddir(filePath,{withFileTypes: true}); - - if(!fileList) { - baseName=path.basename(filePath); - filePath=path.dirname(filePath); - fileList=gracefulReaddir(filePath,{withFileTypes: true})||[]; + let baseName = ''; + let filePath = match[1]; + let fileList = gracefulReaddir(filePath, { withFileTypes: true }); + + if (!fileList) { + baseName = path.basename(filePath); + filePath = path.dirname(filePath); + fileList = gracefulReaddir(filePath, { withFileTypes: true }) || []; } - const completions=ArrayPrototypeMap( + const completions = ArrayPrototypeMap( ArrayPrototypeFilter( fileList, - (dirent) => StringPrototypeStartsWith(dirent.name,baseName), + (dirent) => StringPrototypeStartsWith(dirent.name, baseName), ), (d) => d.name, ); - return [[completions],baseName]; + return [[completions], baseName]; } // Provide a list of completions for the given leading text. This is @@ -1310,224 +1308,224 @@ function completeFSFunctions(match) { // // Warning: This eval's code like "foo.bar.baz", so it will run property // getter code. -function complete(line,callback) { +function complete(line, callback) { // List of completion lists, one for each inheritance "level" - let completionGroups=[]; - let completeOn,group; + let completionGroups = []; + let completeOn, group; // Ignore right whitespace. It could change the outcome. - line=StringPrototypeTrimLeft(line); + line = StringPrototypeTrimLeft(line); - let filter=''; + let filter = ''; let match; // REPL commands (e.g. ".break"). - if((match=RegExpPrototypeExec(/^\s*\.(\w*)$/,line))!==null) { - ArrayPrototypePush(completionGroups,ObjectKeys(this.commands)); - completeOn=match[1]; - if(completeOn.length) { - filter=completeOn; + if ((match = RegExpPrototypeExec(/^\s*\.(\w*)$/, line)) !== null) { + ArrayPrototypePush(completionGroups, ObjectKeys(this.commands)); + completeOn = match[1]; + if (completeOn.length) { + filter = completeOn; } - } else if((match=RegExpPrototypeExec(requireRE,line))!==null) { + } else if ((match = RegExpPrototypeExec(requireRE, line)) !== null) { // require('...') - completeOn=match[1]; - filter=completeOn; - if(this.allowBlockingCompletions) { - const subdir=match[2]||''; - const extensions=ObjectKeys(this.context.require.extensions); - const indexes=ArrayPrototypeMap(extensions, - (extension) => `index${extension}`); - ArrayPrototypePush(indexes,'package.json','index'); - - group=[]; - let paths=[]; - - if(completeOn==='.') { - group=['./','../']; - } else if(completeOn==='..') { - group=['../']; - } else if(RegExpPrototypeExec(/^\.\.?\//,completeOn)!==null) { - paths=[process.cwd()]; + completeOn = match[1]; + filter = completeOn; + if (this.allowBlockingCompletions) { + const subdir = match[2] || ''; + const extensions = ObjectKeys(this.context.require.extensions); + const indexes = ArrayPrototypeMap(extensions, + (extension) => `index${extension}`); + ArrayPrototypePush(indexes, 'package.json', 'index'); + + group = []; + let paths = []; + + if (completeOn === '.') { + group = ['./', '../']; + } else if (completeOn === '..') { + group = ['../']; + } else if (RegExpPrototypeExec(/^\.\.?\//, completeOn) !== null) { + paths = [process.cwd()]; } else { - paths=[]; - ArrayPrototypePushApply(paths,module.paths); - ArrayPrototypePushApply(paths,CJSModule.globalPaths); + paths = []; + ArrayPrototypePushApply(paths, module.paths); + ArrayPrototypePushApply(paths, CJSModule.globalPaths); } - ArrayPrototypeForEach(paths,(dir) => { - dir=path.resolve(dir,subdir); - const dirents=gracefulReaddir(dir,{withFileTypes: true})||[]; - ArrayPrototypeForEach(dirents,(dirent) => { - if(RegExpPrototypeExec(versionedFileNamesRe,dirent.name)!==null|| - dirent.name==='.npm') { + ArrayPrototypeForEach(paths, (dir) => { + dir = path.resolve(dir, subdir); + const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; + ArrayPrototypeForEach(dirents, (dirent) => { + if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null || + dirent.name === '.npm') { // Exclude versioned names that 'npm' installs. return; } - const extension=path.extname(dirent.name); - const base=StringPrototypeSlice(dirent.name,0,-extension.length); - if(!dirent.isDirectory()) { - if(StringPrototypeIncludes(extensions,extension)&& - (!subdir||base!=='index')) { - ArrayPrototypePush(group,`${subdir}${base}`); + const extension = path.extname(dirent.name); + const base = StringPrototypeSlice(dirent.name, 0, -extension.length); + if (!dirent.isDirectory()) { + if (StringPrototypeIncludes(extensions, extension) && + (!subdir || base !== 'index')) { + ArrayPrototypePush(group, `${subdir}${base}`); } return; } - ArrayPrototypePush(group,`${subdir}${dirent.name}/`); - const absolute=path.resolve(dir,dirent.name); - if(ArrayPrototypeSome( - gracefulReaddir(absolute)||[], - (subfile) => ArrayPrototypeIncludes(indexes,subfile), + ArrayPrototypePush(group, `${subdir}${dirent.name}/`); + const absolute = path.resolve(dir, dirent.name); + if (ArrayPrototypeSome( + gracefulReaddir(absolute) || [], + (subfile) => ArrayPrototypeIncludes(indexes, subfile), )) { - ArrayPrototypePush(group,`${subdir}${dirent.name}`); + ArrayPrototypePush(group, `${subdir}${dirent.name}`); } }); }); - if(group.length) { - ArrayPrototypePush(completionGroups,group); + if (group.length) { + ArrayPrototypePush(completionGroups, group); } } - ArrayPrototypePush(completionGroups,_builtinLibs,nodeSchemeBuiltinLibs); - } else if((match=RegExpPrototypeExec(importRE,line))!==null) { + ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); + } else if ((match = RegExpPrototypeExec(importRE, line)) !== null) { // import('...') - completeOn=match[1]; - filter=completeOn; - if(this.allowBlockingCompletions) { - const subdir=match[2]||''; + completeOn = match[1]; + filter = completeOn; + if (this.allowBlockingCompletions) { + const subdir = match[2] || ''; // File extensions that can be imported: - const extensions=ObjectKeys(extensionFormatMap); + const extensions = ObjectKeys(extensionFormatMap); // Only used when loading bare module specifiers from `node_modules`: - const indexes=ArrayPrototypeMap(extensions,(ext) => `index${ext}`); - ArrayPrototypePush(indexes,'package.json'); - - group=[]; - let paths=[]; - if(completeOn==='.') { - group=['./','../']; - } else if(completeOn==='..') { - group=['../']; - } else if(RegExpPrototypeExec(/^\.\.?\//,completeOn)!==null) { - paths=[process.cwd()]; + const indexes = ArrayPrototypeMap(extensions, (ext) => `index${ext}`); + ArrayPrototypePush(indexes, 'package.json'); + + group = []; + let paths = []; + if (completeOn === '.') { + group = ['./', '../']; + } else if (completeOn === '..') { + group = ['../']; + } else if (RegExpPrototypeExec(/^\.\.?\//, completeOn) !== null) { + paths = [process.cwd()]; } else { - paths=ArrayPrototypeSlice(module.paths); + paths = ArrayPrototypeSlice(module.paths); } - ArrayPrototypeForEach(paths,(dir) => { - dir=path.resolve(dir,subdir); - const isInNodeModules=path.basename(dir)==='node_modules'; - const dirents=gracefulReaddir(dir,{withFileTypes: true})||[]; - ArrayPrototypeForEach(dirents,(dirent) => { - const {name}=dirent; - if(RegExpPrototypeExec(versionedFileNamesRe,name)!==null|| - name==='.npm') { + ArrayPrototypeForEach(paths, (dir) => { + dir = path.resolve(dir, subdir); + const isInNodeModules = path.basename(dir) === 'node_modules'; + const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; + ArrayPrototypeForEach(dirents, (dirent) => { + const { name } = dirent; + if (RegExpPrototypeExec(versionedFileNamesRe, name) !== null || + name === '.npm') { // Exclude versioned names that 'npm' installs. return; } - if(!dirent.isDirectory()) { - const extension=path.extname(name); - if(StringPrototypeIncludes(extensions,extension)) { - ArrayPrototypePush(group,`${subdir}${name}`); + if (!dirent.isDirectory()) { + const extension = path.extname(name); + if (StringPrototypeIncludes(extensions, extension)) { + ArrayPrototypePush(group, `${subdir}${name}`); } return; } - ArrayPrototypePush(group,`${subdir}${name}/`); - if(!subdir&&isInNodeModules) { - const absolute=path.resolve(dir,name); - const subfiles=gracefulReaddir(absolute)||[]; - if(ArrayPrototypeSome(subfiles,(subfile) => { - return ArrayPrototypeIncludes(indexes,subfile); + ArrayPrototypePush(group, `${subdir}${name}/`); + if (!subdir && isInNodeModules) { + const absolute = path.resolve(dir, name); + const subfiles = gracefulReaddir(absolute) || []; + if (ArrayPrototypeSome(subfiles, (subfile) => { + return ArrayPrototypeIncludes(indexes, subfile); })) { - ArrayPrototypePush(group,`${subdir}${name}`); + ArrayPrototypePush(group, `${subdir}${name}`); } } }); }); - if(group.length) { - ArrayPrototypePush(completionGroups,group); + if (group.length) { + ArrayPrototypePush(completionGroups, group); } } - ArrayPrototypePush(completionGroups,_builtinLibs,nodeSchemeBuiltinLibs); - } else if((match=RegExpPrototypeExec(fsAutoCompleteRE,line))!==null&& - this.allowBlockingCompletions) { - ({0: completionGroups,1: completeOn}=completeFSFunctions(match)); - // Handle variable member lookup. - // We support simple chained expressions like the following (no function - // calls, etc.). That is for simplicity and also because we *eval* that - // leading expression so for safety (see WARNING above) don't want to - // eval function calls. - // - // foo.bar<|> # completions for 'foo' with filter 'bar' - // spam.eggs.<|> # completions for 'spam.eggs' with filter '' - // foo<|> # all scope vars with filter 'foo' - // foo.<|> # completions for 'foo' with filter '' - } else if(line.length===0|| - RegExpPrototypeExec(/\w|\.|\$/,line[line.length-1])!==null) { - const {0: match}=RegExpPrototypeExec(simpleExpressionRE,line)||['']; - if(line.length!==0&&!match) { + ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); + } else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null && + this.allowBlockingCompletions) { + ({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match)); + // Handle variable member lookup. + // We support simple chained expressions like the following (no function + // calls, etc.). That is for simplicity and also because we *eval* that + // leading expression so for safety (see WARNING above) don't want to + // eval function calls. + // + // foo.bar<|> # completions for 'foo' with filter 'bar' + // spam.eggs.<|> # completions for 'spam.eggs' with filter '' + // foo<|> # all scope vars with filter 'foo' + // foo.<|> # completions for 'foo' with filter '' + } else if (line.length === 0 || + RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { + const { 0: match } = RegExpPrototypeExec(simpleExpressionRE, line) || ['']; + if (line.length !== 0 && !match) { completionGroupsLoaded(); return; } - let expr=''; - completeOn=match; - if(StringPrototypeEndsWith(line,'.')) { - expr=StringPrototypeSlice(match,0,-1); - } else if(line.length!==0) { - const bits=StringPrototypeSplit(match,'.'); - filter=ArrayPrototypePop(bits); - expr=ArrayPrototypeJoin(bits,'.'); + let expr = ''; + completeOn = match; + if (StringPrototypeEndsWith(line, '.')) { + expr = StringPrototypeSlice(match, 0, -1); + } else if (line.length !== 0) { + const bits = StringPrototypeSplit(match, '.'); + filter = ArrayPrototypePop(bits); + expr = ArrayPrototypeJoin(bits, '.'); } // Resolve expr and get its completions. - if(!expr) { + if (!expr) { // Get global vars synchronously ArrayPrototypePush(completionGroups, - getGlobalLexicalScopeNames(this[kContextId])); - let contextProto=this.context; - while((contextProto=ObjectGetPrototypeOf(contextProto))!==null) { + getGlobalLexicalScopeNames(this[kContextId])); + let contextProto = this.context; + while ((contextProto = ObjectGetPrototypeOf(contextProto)) !== null) { ArrayPrototypePush(completionGroups, - filteredOwnPropertyNames(contextProto)); + filteredOwnPropertyNames(contextProto)); } - const contextOwnNames=filteredOwnPropertyNames(this.context); - if(!this.useGlobal) { + const contextOwnNames = filteredOwnPropertyNames(this.context); + 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); + ArrayPrototypePush(contextOwnNames, ...globalBuiltins); } - ArrayPrototypePush(completionGroups,contextOwnNames); - if(filter!=='') addCommonWords(completionGroups); + ArrayPrototypePush(completionGroups, contextOwnNames); + if (filter !== '') addCommonWords(completionGroups); completionGroupsLoaded(); return; } - let chaining='.'; - if(StringPrototypeEndsWith(expr,'?')) { - expr=StringPrototypeSlice(expr,0,-1); - chaining='?.'; + let chaining = '.'; + if (StringPrototypeEndsWith(expr, '?')) { + expr = StringPrototypeSlice(expr, 0, -1); + chaining = '?.'; } - const memberGroups=[]; - const evalExpr=`try { ${expr} } catch {}`; - this.eval(evalExpr,this.context,getREPLResourceName(),(e,obj) => { + const memberGroups = []; + const evalExpr = `try { ${expr} } catch {}`; + this.eval(evalExpr, this.context, getREPLResourceName(), (e, obj) => { try { let p; - if((typeof obj==='object'&&obj!==null)|| - typeof obj==='function') { + if ((typeof obj === 'object' && obj !== null) || + typeof obj === 'function') { memberGroups.push(filteredOwnPropertyNames(obj)); - p=ObjectGetPrototypeOf(obj); + p = ObjectGetPrototypeOf(obj); } else { - p=obj.constructor? obj.constructor.prototype:null; + p = obj.constructor ? obj.constructor.prototype : null; } // Circular refs possible? Let's guard against that. - let sentinel=5; - while(p!==null&&sentinel--!==0) { + let sentinel = 5; + while (p !== null && sentinel-- !== 0) { memberGroups.push(filteredOwnPropertyNames(p)); - p=ObjectGetPrototypeOf(p); + p = ObjectGetPrototypeOf(p); } } catch { // Maybe a Proxy object without `getOwnPropertyNames` trap. @@ -1536,15 +1534,15 @@ function complete(line,callback) { // https://github.com/nodejs/node/issues/2119 } - if(memberGroups.length) { - expr+=chaining; - ArrayPrototypeForEach(memberGroups,(group) => { + if (memberGroups.length) { + expr += chaining; + ArrayPrototypeForEach(memberGroups, (group) => { ArrayPrototypePush(completionGroups, - ArrayPrototypeMap(group, - (member) => `${expr}${member}`)); + ArrayPrototypeMap(group, + (member) => `${expr}${member}`)); }); - if(filter) { - filter=`${expr}${filter}`; + if (filter) { + filter = `${expr}${filter}`; } } @@ -1559,11 +1557,11 @@ function complete(line,callback) { // Useful for async autocompletion function completionGroupsLoaded() { // Filter, sort (within each group), uniq and merge the completion groups. - if(completionGroups.length&&filter) { - const newCompletionGroups=[]; - const lowerCaseFilter=StringPrototypeToLocaleLowerCase(filter); - ArrayPrototypeForEach(completionGroups,(group) => { - const filteredGroup=ArrayPrototypeFilter(group,(str) => { + if (completionGroups.length && filter) { + const newCompletionGroups = []; + const lowerCaseFilter = StringPrototypeToLocaleLowerCase(filter); + ArrayPrototypeForEach(completionGroups, (group) => { + const filteredGroup = ArrayPrototypeFilter(group, (str) => { // Filter is always case-insensitive following chromium autocomplete // behavior. return StringPrototypeStartsWith( @@ -1571,105 +1569,105 @@ function complete(line,callback) { lowerCaseFilter, ); }); - if(filteredGroup.length) { - ArrayPrototypePush(newCompletionGroups,filteredGroup); + if (filteredGroup.length) { + ArrayPrototypePush(newCompletionGroups, filteredGroup); } }); - completionGroups=newCompletionGroups; + completionGroups = newCompletionGroups; } - const completions=[]; + const completions = []; // Unique completions across all groups. - const uniqueSet=new SafeSet(); + const uniqueSet = new SafeSet(); uniqueSet.add(''); // Completion group 0 is the "closest" (least far up the inheritance // chain) so we put its completions last: to be closest in the REPL. - ArrayPrototypeForEach(completionGroups,(group) => { - ArrayPrototypeSort(group,(a,b) => (b>a? 1:-1)); - const setSize=uniqueSet.size; - ArrayPrototypeForEach(group,(entry) => { - if(!uniqueSet.has(entry)) { - ArrayPrototypeUnshift(completions,entry); + ArrayPrototypeForEach(completionGroups, (group) => { + ArrayPrototypeSort(group, (a, b) => (b > a ? 1 : -1)); + const setSize = uniqueSet.size; + ArrayPrototypeForEach(group, (entry) => { + if (!uniqueSet.has(entry)) { + ArrayPrototypeUnshift(completions, entry); uniqueSet.add(entry); } }); // Add a separator between groups. - if(uniqueSet.size!==setSize) { - ArrayPrototypeUnshift(completions,''); + if (uniqueSet.size !== setSize) { + ArrayPrototypeUnshift(completions, ''); } }); // Remove obsolete group entry, if present. - if(completions[0]==='') { + if (completions[0] === '') { ArrayPrototypeShift(completions); } - callback(null,[completions,completeOn]); + callback(null, [completions, completeOn]); } } -REPLServer.prototype.completeOnEditorMode=(callback) => (err,results) => { - if(err) return callback(err); +REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => { + if (err) return callback(err); - const {0: completions,1: completeOn=''}=results; - let result=ArrayPrototypeFilter(completions,Boolean); + const { 0: completions, 1: completeOn = '' } = results; + let result = ArrayPrototypeFilter(completions, Boolean); - if(completeOn&&result.length!==0) { - result=[commonPrefix(result)]; + if (completeOn && result.length !== 0) { + result = [commonPrefix(result)]; } - callback(null,[result,completeOn]); + callback(null, [result, completeOn]); }; -REPLServer.prototype.defineCommand=function(keyword,cmd) { - if(typeof cmd==='function') { - cmd={action: cmd}; +REPLServer.prototype.defineCommand = function(keyword, cmd) { + if (typeof cmd === 'function') { + cmd = { action: cmd }; } else { - validateFunction(cmd.action,'cmd.action'); + validateFunction(cmd.action, 'cmd.action'); } - this.commands[keyword]=cmd; + this.commands[keyword] = cmd; }; // TODO(BridgeAR): This should be replaced with acorn to build an AST. The // language became more complex and using a simple approach like this is not // sufficient anymore. function _memory(cmd) { - const self=this; - self.lines=self.lines||[]; - self.lines.level=self.lines.level||[]; + const self = this; + self.lines = self.lines || []; + self.lines.level = self.lines.level || []; // Save the line so I can do magic later - if(cmd) { - const len=self.lines.level.length? self.lines.level.length-1:0; - ArrayPrototypePush(self.lines,StringPrototypeRepeat(' ',len)+cmd); + if (cmd) { + const len = self.lines.level.length ? self.lines.level.length - 1 : 0; + ArrayPrototypePush(self.lines, StringPrototypeRepeat(' ', len) + cmd); } else { // I don't want to not change the format too much... - ArrayPrototypePush(self.lines,''); + ArrayPrototypePush(self.lines, ''); } - if(!cmd) { - self.lines.level=[]; + if (!cmd) { + self.lines.level = []; return; } // I need to know "depth." // Because I can not tell the difference between a } that // closes an object literal and a } that closes a function - const countMatches=(regex,str) => { - let count=0; - while(RegExpPrototypeExec(regex,str)!==null) count++; + const countMatches = (regex, str) => { + let count = 0; + while (RegExpPrototypeExec(regex, str) !== null) count++; return count; }; // Going down is { and ( e.g. function() { // going up is } and ) - const dw=countMatches(/[{(]/g,cmd); - const up=countMatches(/[})]/g,cmd); - let depth=dw.length-up.length; + const dw = countMatches(/[{(]/g, cmd); + const up = countMatches(/[})]/g, cmd); + let depth = dw.length - up.length; - if(depth) { + if (depth) { (function workIt() { - if(depth>0) { + if (depth > 0) { // Going... down. // Push the line#, depth count, and if the line is a function. // Since JS only has functional scope I only need to remove @@ -1677,23 +1675,23 @@ function _memory(cmd) { // "function() // {" but nothing should break, only tab completion for local // scope will not work for this function. - ArrayPrototypePush(self.lines.level,{ - line: self.lines.length-1, + ArrayPrototypePush(self.lines.level, { + line: self.lines.length - 1, depth: depth, }); - } else if(depth<0) { + } else if (depth < 0) { // Going... up. - const curr=ArrayPrototypePop(self.lines.level); - if(curr) { - const tmp=curr.depth+depth; - if(tmp<0) { + const curr = ArrayPrototypePop(self.lines.level); + if (curr) { + const tmp = curr.depth + depth; + if (tmp < 0) { // More to go, recurse - depth+=curr.depth; + depth += curr.depth; workIt(); - } else if(tmp>0) { + } else if (tmp > 0) { // Remove and push back - curr.depth+=depth; - ArrayPrototypePush(self.lines.level,curr); + curr.depth += depth; + ArrayPrototypePush(self.lines.level, curr); } } } @@ -1704,27 +1702,27 @@ function _memory(cmd) { function addCommonWords(completionGroups) { // Only words which do not yet exist as global property should be added to // this list. - ArrayPrototypePush(completionGroups,[ - 'async','await','break','case','catch','const','continue', - 'debugger','default','delete','do','else','export','false', - 'finally','for','function','if','import','in','instanceof','let', - 'new','null','return','switch','this','throw','true','try', - 'typeof','var','void','while','with','yield', + ArrayPrototypePush(completionGroups, [ + 'async', 'await', 'break', 'case', 'catch', 'const', 'continue', + 'debugger', 'default', 'delete', 'do', 'else', 'export', 'false', + 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let', + 'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try', + 'typeof', 'var', 'void', 'while', 'with', 'yield', ]); } function _turnOnEditorMode(repl) { - repl.editorMode=true; - ReflectApply(Interface.prototype.setPrompt,repl,['']); + repl.editorMode = true; + ReflectApply(Interface.prototype.setPrompt, repl, ['']); } function _turnOffEditorMode(repl) { - repl.editorMode=false; + repl.editorMode = false; repl.setPrompt(repl._initialPrompt); } function defineDefaultCommands(repl) { - repl.defineCommand('break',{ + repl.defineCommand('break', { help: 'Sometimes you get stuck, this gets you out', action: function() { this.clearBufferedCommand(); @@ -1733,16 +1731,16 @@ function defineDefaultCommands(repl) { }); let clearMessage; - if(repl.useGlobal) { - clearMessage='Alias for .break'; + if (repl.useGlobal) { + clearMessage = 'Alias for .break'; } else { - clearMessage='Break, and also clear the local context'; + clearMessage = 'Break, and also clear the local context'; } - repl.defineCommand('clear',{ + repl.defineCommand('clear', { help: clearMessage, action: function() { this.clearBufferedCommand(); - if(!this.useGlobal) { + if (!this.useGlobal) { this.output.write('Clearing context...\n'); this.resetContext(); } @@ -1750,38 +1748,38 @@ function defineDefaultCommands(repl) { }, }); - repl.defineCommand('exit',{ + repl.defineCommand('exit', { help: 'Exit the REPL', action: function() { this.close(); }, }); - repl.defineCommand('help',{ + repl.defineCommand('help', { help: 'Print this help message', action: function() { - const names=ArrayPrototypeSort(ObjectKeys(this.commands)); - const longestNameLength=MathMaxApply( - ArrayPrototypeMap(names,(name) => name.length), + const names = ArrayPrototypeSort(ObjectKeys(this.commands)); + const longestNameLength = MathMaxApply( + ArrayPrototypeMap(names, (name) => name.length), ); - ArrayPrototypeForEach(names,(name) => { - const cmd=this.commands[name]; - const spaces= - StringPrototypeRepeat(' ',longestNameLength-name.length+3); - const line=`.${name}${cmd.help? spaces+cmd.help:''}\n`; + ArrayPrototypeForEach(names, (name) => { + const cmd = this.commands[name]; + const spaces = + StringPrototypeRepeat(' ', longestNameLength - name.length + 3); + const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; this.output.write(line); }); - this.output.write('\nPress Ctrl+C to abort current expression, '+ + this.output.write('\nPress Ctrl+C to abort current expression, ' + 'Ctrl+D to exit the REPL\n'); this.displayPrompt(); }, }); - repl.defineCommand('save',{ + repl.defineCommand('save', { help: 'Save all evaluated commands in this REPL session to a file', action: function(file) { try { - fs.writeFileSync(file,ArrayPrototypeJoin(this.lines,'\n')); + fs.writeFileSync(file, ArrayPrototypeJoin(this.lines, '\n')); this.output.write(`Session saved to: ${file}\n`); } catch { this.output.write(`Failed to save: ${file}\n`); @@ -1790,14 +1788,14 @@ function defineDefaultCommands(repl) { }, }); - repl.defineCommand('load',{ + repl.defineCommand('load', { help: 'Load JS from a file into the REPL session', action: function(file) { try { - const stats=fs.statSync(file); - if(stats&&stats.isFile()) { + const stats = fs.statSync(file); + if (stats && stats.isFile()) { _turnOnEditorMode(this); - const data=fs.readFileSync(file,'utf8'); + const data = fs.readFileSync(file, 'utf8'); this.write(data); _turnOffEditorMode(this); this.write('\n'); @@ -1812,8 +1810,8 @@ function defineDefaultCommands(repl) { this.displayPrompt(); }, }); - if(repl.terminal) { - repl.defineCommand('editor',{ + if (repl.terminal) { + repl.defineCommand('editor', { help: 'Enter editor mode', action() { _turnOnEditorMode(this); @@ -1825,12 +1823,12 @@ function defineDefaultCommands(repl) { } function Recoverable(err) { - this.err=err; + this.err = err; } -ObjectSetPrototypeOf(Recoverable.prototype,SyntaxErrorPrototype); -ObjectSetPrototypeOf(Recoverable,SyntaxError); +ObjectSetPrototypeOf(Recoverable.prototype, SyntaxErrorPrototype); +ObjectSetPrototypeOf(Recoverable, SyntaxError); -module.exports={ +module.exports = { start, writer, REPLServer, @@ -1839,26 +1837,26 @@ module.exports={ Recoverable, }; -ObjectDefineProperty(module.exports,'builtinModules',{ +ObjectDefineProperty(module.exports, 'builtinModules', { __proto__: null, get: () => _builtinLibs, - set: (val) => _builtinLibs=val, + set: (val) => _builtinLibs = val, enumerable: true, configurable: true, }); -ObjectDefineProperty(module.exports,'_builtinLibs',{ +ObjectDefineProperty(module.exports, '_builtinLibs', { __proto__: null, - get: pendingDeprecation? deprecate( + get: pendingDeprecation ? deprecate( () => _builtinLibs, 'repl._builtinLibs is deprecated. Check module.builtinModules instead', 'DEP0142', - ):() => _builtinLibs, - set: pendingDeprecation? deprecate( - (val) => _builtinLibs=val, + ) : () => _builtinLibs, + set: pendingDeprecation ? deprecate( + (val) => _builtinLibs = val, 'repl._builtinLibs is deprecated. Check module.builtinModules instead', 'DEP0142', - ):(val) => _builtinLibs=val, + ) : (val) => _builtinLibs = val, enumerable: false, configurable: true, }); From 6e2f835515ae8cbb6553c7010987a7b0d7bf5733 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 15:43:28 -0700 Subject: [PATCH 12/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index e1313d11974b30..9eeb43db3dab7b 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -867,7 +867,7 @@ alternatively use dynamic import: await import(\'bar\');', ] }, { - send: 'import {default as alias} from "bar";', + send: 'import alias from "bar";', expect: [ kSource, kArrow, From 74df404bd1e08a1f0e614c6d2ec729a6f823bafb Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 15:43:53 -0700 Subject: [PATCH 13/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 9eeb43db3dab7b..d165bb01a5617f 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -874,7 +874,7 @@ alternatively use dynamic import: await import(\'bar\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: await import(\'bar\');', +alternatively use dynamic import: const alias = await import(\'bar\');', ] }, ]; From f259a5fc306755101dc4a22fdaa5deb69a91edd7 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 15:44:08 -0700 Subject: [PATCH 14/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index d165bb01a5617f..e193559ddbea5e 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -863,7 +863,7 @@ alternatively use dynamic import: await import(\'module-name\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: await import(\'bar\');', +alternatively use dynamic import: const { export1: localName1, export2 } = await import(\'bar\');', ] }, { From 556fa837ce9f2c493001dca7f18a09e21762dc39 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 15:44:24 -0700 Subject: [PATCH 15/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index e193559ddbea5e..6f77e03520de15 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -841,7 +841,7 @@ alternatively use dynamic import: await import(\'module-name\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: await import(\'module-name\');', +alternatively use dynamic import: const name = await import(\'module-name\');', ] }, { From f42f73cd13f3639467234c9883256a0055e618e8 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 15:44:35 -0700 Subject: [PATCH 16/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 6f77e03520de15..4d948cb1b1114f 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -819,7 +819,7 @@ const tcpTests = [ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: await import(\'fhqwhgads\');', +alternatively use dynamic import: const comeOn = await import(\'fhqwhgads\');', ] }, { From a83915de29dbd0280a6423a0312c9b66deb9fb46 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 15:51:42 -0700 Subject: [PATCH 17/33] Update lib/repl.js Co-authored-by: Antoine du Hamel --- lib/repl.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 32f3a82c4d9d1a..8dbdc0a0840805 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -230,14 +230,21 @@ const toDynamicImport=(codeLine) => { const ast=acornParse(codeLine,{__proto__: null,sourceType: 'module',ecmaVersion: 'latest'}); acornWalk.ancestor(ast,{ ImportDeclaration(node) { - const importedModules=node.source.value; - const importedSpecifiers=ArrayPrototypeMap(node.specifiers,(specifier) => { - if(specifier.local.name===specifier?.imported?.name) { - return specifier.local.name; - } - return `${specifier?.imported?.name? specifier.imported.name+':':''}${specifier.local.name}`; - }); - dynamicImportStatement+=`const ${moduleName} = await import('${importedModules}');`; + const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; + if (node.specifiers.length === 0) { + dynamicImportStatement += awaitDynamicImport; + } else if ( + node.specifiers.length === 1 && + (node.specifiers.type === 'ImportNamespaceSpecifier' || node.specifiers.type === 'ImportDefaultSpecifier') + ) { + dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; + } else { + const toObjectNotation = ({ local, imported }) => local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`; + const importNames = node.specifiers.length === 1 ? + toObjectNotation(node.specifiers[0]) : + ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, toObjectNotation), ', '); + dynamicImportStatement += `const { ${importNames} } = ${awaitDynamicImport}`; + } }, }); return dynamicImportStatement; From 64842ace564c60599de12915144fad8bbfae3360 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 16:03:57 -0700 Subject: [PATCH 18/33] Update lib/repl.js Co-authored-by: Antoine du Hamel --- lib/repl.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 8dbdc0a0840805..f5f2fff161e128 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -233,10 +233,7 @@ const toDynamicImport=(codeLine) => { const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; if (node.specifiers.length === 0) { dynamicImportStatement += awaitDynamicImport; - } else if ( - node.specifiers.length === 1 && - (node.specifiers.type === 'ImportNamespaceSpecifier' || node.specifiers.type === 'ImportDefaultSpecifier') - ) { + } else if (node.specifiers.length === 1 && node.specifiers.type === 'ImportNamespaceSpecifier') { dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; } else { const toObjectNotation = ({ local, imported }) => local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`; From c7c8488d7005dd11e07daf1eef042d1428cccaf7 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 16:04:13 -0700 Subject: [PATCH 19/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 4d948cb1b1114f..b9f8af36f2b96b 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -830,7 +830,7 @@ alternatively use dynamic import: const comeOn = await import(\'fhqwhgads\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: await import(\'module-name\');', +alternatively use dynamic import: const { export1, export2 } = await import(\'module-name\');', ] }, { From 00348c94f0e0776389006eda3cbb025b379fd7d3 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 16:04:36 -0700 Subject: [PATCH 20/33] Update lib/repl.js Co-authored-by: Antoine du Hamel --- lib/repl.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index f5f2fff161e128..bcdf2c24550b0a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -224,11 +224,11 @@ const writer = (obj) => inspect(obj, writer.options); writer.options = { ...inspect.defaultOptions, showProxy: true }; // Converts static import statement to dynamic import statement -const toDynamicImport=(codeLine) => { - let dynamicImportStatement=''; - let moduleName=''; - const ast=acornParse(codeLine,{__proto__: null,sourceType: 'module',ecmaVersion: 'latest'}); - acornWalk.ancestor(ast,{ +const toDynamicImport = (codeLine) => { + let dynamicImportStatement = ''; + let moduleName = ''; + const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' }); + acornWalk.ancestor(ast, { ImportDeclaration(node) { const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; if (node.specifiers.length === 0) { From 7d4b2c9cdd7d6929a1029cc05e90765e3f66a3ea Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 16:21:18 -0700 Subject: [PATCH 21/33] fix: missing imports --- lib/repl.js | 90 +++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index bcdf2c24550b0a..3d923a87e2a939 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -62,6 +62,7 @@ const { Boolean, Error, FunctionPrototypeBind, + JSONStringify, MathMaxApply, NumberIsNaN, NumberParseFloat, @@ -104,7 +105,9 @@ const { const { isIdentifierStart, isIdentifierChar, + parse: acornParse, } = require('internal/deps/acorn/acorn/dist/acorn'); +const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk'); const { decorateErrorStack, isError, @@ -226,7 +229,6 @@ writer.options = { ...inspect.defaultOptions, showProxy: true }; // Converts static import statement to dynamic import statement const toDynamicImport = (codeLine) => { let dynamicImportStatement = ''; - let moduleName = ''; const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' }); acornWalk.ancestor(ast, { ImportDeclaration(node) { @@ -234,9 +236,9 @@ const toDynamicImport = (codeLine) => { if (node.specifiers.length === 0) { dynamicImportStatement += awaitDynamicImport; } else if (node.specifiers.length === 1 && node.specifiers.type === 'ImportNamespaceSpecifier') { - dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; + dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; } else { - const toObjectNotation = ({ local, imported }) => local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`; + const toObjectNotation = ({ local, imported }) => (local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`); const importNames = node.specifiers.length === 1 ? toObjectNotation(node.specifiers[0]) : ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, toObjectNotation), ', '); @@ -307,13 +309,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.input, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.input, set: pendingDeprecation ? deprecate((val) => this.input = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.input = val, enumerable: false, @@ -324,13 +326,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.output, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.output, set: pendingDeprecation ? deprecate((val) => this.output = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.output = val, enumerable: false, @@ -368,9 +370,9 @@ function REPLServer(prompt, // instance and that could trigger the `MaxListenersExceededWarning`. process.prependListener('newListener', (event, listener) => { if (event === 'uncaughtException' && - process.domain && - listener.name !== 'domainUncaughtExceptionClear' && - domainSet.has(process.domain)) { + process.domain && + listener.name !== 'domainUncaughtExceptionClear' && + domainSet.has(process.domain)) { // Throw an error so that the event will not be added and the current // domain takes over. That way the user is notified about the error // and the current code evaluation is stopped, just as any other code @@ -387,8 +389,8 @@ function REPLServer(prompt, const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; const sep = '\u0000\u0000\u0000'; const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)$`); + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)$`); eval_ = eval_ || defaultEval; @@ -441,7 +443,7 @@ function REPLServer(prompt, // an expression. Note that if the above condition changes, // lib/internal/repl/utils.js needs to be changed to match. if (RegExpPrototypeExec(/^\s*{/, code) !== null && - RegExpPrototypeExec(/;\s*$/, code) === null) { + RegExpPrototypeExec(/;\s*$/, code) === null) { code = `(${StringPrototypeTrim(code)})\n`; wrappedCmd = true; } @@ -516,7 +518,7 @@ function REPLServer(prompt, while (true) { try { if (self.replMode === module.exports.REPL_MODE_STRICT && - RegExpPrototypeExec(/^\s*$/, code) === null) { + RegExpPrototypeExec(/^\s*$/, code) === null) { // "void 0" keeps the repl from returning "use strict" as the result // value for statements and declarations that don't return a value. code = `'use strict'; void 0;\n${code}`; @@ -736,7 +738,7 @@ function REPLServer(prompt, } if (options[kStandaloneREPL] && - process.listenerCount('uncaughtException') !== 0) { + process.listenerCount('uncaughtException') !== 0) { process.nextTick(() => { process.emit('uncaughtException', e); self.clearBufferedCommand(); @@ -753,7 +755,7 @@ function REPLServer(prompt, errStack = ''; ArrayPrototypeForEach(lines, (line) => { if (!matched && - RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { + RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { errStack += writer.options.breakLength >= line.length ? `Uncaught ${line}` : `Uncaught:\n${line}`; @@ -899,8 +901,8 @@ function REPLServer(prompt, // display next prompt and return. if (trimmedCmd) { if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && - StringPrototypeCharAt(trimmedCmd, 1) !== '.' && - NumberIsNaN(NumberParseFloat(trimmedCmd))) { + StringPrototypeCharAt(trimmedCmd, 1) !== '.' && + NumberIsNaN(NumberParseFloat(trimmedCmd))) { const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); const keyword = matches && matches[1]; const rest = matches && matches[2]; @@ -925,10 +927,10 @@ function REPLServer(prompt, ReflectApply(_memory, self, [cmd]); if (e && !self[kBufferedCommandSymbol] && - StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { + StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { self.output.write('npm should be run outside of the ' + - 'Node.js REPL, in your normal shell.\n' + - '(Press Ctrl+D to exit.)\n'); + 'Node.js REPL, in your normal shell.\n' + + '(Press Ctrl+D to exit.)\n'); self.displayPrompt(); return; } @@ -953,11 +955,11 @@ function REPLServer(prompt, // If we got any output - print it (if no error) if (!e && - // When an invalid REPL command is used, error message is printed - // immediately. We don't have to print anything else. So, only when - // the second argument to this function is there, print it. - arguments.length === 2 && - (!self.ignoreUndefined || ret !== undefined)) { + // When an invalid REPL command is used, error message is printed + // immediately. We don't have to print anything else. So, only when + // the second argument to this function is there, print it. + arguments.length === 2 && + (!self.ignoreUndefined || ret !== undefined)) { if (!self.underscoreAssigned) { self.last = ret; } @@ -1008,7 +1010,7 @@ function REPLServer(prompt, if (!self.editorMode || !self.terminal) { // Before exiting, make sure to clear the line. if (key.ctrl && key.name === 'd' && - self.cursor === 0 && self.line.length === 0) { + self.cursor === 0 && self.line.length === 0) { self.clearLine(); } clearPreview(key); @@ -1205,7 +1207,7 @@ 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*["'](.*)/; const simpleExpressionRE = - /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; + /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; const versionedFileNamesRe = /-\d+\.\d+/; function isIdentifier(str) { @@ -1361,7 +1363,7 @@ function complete(line, callback) { const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; ArrayPrototypeForEach(dirents, (dirent) => { if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null || - dirent.name === '.npm') { + dirent.name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1369,7 +1371,7 @@ function complete(line, callback) { const base = StringPrototypeSlice(dirent.name, 0, -extension.length); if (!dirent.isDirectory()) { if (StringPrototypeIncludes(extensions, extension) && - (!subdir || base !== 'index')) { + (!subdir || base !== 'index')) { ArrayPrototypePush(group, `${subdir}${base}`); } return; @@ -1422,7 +1424,7 @@ function complete(line, callback) { ArrayPrototypeForEach(dirents, (dirent) => { const { name } = dirent; if (RegExpPrototypeExec(versionedFileNamesRe, name) !== null || - name === '.npm') { + name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1455,20 +1457,20 @@ function complete(line, callback) { ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); } else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null && - this.allowBlockingCompletions) { + this.allowBlockingCompletions) { ({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match)); - // Handle variable member lookup. - // We support simple chained expressions like the following (no function - // calls, etc.). That is for simplicity and also because we *eval* that - // leading expression so for safety (see WARNING above) don't want to - // eval function calls. - // - // foo.bar<|> # completions for 'foo' with filter 'bar' - // spam.eggs.<|> # completions for 'spam.eggs' with filter '' - // foo<|> # all scope vars with filter 'foo' - // foo.<|> # completions for 'foo' with filter '' + // Handle variable member lookup. + // We support simple chained expressions like the following (no function + // calls, etc.). That is for simplicity and also because we *eval* that + // leading expression so for safety (see WARNING above) don't want to + // eval function calls. + // + // foo.bar<|> # completions for 'foo' with filter 'bar' + // spam.eggs.<|> # completions for 'spam.eggs' with filter '' + // foo<|> # all scope vars with filter 'foo' + // foo.<|> # completions for 'foo' with filter '' } else if (line.length === 0 || - RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { + RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { const { 0: match } = RegExpPrototypeExec(simpleExpressionRE, line) || ['']; if (line.length !== 0 && !match) { completionGroupsLoaded(); @@ -1519,7 +1521,7 @@ function complete(line, callback) { try { let p; if ((typeof obj === 'object' && obj !== null) || - typeof obj === 'function') { + typeof obj === 'function') { memberGroups.push(filteredOwnPropertyNames(obj)); p = ObjectGetPrototypeOf(obj); } else { From 01daa4675da59e59c78ce5f78fc25a4e9c46d60c Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 2 Jun 2023 01:30:03 +0200 Subject: [PATCH 22/33] fixup! fix: missing imports --- lib/repl.js | 89 ++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 3d923a87e2a939..f2f1ec97d1cf40 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -238,10 +238,9 @@ const toDynamicImport = (codeLine) => { } else if (node.specifiers.length === 1 && node.specifiers.type === 'ImportNamespaceSpecifier') { dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; } else { - const toObjectNotation = ({ local, imported }) => (local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`); - const importNames = node.specifiers.length === 1 ? - toObjectNotation(node.specifiers[0]) : - ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, toObjectNotation), ', '); + const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) => + (local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`), + ), ', '); dynamicImportStatement += `const { ${importNames} } = ${awaitDynamicImport}`; } }, @@ -309,13 +308,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.input, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.input, set: pendingDeprecation ? deprecate((val) => this.input = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.input = val, enumerable: false, @@ -326,13 +325,13 @@ function REPLServer(prompt, get: pendingDeprecation ? deprecate(() => this.output, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : () => this.output, set: pendingDeprecation ? deprecate((val) => this.output = val, 'repl.inputStream and repl.outputStream are deprecated. ' + - 'Use repl.input and repl.output instead', + 'Use repl.input and repl.output instead', 'DEP0141') : (val) => this.output = val, enumerable: false, @@ -370,9 +369,9 @@ function REPLServer(prompt, // instance and that could trigger the `MaxListenersExceededWarning`. process.prependListener('newListener', (event, listener) => { if (event === 'uncaughtException' && - process.domain && - listener.name !== 'domainUncaughtExceptionClear' && - domainSet.has(process.domain)) { + process.domain && + listener.name !== 'domainUncaughtExceptionClear' && + domainSet.has(process.domain)) { // Throw an error so that the event will not be added and the current // domain takes over. That way the user is notified about the error // and the current code evaluation is stopped, just as any other code @@ -389,8 +388,8 @@ function REPLServer(prompt, const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; const sep = '\u0000\u0000\u0000'; const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + - `${sep}(.*)$`); + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)$`); eval_ = eval_ || defaultEval; @@ -443,7 +442,7 @@ function REPLServer(prompt, // an expression. Note that if the above condition changes, // lib/internal/repl/utils.js needs to be changed to match. if (RegExpPrototypeExec(/^\s*{/, code) !== null && - RegExpPrototypeExec(/;\s*$/, code) === null) { + RegExpPrototypeExec(/;\s*$/, code) === null) { code = `(${StringPrototypeTrim(code)})\n`; wrappedCmd = true; } @@ -518,7 +517,7 @@ function REPLServer(prompt, while (true) { try { if (self.replMode === module.exports.REPL_MODE_STRICT && - RegExpPrototypeExec(/^\s*$/, code) === null) { + RegExpPrototypeExec(/^\s*$/, code) === null) { // "void 0" keeps the repl from returning "use strict" as the result // value for statements and declarations that don't return a value. code = `'use strict'; void 0;\n${code}`; @@ -738,7 +737,7 @@ function REPLServer(prompt, } if (options[kStandaloneREPL] && - process.listenerCount('uncaughtException') !== 0) { + process.listenerCount('uncaughtException') !== 0) { process.nextTick(() => { process.emit('uncaughtException', e); self.clearBufferedCommand(); @@ -755,7 +754,7 @@ function REPLServer(prompt, errStack = ''; ArrayPrototypeForEach(lines, (line) => { if (!matched && - RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { + RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) { errStack += writer.options.breakLength >= line.length ? `Uncaught ${line}` : `Uncaught:\n${line}`; @@ -901,8 +900,8 @@ function REPLServer(prompt, // display next prompt and return. if (trimmedCmd) { if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && - StringPrototypeCharAt(trimmedCmd, 1) !== '.' && - NumberIsNaN(NumberParseFloat(trimmedCmd))) { + StringPrototypeCharAt(trimmedCmd, 1) !== '.' && + NumberIsNaN(NumberParseFloat(trimmedCmd))) { const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); const keyword = matches && matches[1]; const rest = matches && matches[2]; @@ -927,10 +926,10 @@ function REPLServer(prompt, ReflectApply(_memory, self, [cmd]); if (e && !self[kBufferedCommandSymbol] && - StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { + StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { self.output.write('npm should be run outside of the ' + - 'Node.js REPL, in your normal shell.\n' + - '(Press Ctrl+D to exit.)\n'); + 'Node.js REPL, in your normal shell.\n' + + '(Press Ctrl+D to exit.)\n'); self.displayPrompt(); return; } @@ -955,11 +954,11 @@ function REPLServer(prompt, // If we got any output - print it (if no error) if (!e && - // When an invalid REPL command is used, error message is printed - // immediately. We don't have to print anything else. So, only when - // the second argument to this function is there, print it. - arguments.length === 2 && - (!self.ignoreUndefined || ret !== undefined)) { + // When an invalid REPL command is used, error message is printed + // immediately. We don't have to print anything else. So, only when + // the second argument to this function is there, print it. + arguments.length === 2 && + (!self.ignoreUndefined || ret !== undefined)) { if (!self.underscoreAssigned) { self.last = ret; } @@ -1010,7 +1009,7 @@ function REPLServer(prompt, if (!self.editorMode || !self.terminal) { // Before exiting, make sure to clear the line. if (key.ctrl && key.name === 'd' && - self.cursor === 0 && self.line.length === 0) { + self.cursor === 0 && self.line.length === 0) { self.clearLine(); } clearPreview(key); @@ -1207,7 +1206,7 @@ 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*["'](.*)/; const simpleExpressionRE = - /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; + /(?:[\w$'"`[{(](?:\w|\$|['"`\]})])*\??\.)*[a-zA-Z_$](?:\w|\$)*\??\.?$/; const versionedFileNamesRe = /-\d+\.\d+/; function isIdentifier(str) { @@ -1363,7 +1362,7 @@ function complete(line, callback) { const dirents = gracefulReaddir(dir, { withFileTypes: true }) || []; ArrayPrototypeForEach(dirents, (dirent) => { if (RegExpPrototypeExec(versionedFileNamesRe, dirent.name) !== null || - dirent.name === '.npm') { + dirent.name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1371,7 +1370,7 @@ function complete(line, callback) { const base = StringPrototypeSlice(dirent.name, 0, -extension.length); if (!dirent.isDirectory()) { if (StringPrototypeIncludes(extensions, extension) && - (!subdir || base !== 'index')) { + (!subdir || base !== 'index')) { ArrayPrototypePush(group, `${subdir}${base}`); } return; @@ -1424,7 +1423,7 @@ function complete(line, callback) { ArrayPrototypeForEach(dirents, (dirent) => { const { name } = dirent; if (RegExpPrototypeExec(versionedFileNamesRe, name) !== null || - name === '.npm') { + name === '.npm') { // Exclude versioned names that 'npm' installs. return; } @@ -1457,20 +1456,20 @@ function complete(line, callback) { ArrayPrototypePush(completionGroups, _builtinLibs, nodeSchemeBuiltinLibs); } else if ((match = RegExpPrototypeExec(fsAutoCompleteRE, line)) !== null && - this.allowBlockingCompletions) { + this.allowBlockingCompletions) { ({ 0: completionGroups, 1: completeOn } = completeFSFunctions(match)); - // Handle variable member lookup. - // We support simple chained expressions like the following (no function - // calls, etc.). That is for simplicity and also because we *eval* that - // leading expression so for safety (see WARNING above) don't want to - // eval function calls. - // - // foo.bar<|> # completions for 'foo' with filter 'bar' - // spam.eggs.<|> # completions for 'spam.eggs' with filter '' - // foo<|> # all scope vars with filter 'foo' - // foo.<|> # completions for 'foo' with filter '' + // Handle variable member lookup. + // We support simple chained expressions like the following (no function + // calls, etc.). That is for simplicity and also because we *eval* that + // leading expression so for safety (see WARNING above) don't want to + // eval function calls. + // + // foo.bar<|> # completions for 'foo' with filter 'bar' + // spam.eggs.<|> # completions for 'spam.eggs' with filter '' + // foo<|> # all scope vars with filter 'foo' + // foo.<|> # completions for 'foo' with filter '' } else if (line.length === 0 || - RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { + RegExpPrototypeExec(/\w|\.|\$/, line[line.length - 1]) !== null) { const { 0: match } = RegExpPrototypeExec(simpleExpressionRE, line) || ['']; if (line.length !== 0 && !match) { completionGroupsLoaded(); @@ -1521,7 +1520,7 @@ function complete(line, callback) { try { let p; if ((typeof obj === 'object' && obj !== null) || - typeof obj === 'function') { + typeof obj === 'function') { memberGroups.push(filteredOwnPropertyNames(obj)); p = ObjectGetPrototypeOf(obj); } else { From d59d33d530746a3da738c3ef183d0d770fcbfca5 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 16:37:47 -0700 Subject: [PATCH 23/33] Update test/parallel/test-repl.js Co-authored-by: Antoine du Hamel --- test/parallel/test-repl.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index b9f8af36f2b96b..46b75374ffa070 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -874,7 +874,18 @@ alternatively use dynamic import: const { export1: localName1, export2 } = await '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const alias = await import(\'bar\');', +alternatively use dynamic import: const { default: alias } = await import(\'bar\');', + ] + }, + { + send: 'import alias, {namedExport} from "bar";', + expect: [ + kSource, + kArrow, + '', + 'Uncaught:', + 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ +alternatively use dynamic import: const { default: alias, namedExport } = await import(\'bar\');', ] }, ]; From 91210be420d563206ab828a8841d34e947f4c72f Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Thu, 1 Jun 2023 23:34:07 -0700 Subject: [PATCH 24/33] test: fix deafult import --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 46b75374ffa070..014db7c40359b8 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -819,7 +819,7 @@ const tcpTests = [ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const comeOn = await import(\'fhqwhgads\');', +alternatively use dynamic import: const { default: comeOn } = await import(\'fhqwhgads\');', ] }, { From 4d61565f6ca400cfbf8d793f5fca61c56f1e8d3f Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Fri, 2 Jun 2023 07:27:49 -0700 Subject: [PATCH 25/33] fix: quote fix --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 014db7c40359b8..c24d6f6581445b 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -819,7 +819,7 @@ const tcpTests = [ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const { default: comeOn } = await import(\'fhqwhgads\');', +alternatively use dynamic import: const { default: comeOn } = await import("fhqwhgads");', ] }, { From 9a619e584bbb082ec0d71b897999df5b4a075cdc Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Fri, 2 Jun 2023 10:21:56 -0700 Subject: [PATCH 26/33] Update lib/repl.js Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index f2f1ec97d1cf40..b2bc2c9b4bb27b 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -709,7 +709,7 @@ function REPLServer(prompt, 'module'; if (StringPrototypeIncludes(e.message, importErrorStr)) { e.message = 'Cannot use import statement inside the Node.js ' + - 'REPL, alternatively use dynamic import: ' + toDynamicImport(self.lines.at(-1)); + 'REPL, alternatively use dynamic import: ' + toDynamicImport(ArrayPrototypeAt(self.lines, -1)); e.stack = SideEffectFreeRegExpPrototypeSymbolReplace( /SyntaxError:.*\n/, e.stack, From 9ef04900310c6c302da582ad90005ea9de1432de Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Fri, 2 Jun 2023 09:45:45 -0700 Subject: [PATCH 27/33] test: fix quotes --- test/parallel/test-repl.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index c24d6f6581445b..74299d4b4cbad5 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -830,7 +830,7 @@ alternatively use dynamic import: const { default: comeOn } = await import("fhqw '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const { export1, export2 } = await import(\'module-name\');', +alternatively use dynamic import: const { export1, export2 } = await import("module-name");', ] }, { @@ -841,7 +841,7 @@ alternatively use dynamic import: const { export1, export2 } = await import(\'mo '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const name = await import(\'module-name\');', +alternatively use dynamic import: const name = await import("module-name");', ] }, { @@ -852,7 +852,7 @@ alternatively use dynamic import: const name = await import(\'module-name\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: await import(\'module-name\');', +alternatively use dynamic import: await import("module-name");', ] }, { @@ -863,7 +863,7 @@ alternatively use dynamic import: await import(\'module-name\');', '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const { export1: localName1, export2 } = await import(\'bar\');', +alternatively use dynamic import: const { export1: localName1, export2 } = await import("bar");', ] }, { @@ -874,7 +874,7 @@ alternatively use dynamic import: const { export1: localName1, export2 } = await '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const { default: alias } = await import(\'bar\');', +alternatively use dynamic import: const { default: alias } = await import("bar");', ] }, { @@ -885,7 +885,7 @@ alternatively use dynamic import: const { default: alias } = await import(\'bar\ '', 'Uncaught:', 'SyntaxError: Cannot use import statement inside the Node.js REPL, \ -alternatively use dynamic import: const { default: alias, namedExport } = await import(\'bar\');', +alternatively use dynamic import: const { default: alias, namedExport } = await import("bar");', ] }, ]; From 605c8cb5444af1220ab4fa7fe62cb936ba310268 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Fri, 2 Jun 2023 12:29:52 -0700 Subject: [PATCH 28/33] Update lib/repl.js Co-authored-by: Antoine du Hamel --- lib/repl.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/repl.js b/lib/repl.js index b2bc2c9b4bb27b..4b0e1df6217914 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -59,6 +59,7 @@ const { ArrayPrototypeSort, ArrayPrototypeSplice, ArrayPrototypeUnshift, + ArrayPrototypeAt, Boolean, Error, FunctionPrototypeBind, From 89e992ca0dbd5e7f46dcb5a5fbefa7ef0c1c9d43 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Sat, 3 Jun 2023 11:34:12 -0700 Subject: [PATCH 29/33] Update lib/repl.js Co-authored-by: Antoine du Hamel --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 4b0e1df6217914..4d422ac9b060e8 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -236,7 +236,7 @@ const toDynamicImport = (codeLine) => { const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; if (node.specifiers.length === 0) { dynamicImportStatement += awaitDynamicImport; - } else if (node.specifiers.length === 1 && node.specifiers.type === 'ImportNamespaceSpecifier') { + } else if (node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier') { dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; } else { const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) => From 1b0bb6c117ce5af592561412c162a1e36f123612 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Fri, 9 Jun 2023 19:57:17 -0700 Subject: [PATCH 30/33] fix: specifier index check Co-authored-by: Jordan Harband --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 4d422ac9b060e8..c12f7120a69da7 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -236,7 +236,7 @@ const toDynamicImport = (codeLine) => { const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; if (node.specifiers.length === 0) { dynamicImportStatement += awaitDynamicImport; - } else if (node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier') { + } else if (node.specifiers.length === 1 && node.specifiers[0] && node.specifiers[0].type === 'ImportNamespaceSpecifier') { dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; } else { const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) => From b9903bae5a3a765cdf0bdd7bb78b2c61fbb234da Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Fri, 9 Jun 2023 20:01:13 -0700 Subject: [PATCH 31/33] chore: typo specifiers --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index c12f7120a69da7..4d9067c0f50882 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -237,7 +237,7 @@ const toDynamicImport = (codeLine) => { if (node.specifiers.length === 0) { dynamicImportStatement += awaitDynamicImport; } else if (node.specifiers.length === 1 && node.specifiers[0] && node.specifiers[0].type === 'ImportNamespaceSpecifier') { - dynamicImportStatement += `const ${node.specifier[0].local.name} = ${awaitDynamicImport}`; + dynamicImportStatement += `const ${node.specifiers[0].local.name} = ${awaitDynamicImport}`; } else { const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) => (local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`), From e9fe19370b605d705a58fed101147a45db2c61c0 Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Sat, 10 Jun 2023 07:10:13 -0700 Subject: [PATCH 32/33] Update lib/repl.js Co-authored-by: Antoine du Hamel --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 4d9067c0f50882..a870bce1c3faed 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -236,7 +236,7 @@ const toDynamicImport = (codeLine) => { const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; if (node.specifiers.length === 0) { dynamicImportStatement += awaitDynamicImport; - } else if (node.specifiers.length === 1 && node.specifiers[0] && node.specifiers[0].type === 'ImportNamespaceSpecifier') { + } else if (node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier') { dynamicImportStatement += `const ${node.specifiers[0].local.name} = ${awaitDynamicImport}`; } else { const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) => From c0459010b63e35fd1ce23023f649e481216d97ab Mon Sep 17 00:00:00 2001 From: Hemanth HM Date: Sat, 10 Jun 2023 11:05:06 -0700 Subject: [PATCH 33/33] chore: sort imports --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index a870bce1c3faed..2a63050923b198 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -43,6 +43,7 @@ 'use strict'; const { + ArrayPrototypeAt, ArrayPrototypeFilter, ArrayPrototypeFindIndex, ArrayPrototypeForEach, @@ -59,7 +60,6 @@ const { ArrayPrototypeSort, ArrayPrototypeSplice, ArrayPrototypeUnshift, - ArrayPrototypeAt, Boolean, Error, FunctionPrototypeBind,