Skip to content

Commit

Permalink
chore: prefer .includes over indexOf (#14524)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 14, 2023
1 parent 9b077f7 commit 8cc8dae
Show file tree
Hide file tree
Showing 24 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ module.exports = {
'wrap-regex': 'off',
yoda: 'off',

'unicorn/prefer-includes': 'error',
'unicorn/template-indent': 'error',
},
settings: {
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/customInlineSnapshotMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/customMatcherStackTrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/expectAsyncMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion e2e/filter/my-broken-setup-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})),
};
};

Expand Down
2 changes: 1 addition & 1 deletion e2e/filter/my-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion e2e/filter/my-secondary-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})),
};
};
2 changes: 1 addition & 1 deletion e2e/filter/my-setup-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})),
};
};
Expand Down
2 changes: 1 addition & 1 deletion e2e/resolve/hasteImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/lib/handleDeprecationWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/crawlers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/crawlers/watchman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/lib/getPlatformExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmine/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class Spec {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
e.toString().includes(Spec.pendingSpecExceptionMessage)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/treeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-reporters/src/CoverageReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
Expand All @@ -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]]);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/plugins/DOMCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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<Record<string, string>>(
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/lib/markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down
2 changes: 1 addition & 1 deletion scripts/lintTs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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), '../..');

Expand Down
10 changes: 5 additions & 5 deletions website/src/components/v1/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 8cc8dae

Please sign in to comment.