From be9d6f21761de0323eecb0ebbef92bb80d5ab1fc Mon Sep 17 00:00:00 2001 From: Jungku Lee Date: Mon, 13 May 2024 05:35:31 +0900 Subject: [PATCH] url,tools,benchmark: replace deprecated `substr()` PR-URL: https://github.com/nodejs/node/pull/51546 Refs: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/substr Reviewed-By: Jithil P Ponnan Reviewed-By: Marco Ippolito Reviewed-By: Luigi Pinca --- .eslintrc.js | 4 ++++ benchmark/http/bench-parser.js | 2 +- lib/url.js | 2 +- tools/doc/html.mjs | 4 ++-- tools/doc/json.mjs | 4 ++-- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index c2c8e1df7e58d3..f86f42e8f2e89f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -210,6 +210,10 @@ module.exports = { selector: 'ThrowStatement > CallExpression[callee.name=/Error$/]', message: 'Use `new` keyword when throwing an `Error`.', }, + { + selector: "CallExpression[callee.property.name='substr']", + message: 'Use String.prototype.slice() or String.prototype.substring() instead of String.prototype.substr()', + }, { selector: "CallExpression[callee.name='isNaN']", message: 'Use Number.isNaN() instead of the global isNaN() function.', diff --git a/benchmark/http/bench-parser.js b/benchmark/http/bench-parser.js index 6b8bbd8808c720..f2c120cd97f133 100644 --- a/benchmark/http/bench-parser.js +++ b/benchmark/http/bench-parser.js @@ -46,7 +46,7 @@ function main({ len, n }) { let header = `GET /hello HTTP/1.1${CRLF}Content-Type: text/plain${CRLF}`; for (let i = 0; i < len; i++) { - header += `X-Filler${i}: ${Math.random().toString(36).substr(2)}${CRLF}`; + header += `X-Filler${i}: ${Math.random().toString(36).substring(2)}${CRLF}`; } header += CRLF; diff --git a/lib/url.js b/lib/url.js index 6919bc0f905e29..312750e86d600b 100644 --- a/lib/url.js +++ b/lib/url.js @@ -958,7 +958,7 @@ Url.prototype.resolveObject = function resolveObject(relative) { srcPath.unshift(''); } - if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { + if (hasTrailingSlash && (srcPath.join('/').slice(-1) !== '/')) { srcPath.push(''); } diff --git a/tools/doc/html.mjs b/tools/doc/html.mjs index ff1c6854b58e22..e0e7706821908f 100644 --- a/tools/doc/html.mjs +++ b/tools/doc/html.mjs @@ -394,8 +394,8 @@ function versionSort(a, b) { b = minVersion(b).trim(); let i = 0; // Common prefix length. while (i < a.length && i < b.length && a[i] === b[i]) i++; - a = a.substr(i); - b = b.substr(i); + a = a.substring(i); + b = b.substring(i); return +b.match(numberRe)[0] - +a.match(numberRe)[0]; } diff --git a/tools/doc/json.mjs b/tools/doc/json.mjs index b57987ef9a37ad..509173ca3864a2 100644 --- a/tools/doc/json.mjs +++ b/tools/doc/json.mjs @@ -320,8 +320,8 @@ function parseSignature(text, sig) { const eq = sigParam.indexOf('='); if (eq !== -1) { - defaultValue = sigParam.substr(eq + 1); - sigParam = sigParam.substr(0, eq); + defaultValue = sigParam.substring(eq + 1); + sigParam = sigParam.substring(0, eq); } // At this point, the name should match. If it doesn't find one that does.