Skip to content

Commit c8362e3

Browse files
authored
Enable unicorn/prefer-set-has rule (#16822)
* Enable `unicorn/prefer-set-has` rule * Run `--fix` * `yarn dedupe` * Fix * fix
1 parent d19d001 commit c8362e3

8 files changed

Lines changed: 179 additions & 38 deletions

File tree

eslint.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import pluginJest from "eslint-plugin-jest";
99
import pluginN from "eslint-plugin-n";
1010
import pluginPrettier from "eslint-plugin-prettier";
1111
import pluginRegexp from "eslint-plugin-regexp";
12+
import pluginUnicorn from "eslint-plugin-unicorn";
1213
import pluginBabelDevelopment from "@babel/eslint-plugin-development";
1314
import pluginBabelDevelopmentInternal from "@babel/eslint-plugin-development-internal";
1415
import typescriptEslint from "typescript-eslint";
@@ -104,6 +105,7 @@ export default [
104105
import: pluginImport,
105106
n: pluginN,
106107
prettier: pluginPrettier,
108+
unicorn: pluginUnicorn,
107109
"@babel/development": pluginBabelDevelopment,
108110
"@babel/development-internal": pluginBabelDevelopmentInternal,
109111
},
@@ -112,6 +114,7 @@ export default [
112114
"prettier/prettier": "error",
113115
"import/no-extraneous-dependencies": "error",
114116
"regexp/match-any": ["error", { allows: ["[^]", "dotAll"] }],
117+
"unicorn/prefer-set-has": "error",
115118
},
116119
},
117120
...typescriptEslint.config({

eslint/babel-eslint-parser/src/analyze-scope.cts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ function getVisitorValues(nodeType: string, client: Client) {
1919

2020
const { FLOW_FLIPPED_ALIAS_KEYS, VISITOR_KEYS } = client.getTypesInfo();
2121

22-
const flowFlippedAliasKeys = FLOW_FLIPPED_ALIAS_KEYS.concat([
23-
"ArrayPattern",
24-
"ClassDeclaration",
25-
"ClassExpression",
26-
"FunctionDeclaration",
27-
"FunctionExpression",
28-
"Identifier",
29-
"ObjectPattern",
30-
"RestElement",
31-
]);
22+
const flowFlippedAliasKeys = new Set(
23+
FLOW_FLIPPED_ALIAS_KEYS.concat([
24+
"ArrayPattern",
25+
"ClassDeclaration",
26+
"ClassExpression",
27+
"FunctionDeclaration",
28+
"FunctionExpression",
29+
"Identifier",
30+
"ObjectPattern",
31+
"RestElement",
32+
]),
33+
);
3234

3335
visitorKeysMap = Object.entries(VISITOR_KEYS).reduce((acc, [key, value]) => {
34-
if (!flowFlippedAliasKeys.includes(value)) {
36+
if (!flowFlippedAliasKeys.has(value)) {
3537
// @ts-expect-error FIXME: value is not assignable to type string[]
3638
acc[key] = value;
3739
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"eslint-plugin-n": "^17.9.0",
6060
"eslint-plugin-prettier": "^5.1.3",
6161
"eslint-plugin-regexp": "^2.6.0",
62+
"eslint-plugin-unicorn": "^55.0.0",
6263
"execa": "^9.0.0",
6364
"glob": "^10.3.10",
6465
"globals": "^15.9.0",

packages/babel-plugin-transform-dynamic-import/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { declare } from "@babel/helper-plugin-utils";
22

3-
const SUPPORTED_MODULES = ["commonjs", "amd", "systemjs"];
3+
const SUPPORTED_MODULES = new Set(["commonjs", "amd", "systemjs"]);
44

55
const MODULES_NOT_FOUND = `\
66
@babel/plugin-transform-dynamic-import depends on a modules
@@ -39,7 +39,7 @@ export default declare(api => {
3939
Program() {
4040
const modules = this.file.get("@babel/plugin-transform-modules-*");
4141

42-
if (!SUPPORTED_MODULES.includes(modules)) {
42+
if (!SUPPORTED_MODULES.has(modules)) {
4343
throw new Error(MODULES_NOT_FOUND);
4444
}
4545
},

packages/babel-standalone/src/transformScriptTags.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* LICENSE file in the root directory of the React source tree.
77
*/
88

9-
const scriptTypes = ["text/jsx", "text/babel"];
9+
const scriptTypes = new Set(["text/jsx", "text/babel"]);
1010

1111
import type { transform } from "./index.ts";
1212
import type { InputOptions } from "@babel/core";
@@ -241,7 +241,7 @@ export function runScripts(
241241
const script = scripts.item(i);
242242
// Support the old type="text/jsx;harmony=true"
243243
const type = script.type.split(";")[0];
244-
if (scriptTypes.includes(type)) {
244+
if (scriptTypes.has(type)) {
245245
jsxScripts.push(script);
246246
}
247247
}

packages/babel-types/src/definitions/utils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,21 @@ export function chain(...fns: Array<Validator>): Validator {
269269
return validate;
270270
}
271271

272-
const validTypeOpts = [
272+
const validTypeOpts = new Set([
273273
"aliases",
274274
"builder",
275275
"deprecatedAlias",
276276
"fields",
277277
"inherits",
278278
"visitor",
279279
"validate",
280-
];
281-
const validFieldKeys = ["default", "optional", "deprecated", "validate"];
280+
]);
281+
const validFieldKeys = new Set([
282+
"default",
283+
"optional",
284+
"deprecated",
285+
"validate",
286+
]);
282287

283288
const store = {} as Record<string, DefineTypeOpts>;
284289

@@ -331,7 +336,7 @@ export default function defineType(type: string, opts: DefineTypeOpts = {}) {
331336
opts.builder || inherits.builder || opts.visitor || [];
332337

333338
for (const k of Object.keys(opts)) {
334-
if (!validTypeOpts.includes(k)) {
339+
if (!validTypeOpts.has(k)) {
335340
throw new Error(`Unknown type option "${k}" on ${type}`);
336341
}
337342
}
@@ -358,7 +363,7 @@ export default function defineType(type: string, opts: DefineTypeOpts = {}) {
358363
}
359364

360365
for (const k of Object.keys(field)) {
361-
if (!validFieldKeys.includes(k)) {
366+
if (!validFieldKeys.has(k)) {
362367
throw new Error(`Unknown field key "${k}" on ${type}.${key}`);
363368
}
364369
}

scripts/parser-tests/utils/parser-test-runner.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,24 @@ class TestRunner {
8181
async updateAllowlist(summary) {
8282
const contents = await fs.readFile(this.allowlist, "utf-8");
8383

84-
const toRemove = summary.disallowed.success
85-
.concat(summary.disallowed.failure)
86-
.map(test => test.id)
87-
.concat(summary.unrecognized);
84+
const toRemove = new Set(
85+
summary.disallowed.success
86+
.concat(summary.disallowed.failure)
87+
.map(test => test.id)
88+
.concat(summary.unrecognized)
89+
);
8890

89-
const allowedFalsePositiveIds = summary.allowed.falsePositive.map(
90-
test => test.id
91+
const allowedFalsePositiveIds = new Set(
92+
summary.allowed.falsePositive.map(test => test.id)
9193
);
9294

9395
let invalidWithoutError = [];
9496
let validWithError = [];
9597

9698
for (const line of contents.split("\n")) {
9799
const testId = line.replace(/#.*$/, "").trim();
98-
if (testId && !toRemove.includes(testId)) {
99-
if (allowedFalsePositiveIds.includes(testId)) {
100+
if (testId && !toRemove.has(testId)) {
101+
if (allowedFalsePositiveIds.has(testId)) {
100102
invalidWithoutError.push(line);
101103
} else {
102104
validWithError.push(line);

0 commit comments

Comments
 (0)