Skip to content

Commit

Permalink
chore: prefer Set over Array for existence checks (jestjs#14802)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 29, 2023
1 parent 0469b4c commit 5a21906
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
9 changes: 5 additions & 4 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -707,21 +707,22 @@ module.exports = {
'unicorn/prefer-array-some': 'off',
'unicorn/prefer-at': 'off',
'unicorn/prefer-date-now': 'off',
'unicorn/prefer-export-from': 'off',
'unicorn/prefer-logical-operator-over-ternary': 'off',
'unicorn/prefer-math-trunc': 'off',
'unicorn/prefer-native-coercion-functions': 'off',
'unicorn/prefer-node-protocol': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/prefer-object-from-entries': 'off',
'unicorn/prefer-optional-catch-binding': 'off',
'unicorn/prefer-prototype-methods': 'off',
'unicorn/prefer-regexp-test': 'off',
'unicorn/prefer-set-has': 'off',
'unicorn/prefer-spread': 'off',
'unicorn/prefer-string-replace-all': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/text-encoding-identifier-case': 'off',

// enabling this is blocked by https://github.com/microsoft/rushstack/issues/2780
'unicorn/prefer-export-from': 'off',
// enabling this is blocked by https://github.com/jestjs/jest/pull/14297
'unicorn/prefer-node-protocol': 'off',
},
settings: {
'import/ignore': ['react-native'],
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/assertionCounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('numPassingAsserts', () => {
expect('a').toBe('a');
try {
expect('a').toBe('b');
} catch (error) {}
} catch {}
const {numPassingAsserts} = jestExpect.getState();
expect(numPassingAsserts).toBe(1);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-config/src/setFromArgv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import type {Config} from '@jest/types';
import {isJSONString} from './utils';

const specialArgs = ['_', '$0', 'h', 'help', 'config'];
const specialArgs = new Set(['_', '$0', 'h', 'help', 'config']);

export default function setFromArgv(
options: Config.InitialOptions,
argv: Config.Argv,
): Config.InitialOptions {
const argvToOptions = Object.keys(argv).reduce(
(options: Record<string, unknown>, key) => {
if (argv[key] === undefined || specialArgs.includes(key)) {
if (argv[key] === undefined || specialArgs.has(key)) {
return options;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ export default class SearchSource {
{skipNodeResolution: this._context.config.skipNodeResolution},
);

const allPathsAbsolute = Array.from(allPaths).map(p => path.resolve(p));
const allPathsAbsolute = new Set(
Array.from(allPaths).map(p => path.resolve(p)),
);

const collectCoverageFrom = new Set<string>();

Expand All @@ -201,7 +203,7 @@ export default class SearchSource {
}

for (const p of testModule.dependencies) {
if (!allPathsAbsolute.includes(p)) {
if (!allPathsAbsolute.has(p)) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/jest-matcher-utils/src/Replaceable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import {getType} from 'jest-get-type';

const supportTypes = ['map', 'array', 'object'];
const supportTypes = new Set(['map', 'array', 'object']);

type ReplaceableForEachCallBack = (
value: unknown,
Expand All @@ -23,15 +23,15 @@ export default class Replaceable {
constructor(object: any) {
this.object = object;
this.type = getType(object);
if (!supportTypes.includes(this.type)) {
if (!supportTypes.has(this.type)) {
throw new Error(`Type ${this.type} is not support in Replaceable!`);
}
}

static isReplaceable(obj1: unknown, obj2: unknown): boolean {
const obj1Type = getType(obj1);
const obj2Type = getType(obj2);
return obj1Type === obj2Type && supportTypes.includes(obj1Type);
return obj1Type === obj2Type && supportTypes.has(obj1Type);
}

forEach(cb: ReplaceableForEachCallBack): void {
Expand Down
6 changes: 3 additions & 3 deletions packages/pretty-format/src/plugins/DOMCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import type {Config, NewPlugin, Printer, Refs} from '../types';

const SPACE = ' ';

const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap'];
const OBJECT_NAMES = new Set(['DOMStringMap', 'NamedNodeMap']);
const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/;

const testName = (name: any) =>
OBJECT_NAMES.includes(name) || ARRAY_REGEXP.test(name);
OBJECT_NAMES.has(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.includes(name)
(OBJECT_NAMES.has(name)
? `{${printObjectProperties(
isNamedNodeMap(collection)
? Array.from(collection).reduce<Record<string, string>>(
Expand Down

0 comments on commit 5a21906

Please sign in to comment.