From b84b05f6fc6970748523105f1a2390d5de89ea70 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Thu, 23 Sep 2021 20:01:20 -0300 Subject: [PATCH] refactor(eslint-plugin): avoid looking for nodes that do not match the provided options --- .../src/rules/class-literal-property-style.ts | 65 +++---- .../src/rules/consistent-type-definitions.ts | 20 +- .../rules/explicit-module-boundary-types.ts | 1 - .../src/rules/method-signature-style.ts | 182 +++++++++--------- .../src/rules/restrict-plus-operands.ts | 8 +- .../sort-type-union-intersection-members.ts | 16 +- packages/eslint-plugin/src/rules/typedef.ts | 157 ++++++++------- 7 files changed, 228 insertions(+), 221 deletions(-) diff --git a/packages/eslint-plugin/src/rules/class-literal-property-style.ts b/packages/eslint-plugin/src/rules/class-literal-property-style.ts index 0a257427ec10..d7ee91bd72dc 100644 --- a/packages/eslint-plugin/src/rules/class-literal-property-style.ts +++ b/packages/eslint-plugin/src/rules/class-literal-property-style.ts @@ -56,8 +56,8 @@ export default util.createRule({ }, defaultOptions: ['fields'], create(context, [style]) { - if (style === 'fields') { - return { + return { + ...(style === 'fields' && { MethodDefinition(node: TSESTree.MethodDefinition): void { if ( node.kind !== 'get' || @@ -96,38 +96,37 @@ export default util.createRule({ }, }); }, - }; - } + }), + ...(style === 'getters' && { + ClassProperty(node: TSESTree.ClassProperty): void { + if (!node.readonly || node.declare) { + return; + } - return { - ClassProperty(node: TSESTree.ClassProperty): void { - if (!node.readonly || node.declare) { - return; - } - - const { value } = node; - - if (!value || !isSupportedLiteral(value)) { - return; - } - - context.report({ - node: node.key, - messageId: 'preferGetterStyle', - fix(fixer) { - const sourceCode = context.getSourceCode(); - const name = sourceCode.getText(node.key); - - let text = ''; - - text += printNodeModifiers(node, 'get'); - text += node.computed ? `[${name}]` : name; - text += `() { return ${sourceCode.getText(value)}; }`; - - return fixer.replaceText(node, text); - }, - }); - }, + const { value } = node; + + if (!value || !isSupportedLiteral(value)) { + return; + } + + context.report({ + node: node.key, + messageId: 'preferGetterStyle', + fix(fixer) { + const sourceCode = context.getSourceCode(); + const name = sourceCode.getText(node.key); + + let text = ''; + + text += printNodeModifiers(node, 'get'); + text += node.computed ? `[${name}]` : name; + text += `() { return ${sourceCode.getText(value)}; }`; + + return fixer.replaceText(node, text); + }, + }); + }, + }), }; }, }); diff --git a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts index 07d848db298a..a664807574e7 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts @@ -48,10 +48,10 @@ export default util.createRule({ } return { - "TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"( - node: TSESTree.TSTypeAliasDeclaration, - ): void { - if (option === 'interface') { + ...(option === 'interface' && { + "TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"( + node: TSESTree.TSTypeAliasDeclaration, + ): void { context.report({ node: node.id, messageId: 'interfaceOverType', @@ -82,10 +82,10 @@ export default util.createRule({ return fixes; }, }); - } - }, - TSInterfaceDeclaration(node): void { - if (option === 'type') { + }, + }), + ...(option === 'type' && { + TSInterfaceDeclaration(node): void { context.report({ node: node.id, messageId: 'typeOverInterface', @@ -138,8 +138,8 @@ export default util.createRule({ return fixes; }, }); - } - }, + }, + }), }; }, }); diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index 33caf65f19b4..7a0b43a00dff 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -130,7 +130,6 @@ export default util.createRule({ TSExportAssignment(node): void { checkNode(node.expression); }, - 'ArrowFunctionExpression, FunctionDeclaration, FunctionExpression'( node: FunctionNode, ): void { diff --git a/packages/eslint-plugin/src/rules/method-signature-style.ts b/packages/eslint-plugin/src/rules/method-signature-style.ts index 4775a1fec718..2587c5a026ff 100644 --- a/packages/eslint-plugin/src/rules/method-signature-style.ts +++ b/packages/eslint-plugin/src/rules/method-signature-style.ts @@ -117,29 +117,72 @@ export default util.createRule({ } return { - TSMethodSignature(methodNode): void { - if (mode === 'method') { - return; - } - - const parent = methodNode.parent; - const members = - parent?.type === AST_NODE_TYPES.TSInterfaceBody - ? parent.body - : parent?.type === AST_NODE_TYPES.TSTypeLiteral - ? parent.members - : []; - - const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = - members.filter( - (element): element is TSESTree.TSMethodSignature => - element.type === AST_NODE_TYPES.TSMethodSignature && - element !== methodNode && - getMethodKey(element) === getMethodKey(methodNode), - ); - const isParentModule = isNodeParentModuleDeclaration(methodNode); - - if (duplicatedKeyMethodNodes.length > 0) { + ...(mode === 'property' && { + TSMethodSignature(methodNode): void { + const parent = methodNode.parent; + const members = + parent?.type === AST_NODE_TYPES.TSInterfaceBody + ? parent.body + : parent?.type === AST_NODE_TYPES.TSTypeLiteral + ? parent.members + : []; + + const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = + members.filter( + (element): element is TSESTree.TSMethodSignature => + element.type === AST_NODE_TYPES.TSMethodSignature && + element !== methodNode && + getMethodKey(element) === getMethodKey(methodNode), + ); + const isParentModule = isNodeParentModuleDeclaration(methodNode); + + if (duplicatedKeyMethodNodes.length > 0) { + if (isParentModule) { + context.report({ + node: methodNode, + messageId: 'errorMethod', + }); + } else { + context.report({ + node: methodNode, + messageId: 'errorMethod', + *fix(fixer) { + const methodNodes = [ + methodNode, + ...duplicatedKeyMethodNodes, + ].sort((a, b) => (a.range[0] < b.range[0] ? -1 : 1)); + const typeString = methodNodes + .map(node => { + const params = getMethodParams(node); + const returnType = getMethodReturnType(node); + return `(${params} => ${returnType})`; + }) + .join(' & '); + const key = getMethodKey(methodNode); + const delimiter = getDelimiter(methodNode); + yield fixer.replaceText( + methodNode, + `${key}: ${typeString}${delimiter}`, + ); + for (const node of duplicatedKeyMethodNodes) { + const lastToken = sourceCode.getLastToken(node); + if (lastToken) { + const nextToken = sourceCode.getTokenAfter(lastToken); + if (nextToken) { + yield fixer.remove(node); + yield fixer.replaceTextRange( + [lastToken.range[1], nextToken.range[0]], + '', + ); + } + } + } + }, + }); + } + return; + } + if (isParentModule) { context.report({ node: methodNode, @@ -149,90 +192,43 @@ export default util.createRule({ context.report({ node: methodNode, messageId: 'errorMethod', - *fix(fixer) { - const methodNodes = [ - methodNode, - ...duplicatedKeyMethodNodes, - ].sort((a, b) => (a.range[0] < b.range[0] ? -1 : 1)); - const typeString = methodNodes - .map(node => { - const params = getMethodParams(node); - const returnType = getMethodReturnType(node); - return `(${params} => ${returnType})`; - }) - .join(' & '); + fix: fixer => { const key = getMethodKey(methodNode); + const params = getMethodParams(methodNode); + const returnType = getMethodReturnType(methodNode); const delimiter = getDelimiter(methodNode); - yield fixer.replaceText( + return fixer.replaceText( methodNode, - `${key}: ${typeString}${delimiter}`, + `${key}: ${params} => ${returnType}${delimiter}`, ); - for (const node of duplicatedKeyMethodNodes) { - const lastToken = sourceCode.getLastToken(node); - if (lastToken) { - const nextToken = sourceCode.getTokenAfter(lastToken); - if (nextToken) { - yield fixer.remove(node); - yield fixer.replaceTextRange( - [lastToken.range[1], nextToken.range[0]], - '', - ); - } - } - } }, }); } - return; - } + }, + }), + ...(mode === 'method' && { + TSPropertySignature(propertyNode): void { + const typeNode = propertyNode.typeAnnotation?.typeAnnotation; + if (typeNode?.type !== AST_NODE_TYPES.TSFunctionType) { + return; + } - if (isParentModule) { - context.report({ - node: methodNode, - messageId: 'errorMethod', - }); - } else { context.report({ - node: methodNode, - messageId: 'errorMethod', + node: propertyNode, + messageId: 'errorProperty', fix: fixer => { - const key = getMethodKey(methodNode); - const params = getMethodParams(methodNode); - const returnType = getMethodReturnType(methodNode); - const delimiter = getDelimiter(methodNode); + const key = getMethodKey(propertyNode); + const params = getMethodParams(typeNode); + const returnType = getMethodReturnType(typeNode); + const delimiter = getDelimiter(propertyNode); return fixer.replaceText( - methodNode, - `${key}: ${params} => ${returnType}${delimiter}`, + propertyNode, + `${key}${params}: ${returnType}${delimiter}`, ); }, }); - } - }, - TSPropertySignature(propertyNode): void { - const typeNode = propertyNode.typeAnnotation?.typeAnnotation; - if (typeNode?.type !== AST_NODE_TYPES.TSFunctionType) { - return; - } - - if (mode === 'property') { - return; - } - - context.report({ - node: propertyNode, - messageId: 'errorProperty', - fix: fixer => { - const key = getMethodKey(propertyNode); - const params = getMethodParams(typeNode); - const returnType = getMethodReturnType(typeNode); - const delimiter = getDelimiter(propertyNode); - return fixer.replaceText( - propertyNode, - `${key}${params}: ${returnType}${delimiter}`, - ); - }, - }); - }, + }, + }), }; }, }); diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 0f3b07e9fb27..86055d5c7e0b 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -133,11 +133,11 @@ export default util.createRule({ return { "BinaryExpression[operator='+']": checkPlusOperands, - "AssignmentExpression[operator='+=']"(node): void { - if (checkCompoundAssignments) { + ...(checkCompoundAssignments && { + "AssignmentExpression[operator='+=']"(node): void { checkPlusOperands(node); - } - }, + }, + }), }; }, }); diff --git a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts index af80720c2904..41935ad62d45 100644 --- a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts +++ b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts @@ -240,16 +240,16 @@ export default util.createRule({ } return { - TSIntersectionType(node): void { - if (checkIntersections === true) { + ...(checkIntersections && { + TSIntersectionType(node): void { checkSorting(node); - } - }, - TSUnionType(node): void { - if (checkUnions === true) { + }, + }), + ...(checkUnions && { + TSUnionType(node): void { checkSorting(node); - } - }, + }, + }), }; }, }); diff --git a/packages/eslint-plugin/src/rules/typedef.ts b/packages/eslint-plugin/src/rules/typedef.ts index 766401040224..b7b84206b5b7 100644 --- a/packages/eslint-plugin/src/rules/typedef.ts +++ b/packages/eslint-plugin/src/rules/typedef.ts @@ -1,6 +1,6 @@ import { - TSESTree, AST_NODE_TYPES, + TSESTree, } from '@typescript-eslint/experimental-utils'; import * as util from '../util'; @@ -60,7 +60,21 @@ export default util.createRule<[Options], MessageIds>({ [OptionKeys.VariableDeclarationIgnoreFunction]: false, }, ], - create(context, [options]) { + create( + context, + [ + { + arrayDestructuring, + arrowParameter, + memberVariableDeclaration, + objectDestructuring, + parameter, + propertyDeclaration, + variableDeclaration, + variableDeclarationIgnoreFunction, + }, + ], + ) { function report(location: TSESTree.Node, name?: string): void { context.report({ node: location, @@ -133,87 +147,86 @@ export default util.createRule<[Options], MessageIds>({ function isVariableDeclarationIgnoreFunction(node: TSESTree.Node): boolean { return ( - !!options[OptionKeys.VariableDeclarationIgnoreFunction] && - (node.type === AST_NODE_TYPES.FunctionExpression || - node.type === AST_NODE_TYPES.ArrowFunctionExpression) + node.type === AST_NODE_TYPES.FunctionExpression || + node.type === AST_NODE_TYPES.ArrowFunctionExpression ); } return { - ArrayPattern(node): void { - if ( - node.parent?.type === AST_NODE_TYPES.RestElement && - node.parent.typeAnnotation - ) { - return; - } - if ( - options[OptionKeys.ArrayDestructuring] && - !node.typeAnnotation && - !isForOfStatementContext(node) - ) { - report(node); - } - }, - ArrowFunctionExpression(node): void { - if (options[OptionKeys.ArrowParameter]) { + ...(arrayDestructuring && { + ArrayPattern(node): void { + if ( + node.parent?.type === AST_NODE_TYPES.RestElement && + node.parent.typeAnnotation + ) { + return; + } + if (!node.typeAnnotation && !isForOfStatementContext(node)) { + report(node); + } + }, + }), + ...(arrowParameter && { + ArrowFunctionExpression(node): void { checkParameters(node.params); - } - }, - ClassProperty(node): void { - if (node.value && isVariableDeclarationIgnoreFunction(node.value)) { - return; - } - - if ( - options[OptionKeys.MemberVariableDeclaration] && - !node.typeAnnotation - ) { - report( - node, - node.key.type === AST_NODE_TYPES.Identifier - ? node.key.name - : undefined, - ); - } - }, - 'FunctionDeclaration, FunctionExpression'( - node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, - ): void { - if (options[OptionKeys.Parameter]) { + }, + }), + ...(memberVariableDeclaration && + !variableDeclarationIgnoreFunction && { + ClassProperty(node): void { + if ( + !node.value || + !isVariableDeclarationIgnoreFunction(node.value) || + !node.typeAnnotation + ) { + report( + node, + node.key.type === AST_NODE_TYPES.Identifier + ? node.key.name + : undefined, + ); + } + }, + }), + ...(parameter && { + 'FunctionDeclaration, FunctionExpression'( + node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, + ): void { checkParameters(node.params); - } - }, - ObjectPattern(node): void { - if ( - options[OptionKeys.ObjectDestructuring] && - !node.typeAnnotation && - !isForOfStatementContext(node) - ) { - report(node); - } - }, - 'TSIndexSignature, TSPropertySignature'( - node: TSESTree.TSIndexSignature | TSESTree.TSPropertySignature, - ): void { - if (options[OptionKeys.PropertyDeclaration] && !node.typeAnnotation) { - report( - node, - node.type === AST_NODE_TYPES.TSPropertySignature - ? getNodeName(node.key) - : undefined, - ); - } - }, + }, + }), + ...(objectDestructuring && { + ObjectPattern(node): void { + if (!node.typeAnnotation && !isForOfStatementContext(node)) { + report(node); + } + }, + }), + ...(propertyDeclaration && { + 'TSIndexSignature, TSPropertySignature'( + node: TSESTree.TSIndexSignature | TSESTree.TSPropertySignature, + ): void { + if (!node.typeAnnotation) { + report( + node, + node.type === AST_NODE_TYPES.TSPropertySignature + ? getNodeName(node.key) + : undefined, + ); + } + }, + }), VariableDeclarator(node): void { if ( - !options[OptionKeys.VariableDeclaration] || + !variableDeclaration || node.id.typeAnnotation || (node.id.type === AST_NODE_TYPES.ArrayPattern && - !options[OptionKeys.ArrayDestructuring]) || + !arrayDestructuring) || (node.id.type === AST_NODE_TYPES.ObjectPattern && - !options[OptionKeys.ObjectDestructuring]) || - (node.init && isVariableDeclarationIgnoreFunction(node.init)) + !objectDestructuring) || + (node.init && + variableDeclarationIgnoreFunction && + isVariableDeclarationIgnoreFunction(node.init)) ) { return; }