From 63b20b26338284e6d45f2051828a7688d003139b Mon Sep 17 00:00:00 2001 From: Kral <118106297+dashitongzhi@users.noreply.github.com> Date: Wed, 20 May 2026 09:28:01 +0800 Subject: [PATCH] refactor: use Array.includes() instead of indexOf() > -1 Replace legacy indexOf() > -1 pattern with more readable Array.includes() method across multiple files. Also replace map() with forEach() when the return value is not used (side-effect only). Changes: - src/utils/handle-errors.js: map -> forEach for console.log side effects - src/client-functions/selectors/create-snapshot-methods.js: hasClass method - src/client-functions/selectors/add-api.js: filterNodes and expandSelectorResults - src/utils/string.js: splitQuotedText and getConcatenatedValuesString - src/browser/provider/built-in/locally-installed.js: isValidBrowserName - src/client/ui/select-element.js: onWindowClick disabled class check - src/compiler/test-file/formats/typescript/get-test-list.js: replaceComments and getStringValue - src/compiler/test-file/formats/es-next/get-test-list.js: getStringValue - src/compiler/test-file/test-file-parser-base.js: checkExpDefineTargetName This improves code readability and follows modern JavaScript best practices. --- src/browser/provider/built-in/locally-installed.js | 2 +- src/client-functions/selectors/add-api.js | 4 ++-- src/client-functions/selectors/create-snapshot-methods.js | 2 +- src/client/ui/select-element.js | 2 +- src/compiler/test-file/formats/es-next/get-test-list.js | 2 +- src/compiler/test-file/formats/typescript/get-test-list.js | 4 ++-- src/compiler/test-file/test-file-parser-base.js | 2 +- src/utils/handle-errors.js | 2 +- src/utils/string.js | 4 ++-- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/browser/provider/built-in/locally-installed.js b/src/browser/provider/built-in/locally-installed.js index 3da85c4e48e..aa57a4e98f9 100644 --- a/src/browser/provider/built-in/locally-installed.js +++ b/src/browser/provider/built-in/locally-installed.js @@ -31,6 +31,6 @@ export default { browserName = browserName.toLowerCase().split(' ')[0]; - return browserNames.indexOf(browserName) > -1; + return browserNames.includes(browserName); }, }; diff --git a/src/client-functions/selectors/add-api.js b/src/client-functions/selectors/add-api.js index 994e97ebb96..10ecd1cccd2 100644 --- a/src/client-functions/selectors/add-api.js +++ b/src/client-functions/selectors/add-api.js @@ -35,7 +35,7 @@ const filterNodes = new ClientFunctionBuilder((nodes, filter, querySelectorRoot, for (let i = 0; i < matching.length; i++) matchingArr.push(matching[i]); - filter = node => matchingArr.indexOf(node) > -1; + filter = node => matchingArr.includes(node); } if (typeof filter === 'function') { @@ -61,7 +61,7 @@ const expandSelectorResults = new ClientFunctionBuilder((selector, populateDeriv if (derivativeNodes) { for (let j = 0; j < derivativeNodes.length; j++) { - if (result.indexOf(derivativeNodes[j]) < 0) + if (!result.includes(derivativeNodes[j])) result.push(derivativeNodes[j]); } } diff --git a/src/client-functions/selectors/create-snapshot-methods.js b/src/client-functions/selectors/create-snapshot-methods.js index 8a70ca16831..992078c97bc 100644 --- a/src/client-functions/selectors/create-snapshot-methods.js +++ b/src/client-functions/selectors/create-snapshot-methods.js @@ -2,7 +2,7 @@ export default function createSnapshotMethods (snapshot) { const isElementSnapshot = !!snapshot.tagName; if (isElementSnapshot) { - snapshot.hasClass = name => snapshot.classNames.indexOf(name) > -1; + snapshot.hasClass = name => snapshot.classNames.includes(name); snapshot.getStyleProperty = prop => snapshot.style[prop]; snapshot.getAttribute = attrName => snapshot.attributes[attrName]; snapshot.hasAttribute = attrName => snapshot.attributes.hasOwnProperty(attrName); diff --git a/src/client/ui/select-element.js b/src/client/ui/select-element.js index 8a65fa8d6c6..aabb4d4b417 100644 --- a/src/client/ui/select-element.js +++ b/src/client/ui/select-element.js @@ -43,7 +43,7 @@ function onWindowClick (e, dispatched, preventDefault) { preventDefault(); - const isDisabled = target.className.indexOf(DISABLED_CLASS) > -1; + const isDisabled = target.className.includes(DISABLED_CLASS); if (isDisabled && browserUtils.isWebKit) return; diff --git a/src/compiler/test-file/formats/es-next/get-test-list.js b/src/compiler/test-file/formats/es-next/get-test-list.js index acf5bcf9442..0f3daca2871 100644 --- a/src/compiler/test-file/formats/es-next/get-test-list.js +++ b/src/compiler/test-file/formats/es-next/get-test-list.js @@ -45,7 +45,7 @@ export class EsNextTestFileParser extends TestFileParserBase { getStringValue (token) { const stringTypes = [this.tokenType.StringLiteral, this.tokenType.TemplateLiteral, this.tokenType.Identifier]; - if (stringTypes.indexOf(token.type) > -1) + if (stringTypes.includes(token.type)) return this.formatFnArg(token); return null; diff --git a/src/compiler/test-file/formats/typescript/get-test-list.js b/src/compiler/test-file/formats/typescript/get-test-list.js index 4dc9e02878c..d3072546276 100644 --- a/src/compiler/test-file/formats/typescript/get-test-list.js +++ b/src/compiler/test-file/formats/typescript/get-test-list.js @@ -5,7 +5,7 @@ import TypescriptConfiguration from '../../../../configuration/typescript-config function replaceComments (code) { return code.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, match => { - const lastSymbol = match.indexOf('\n') > -1 ? '\n' : ' '; + const lastSymbol = match.includes('\n') ? '\n' : ' '; return repeat(' ', match.length + lastSymbol); }); @@ -85,7 +85,7 @@ class TypeScriptTestFileParser extends TestFileParserBase { getStringValue (token) { const stringTypes = [this.tokenType.StringLiteral, this.tokenType.TemplateExpression]; - if (stringTypes.indexOf(token.kind) > -1 || token.text && token.kind !== this.tokenType.NumericLiteral) + if (stringTypes.includes(token.kind) || token.text && token.kind !== this.tokenType.NumericLiteral) return this.formatFnArg(token); return null; diff --git a/src/compiler/test-file/test-file-parser-base.js b/src/compiler/test-file/test-file-parser-base.js index d73affb50fb..679185bcfff 100644 --- a/src/compiler/test-file/test-file-parser-base.js +++ b/src/compiler/test-file/test-file-parser-base.js @@ -189,7 +189,7 @@ export class TestFileParserBase { //NOTE: fixture.skip('fixtureName'), test.only('testName') etc. const isMemberCall = type === this.tokenType.PropertyAccessExpression && - METHODS_SPECIFYING_NAME.indexOf(apiFn) > -1; + METHODS_SPECIFYING_NAME.includes(apiFn); //NOTE: fixture.before().after()('fixtureName'), test.before()`testName`.after() etc. const isTailCall = type === this.tokenType.CallExpression; diff --git a/src/utils/handle-errors.js b/src/utils/handle-errors.js index b642b38f4c5..6ea8f7d7f4f 100644 --- a/src/utils/handle-errors.js +++ b/src/utils/handle-errors.js @@ -6,7 +6,7 @@ let handlingTestErrors = false; function printErrorMessagesAndTerminate (...messages) { // eslint-disable-next-line no-console - messages.map(msg => console.log(msg)); + messages.forEach(msg => console.log(msg)); // eslint-disable-next-line no-process-exit setTimeout(() => process.exit(1), 0); diff --git a/src/utils/string.js b/src/utils/string.js index 01a175dc785..dd96f75ab2b 100644 --- a/src/utils/string.js +++ b/src/utils/string.js @@ -61,7 +61,7 @@ export function splitQuotedText (str, splitChar, quotes = '"\'') { currentPart = ''; } } - else if (quotes.indexOf(currentChar) > -1) { + else if (quotes.includes(currentChar)) { if (quoteChar === currentChar) quoteChar = null; else if (!quoteChar) @@ -90,7 +90,7 @@ function getDisplayedItemText (item, quote) { export function getConcatenatedValuesString (array, separator = DEFAULT_CONCATENATED_VALUES.SEPARATOR, quoteChar = DEFAULT_CONCATENATED_VALUES.QUOTE_CHAR) { const clonedArray = [...array]; - if (separator.indexOf('\n') > -1) + if (separator.includes('\n')) return clonedArray.map(item => getDisplayedItemText(item, quoteChar)).join(separator); else if (clonedArray.length === 1)