From 3da1022de1fa32df03af4a1524f54323f7885a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 16 Oct 2024 18:53:10 +0200 Subject: [PATCH] chore: remove startsWith/endsWith primordials for char checks --- lib/_http_agent.js | 2 +- lib/internal/main/watch_mode.js | 2 +- lib/internal/modules/esm/fetch_module.js | 6 +++--- lib/internal/modules/esm/resolve.js | 5 +++-- lib/internal/modules/helpers.js | 2 +- lib/internal/process/per_thread.js | 3 +-- lib/internal/process/pre_execution.js | 4 +--- lib/internal/repl/utils.js | 4 +--- lib/internal/url.js | 2 +- lib/internal/util/inspect.js | 4 ++-- lib/repl.js | 2 +- test/fixtures/wpt/resources/webidl2/lib/webidl2.js | 6 +++--- 12 files changed, 19 insertions(+), 23 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index fb20b4780ba332..e8d31d83c26c51 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -341,7 +341,7 @@ function calculateServerName(options, req) { // abc:123 => abc // [::1] => ::1 // [::1]:123 => ::1 - if (hostHeader.startsWith('[')) { + if (hostHeader[0] === '[') { const index = hostHeader.indexOf(']'); if (index === -1) { // Leading '[', but no ']'. Need to do something... diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index 2fb689bd7d5abe..6e2528e64737c7 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -46,7 +46,7 @@ for (let i = 0; i < process.execArgv.length; i++) { if (StringPrototypeStartsWith(arg, '--watch')) { i++; const nextArg = process.execArgv[i]; - if (nextArg && StringPrototypeStartsWith(nextArg, '-')) { + if (nextArg && nextArg[0] === '-') { ArrayPrototypePush(argsWithoutWatchOptions, nextArg); } continue; diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 07904e685becf8..332c18e3a26f61 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -5,7 +5,6 @@ const { SafeMap, StringPrototypeEndsWith, StringPrototypeSlice, - StringPrototypeStartsWith, } = primordials; const { Buffer: { concat: BufferConcat }, @@ -248,8 +247,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255'); async function isLocalAddress(hostname) { try { if ( - StringPrototypeStartsWith(hostname, '[') && - StringPrototypeEndsWith(hostname, ']') + hostname && + hostname[0] === '[' && + hostname[hostname.length - 1] === ']' ) { hostname = StringPrototypeSlice(hostname, 1, -1); } diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 35925ef0817273..09c0265c8be2a2 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -373,8 +373,9 @@ function resolvePackageTargetString( } if (!StringPrototypeStartsWith(target, './')) { - if (internal && !StringPrototypeStartsWith(target, '../') && - !StringPrototypeStartsWith(target, '/')) { + if (internal && + target[0] !== '/' && + !StringPrototypeStartsWith(target, '../')) { // No need to convert target to string, since it's already presumed to be if (!URLCanParse(target)) { const exportTarget = pattern ? diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index 172f0fdc02a686..62c7dff1450a64 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -202,7 +202,7 @@ function addBuiltinLibsToObject(object, dummyModuleName) { ArrayPrototypeForEach(builtinModules, (name) => { // Neither add underscored modules, nor ones that contain slashes (e.g., // 'fs/promises') or ones that are already defined. - if (StringPrototypeStartsWith(name, '_') || + if (name[0] === '_' || StringPrototypeIncludes(name, '/') || ObjectPrototypeHasOwnProperty(object, name)) { return; diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index 9995dadfc648eb..10c8f8b0a67842 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -25,7 +25,6 @@ const { StringPrototypeEndsWith, StringPrototypeReplace, StringPrototypeSlice, - StringPrototypeStartsWith, Symbol, SymbolIterator, } = primordials; @@ -296,7 +295,7 @@ function buildAllowedFlags() { } function isAccepted(to) { - if (!StringPrototypeStartsWith(to, '-') || to === '--') return true; + if (!to || to[0] !== '-' || to === '--') return true; const recursiveExpansion = aliases.get(to); if (recursiveExpansion) { if (recursiveExpansion[0] === to) diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index dfb672b49b4114..08945a62d4277b 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -13,7 +13,6 @@ const { ObjectDefineProperty, ObjectFreeze, String, - StringPrototypeStartsWith, globalThis, } = primordials; @@ -206,8 +205,7 @@ function patchProcessObject(expandArgv1) { let mainEntry; // If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of // the entry point. - if (expandArgv1 && process.argv[1] && - !StringPrototypeStartsWith(process.argv[1], '-')) { + if (expandArgv1 && process.argv[1] && process.argv[1][0] !== '-') { // Expand process.argv[1] into a full path. const path = require('path'); try { diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 27e1011ec9daf6..9f2e3c33aadcd9 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -15,7 +15,6 @@ const { StringPrototypeLastIndexOf, StringPrototypeReplaceAll, StringPrototypeSlice, - StringPrototypeStartsWith, StringPrototypeToLowerCase, StringPrototypeTrim, Symbol, @@ -298,8 +297,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { function getInputPreview(input, callback) { // For similar reasons as `defaultEval`, wrap expressions starting with a // curly brace with parenthesis. - if (StringPrototypeStartsWith(input, '{') && - !StringPrototypeEndsWith(input, ';') && !wrapped) { + if (!wrapped && input[0] === '{' && input[input.length - 1] !== ';') { input = `(${input})`; wrapped = true; } diff --git a/lib/internal/url.js b/lib/internal/url.js index b62766b02987d1..0ac80b6f1307cb 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1416,7 +1416,7 @@ function urlToHttpOptions(url) { __proto__: null, ...url, // In case the url object was extended by the user. protocol: url.protocol, - hostname: hostname && StringPrototypeStartsWith(hostname, '[') ? + hostname: hostname && hostname[0] === '[' ? StringPrototypeSlice(hostname, 1, -1) : hostname, hash: url.hash, diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index ade91b3c5a6711..bbf20fe25fbc1b 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1190,7 +1190,7 @@ function getClassBase(value, constructor, tag) { function getFunctionBase(value, constructor, tag) { const stringified = FunctionPrototypeToString(value); - if (StringPrototypeStartsWith(stringified, 'class') && StringPrototypeEndsWith(stringified, '}')) { + if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') { const slice = StringPrototypeSlice(stringified, 5, -1); const bracketIndex = StringPrototypeIndexOf(slice, '{'); if (bracketIndex !== -1 && @@ -1573,7 +1573,7 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) { function addNumericSeparator(integerString) { let result = ''; let i = integerString.length; - const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0; + const start = integerString[0] === '-' ? 1 : 0; for (; i >= start + 4; i -= 3) { result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`; } diff --git a/lib/repl.js b/lib/repl.js index bbdf3e916daeb6..37b34af2917643 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -130,7 +130,7 @@ const { shouldColorize } = require('internal/util/colors'); const CJSModule = require('internal/modules/cjs/loader').Module; let _builtinLibs = ArrayPrototypeFilter( CJSModule.builtinModules, - (e) => !StringPrototypeStartsWith(e, '_'), + (e) => e[0] !== '_', ); const nodeSchemeBuiltinLibs = ArrayPrototypeMap( _builtinLibs, (lib) => `node:${lib}`); diff --git a/test/fixtures/wpt/resources/webidl2/lib/webidl2.js b/test/fixtures/wpt/resources/webidl2/lib/webidl2.js index 7161def899cf24..eba0e79096b6c2 100644 --- a/test/fixtures/wpt/resources/webidl2/lib/webidl2.js +++ b/test/fixtures/wpt/resources/webidl2/lib/webidl2.js @@ -712,7 +712,7 @@ __webpack_require__.r(__webpack_exports__); * @param {string} identifier */ function unescape(identifier) { - return identifier.startsWith("_") ? identifier.slice(1) : identifier; + return identifier && identifier[0] === "_" ? identifier.slice(1) : identifier; } /** @@ -775,7 +775,7 @@ function const_data({ type, value }) { return { type: "boolean", value: value === "true" }; case "Infinity": case "-Infinity": - return { type: "Infinity", negative: value.startsWith("-") }; + return { type: "Infinity", negative: value[0] === "-" }; case "[": return { type: "sequence", value: [] }; case "{": @@ -3590,7 +3590,7 @@ class Writer { */ reference(raw, { unescaped, context }) { if (!unescaped) { - unescaped = raw.startsWith("_") ? raw.slice(1) : raw; + unescaped = raw && raw[0] === "_" ? raw.slice(1) : raw; } return this.ts.reference(raw, unescaped, context); }