Skip to content

Commit 8cc8dae

Browse files
authored
chore: prefer .includes over indexOf (#14524)
1 parent 9b077f7 commit 8cc8dae

File tree

24 files changed

+31
-30
lines changed

24 files changed

+31
-30
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ module.exports = {
591591
'wrap-regex': 'off',
592592
yoda: 'off',
593593

594+
'unicorn/prefer-includes': 'error',
594595
'unicorn/template-indent': 'error',
595596
},
596597
settings: {

e2e/__tests__/customInlineSnapshotMatchers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('works with custom inline snapshot matchers', () => {
1919

2020
rest = rest
2121
.split('\n')
22-
.filter(line => line.indexOf('at Error (native)') < 0)
22+
.filter(line => !line.includes('at Error (native)'))
2323
.join('\n');
2424

2525
expect(rest).toMatchSnapshot();
@@ -36,7 +36,7 @@ test('can bail with a custom inline snapshot matcher', () => {
3636

3737
rest = rest
3838
.split('\n')
39-
.filter(line => line.indexOf('at Error (native)') < 0)
39+
.filter(line => !line.includes('at Error (native)'))
4040
.join('\n');
4141

4242
expect(rest).toMatchSnapshot();

e2e/__tests__/customMatcherStackTrace.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('works with custom matchers', () => {
1515

1616
rest = rest
1717
.split('\n')
18-
.filter(line => line.indexOf('at Error (native)') < 0)
18+
.filter(line => !line.includes('at Error (native)'))
1919
.join('\n');
2020

2121
expect(rest).toMatchSnapshot();

e2e/__tests__/expectAsyncMatcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('shows the correct errors in stderr when failing tests', () => {
2727

2828
const rest = extractSummary(result.stderr)
2929
.rest.split('\n')
30-
.filter(line => line.indexOf('packages/expect/build/index.js') === -1)
30+
.filter(line => !line.includes('packages/expect/build/index.js'))
3131
.join('\n');
3232

3333
expect(rest).toMatchSnapshot();

e2e/__tests__/failures.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test('works with async failures', () => {
5858

5959
const rest = cleanStderr(stderr)
6060
.split('\n')
61-
.filter(line => line.indexOf('packages/expect/build/index.js') === -1)
61+
.filter(line => !line.includes('packages/expect/build/index.js'))
6262
.join('\n');
6363

6464
// Remove replacements when jasmine is gone

e2e/filter/my-broken-setup-filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
module.exports = function (tests) {
1111
return {
12-
filtered: tests.filter(t => t.indexOf('foo') !== -1).map(test => ({test})),
12+
filtered: tests.filter(t => t.includes('foo')).map(test => ({test})),
1313
};
1414
};
1515

e2e/filter/my-filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function (tests) {
1212
setTimeout(() => {
1313
resolve({
1414
filtered: tests
15-
.filter(t => t.indexOf('foo') !== -1)
15+
.filter(t => t.includes('foo'))
1616
.map(test => ({message: 'some message', test})),
1717
});
1818
}, 100);

e2e/filter/my-secondary-filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
module.exports = function (tests) {
1111
return {
12-
filtered: tests.filter(t => t.indexOf('foo') !== -1).map(test => ({test})),
12+
filtered: tests.filter(t => t.includes('foo')).map(test => ({test})),
1313
};
1414
};

e2e/filter/my-setup-filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const setupData = {
1414
module.exports = function (tests) {
1515
return {
1616
filtered: tests
17-
.filter(t => t.indexOf(setupData.filterText) !== -1)
17+
.filter(t => t.includes(setupData.filterText))
1818
.map(test => ({test})),
1919
};
2020
};

e2e/resolve/hasteImpl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const path = require('path');
1212
module.exports = {
1313
getHasteName(filePath) {
1414
const name = path.parse(filePath).name;
15-
const isMock = filePath.indexOf('__mocks__') !== -1;
15+
const isMock = filePath.includes('__mocks__');
1616

1717
// Mocks are automatically parsed by Jest already.
1818
return name.startsWith('Test') && !isMock ? name : null;

packages/jest-config/src/__tests__/normalize.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ describe('babel-jest', () => {
828828
Resolver = (require('jest-resolve') as typeof import('jest-resolve'))
829829
.default;
830830
Resolver.findNodeModule = jest.fn((name: string) =>
831-
name.indexOf('babel-jest') === -1
831+
!name.includes('babel-jest')
832832
? `${path.sep}node_modules${path.sep}${name}`
833833
: name,
834834
);

packages/jest-core/src/lib/handleDeprecationWarnings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function handleDeprecationWarnings(
3030
if (key === KEYS.ENTER) {
3131
resolve();
3232
} else if (
33-
[KEYS.ESCAPE, KEYS.CONTROL_C, KEYS.CONTROL_D].indexOf(key) !== -1
33+
[KEYS.ESCAPE, KEYS.CONTROL_C, KEYS.CONTROL_D].includes(key)
3434
) {
3535
reject();
3636
}

packages/jest-haste-map/src/crawlers/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function find(
105105
search(file);
106106
} else {
107107
const ext = path.extname(file).substr(1);
108-
if (extensions.indexOf(ext) !== -1) {
108+
if (extensions.includes(ext)) {
109109
result.push([file, stat.mtime.getTime(), stat.size]);
110110
}
111111
}

packages/jest-haste-map/src/crawlers/watchman.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export async function watchmanCrawl(options: CrawlerOptions): Promise<{
135135
'list-capabilities',
136136
);
137137

138-
if (capabilities.indexOf('field-content.sha1hex') !== -1) {
138+
if (capabilities.includes('field-content.sha1hex')) {
139139
fields.push('content.sha1hex');
140140
}
141141
}

packages/jest-haste-map/src/lib/getPlatformExtension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function getPlatformExtension(
2020
const platform = file.substring(secondToLast + 1, last);
2121
// If an overriding platform array is passed, check that first
2222

23-
if (platforms && platforms.indexOf(platform) !== -1) {
23+
if (platforms && platforms.includes(platform)) {
2424
return platform;
2525
}
2626
return SUPPORTED_PLATFORM_EXTS.has(platform) ? platform : null;

packages/jest-jasmine2/src/jasmine/Spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default class Spec {
103103
return !!(
104104
e &&
105105
e.toString &&
106-
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
106+
e.toString().includes(Spec.pendingSpecExceptionMessage)
107107
);
108108
}
109109

packages/jest-jasmine2/src/treeProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function treeProcessor(options: Options): void {
3333
options;
3434

3535
function isEnabled(node: TreeNode, parentEnabled: boolean) {
36-
return parentEnabled || runnableIds.indexOf(node.id) !== -1;
36+
return parentEnabled || runnableIds.includes(node.id);
3737
}
3838

3939
function getNodeHandler(node: TreeNode, parentEnabled: boolean) {

packages/jest-reporters/src/CoverageReporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export default class CoverageReporter extends BaseReporter {
303303
.map(filePath => path.resolve(filePath));
304304
}
305305

306-
if (filesByGlob[absoluteThresholdGroup].indexOf(file) > -1) {
306+
if (filesByGlob[absoluteThresholdGroup].includes(file)) {
307307
groupTypeByThresholdGroup[thresholdGroup] =
308308
THRESHOLD_GROUP_TYPES.GLOB;
309309
return agg.concat([[file, thresholdGroup]]);
@@ -317,7 +317,7 @@ export default class CoverageReporter extends BaseReporter {
317317
}
318318

319319
// Neither a glob or a path? Toss it in global if there's a global threshold:
320-
if (thresholdGroups.indexOf(THRESHOLD_GROUP_TYPES.GLOBAL) > -1) {
320+
if (thresholdGroups.includes(THRESHOLD_GROUP_TYPES.GLOBAL)) {
321321
groupTypeByThresholdGroup[THRESHOLD_GROUP_TYPES.GLOBAL] =
322322
THRESHOLD_GROUP_TYPES.GLOBAL;
323323
return files.concat([[file, THRESHOLD_GROUP_TYPES.GLOBAL]]);

packages/jest-runtime/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@ export default class Runtime {
24082408
const originalStack = new ReferenceError(`${errorMessage}${testPath}`)
24092409
.stack!.split('\n')
24102410
// Remove this file from the stack (jest-message-utils will keep one line)
2411-
.filter(line => line.indexOf(__filename) === -1)
2411+
.filter(line => !line.includes(__filename))
24122412
.join('\n');
24132413

24142414
const {message, stack} = separateMessageFromStack(originalStack);

packages/pretty-format/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function printComplexValue(
207207
refs: Refs,
208208
hasCalledToJSON?: boolean,
209209
): string {
210-
if (refs.indexOf(val) !== -1) {
210+
if (refs.includes(val)) {
211211
return '[Circular]';
212212
}
213213
refs = refs.slice();

packages/pretty-format/src/plugins/DOMCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap'];
1414
const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/;
1515

1616
const testName = (name: any) =>
17-
OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name);
17+
OBJECT_NAMES.includes(name) || ARRAY_REGEXP.test(name);
1818

1919
export const test: NewPlugin['test'] = (val: object) =>
2020
val &&
@@ -40,7 +40,7 @@ export const serialize: NewPlugin['serialize'] = (
4040

4141
return (
4242
(config.min ? '' : name + SPACE) +
43-
(OBJECT_NAMES.indexOf(name) !== -1
43+
(OBJECT_NAMES.includes(name)
4444
? `{${printObjectProperties(
4545
isNamedNodeMap(collection)
4646
? Array.from(collection).reduce<Record<string, string>>(

packages/pretty-format/src/plugins/lib/markup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const printProps = (
2626
let printed = printer(value, config, indentationNext, depth, refs);
2727

2828
if (typeof value !== 'string') {
29-
if (printed.indexOf('\n') !== -1) {
29+
if (printed.includes('\n')) {
3030
printed =
3131
config.spacingOuter +
3232
indentationNext +

scripts/lintTs.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const cpus = Math.max(
2323

2424
const mutex = pLimit(cpus);
2525

26-
const fix = process.argv.slice(2).some(arg => arg === '--fix');
26+
const fix = process.argv.slice(2).includes('--fix');
2727

2828
const monorepoRoot = path.resolve(url.fileURLToPath(import.meta.url), '../..');
2929

website/src/components/v1/Container.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export default function Container(props) {
1313
darkBackground: props.background === 'dark',
1414
highlightBackground: props.background === 'highlight',
1515
lightBackground: props.background === 'light',
16-
paddingAll: props.padding.indexOf('all') >= 0,
17-
paddingBottom: props.padding.indexOf('bottom') >= 0,
18-
paddingLeft: props.padding.indexOf('left') >= 0,
19-
paddingRight: props.padding.indexOf('right') >= 0,
20-
paddingTop: props.padding.indexOf('top') >= 0,
16+
paddingAll: props.padding.includes('all'),
17+
paddingBottom: props.padding.includes('bottom'),
18+
paddingLeft: props.padding.includes('left'),
19+
paddingRight: props.padding.includes('right'),
20+
paddingTop: props.padding.includes('top'),
2121
});
2222
let wrappedChildren;
2323

0 commit comments

Comments
 (0)