Skip to content

Commit 6362fb2

Browse files
authored
Replace eslint rulesdir with eslint-plugin-local, convert eslint rules to JS (#50380)
1 parent aaa4f9d commit 6362fb2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+200
-165
lines changed

.eslintplugin.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const rulesDir = path.join(__dirname, "scripts", "eslint", "rules");
5+
const ext = ".js";
6+
const ruleFiles = fs.readdirSync(rulesDir).filter((p) => p.endsWith(ext));
7+
8+
module.exports = {
9+
rules: Object.fromEntries(ruleFiles.map((p) => {
10+
return [p.slice(0, -ext.length), require(path.join(rulesDir, p))];
11+
})),
12+
}

.eslintrc.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"es6": true
1212
},
1313
"plugins": [
14-
"@typescript-eslint", "jsdoc", "no-null", "import"
14+
"@typescript-eslint", "jsdoc", "no-null", "import", "eslint-plugin-local"
1515
],
1616
"overrides": [
1717
// By default, the ESLint CLI only looks at .js files. But, it will also look at
@@ -81,20 +81,20 @@
8181
"@typescript-eslint/unified-signatures": "error",
8282

8383
// scripts/eslint/rules
84-
"object-literal-surrounding-space": "error",
85-
"no-type-assertion-whitespace": "error",
86-
"type-operator-spacing": "error",
87-
"only-arrow-functions": ["error", {
84+
"local/object-literal-surrounding-space": "error",
85+
"local/no-type-assertion-whitespace": "error",
86+
"local/type-operator-spacing": "error",
87+
"local/only-arrow-functions": ["error", {
8888
"allowNamedFunctions": true ,
8989
"allowDeclarations": true
9090
}],
91-
"no-double-space": "error",
92-
"boolean-trivia": "error",
93-
"no-in-operator": "error",
94-
"simple-indent": "error",
95-
"debug-assert": "error",
96-
"no-keywords": "error",
97-
"one-namespace-per-file": "error",
91+
"local/no-double-space": "error",
92+
"local/boolean-trivia": "error",
93+
"local/no-in-operator": "error",
94+
"local/simple-indent": "error",
95+
"local/debug-assert": "error",
96+
"local/no-keywords": "error",
97+
"local/one-namespace-per-file": "error",
9898

9999
// eslint-plugin-import
100100
"import/no-extraneous-dependencies": ["error", { "optionalDependencies": false }],

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ Dockerfile
3737
.eslintrc.json
3838
.yarnrc
3939
tmp
40+
.eslintplugin.js
41+
.eslintcache

.vscode/settings.template.json

-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
// Rename this file 'settings.json' or merge its
22
// contents into your existing settings.
33
{
4-
"eslint.validate": [
5-
"typescript"
6-
],
7-
"eslint.options": {
8-
"rulePaths": ["./scripts/eslint/built/rules/"],
9-
},
104
// To use the last-known-good (LKG) compiler version:
115
// "typescript.tsdk": "lib"
126

Gulpfile.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ const eslint = (folder) => async () => {
356356
"--cache",
357357
"--cache-location", `${folder}/.eslintcache`,
358358
"--format", formatter,
359-
"--rulesdir", "scripts/eslint/built/rules",
360359
];
361360

362361
if (cmdLineOptions.fix) {
@@ -369,10 +368,7 @@ const eslint = (folder) => async () => {
369368
return exec(process.execPath, args);
370369
};
371370

372-
const lintRoot = eslint(".");
373-
lintRoot.displayName = "lint";
374-
375-
const lint = series([buildEslintRules, lintRoot]);
371+
const lint = eslint(".");
376372
lint.displayName = "lint";
377373
task("lint", lint);
378374
task("lint").description = "Runs eslint on the compiler and scripts sources.";
@@ -431,7 +427,7 @@ task("watch-local").flags = {
431427
const preTest = parallel(buildTsc, buildTests, buildServices, buildLssl);
432428
preTest.displayName = "preTest";
433429

434-
const postTest = (done) => cmdLineOptions.lint ? lint(done) : done();
430+
const postTest = (done) => cmdLineOptions.lint ? lint() : done();
435431

436432
const runTests = () => runConsoleTests("built/local/run.js", "mocha-fivemat-progress-reporter", /*runInParallel*/ false, /*watchMode*/ false);
437433
task("runtests", series(preBuild, preTest, runTests, postTest));

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"eslint-formatter-autolinkable-stylish": "^1.2.0",
6363
"eslint-plugin-import": "^2.26.0",
6464
"eslint-plugin-jsdoc": "^39.3.6",
65+
"eslint-plugin-local": "^1.0.0",
6566
"eslint-plugin-no-null": "^1.0.2",
6667
"fancy-log": "latest",
6768
"fs-extra": "^9.1.0",
@@ -90,7 +91,6 @@
9091
"es5-ext": "0.10.53"
9192
},
9293
"scripts": {
93-
"prepare": "gulp build-eslint-rules",
9494
"pretest": "gulp tests",
9595
"test": "gulp runtests-parallel --light=false",
9696
"test:eslint-rules": "gulp run-eslint-rules-tests",

scripts/build/prepend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function prepend(data) {
4242
sourcesContent: input.sourcesContent
4343
};
4444
}
45-
// eslint-disable-next-line boolean-trivia, no-null/no-null
45+
// eslint-disable-next-line local/boolean-trivia, no-null/no-null
4646
return cb(null, output);
4747
}
4848
catch (e) {

scripts/eslint/rules/boolean-trivia.ts renamed to scripts/eslint/rules/boolean-trivia.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
2-
import { createRule } from "./utils";
1+
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils");
33

4-
export = createRule({
4+
module.exports = createRule({
55
name: "boolean-trivia",
66
meta: {
77
docs: {
@@ -21,8 +21,10 @@ export = createRule({
2121
const sourceCode = context.getSourceCode();
2222
const sourceCodeText = sourceCode.getText();
2323

24-
const isSetOrAssert = (name: string): boolean => name.startsWith("set") || name.startsWith("assert");
25-
const isTrivia = (node: TSESTree.Node): boolean => {
24+
/** @type {(name: string) => boolean} */
25+
const isSetOrAssert = (name) => name.startsWith("set") || name.startsWith("assert");
26+
/** @type {(node: TSESTree.Node) => boolean} */
27+
const isTrivia = (node) => {
2628
if (node.type === AST_NODE_TYPES.Identifier) {
2729
return node.name === "undefined";
2830
}
@@ -35,7 +37,8 @@ export = createRule({
3537
return false;
3638
};
3739

38-
const shouldIgnoreCalledExpression = (node: TSESTree.CallExpression): boolean => {
40+
/** @type {(node: TSESTree.CallExpression) => boolean} */
41+
const shouldIgnoreCalledExpression = (node) => {
3942
if (node.callee && node.callee.type === AST_NODE_TYPES.MemberExpression) {
4043
const methodName = node.callee.property.type === AST_NODE_TYPES.Identifier
4144
? node.callee.property.name
@@ -68,7 +71,8 @@ export = createRule({
6871
return false;
6972
};
7073

71-
const checkArg = (node: TSESTree.Node): void => {
74+
/** @type {(node: TSESTree.Node) => void} */
75+
const checkArg = (node) => {
7276
if (!isTrivia(node)) {
7377
return;
7478
}
@@ -88,7 +92,8 @@ export = createRule({
8892
}
8993
};
9094

91-
const checkBooleanTrivia = (node: TSESTree.CallExpression) => {
95+
/** @type {(node: TSESTree.CallExpression) => void} */
96+
const checkBooleanTrivia = (node) => {
9297
if (shouldIgnoreCalledExpression(node)) {
9398
return;
9499
}

scripts/eslint/rules/debug-assert.ts renamed to scripts/eslint/rules/debug-assert.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
2-
import { createRule } from "./utils";
1+
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils");
33

4-
export = createRule({
4+
module.exports = createRule({
55
name: "debug-assert",
66
meta: {
77
docs: {
@@ -18,19 +18,23 @@ export = createRule({
1818
defaultOptions: [],
1919

2020
create(context) {
21-
const isArrowFunction = (node: TSESTree.Node) => node.type === AST_NODE_TYPES.ArrowFunctionExpression;
22-
const isStringLiteral = (node: TSESTree.Node): boolean => (
21+
/** @type {(node: TSESTree.Node) => boolean} */
22+
const isArrowFunction = (node) => node.type === AST_NODE_TYPES.ArrowFunctionExpression;
23+
/** @type {(node: TSESTree.Node) => boolean} */
24+
const isStringLiteral = (node) => (
2325
(node.type === AST_NODE_TYPES.Literal && typeof node.value === "string") || node.type === AST_NODE_TYPES.TemplateLiteral
2426
);
2527

26-
const isDebugAssert = (node: TSESTree.MemberExpression): boolean => (
28+
/** @type {(node: TSESTree.MemberExpression) => boolean} */
29+
const isDebugAssert = (node) => (
2730
node.object.type === AST_NODE_TYPES.Identifier
2831
&& node.object.name === "Debug"
2932
&& node.property.type === AST_NODE_TYPES.Identifier
3033
&& node.property.name === "assert"
3134
);
3235

33-
const checkDebugAssert = (node: TSESTree.CallExpression) => {
36+
/** @type {(node: TSESTree.CallExpression) => void} */
37+
const checkDebugAssert = (node) => {
3438
const args = node.arguments;
3539
const argsLen = args.length;
3640
if (!(node.callee.type === AST_NODE_TYPES.MemberExpression && isDebugAssert(node.callee)) || argsLen < 2) {

scripts/eslint/rules/no-double-space.ts renamed to scripts/eslint/rules/no-double-space.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TSESTree, AST_NODE_TYPES } from "@typescript-eslint/utils";
2-
import { createRule } from "./utils";
1+
const { TSESTree, AST_NODE_TYPES } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils");
33

4-
export = createRule({
4+
module.exports = createRule({
55
name: "no-double-space",
66
meta: {
77
docs: {
@@ -20,19 +20,22 @@ export = createRule({
2020
const sourceCode = context.getSourceCode();
2121
const lines = sourceCode.getLines();
2222

23-
const isStringLiteral = (node: TSESTree.Node | null): boolean => {
23+
/** @type {(node: TSESTree.Node | null) => boolean} */
24+
const isStringLiteral = (node) => {
2425
return !!(node && (
2526
(node.type === AST_NODE_TYPES.TemplateElement) ||
2627
(node.type === AST_NODE_TYPES.TemplateLiteral && node.quasis) ||
2728
(node.type === AST_NODE_TYPES.Literal && typeof node.value === "string")
2829
));
2930
};
3031

31-
const isRegexLiteral = (node: TSESTree.Node | null): boolean => {
32+
/** @type {(node: TSESTree.Node | null) => boolean} */
33+
const isRegexLiteral = (node) => {
3234
return !!(node && node.type === AST_NODE_TYPES.Literal && Object.prototype.hasOwnProperty.call(node, "regex"));
3335
};
3436

35-
const checkDoubleSpace = (node: TSESTree.Node) => {
37+
/** @type {(node: TSESTree.Node) => void} */
38+
const checkDoubleSpace = (node) => {
3639
lines.forEach((line, index) => {
3740
const firstNonSpace = /\S/.exec(line);
3841
if (!firstNonSpace || line.includes("@param")) {

scripts/eslint/rules/no-in-operator.ts renamed to scripts/eslint/rules/no-in-operator.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TSESTree } from "@typescript-eslint/utils";
2-
import { createRule } from "./utils";
1+
const { TSESTree } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils");
33

4-
export = createRule({
4+
module.exports = createRule({
55
name: "no-in-operator",
66
meta: {
77
docs: {
@@ -18,7 +18,8 @@ export = createRule({
1818

1919
create(context) {
2020
const IN_OPERATOR = "in";
21-
const checkInOperator = (node: TSESTree.BinaryExpression) => {
21+
/** @type {(node: TSESTree.BinaryExpression) => void} */
22+
const checkInOperator = (node) => {
2223
if (node.operator === IN_OPERATOR) {
2324
context.report({ messageId: "noInOperatorError", node });
2425
}

scripts/eslint/rules/no-keywords.ts renamed to scripts/eslint/rules/no-keywords.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TSESTree, AST_NODE_TYPES } from "@typescript-eslint/utils";
2-
import { createRule } from "./utils";
1+
const { TSESTree, AST_NODE_TYPES } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils");
33

4-
export = createRule({
4+
module.exports = createRule({
55
name: "no-keywords",
66
meta: {
77
docs: {
@@ -35,13 +35,16 @@ export = createRule({
3535
"any",
3636
];
3737

38-
const isKeyword = (name: string) => keywords.includes(name);
38+
/** @type {(name: string) => boolean} */
39+
const isKeyword = (name) => keywords.includes(name);
3940

40-
const report = (node: TSESTree.Identifier) => {
41+
/** @type {(node: TSESTree.Identifier) => void} */
42+
const report = (node) => {
4143
context.report({ messageId: "noKeywordsError", data: { name: node.name }, node });
4244
};
4345

44-
const checkProperties = (node: TSESTree.ObjectPattern): void => {
46+
/** @type {(node: TSESTree.ObjectPattern) => void} */
47+
const checkProperties = (node) => {
4548
node.properties.forEach(property => {
4649
if (
4750
property &&
@@ -54,7 +57,8 @@ export = createRule({
5457
});
5558
};
5659

57-
const checkElements = (node: TSESTree.ArrayPattern): void => {
60+
/** @type {(node: TSESTree.ArrayPattern) => void} */
61+
const checkElements = (node) => {
5862
node.elements.forEach(element => {
5963
if (
6064
element &&
@@ -66,14 +70,8 @@ export = createRule({
6670
});
6771
};
6872

69-
const checkParams = (
70-
node:
71-
| TSESTree.ArrowFunctionExpression
72-
| TSESTree.FunctionDeclaration
73-
| TSESTree.FunctionExpression
74-
| TSESTree.TSMethodSignature
75-
| TSESTree.TSFunctionType
76-
): void => {
73+
/** @type {(node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.TSMethodSignature | TSESTree.TSFunctionType) => void} */
74+
const checkParams = (node) => {
7775
if (!node || !node.params || !node.params.length) {
7876
return;
7977
}

scripts/eslint/rules/no-type-assertion-whitespace.ts renamed to scripts/eslint/rules/no-type-assertion-whitespace.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TSESTree } from "@typescript-eslint/utils";
2-
import { createRule } from "./utils";
1+
const { TSESTree } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils");
33

4-
export = createRule({
4+
module.exports = createRule({
55
name: "no-type-assertion-whitespace",
66
meta: {
77
docs: {
@@ -18,7 +18,8 @@ export = createRule({
1818

1919
create(context) {
2020
const sourceCode = context.getSourceCode();
21-
const checkTypeAssertionWhitespace = (node: TSESTree.TSTypeAssertion) => {
21+
/** @type {(node: TSESTree.TSTypeAssertion) => void} */
22+
const checkTypeAssertionWhitespace = (node) => {
2223
const leftToken = sourceCode.getLastToken(node.typeAnnotation);
2324
const rightToken = sourceCode.getFirstToken(node.expression);
2425

0 commit comments

Comments
 (0)