chore: prefer `.includes` over `indexOf` (#14524)

This commit is contained in:
Simen Bekkhus 2023-09-14 15:13:41 +02:00 committed by GitHub
parent 9b077f7487
commit 8cc8daef4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 31 additions and 30 deletions

View File

@ -591,6 +591,7 @@ module.exports = {
'wrap-regex': 'off',
yoda: 'off',
'unicorn/prefer-includes': 'error',
'unicorn/template-indent': 'error',
},
settings: {

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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

View File

@ -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})),
};
};

View File

@ -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);

View File

@ -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})),
};
};

View File

@ -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})),
};
};

View File

@ -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;

View File

@ -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,
);

View File

@ -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();
}

View File

@ -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]);
}
}

View File

@ -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');
}
}

View File

@ -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;

View File

@ -103,7 +103,7 @@ export default class Spec {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
e.toString().includes(Spec.pendingSpecExceptionMessage)
);
}

View File

@ -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) {

View File

@ -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]]);

View File

@ -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);

View File

@ -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();

View File

@ -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<Record<string, string>>(

View File

@ -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 +

View File

@ -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), '../..');

View File

@ -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;