Replace eslint rulesdir with eslint-plugin-local, convert eslint rules to JS (#50380)

This commit is contained in:
Jake Bailey
2022-08-22 13:46:03 -07:00
committed by GitHub
parent aaa4f9d9ff
commit 6362fb2dce
44 changed files with 200 additions and 165 deletions

View File

@@ -42,7 +42,7 @@ function prepend(data) {
sourcesContent: input.sourcesContent
};
}
// eslint-disable-next-line boolean-trivia, no-null/no-null
// eslint-disable-next-line local/boolean-trivia, no-null/no-null
return cb(null, output);
}
catch (e) {

View File

@@ -1,7 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "boolean-trivia",
meta: {
docs: {
@@ -21,8 +21,10 @@ export = createRule({
const sourceCode = context.getSourceCode();
const sourceCodeText = sourceCode.getText();
const isSetOrAssert = (name: string): boolean => name.startsWith("set") || name.startsWith("assert");
const isTrivia = (node: TSESTree.Node): boolean => {
/** @type {(name: string) => boolean} */
const isSetOrAssert = (name) => name.startsWith("set") || name.startsWith("assert");
/** @type {(node: TSESTree.Node) => boolean} */
const isTrivia = (node) => {
if (node.type === AST_NODE_TYPES.Identifier) {
return node.name === "undefined";
}
@@ -35,7 +37,8 @@ export = createRule({
return false;
};
const shouldIgnoreCalledExpression = (node: TSESTree.CallExpression): boolean => {
/** @type {(node: TSESTree.CallExpression) => boolean} */
const shouldIgnoreCalledExpression = (node) => {
if (node.callee && node.callee.type === AST_NODE_TYPES.MemberExpression) {
const methodName = node.callee.property.type === AST_NODE_TYPES.Identifier
? node.callee.property.name
@@ -68,7 +71,8 @@ export = createRule({
return false;
};
const checkArg = (node: TSESTree.Node): void => {
/** @type {(node: TSESTree.Node) => void} */
const checkArg = (node) => {
if (!isTrivia(node)) {
return;
}
@@ -88,7 +92,8 @@ export = createRule({
}
};
const checkBooleanTrivia = (node: TSESTree.CallExpression) => {
/** @type {(node: TSESTree.CallExpression) => void} */
const checkBooleanTrivia = (node) => {
if (shouldIgnoreCalledExpression(node)) {
return;
}

View File

@@ -1,7 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "debug-assert",
meta: {
docs: {
@@ -18,19 +18,23 @@ export = createRule({
defaultOptions: [],
create(context) {
const isArrowFunction = (node: TSESTree.Node) => node.type === AST_NODE_TYPES.ArrowFunctionExpression;
const isStringLiteral = (node: TSESTree.Node): boolean => (
/** @type {(node: TSESTree.Node) => boolean} */
const isArrowFunction = (node) => node.type === AST_NODE_TYPES.ArrowFunctionExpression;
/** @type {(node: TSESTree.Node) => boolean} */
const isStringLiteral = (node) => (
(node.type === AST_NODE_TYPES.Literal && typeof node.value === "string") || node.type === AST_NODE_TYPES.TemplateLiteral
);
const isDebugAssert = (node: TSESTree.MemberExpression): boolean => (
/** @type {(node: TSESTree.MemberExpression) => boolean} */
const isDebugAssert = (node) => (
node.object.type === AST_NODE_TYPES.Identifier
&& node.object.name === "Debug"
&& node.property.type === AST_NODE_TYPES.Identifier
&& node.property.name === "assert"
);
const checkDebugAssert = (node: TSESTree.CallExpression) => {
/** @type {(node: TSESTree.CallExpression) => void} */
const checkDebugAssert = (node) => {
const args = node.arguments;
const argsLen = args.length;
if (!(node.callee.type === AST_NODE_TYPES.MemberExpression && isDebugAssert(node.callee)) || argsLen < 2) {

View File

@@ -1,7 +1,7 @@
import { TSESTree, AST_NODE_TYPES } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree, AST_NODE_TYPES } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "no-double-space",
meta: {
docs: {
@@ -20,7 +20,8 @@ export = createRule({
const sourceCode = context.getSourceCode();
const lines = sourceCode.getLines();
const isStringLiteral = (node: TSESTree.Node | null): boolean => {
/** @type {(node: TSESTree.Node | null) => boolean} */
const isStringLiteral = (node) => {
return !!(node && (
(node.type === AST_NODE_TYPES.TemplateElement) ||
(node.type === AST_NODE_TYPES.TemplateLiteral && node.quasis) ||
@@ -28,11 +29,13 @@ export = createRule({
));
};
const isRegexLiteral = (node: TSESTree.Node | null): boolean => {
/** @type {(node: TSESTree.Node | null) => boolean} */
const isRegexLiteral = (node) => {
return !!(node && node.type === AST_NODE_TYPES.Literal && Object.prototype.hasOwnProperty.call(node, "regex"));
};
const checkDoubleSpace = (node: TSESTree.Node) => {
/** @type {(node: TSESTree.Node) => void} */
const checkDoubleSpace = (node) => {
lines.forEach((line, index) => {
const firstNonSpace = /\S/.exec(line);
if (!firstNonSpace || line.includes("@param")) {

View File

@@ -1,7 +1,7 @@
import { TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "no-in-operator",
meta: {
docs: {
@@ -18,7 +18,8 @@ export = createRule({
create(context) {
const IN_OPERATOR = "in";
const checkInOperator = (node: TSESTree.BinaryExpression) => {
/** @type {(node: TSESTree.BinaryExpression) => void} */
const checkInOperator = (node) => {
if (node.operator === IN_OPERATOR) {
context.report({ messageId: "noInOperatorError", node });
}

View File

@@ -1,7 +1,7 @@
import { TSESTree, AST_NODE_TYPES } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree, AST_NODE_TYPES } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "no-keywords",
meta: {
docs: {
@@ -35,13 +35,16 @@ export = createRule({
"any",
];
const isKeyword = (name: string) => keywords.includes(name);
/** @type {(name: string) => boolean} */
const isKeyword = (name) => keywords.includes(name);
const report = (node: TSESTree.Identifier) => {
/** @type {(node: TSESTree.Identifier) => void} */
const report = (node) => {
context.report({ messageId: "noKeywordsError", data: { name: node.name }, node });
};
const checkProperties = (node: TSESTree.ObjectPattern): void => {
/** @type {(node: TSESTree.ObjectPattern) => void} */
const checkProperties = (node) => {
node.properties.forEach(property => {
if (
property &&
@@ -54,7 +57,8 @@ export = createRule({
});
};
const checkElements = (node: TSESTree.ArrayPattern): void => {
/** @type {(node: TSESTree.ArrayPattern) => void} */
const checkElements = (node) => {
node.elements.forEach(element => {
if (
element &&
@@ -66,14 +70,8 @@ export = createRule({
});
};
const checkParams = (
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.TSMethodSignature
| TSESTree.TSFunctionType
): void => {
/** @type {(node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.TSMethodSignature | TSESTree.TSFunctionType) => void} */
const checkParams = (node) => {
if (!node || !node.params || !node.params.length) {
return;
}

View File

@@ -1,7 +1,7 @@
import { TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "no-type-assertion-whitespace",
meta: {
docs: {
@@ -18,7 +18,8 @@ export = createRule({
create(context) {
const sourceCode = context.getSourceCode();
const checkTypeAssertionWhitespace = (node: TSESTree.TSTypeAssertion) => {
/** @type {(node: TSESTree.TSTypeAssertion) => void} */
const checkTypeAssertionWhitespace = (node) => {
const leftToken = sourceCode.getLastToken(node.typeAnnotation);
const rightToken = sourceCode.getFirstToken(node.expression);

View File

@@ -1,7 +1,7 @@
import { TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "object-literal-surrounding-space",
meta: {
docs: {
@@ -26,11 +26,13 @@ export = createRule({
const CLOSE_SYMBOL = "}";
const OPEN_SYMBOL = "{";
const manySpaces = (text: string, startIndex: number): boolean => (
/** @type {(text: string, startIndex: number) => boolean} */
const manySpaces = (text, startIndex) => (
[startIndex, startIndex + 1].every(i => text.charAt(i) === SPACE_SYMBOL)
);
const checkObjectLiteralSurroundingSpace = (node: TSESTree.ObjectExpression) => {
/** @type {(node: TSESTree.ObjectExpression) => void} */
const checkObjectLiteralSurroundingSpace = (node) => {
const text = sourceCode.getText(node);
const startLine = node.loc.start.line;
const endLine = node.loc.end.line;

View File

@@ -1,7 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "one-namespace-per-file",
meta: {
docs: {
@@ -17,9 +17,11 @@ export = createRule({
defaultOptions: [],
create(context) {
const isNamespaceDeclaration = (node: TSESTree.Node): node is TSESTree.TSModuleDeclaration => node.type === AST_NODE_TYPES.TSModuleDeclaration;
/** @type {(node: TSESTree.Node) => node is TSESTree.TSModuleDeclaration} */
const isNamespaceDeclaration = (node) => node.type === AST_NODE_TYPES.TSModuleDeclaration;
const checkSourceFile = (node: TSESTree.Program) => {
/** @type {(node: TSESTree.Program) => void} */
const checkSourceFile = (node) => {
if (context.getFilename().endsWith(".d.ts")) {
return;
}

View File

@@ -1,13 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
type MessageId = "onlyArrowFunctionsError";
type Options = [{
allowNamedFunctions?: boolean;
allowDeclarations?: boolean;
}];
export = createRule<Options, MessageId>({
module.exports = createRule({
name: "only-arrow-functions",
meta: {
docs: {
@@ -27,6 +21,7 @@ export = createRule<Options, MessageId>({
}],
type: "suggestion",
},
/** @type {[{ allowNamedFunctions?: boolean; allowDeclarations?: boolean }]} */
defaultOptions: [{
allowNamedFunctions: false,
allowDeclarations: false,
@@ -34,11 +29,11 @@ export = createRule<Options, MessageId>({
create(context, [{ allowNamedFunctions, allowDeclarations }]) {
const isThisParameter = (node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => (
node.params.length && !!node.params.find(param => param.type === AST_NODE_TYPES.Identifier && param.name === "this")
);
/** @type {(node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => boolean} */
const isThisParameter = (node) => !!node.params.length && !!node.params.find(param => param.type === AST_NODE_TYPES.Identifier && param.name === "this");
const isMethodType = (node: TSESTree.Node) => {
/** @type {(node: TSESTree.Node) => boolean} */
const isMethodType = (node) => {
const types = [
AST_NODE_TYPES.MethodDefinition,
AST_NODE_TYPES.Property,
@@ -52,7 +47,8 @@ export = createRule<Options, MessageId>({
return node.type === AST_NODE_TYPES.FunctionExpression && types.includes(parent.type);
};
const stack: boolean[] = [];
/** @type {boolean[]} */
const stack = [];
const enterFunction = () => {
stack.push(false);
};
@@ -63,7 +59,8 @@ export = createRule<Options, MessageId>({
}
};
const exitFunction = (node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => {
/** @type {(node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void} */
const exitFunction = (node) => {
const methodUsesThis = stack.pop();
if (node.type === AST_NODE_TYPES.FunctionDeclaration && allowDeclarations) {

View File

@@ -1,7 +1,7 @@
import { TSESTree } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "simple-indent",
meta: {
docs: {
@@ -22,7 +22,8 @@ export = createRule({
const sourceCode = context.getSourceCode();
const linebreaks = sourceCode.getText().match(/\r\n|[\r\n\u2028\u2029]/gu);
const checkIndent = (node: TSESTree.Program) => {
/** @type {(node: TSESTree.Program) => void} */
const checkIndent = (node) => {
const lines = sourceCode.getLines();
const linesLen = lines.length;

View File

@@ -1,7 +1,7 @@
import { TSESTree, AST_TOKEN_TYPES } from "@typescript-eslint/utils";
import { createRule } from "./utils";
const { TSESTree, AST_TOKEN_TYPES } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
export = createRule({
module.exports = createRule({
name: "type-operator-spacing",
meta: {
docs: {
@@ -21,7 +21,8 @@ export = createRule({
const tokens = ["|", "&"];
const text = sourceCode.getText();
const checkTypeOperatorSpacing = (node: TSESTree.TSIntersectionType | TSESTree.TSUnionType) => {
/** @type {(node: TSESTree.TSIntersectionType | TSESTree.TSUnionType) => void} */
const checkTypeOperatorSpacing = (node) => {
node.types.forEach(node => {
const token = sourceCode.getTokenBefore(node);

View File

@@ -0,0 +1,2 @@
const { ESLintUtils } = require("@typescript-eslint/utils");
module.exports.createRule = ESLintUtils.RuleCreator(() => "");

View File

@@ -1,2 +0,0 @@
import { ESLintUtils } from "@typescript-eslint/utils";
export const createRule = ESLintUtils.RuleCreator(() => "");

View File

@@ -1,23 +1,26 @@
{
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"suppressImplicitAnyIndexErrors": true,
"experimentalDecorators": true,
"noImplicitReturns": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"esModuleInterop": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noImplicitAny": true,
"skipLibCheck": true,
"declaration": false,
"noResolve": false,
"strict": true,
"lib": [
"es6"
],
"module": "commonjs",
"target": "es6",
"outDir": "./built",
"lib": ["es2015", "es2016"]
"declaration": false,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"allowUnusedLabels": false,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowJs": true,
"checkJs": true
},
"include": [