From 8cc8daef4b25c3898bee3e70dfcc2e040928e0e3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 14 Sep 2023 15:13:41 +0200 Subject: [PATCH] chore: prefer `.includes` over `indexOf` (#14524) --- .eslintrc.cjs | 1 + e2e/__tests__/customInlineSnapshotMatchers.test.ts | 4 ++-- e2e/__tests__/customMatcherStackTrace.test.ts | 2 +- e2e/__tests__/expectAsyncMatcher.test.ts | 2 +- e2e/__tests__/failures.test.ts | 2 +- e2e/filter/my-broken-setup-filter.js | 2 +- e2e/filter/my-filter.js | 2 +- e2e/filter/my-secondary-filter.js | 2 +- e2e/filter/my-setup-filter.js | 2 +- e2e/resolve/hasteImpl.js | 2 +- packages/jest-config/src/__tests__/normalize.test.ts | 2 +- .../jest-core/src/lib/handleDeprecationWarnings.ts | 2 +- packages/jest-haste-map/src/crawlers/node.ts | 2 +- packages/jest-haste-map/src/crawlers/watchman.ts | 2 +- .../jest-haste-map/src/lib/getPlatformExtension.ts | 2 +- packages/jest-jasmine2/src/jasmine/Spec.ts | 2 +- packages/jest-jasmine2/src/treeProcessor.ts | 2 +- packages/jest-reporters/src/CoverageReporter.ts | 4 ++-- packages/jest-runtime/src/index.ts | 2 +- packages/pretty-format/src/index.ts | 2 +- packages/pretty-format/src/plugins/DOMCollection.ts | 4 ++-- packages/pretty-format/src/plugins/lib/markup.ts | 2 +- scripts/lintTs.mjs | 2 +- website/src/components/v1/Container.js | 10 +++++----- 24 files changed, 31 insertions(+), 30 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 7659014655..6c9a1586ec 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -591,6 +591,7 @@ module.exports = { 'wrap-regex': 'off', yoda: 'off', + 'unicorn/prefer-includes': 'error', 'unicorn/template-indent': 'error', }, settings: { diff --git a/e2e/__tests__/customInlineSnapshotMatchers.test.ts b/e2e/__tests__/customInlineSnapshotMatchers.test.ts index 49b5783e69..07c241c5e9 100644 --- a/e2e/__tests__/customInlineSnapshotMatchers.test.ts +++ b/e2e/__tests__/customInlineSnapshotMatchers.test.ts @@ -19,7 +19,7 @@ test('works with custom inline snapshot matchers', () => { rest = rest .split('\n') - .filter(line => line.indexOf('at Error (native)') < 0) + .filter(line => !line.includes('at Error (native)')) .join('\n'); expect(rest).toMatchSnapshot(); @@ -36,7 +36,7 @@ test('can bail with a custom inline snapshot matcher', () => { rest = rest .split('\n') - .filter(line => line.indexOf('at Error (native)') < 0) + .filter(line => !line.includes('at Error (native)')) .join('\n'); expect(rest).toMatchSnapshot(); diff --git a/e2e/__tests__/customMatcherStackTrace.test.ts b/e2e/__tests__/customMatcherStackTrace.test.ts index 2d0a3e816d..0f6e0cf849 100644 --- a/e2e/__tests__/customMatcherStackTrace.test.ts +++ b/e2e/__tests__/customMatcherStackTrace.test.ts @@ -15,7 +15,7 @@ test('works with custom matchers', () => { rest = rest .split('\n') - .filter(line => line.indexOf('at Error (native)') < 0) + .filter(line => !line.includes('at Error (native)')) .join('\n'); expect(rest).toMatchSnapshot(); diff --git a/e2e/__tests__/expectAsyncMatcher.test.ts b/e2e/__tests__/expectAsyncMatcher.test.ts index ca9a9c5843..695d1eade3 100644 --- a/e2e/__tests__/expectAsyncMatcher.test.ts +++ b/e2e/__tests__/expectAsyncMatcher.test.ts @@ -27,7 +27,7 @@ test('shows the correct errors in stderr when failing tests', () => { const rest = extractSummary(result.stderr) .rest.split('\n') - .filter(line => line.indexOf('packages/expect/build/index.js') === -1) + .filter(line => !line.includes('packages/expect/build/index.js')) .join('\n'); expect(rest).toMatchSnapshot(); diff --git a/e2e/__tests__/failures.test.ts b/e2e/__tests__/failures.test.ts index 031035e624..bd717a9eac 100644 --- a/e2e/__tests__/failures.test.ts +++ b/e2e/__tests__/failures.test.ts @@ -58,7 +58,7 @@ test('works with async failures', () => { const rest = cleanStderr(stderr) .split('\n') - .filter(line => line.indexOf('packages/expect/build/index.js') === -1) + .filter(line => !line.includes('packages/expect/build/index.js')) .join('\n'); // Remove replacements when jasmine is gone diff --git a/e2e/filter/my-broken-setup-filter.js b/e2e/filter/my-broken-setup-filter.js index 75459689dc..a360c67d55 100644 --- a/e2e/filter/my-broken-setup-filter.js +++ b/e2e/filter/my-broken-setup-filter.js @@ -9,7 +9,7 @@ module.exports = function (tests) { return { - filtered: tests.filter(t => t.indexOf('foo') !== -1).map(test => ({test})), + filtered: tests.filter(t => t.includes('foo')).map(test => ({test})), }; }; diff --git a/e2e/filter/my-filter.js b/e2e/filter/my-filter.js index 83bcbd7bd2..10c71b8363 100644 --- a/e2e/filter/my-filter.js +++ b/e2e/filter/my-filter.js @@ -12,7 +12,7 @@ module.exports = function (tests) { setTimeout(() => { resolve({ filtered: tests - .filter(t => t.indexOf('foo') !== -1) + .filter(t => t.includes('foo')) .map(test => ({message: 'some message', test})), }); }, 100); diff --git a/e2e/filter/my-secondary-filter.js b/e2e/filter/my-secondary-filter.js index 39eba9edc1..b6551712ee 100644 --- a/e2e/filter/my-secondary-filter.js +++ b/e2e/filter/my-secondary-filter.js @@ -9,6 +9,6 @@ module.exports = function (tests) { return { - filtered: tests.filter(t => t.indexOf('foo') !== -1).map(test => ({test})), + filtered: tests.filter(t => t.includes('foo')).map(test => ({test})), }; }; diff --git a/e2e/filter/my-setup-filter.js b/e2e/filter/my-setup-filter.js index 70b8cb0efd..751568123d 100644 --- a/e2e/filter/my-setup-filter.js +++ b/e2e/filter/my-setup-filter.js @@ -14,7 +14,7 @@ const setupData = { module.exports = function (tests) { return { filtered: tests - .filter(t => t.indexOf(setupData.filterText) !== -1) + .filter(t => t.includes(setupData.filterText)) .map(test => ({test})), }; }; diff --git a/e2e/resolve/hasteImpl.js b/e2e/resolve/hasteImpl.js index 68b0243152..c02806ad4e 100644 --- a/e2e/resolve/hasteImpl.js +++ b/e2e/resolve/hasteImpl.js @@ -12,7 +12,7 @@ const path = require('path'); module.exports = { getHasteName(filePath) { const name = path.parse(filePath).name; - const isMock = filePath.indexOf('__mocks__') !== -1; + const isMock = filePath.includes('__mocks__'); // Mocks are automatically parsed by Jest already. return name.startsWith('Test') && !isMock ? name : null; diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index afbfe417a2..da775c1822 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -828,7 +828,7 @@ describe('babel-jest', () => { Resolver = (require('jest-resolve') as typeof import('jest-resolve')) .default; Resolver.findNodeModule = jest.fn((name: string) => - name.indexOf('babel-jest') === -1 + !name.includes('babel-jest') ? `${path.sep}node_modules${path.sep}${name}` : name, ); diff --git a/packages/jest-core/src/lib/handleDeprecationWarnings.ts b/packages/jest-core/src/lib/handleDeprecationWarnings.ts index 9cd0fd62ef..b196edeb05 100644 --- a/packages/jest-core/src/lib/handleDeprecationWarnings.ts +++ b/packages/jest-core/src/lib/handleDeprecationWarnings.ts @@ -30,7 +30,7 @@ export default function handleDeprecationWarnings( if (key === KEYS.ENTER) { resolve(); } else if ( - [KEYS.ESCAPE, KEYS.CONTROL_C, KEYS.CONTROL_D].indexOf(key) !== -1 + [KEYS.ESCAPE, KEYS.CONTROL_C, KEYS.CONTROL_D].includes(key) ) { reject(); } diff --git a/packages/jest-haste-map/src/crawlers/node.ts b/packages/jest-haste-map/src/crawlers/node.ts index 8555eeb7b4..0d9b4f014c 100644 --- a/packages/jest-haste-map/src/crawlers/node.ts +++ b/packages/jest-haste-map/src/crawlers/node.ts @@ -105,7 +105,7 @@ function find( search(file); } else { const ext = path.extname(file).substr(1); - if (extensions.indexOf(ext) !== -1) { + if (extensions.includes(ext)) { result.push([file, stat.mtime.getTime(), stat.size]); } } diff --git a/packages/jest-haste-map/src/crawlers/watchman.ts b/packages/jest-haste-map/src/crawlers/watchman.ts index ef5b63ca00..4bd13fa445 100644 --- a/packages/jest-haste-map/src/crawlers/watchman.ts +++ b/packages/jest-haste-map/src/crawlers/watchman.ts @@ -135,7 +135,7 @@ export async function watchmanCrawl(options: CrawlerOptions): Promise<{ 'list-capabilities', ); - if (capabilities.indexOf('field-content.sha1hex') !== -1) { + if (capabilities.includes('field-content.sha1hex')) { fields.push('content.sha1hex'); } } diff --git a/packages/jest-haste-map/src/lib/getPlatformExtension.ts b/packages/jest-haste-map/src/lib/getPlatformExtension.ts index 079470b62b..2048e16242 100644 --- a/packages/jest-haste-map/src/lib/getPlatformExtension.ts +++ b/packages/jest-haste-map/src/lib/getPlatformExtension.ts @@ -20,7 +20,7 @@ export default function getPlatformExtension( const platform = file.substring(secondToLast + 1, last); // If an overriding platform array is passed, check that first - if (platforms && platforms.indexOf(platform) !== -1) { + if (platforms && platforms.includes(platform)) { return platform; } return SUPPORTED_PLATFORM_EXTS.has(platform) ? platform : null; diff --git a/packages/jest-jasmine2/src/jasmine/Spec.ts b/packages/jest-jasmine2/src/jasmine/Spec.ts index c04dc85203..6ca9ceee8a 100644 --- a/packages/jest-jasmine2/src/jasmine/Spec.ts +++ b/packages/jest-jasmine2/src/jasmine/Spec.ts @@ -103,7 +103,7 @@ export default class Spec { return !!( e && e.toString && - e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1 + e.toString().includes(Spec.pendingSpecExceptionMessage) ); } diff --git a/packages/jest-jasmine2/src/treeProcessor.ts b/packages/jest-jasmine2/src/treeProcessor.ts index 6a8924471c..960336c506 100644 --- a/packages/jest-jasmine2/src/treeProcessor.ts +++ b/packages/jest-jasmine2/src/treeProcessor.ts @@ -33,7 +33,7 @@ export default function treeProcessor(options: Options): void { options; function isEnabled(node: TreeNode, parentEnabled: boolean) { - return parentEnabled || runnableIds.indexOf(node.id) !== -1; + return parentEnabled || runnableIds.includes(node.id); } function getNodeHandler(node: TreeNode, parentEnabled: boolean) { diff --git a/packages/jest-reporters/src/CoverageReporter.ts b/packages/jest-reporters/src/CoverageReporter.ts index 953c4ed4be..4f9aba2683 100644 --- a/packages/jest-reporters/src/CoverageReporter.ts +++ b/packages/jest-reporters/src/CoverageReporter.ts @@ -303,7 +303,7 @@ export default class CoverageReporter extends BaseReporter { .map(filePath => path.resolve(filePath)); } - if (filesByGlob[absoluteThresholdGroup].indexOf(file) > -1) { + if (filesByGlob[absoluteThresholdGroup].includes(file)) { groupTypeByThresholdGroup[thresholdGroup] = THRESHOLD_GROUP_TYPES.GLOB; return agg.concat([[file, thresholdGroup]]); @@ -317,7 +317,7 @@ export default class CoverageReporter extends BaseReporter { } // Neither a glob or a path? Toss it in global if there's a global threshold: - if (thresholdGroups.indexOf(THRESHOLD_GROUP_TYPES.GLOBAL) > -1) { + if (thresholdGroups.includes(THRESHOLD_GROUP_TYPES.GLOBAL)) { groupTypeByThresholdGroup[THRESHOLD_GROUP_TYPES.GLOBAL] = THRESHOLD_GROUP_TYPES.GLOBAL; return files.concat([[file, THRESHOLD_GROUP_TYPES.GLOBAL]]); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 9b56472151..80ed811492 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2408,7 +2408,7 @@ export default class Runtime { const originalStack = new ReferenceError(`${errorMessage}${testPath}`) .stack!.split('\n') // Remove this file from the stack (jest-message-utils will keep one line) - .filter(line => line.indexOf(__filename) === -1) + .filter(line => !line.includes(__filename)) .join('\n'); const {message, stack} = separateMessageFromStack(originalStack); diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index fe59d3f33e..d0522eec5f 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -207,7 +207,7 @@ function printComplexValue( refs: Refs, hasCalledToJSON?: boolean, ): string { - if (refs.indexOf(val) !== -1) { + if (refs.includes(val)) { return '[Circular]'; } refs = refs.slice(); diff --git a/packages/pretty-format/src/plugins/DOMCollection.ts b/packages/pretty-format/src/plugins/DOMCollection.ts index 40b27e2005..29beb8b61f 100644 --- a/packages/pretty-format/src/plugins/DOMCollection.ts +++ b/packages/pretty-format/src/plugins/DOMCollection.ts @@ -14,7 +14,7 @@ const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap']; const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/; const testName = (name: any) => - OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name); + OBJECT_NAMES.includes(name) || ARRAY_REGEXP.test(name); export const test: NewPlugin['test'] = (val: object) => val && @@ -40,7 +40,7 @@ export const serialize: NewPlugin['serialize'] = ( return ( (config.min ? '' : name + SPACE) + - (OBJECT_NAMES.indexOf(name) !== -1 + (OBJECT_NAMES.includes(name) ? `{${printObjectProperties( isNamedNodeMap(collection) ? Array.from(collection).reduce>( diff --git a/packages/pretty-format/src/plugins/lib/markup.ts b/packages/pretty-format/src/plugins/lib/markup.ts index 923c6f8396..ce30921d62 100644 --- a/packages/pretty-format/src/plugins/lib/markup.ts +++ b/packages/pretty-format/src/plugins/lib/markup.ts @@ -26,7 +26,7 @@ export const printProps = ( let printed = printer(value, config, indentationNext, depth, refs); if (typeof value !== 'string') { - if (printed.indexOf('\n') !== -1) { + if (printed.includes('\n')) { printed = config.spacingOuter + indentationNext + diff --git a/scripts/lintTs.mjs b/scripts/lintTs.mjs index f8ca8ed954..a7e1a3d47a 100644 --- a/scripts/lintTs.mjs +++ b/scripts/lintTs.mjs @@ -23,7 +23,7 @@ const cpus = Math.max( const mutex = pLimit(cpus); -const fix = process.argv.slice(2).some(arg => arg === '--fix'); +const fix = process.argv.slice(2).includes('--fix'); const monorepoRoot = path.resolve(url.fileURLToPath(import.meta.url), '../..'); diff --git a/website/src/components/v1/Container.js b/website/src/components/v1/Container.js index c96a9caa72..660f7b01a9 100644 --- a/website/src/components/v1/Container.js +++ b/website/src/components/v1/Container.js @@ -13,11 +13,11 @@ export default function Container(props) { darkBackground: props.background === 'dark', highlightBackground: props.background === 'highlight', lightBackground: props.background === 'light', - paddingAll: props.padding.indexOf('all') >= 0, - paddingBottom: props.padding.indexOf('bottom') >= 0, - paddingLeft: props.padding.indexOf('left') >= 0, - paddingRight: props.padding.indexOf('right') >= 0, - paddingTop: props.padding.indexOf('top') >= 0, + paddingAll: props.padding.includes('all'), + paddingBottom: props.padding.includes('bottom'), + paddingLeft: props.padding.includes('left'), + paddingRight: props.padding.includes('right'), + paddingTop: props.padding.includes('top'), }); let wrappedChildren;