diff --git a/lib/_tls_common.js b/lib/_tls_common.js index 616b7b47f46dac..81c46597e82973 100644 --- a/lib/_tls_common.js +++ b/lib/_tls_common.js @@ -27,7 +27,7 @@ const { ArrayPrototypeJoin, ArrayPrototypePush, ObjectCreate, - StringPrototypeReplace, + RegExpPrototypeSymbolReplace, StringPrototypeSplit, StringPrototypeStartsWith, } = primordials; @@ -394,13 +394,15 @@ exports.translatePeerCertificate = function translatePeerCertificate(c) { c.infoAccess = ObjectCreate(null); // XXX: More key validation? - StringPrototypeReplace(info, /([^\n:]*):([^\n]*)(?:\n|$)/g, - (all, key, val) => { - if (key in c.infoAccess) - ArrayPrototypePush(c.infoAccess[key], val); - else - c.infoAccess[key] = [val]; - }); + RegExpPrototypeSymbolReplace( + /([^\n:]*):([^\n]*)(?:\n|$)/g, + info, + (all, key, val) => { + if (key in c.infoAccess) + ArrayPrototypePush(c.infoAccess[key], val); + else + c.infoAccess[key] = [val]; + }); } return c; }; diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index adbefa4839826d..01ae43dc51b8db 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -31,8 +31,9 @@ const { ObjectSetPrototypeOf, ReflectApply, RegExp, + RegExpPrototypeSymbolReplace, RegExpPrototypeTest, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, Symbol, SymbolFor, @@ -1433,9 +1434,10 @@ Server.prototype.addContext = function(servername, context) { throw new ERR_TLS_REQUIRED_SERVER_NAME(); } - const re = new RegExp('^' + StringPrototypeReplace( - StringPrototypeReplace(servername, /([.^$+?\-\\[\]{}])/g, '\\$1'), - /\*/g, '[^.]*' + const re = new RegExp('^' + StringPrototypeReplaceAll( + RegExpPrototypeSymbolReplace(/([.^$+?\-\\[\]{}])/g, servername, '\\$1'), + '*', + '[^.]*', ) + '$'); ArrayPrototypePush(this._contexts, [re, tls.createSecureContext(context).context]); diff --git a/lib/assert.js b/lib/assert.js index 8ca9b211394ba8..8ff457aaa85681 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -35,13 +35,14 @@ const { ObjectKeys, ObjectPrototypeIsPrototypeOf, ReflectApply, + RegExpPrototypeSymbolReplace, RegExpPrototypeTest, SafeMap, String, StringPrototypeCharCodeAt, StringPrototypeIncludes, StringPrototypeIndexOf, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -271,9 +272,10 @@ function parseCode(code, offset) { return [ node.node.start, - StringPrototypeReplace(StringPrototypeSlice(code, - node.node.start, node.node.end), - escapeSequencesRegExp, escapeFn) + RegExpPrototypeSymbolReplace( + escapeSequencesRegExp, + StringPrototypeSlice(code, node.node.start, node.node.end), + escapeFn) ]; } @@ -345,7 +347,7 @@ function getErrMessage(message, fn) { // Always normalize indentation, otherwise the message could look weird. if (StringPrototypeIncludes(message, '\n')) { if (EOL === '\r\n') { - message = StringPrototypeReplace(message, /\r\n/g, '\n'); + message = StringPrototypeReplaceAll(message, '\r\n', '\n'); } const frames = StringPrototypeSplit(message, '\n'); message = ArrayPrototypeShift(frames); diff --git a/lib/buffer.js b/lib/buffer.js index d3034a46c60b29..bace71401ba0eb 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -36,8 +36,8 @@ const { ObjectDefineProperties, ObjectDefineProperty, ObjectSetPrototypeOf, + RegExpPrototypeSymbolReplace, StringPrototypeCharCodeAt, - StringPrototypeReplace, StringPrototypeSlice, StringPrototypeToLowerCase, StringPrototypeTrim, @@ -812,8 +812,8 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) { const max = INSPECT_MAX_BYTES; const actualMax = MathMin(max, this.length); const remaining = this.length - max; - let str = StringPrototypeTrim(StringPrototypeReplace( - this.hexSlice(0, actualMax), /(.{2})/g, '$1 ')); + let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace( + /(.{2})/g, this.hexSlice(0, actualMax), '$1 ')); if (remaining > 0) str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; // Inspect special properties as well, if possible. diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index dc2817ad2a54ec..8f331ff1ae95e0 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -25,7 +25,7 @@ const { StringPrototypeIncludes, StringPrototypePadStart, StringPrototypeRepeat, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeSplit, Symbol, @@ -266,7 +266,7 @@ ObjectDefineProperties(Console.prototype, { if (groupIndent.length !== 0) { if (StringPrototypeIncludes(string, '\n')) { - string = StringPrototypeReplace(string, /\n/g, `\n${groupIndent}`); + string = StringPrototypeReplaceAll(string, '\n', `\n${groupIndent}`); } string = groupIndent + string; } diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 27d25c92ad93aa..5c837e2baae611 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -8,8 +8,8 @@ const { ArrayPrototypePush, FunctionPrototypeBind, NumberParseInt, - StringPrototypeMatch, - StringPrototypeReplace, + RegExpPrototypeExec, + RegExpPrototypeSymbolReplace, } = primordials; const errors = require('internal/errors'); @@ -79,7 +79,7 @@ class Resolver { if (ipVersion !== 0) return ArrayPrototypePush(newSet, [ipVersion, serv, IANA_DNS_PORT]); - const match = StringPrototypeMatch(serv, IPv6RE); + const match = RegExpPrototypeExec(IPv6RE, serv); // Check for an IPv6 in brackets. if (match) { @@ -87,13 +87,14 @@ class Resolver { if (ipVersion !== 0) { const port = NumberParseInt( - StringPrototypeReplace(serv, addrSplitRE, '$2')) || IANA_DNS_PORT; + RegExpPrototypeSymbolReplace(addrSplitRE, serv, '$2') + ) || IANA_DNS_PORT; return ArrayPrototypePush(newSet, [ipVersion, match[1], port]); } } // addr::port - const addrSplitMatch = StringPrototypeMatch(serv, addrSplitRE); + const addrSplitMatch = RegExpPrototypeExec(addrSplitRE, serv); if (addrSplitMatch) { const hostIP = addrSplitMatch[1]; diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 8a7e744c5fe667..7267cc1b6c2f85 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -34,13 +34,13 @@ const { ObjectKeys, RangeError, ReflectApply, + RegExpPrototypeExec, RegExpPrototypeTest, SafeMap, SafeWeakMap, String, StringPrototypeEndsWith, StringPrototypeIncludes, - StringPrototypeMatch, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -381,7 +381,7 @@ function getMessage(key, args, self) { } const expectedLength = - (StringPrototypeMatch(msg, /%[dfijoOs]/g) || []).length; + (RegExpPrototypeExec(/%[dfijoOs]/g, msg) || []).length; assert( expectedLength === args.length, `Code: ${key}; The provided arguments length (${args.length}) does not ` + diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index d5f3b4c78f16a2..048a4f72171974 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -18,7 +18,7 @@ const { ReflectOwnKeys, StringPrototypeEndsWith, StringPrototypeIncludes, - StringPrototypeReplace, + StringPrototypeReplaceAll, Symbol, TypedArrayPrototypeIncludes, } = primordials; @@ -369,7 +369,7 @@ function preprocessSymlinkDestination(path, type, linkPath) { return pathModule.toNamespacedPath(path); } // Windows symlinks don't tolerate forward slashes. - return StringPrototypeReplace(path, /\//g, '\\'); + return StringPrototypeReplaceAll(path, '/', '\\'); } // Constructor for file stats. diff --git a/lib/internal/main/print_help.js b/lib/internal/main/print_help.js index 94c81c484880b3..0db509aa3b8ef1 100644 --- a/lib/internal/main/print_help.js +++ b/lib/internal/main/print_help.js @@ -8,9 +8,9 @@ const { MathMax, ObjectKeys, RegExp, + RegExpPrototypeSymbolReplace, StringPrototypeTrimLeft, StringPrototypeRepeat, - StringPrototypeReplace, SafeMap, } = primordials; @@ -72,14 +72,14 @@ const envVars = new SafeMap(ArrayPrototypeConcat([ function indent(text, depth) { - return StringPrototypeReplace(text, /^/gm, StringPrototypeRepeat(' ', depth)); + return RegExpPrototypeSymbolReplace(/^/gm, text, StringPrototypeRepeat(' ', depth)); } function fold(text, width) { - return StringPrototypeReplace(text, - new RegExp(`([^\n]{0,${width}})( |$)`, 'g'), - (_, newLine, end) => - newLine + (end === ' ' ? '\n' : '')); + return RegExpPrototypeSymbolReplace( + new RegExp(`([^\n]{0,${width}})( |$)`, 'g'), + text, + (_, newLine, end) => newLine + (end === ' ' ? '\n' : '')); } function getArgDescription(type) { diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 037d872e1d4fa1..e61a5602c90a5a 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -46,6 +46,7 @@ const { ObjectSetPrototypeOf, ReflectApply, ReflectSet, + RegExpPrototypeExec, RegExpPrototypeTest, SafeMap, SafeWeakMap, @@ -55,7 +56,6 @@ const { StringPrototypeEndsWith, StringPrototypeLastIndexOf, StringPrototypeIndexOf, - StringPrototypeMatch, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -463,7 +463,7 @@ const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/; function resolveExports(nmPath, request) { // The implementation's behavior is meant to mirror resolution in ESM. const [, name, expansion = ''] = - StringPrototypeMatch(request, EXPORTS_PATTERN) || []; + RegExpPrototypeExec(EXPORTS_PATTERN, request) || []; if (!name) return; const pkgPath = path.resolve(nmPath, name); diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 549d43cc20e119..fc84ef3d4bf1fe 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -10,10 +10,10 @@ const { PromiseResolve, PromisePrototypeCatch, ReflectApply, + RegExpPrototypeExec, + RegExpPrototypeSymbolReplace, SafeSet, StringPrototypeIncludes, - StringPrototypeMatch, - StringPrototypeReplace, StringPrototypeSplit, } = primordials; @@ -109,8 +109,9 @@ class ModuleJob { ' does not provide an export named')) { const splitStack = StringPrototypeSplit(e.stack, '\n'); const parentFileUrl = splitStack[0]; - const [, childSpecifier, name] = StringPrototypeMatch(e.message, - /module '(.*)' does not provide an export named '(.+)'/); + const [, childSpecifier, name] = RegExpPrototypeExec( + /module '(.*)' does not provide an export named '(.+)'/, + e.message); const childFileURL = await this.loader.resolve(childSpecifier, parentFileUrl); const format = await this.loader.getFormat(childFileURL); @@ -120,9 +121,9 @@ class ModuleJob { // line which causes the error. For multi-line import statements we // cannot generate an equivalent object descructuring assignment by // just parsing the error stack. - const oneLineNamedImports = StringPrototypeMatch(importStatement, /{.*}/); + const oneLineNamedImports = RegExpPrototypeExec(/{.*}/, importStatement); const destructuringAssignment = oneLineNamedImports && - StringPrototypeReplace(oneLineNamedImports, /\s+as\s+/g, ': '); + RegExpPrototypeSymbolReplace(/\s+as\s+/g, oneLineNamedImports, ': '); e.message = `Named export '${name}' not found. The requested module` + ` '${childSpecifier}' is a CommonJS module, which may not support` + ' all module.exports as named exports.\nCommonJS modules can ' + diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 8a5bc841ada22a..7656ab1124b506 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -9,7 +9,6 @@ const { ObjectFreeze, ObjectGetOwnPropertyNames, ObjectPrototypeHasOwnProperty, - RegExp, RegExpPrototypeTest, SafeMap, SafeSet, @@ -17,7 +16,7 @@ const { StringPrototypeEndsWith, StringPrototypeIndexOf, StringPrototypeLastIndexOf, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -343,8 +342,6 @@ function throwInvalidPackageTarget( } const invalidSegmentRegEx = /(^|\\|\/)(\.\.?|node_modules)(\\|\/|$)/; -const patternRegEx = /\*/g; - function resolvePackageTargetString( target, subpath, match, packageJSONUrl, base, pattern, internal, conditions) { if (subpath !== '' && !pattern && target[target.length - 1] !== '/') @@ -360,7 +357,7 @@ function resolvePackageTargetString( } catch {} if (!isURL) { const exportTarget = pattern ? - StringPrototypeReplace(target, patternRegEx, subpath) : + StringPrototypeReplaceAll(target, '*', subpath) : target + subpath; return packageResolve(exportTarget, packageJSONUrl, conditions); } @@ -384,8 +381,7 @@ function resolvePackageTargetString( throwInvalidSubpath(match + subpath, packageJSONUrl, internal, base); if (pattern) - return new URL(StringPrototypeReplace(resolved.href, patternRegEx, - subpath)); + return new URL(StringPrototypeReplaceAll(resolved.href, '*', subpath)); return new URL(subpath, resolved); } @@ -779,7 +775,7 @@ function resolveAsCommonJS(specifier, parentURL) { // Normalize the path separator to give a valid suggestion // on Windows if (process.platform === 'win32') { - found = StringPrototypeReplace(found, new RegExp(`\\${sep}`, 'g'), '/'); + found = StringPrototypeReplaceAll(found, sep, '/'); } return found; } catch { diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 66636d9ce092b5..31321bbb6f42f7 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -14,7 +14,7 @@ const { RegExpPrototypeTest, SafeMap, SafeSet, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -174,13 +174,12 @@ function enrichCJSError(err) { // Strategy for loading a node-style CommonJS module const isWindows = process.platform === 'win32'; -const winSepRegEx = /\//g; translators.set('commonjs', async function commonjsStrategy(url, isMain) { debug(`Translating CJSModule ${url}`); let filename = internalURLModule.fileURLToPath(new URL(url)); if (isWindows) - filename = StringPrototypeReplace(filename, winSepRegEx, '\\'); + filename = StringPrototypeReplaceAll(filename, '/', '\\'); if (!cjsParse) await initCJSParse(); const { module, exportNames } = cjsPreparseModuleExports(filename); @@ -299,7 +298,7 @@ translators.set('json', async function jsonStrategy(url) { let module; if (pathname) { modulePath = isWindows ? - StringPrototypeReplace(pathname, winSepRegEx, '\\') : pathname; + StringPrototypeReplaceAll(pathname, '/', '\\') : pathname; module = CJSModule._cache[modulePath]; if (module && module.loaded) { const exports = module.exports; diff --git a/lib/internal/policy/manifest.js b/lib/internal/policy/manifest.js index 099fb630e0b176..7996b8ac35715b 100644 --- a/lib/internal/policy/manifest.js +++ b/lib/internal/policy/manifest.js @@ -7,11 +7,11 @@ const { ObjectFreeze, ObjectKeys, ObjectSetPrototypeOf, + RegExpPrototypeSymbolReplace, RegExpPrototypeTest, SafeMap, SafeSet, StringPrototypeEndsWith, - StringPrototypeReplace, Symbol, uncurryThis, } = primordials; @@ -330,10 +330,10 @@ class Manifest { const protocolOrResolve = (resourceHREF) => { if (StringPrototypeEndsWith(resourceHREF, ':')) { // URL parse will trim these anyway, save the compute - resourceHREF = StringPrototypeReplace( - resourceHREF, - // eslint-disable-next-line + resourceHREF = RegExpPrototypeSymbolReplace( + // eslint-disable-next-line no-control-regex /^[\x00-\x1F\x20]|\x09\x0A\x0D|[\x00-\x1F\x20]$/g, + resourceHREF, '' ); if (RegExpPrototypeTest(/^[a-zA-Z][a-zA-Z+\-.]*:$/, resourceHREF)) { diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index b16705f9fb95b0..14edaaa8acda53 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -15,10 +15,11 @@ const { ObjectDefineProperty, ObjectFreeze, ReflectApply, + RegExpPrototypeSymbolReplace, RegExpPrototypeTest, SafeSet, StringPrototypeEndsWith, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeStartsWith, Uint32Array, @@ -244,7 +245,6 @@ function wrapProcessMethods(binding) { }; } -const replaceUnderscoresRegex = /_/g; const leadingDashesRegex = /^--?/; const trailingValuesRegex = /=.*$/; @@ -288,7 +288,7 @@ function buildAllowedFlags() { } const trimLeadingDashes = - (flag) => StringPrototypeReplace(flag, leadingDashesRegex, ''); + (flag) => RegExpPrototypeSymbolReplace(leadingDashesRegex, flag, ''); // Save these for comparison against flags provided to // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. @@ -324,9 +324,9 @@ function buildAllowedFlags() { // on a dummy option set and see whether it rejects the argument or // not. if (typeof key === 'string') { - key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-'); + key = StringPrototypeReplaceAll(key, '_', '-'); if (RegExpPrototypeTest(leadingDashesRegex, key)) { - key = StringPrototypeReplace(key, trailingValuesRegex, ''); + key = RegExpPrototypeSymbolReplace(trailingValuesRegex, key, ''); return super.has(key); } return nodeFlags.has(key); diff --git a/lib/internal/readline/utils.js b/lib/internal/readline/utils.js index 55b3e07b4c1782..166cfc261186b7 100644 --- a/lib/internal/readline/utils.js +++ b/lib/internal/readline/utils.js @@ -3,11 +3,11 @@ const { ArrayPrototypeSlice, ArrayPrototypeSort, + RegExpPrototypeExec, RegExpPrototypeTest, StringFromCharCode, StringPrototypeCharCodeAt, StringPrototypeCodePointAt, - StringPrototypeMatch, StringPrototypeSlice, StringPrototypeToLowerCase, Symbol, @@ -190,11 +190,13 @@ function* emitKeys(stream) { const cmd = StringPrototypeSlice(s, cmdStart); let match; - if ((match = StringPrototypeMatch(cmd, /^(\d\d?)(;(\d))?([~^$])$/))) { + if ( + (match = RegExpPrototypeExec(/^(\d\d?)(;(\d))?([~^$])$/, cmd)) + ) { code += match[1] + match[4]; modifier = (match[3] || 1) - 1; } else if ( - (match = StringPrototypeMatch(cmd, /^((\d;)?(\d))?([A-Za-z])$/)) + (match = RegExpPrototypeExec(/^((\d;)?(\d))?([A-Za-z])$/, cmd)) ) { code += match[4]; modifier = (match[3] || 1) - 1; diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index 74ef94e81070dc..9300283e0015cc 100644 --- a/lib/internal/repl/history.js +++ b/lib/internal/repl/history.js @@ -4,7 +4,7 @@ const { ArrayPrototypeJoin, Boolean, FunctionPrototype, - StringPrototypeSplit, + RegExpPrototypeSymbolSplit, StringPrototypeTrim, } = primordials; @@ -90,7 +90,7 @@ function setupHistory(repl, historyPath, ready) { } if (data) { - repl.history = StringPrototypeSplit(data, /[\n\r]+/, repl.historySize); + repl.history = RegExpPrototypeSymbolSplit(/[\n\r]+/, data, repl.historySize); } else { repl.history = []; } diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index e2bda7665a9138..508651548a4081 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -12,7 +12,7 @@ const { StringPrototypeEndsWith, StringPrototypeIndexOf, StringPrototypeLastIndexOf, - StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeStartsWith, StringPrototypeToLowerCase, @@ -291,9 +291,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { function isInStrictMode(repl) { return repl.replMode === REPL_MODE_STRICT || ArrayPrototypeIncludes( ArrayPrototypeMap(process.execArgv, - (e) => StringPrototypeReplace( + (e) => StringPrototypeReplaceAll( StringPrototypeToLowerCase(e), - /_/g, + '_', '-' )), '--use-strict'); diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index 60bcb2fcadd1a1..63a23c1c964aa3 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -5,9 +5,9 @@ const { ArrayPrototypeJoin, ArrayPrototypeMap, ErrorPrototypeToString, + RegExpPrototypeSymbolSplit, StringPrototypeRepeat, StringPrototypeSlice, - StringPrototypeSplit, StringPrototypeStartsWith, } = primordials; @@ -137,7 +137,7 @@ function getErrorSource( sourceMap.payload, originalSourcePathNoScheme ); - const lines = StringPrototypeSplit(source, /\r?\n/, originalLine + 1); + const lines = RegExpPrototypeSymbolSplit(/\r?\n/, source, originalLine + 1); const line = lines[originalLine]; if (!line) return exceptionLine; diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index b95653ebba84e9..ee6b392cee8fa0 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -7,9 +7,10 @@ const { ObjectKeys, ObjectGetOwnPropertyDescriptor, ObjectPrototypeHasOwnProperty, + RegExpPrototypeExec, + RegExpPrototypeSymbolSplit, RegExpPrototypeTest, SafeMap, - StringPrototypeMatch, StringPrototypeSplit, } = primordials; @@ -68,9 +69,9 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance) { debug(err.stack); return; } - const match = StringPrototypeMatch( + const match = RegExpPrototypeExec( + /\/[*/]#\s+sourceMappingURL=(?[^\s]+)/, content, - /\/[*/]#\s+sourceMappingURL=(?[^\s]+)/ ); if (match) { const data = dataFromUrl(filename, match.groups.sourceMappingURL); @@ -119,7 +120,7 @@ function lineLengths(content) { // We purposefully keep \r as part of the line-length calculation, in // cases where there is a \r\n separator, so that this can be taken into // account in coverage calculations. - return ArrayPrototypeMap(StringPrototypeSplit(content, /\n|\u2028|\u2029/), (line) => { + return ArrayPrototypeMap(RegExpPrototypeSymbolSplit(/\n|\u2028|\u2029/, content), (line) => { return line.length; }); } diff --git a/lib/readline.js b/lib/readline.js index 352b7ed64f06ba..a93ebb31c63349 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -47,14 +47,14 @@ const { NumberIsNaN, ObjectDefineProperty, ObjectSetPrototypeOf, + RegExpPrototypeExec, + RegExpPrototypeSymbolReplace, + RegExpPrototypeSymbolSplit, RegExpPrototypeTest, StringPrototypeCodePointAt, StringPrototypeEndsWith, - StringPrototypeMatch, StringPrototypeRepeat, - StringPrototypeReplace, StringPrototypeSlice, - StringPrototypeSplit, StringPrototypeStartsWith, StringPrototypeTrim, Symbol, @@ -493,7 +493,7 @@ Interface.prototype._normalWrite = function(b) { let string = this._decoder.write(b); if (this._sawReturnAt && DateNow() - this._sawReturnAt <= this.crlfDelay) { - string = StringPrototypeReplace(string, /^\n/, ''); + string = RegExpPrototypeSymbolReplace(/^\n/, string, ''); this._sawReturnAt = 0; } @@ -508,7 +508,7 @@ Interface.prototype._normalWrite = function(b) { this._sawReturnAt = StringPrototypeEndsWith(string, '\r') ? DateNow() : 0; // Got one or more newlines; process into "line" events - const lines = StringPrototypeSplit(string, lineEnding); + const lines = RegExpPrototypeSymbolSplit(lineEnding, string); // Either '' or (conceivably) the unfinished portion of the next line string = ArrayPrototypePop(lines); this._line_buffer = string; @@ -612,7 +612,7 @@ Interface.prototype._wordLeft = function() { const leading = StringPrototypeSlice(this.line, 0, this.cursor); const reversed = ArrayPrototypeJoin( ArrayPrototypeReverse(ArrayFrom(leading)), ''); - const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/); + const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed); this._moveCursor(-match[0].length); } }; @@ -621,7 +621,7 @@ Interface.prototype._wordLeft = function() { Interface.prototype._wordRight = function() { if (this.cursor < this.line.length) { const trailing = StringPrototypeSlice(this.line, this.cursor); - const match = StringPrototypeMatch(trailing, /^(?:\s+|[^\w\s]+|\w+)\s*/); + const match = RegExpPrototypeExec(/^(?:\s+|[^\w\s]+|\w+)\s*/, trailing); this._moveCursor(match[0].length); } }; @@ -657,7 +657,7 @@ Interface.prototype._deleteWordLeft = function() { let leading = StringPrototypeSlice(this.line, 0, this.cursor); const reversed = ArrayPrototypeJoin( ArrayPrototypeReverse(ArrayFrom(leading)), ''); - const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/); + const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed); leading = StringPrototypeSlice(leading, 0, leading.length - match[0].length); this.line = leading + StringPrototypeSlice(this.line, this.cursor, @@ -671,7 +671,7 @@ Interface.prototype._deleteWordLeft = function() { Interface.prototype._deleteWordRight = function() { if (this.cursor < this.line.length) { const trailing = StringPrototypeSlice(this.line, this.cursor); - const match = StringPrototypeMatch(trailing, /^(?:\s+|\W+|\w+)\s*/); + const match = RegExpPrototypeExec(/^(?:\s+|\W+|\w+)\s*/, trailing); this.line = StringPrototypeSlice(this.line, 0, this.cursor) + StringPrototypeSlice(trailing, match[0].length); this._refreshLine(); @@ -1104,7 +1104,7 @@ Interface.prototype._ttyWrite = function(s, key) { // falls through default: if (typeof s === 'string' && s) { - const lines = StringPrototypeSplit(s, /\r\n|\n|\r/); + const lines = RegExpPrototypeSymbolSplit(/\r\n|\n|\r/, s); for (let i = 0, len = lines.length; i < len; i++) { if (i > 0) { this._line(); diff --git a/lib/repl.js b/lib/repl.js index 3368b5997ae01d..7467609877ffc9 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -77,6 +77,8 @@ const { ReflectApply, RegExp, RegExpPrototypeExec, + RegExpPrototypeSymbolReplace, + RegExpPrototypeSymbolSplit, RegExpPrototypeTest, SafeSet, SafeWeakSet, @@ -84,9 +86,7 @@ const { StringPrototypeCodePointAt, StringPrototypeEndsWith, StringPrototypeIncludes, - StringPrototypeMatch, StringPrototypeRepeat, - StringPrototypeReplace, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -614,22 +614,25 @@ function REPLServer(prompt, if (e.stack) { if (e.name === 'SyntaxError') { // Remove stack trace. - e.stack = StringPrototypeReplace(StringPrototypeReplace(e.stack, - /^REPL\d+:\d+\r?\n/, ''), - /^\s+at\s.*\n?/gm, ''); + e.stack = RegExpPrototypeSymbolReplace( + /^\s+at\s.*\n?/gm, + RegExpPrototypeSymbolReplace(/^REPL\d+:\d+\r?\n/, e.stack, ''), + '', + ); 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 = StringPrototypeReplace(e.stack, - /SyntaxError:.*\n/, - `SyntaxError: ${e.message}\n`); + e.stack = RegExpPrototypeSymbolReplace( + /SyntaxError:.*\n/, + e.stack, + `SyntaxError: ${e.message}\n`); } } else if (self.replMode === module.exports.REPL_MODE_STRICT) { - e.stack = StringPrototypeReplace( - e.stack, + e.stack = RegExpPrototypeSymbolReplace( /(\s+at\s+REPL\d+:)(\d+)/, + e.stack, (_, pre, line) => pre + (line - 1) ); } @@ -659,7 +662,7 @@ function REPLServer(prompt, if (errStack === '') { errStack = self.writer(e); } - const lines = StringPrototypeSplit(errStack, /(?<=\n)/); + const lines = RegExpPrototypeSymbolSplit(/(?<=\n)/, errStack); let matched = false; errStack = ''; @@ -794,7 +797,7 @@ function REPLServer(prompt, // code alignment const matches = self._sawKeyPress ? - StringPrototypeMatch(cmd, /^\s+/) : null; + RegExpPrototypeExec(/^\s+/, cmd) : null; if (matches) { const prefix = matches[0]; self.write(prefix); @@ -814,7 +817,7 @@ function REPLServer(prompt, if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && StringPrototypeCharAt(trimmedCmd, 1) !== '.' && NumberIsNaN(NumberParseFloat(trimmedCmd))) { - const matches = StringPrototypeMatch(trimmedCmd, /^\.([^\s]+)\s*(.*)$/); + const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd); const keyword = matches && matches[1]; const rest = matches && matches[2]; if (ReflectApply(_parseREPLKeyword, self, [keyword, rest]) === true) { @@ -1159,7 +1162,7 @@ function gracefulReaddir(...args) { function completeFSFunctions(line) { let baseName = ''; - let filePath = StringPrototypeMatch(line, fsAutoCompleteRE)[1]; + let filePath = RegExpPrototypeExec(fsAutoCompleteRE, line)[1]; let fileList = gracefulReaddir(filePath, { withFileTypes: true }); if (!fileList) { @@ -1201,7 +1204,7 @@ function complete(line, callback) { let filter = ''; if (RegExpPrototypeTest(/^\s*\.(\w*)$/, line)) { ArrayPrototypePush(completionGroups, ObjectKeys(this.commands)); - completeOn = StringPrototypeMatch(line, /^\s*\.(\w*)$/)[1]; + completeOn = RegExpPrototypeExec(/^\s*\.(\w*)$/, line)[1]; if (completeOn.length) { filter = completeOn; } @@ -1214,7 +1217,7 @@ function complete(line, callback) { ArrayPrototypePush(indexes, 'package.json', 'index'); const versionedFileNamesRe = /-\d+\.\d+/; - const match = StringPrototypeMatch(line, requireRE); + const match = RegExpPrototypeExec(requireRE, line); completeOn = match[1]; const subdir = match[2] || ''; filter = completeOn; @@ -1466,8 +1469,8 @@ function _memory(cmd) { // Going down is { and ( e.g. function() { // going up is } and ) - let dw = StringPrototypeMatch(cmd, /[{(]/g); - let up = StringPrototypeMatch(cmd, /[})]/g); + let dw = RegExpPrototypeExec(/[{(]/g, cmd); + let up = RegExpPrototypeExec(/[})]/g, cmd); up = up ? up.length : 0; dw = dw ? dw.length : 0; let depth = dw - up; diff --git a/lib/tls.js b/lib/tls.js index 7ee9ae49f03a68..03303d9f70a0f2 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -31,12 +31,12 @@ const { ArrayPrototypeSome, ObjectDefineProperty, ObjectFreeze, + RegExpPrototypeSymbolReplace, RegExpPrototypeTest, StringFromCharCode, StringPrototypeCharCodeAt, StringPrototypeEndsWith, StringPrototypeIncludes, - StringPrototypeReplace, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -148,7 +148,7 @@ exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) { }; function unfqdn(host) { - return StringPrototypeReplace(host, /[.]$/, ''); + return RegExpPrototypeSymbolReplace(/[.]$/, host, ''); } // String#toLowerCase() is locale-sensitive so we use @@ -159,7 +159,7 @@ function toLowerCase(c) { function splitHost(host) { return StringPrototypeSplit( - StringPrototypeReplace(unfqdn(host), /[A-Z]/g, toLowerCase), + RegExpPrototypeSymbolReplace(/[A-Z]/g, unfqdn(host), toLowerCase), '.' ); }