Skip to content

Commit

Permalink
lib: remove startsWith/endsWith primordials for char checks
Browse files Browse the repository at this point in the history
PR-URL: #55407
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
  • Loading branch information
gurgunday authored and aduh95 committed Oct 23, 2024
1 parent 4e5c90b commit 714f272
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const {
ObjectPrototypeHasOwnProperty,
PromisePrototypeThen,
SafeMap,
StringPrototypeEndsWith,
StringPrototypeSlice,
StringPrototypeStartsWith,
} = primordials;
const {
Buffer: { concat: BufferConcat },
Expand Down Expand Up @@ -248,8 +246,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255');
async function isLocalAddress(hostname) {
try {
if (
StringPrototypeStartsWith(hostname, '[') &&
StringPrototypeEndsWith(hostname, ']')
hostname.length &&
hostname[0] === '[' &&
hostname[hostname.length - 1] === ']'
) {
hostname = StringPrototypeSlice(hostname, 1, -1);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const {
StringPrototypeEndsWith,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeStartsWith,
Symbol,
SymbolIterator,
} = primordials;
Expand Down Expand Up @@ -296,7 +295,7 @@ function buildAllowedFlags() {
}

function isAccepted(to) {
if (!StringPrototypeStartsWith(to, '-') || to === '--') return true;
if (!to.length || to[0] !== '-' || to === '--') return true;
const recursiveExpansion = aliases.get(to);
if (recursiveExpansion) {
if (recursiveExpansion[0] === to)
Expand Down
4 changes: 1 addition & 3 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
ObjectDefineProperty,
ObjectFreeze,
String,
StringPrototypeStartsWith,
globalThis,
} = primordials;

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ const {
RegExpPrototypeExec,
SafeSet,
SafeStringIterator,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeStartsWith,
StringPrototypeToLowerCase,
StringPrototypeTrim,
Symbol,
Expand Down Expand Up @@ -298,8 +296,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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -1573,7 +1573,8 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
function addNumericSeparator(integerString) {
let result = '';
let i = integerString.length;
const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0;
assert(i !== 0);
const start = integerString[0] === '-' ? 1 : 0;
for (; i >= start + 4; i -= 3) {
result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down

0 comments on commit 714f272

Please sign in to comment.