Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): use AST_NODE_TYPES instead of strings in rules #1366

Merged
merged 2 commits into from Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,8 @@
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
import {
AST_TOKEN_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';

export default util.createRule({
Expand Down Expand Up @@ -53,7 +57,7 @@ export default util.createRule({
const afterToken = sourceCode.getTokenAfter(node.typeAnnotation);
if (
afterToken &&
afterToken.type === 'Punctuator' &&
afterToken.type === AST_TOKEN_TYPES.Punctuator &&
afterToken.value === ';'
) {
fixes.push(fixer.remove(afterToken));
Expand Down
38 changes: 21 additions & 17 deletions packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts
Expand Up @@ -4,21 +4,21 @@

import {
AST_NODE_TYPES,
TSESTree,
AST_TOKEN_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
isOpeningParenToken,
isClosingParenToken,
isNotOpeningParenToken,
isSemicolonToken,
isClosingBracketToken,
isClosingBraceToken,
isOpeningBraceToken,
isNotClosingParenToken,
isClosingBracketToken,
isClosingParenToken,
isColonToken,
isCommentToken,
isNotClosingParenToken,
isNotOpeningParenToken,
isOpeningBraceToken,
isOpeningParenToken,
isSemicolonToken,
} from 'eslint-utils';
import { TokenOrComment } from './BinarySearchTree';
import { OffsetStorage } from './OffsetStorage';
Expand Down Expand Up @@ -523,7 +523,7 @@ export default createRule<Options, MessageIds>({
*/
if (
!node.parent ||
node.parent.type !== 'CallExpression' ||
node.parent.type !== AST_NODE_TYPES.CallExpression ||
node.parent.callee !== node
) {
return false;
Expand Down Expand Up @@ -659,7 +659,7 @@ export default createRule<Options, MessageIds>({
* Scenarios are for or while statements without braces around them
*/
function addBlocklessNodeIndent(node: TSESTree.Node): void {
if (node.type !== 'BlockStatement') {
if (node.type !== AST_NODE_TYPES.BlockStatement) {
const lastParentToken = sourceCode.getTokenBefore(
node,
isNotOpeningParenToken,
Expand Down Expand Up @@ -692,7 +692,7 @@ export default createRule<Options, MessageIds>({

if (
lastToken &&
node.type !== 'EmptyStatement' &&
node.type !== AST_NODE_TYPES.EmptyStatement &&
isSemicolonToken(lastToken)
) {
offsets.setDesiredOffset(lastToken, lastParentToken, 0);
Expand Down Expand Up @@ -1127,15 +1127,18 @@ export default createRule<Options, MessageIds>({

IfStatement(node) {
addBlocklessNodeIndent(node.consequent);
if (node.alternate && node.alternate.type !== 'IfStatement') {
if (
node.alternate &&
node.alternate.type !== AST_NODE_TYPES.IfStatement
) {
addBlocklessNodeIndent(node.alternate);
}
},

ImportDeclaration(node) {
if (
node.specifiers.some(
specifier => specifier.type === 'ImportSpecifier',
specifier => specifier.type === AST_NODE_TYPES.ImportSpecifier,
)
) {
const openingCurly = sourceCode.getFirstToken(
Expand All @@ -1149,7 +1152,7 @@ export default createRule<Options, MessageIds>({

addElementListIndent(
node.specifiers.filter(
specifier => specifier.type === 'ImportSpecifier',
specifier => specifier.type === AST_NODE_TYPES.ImportSpecifier,
),
openingCurly,
closingCurly,
Expand All @@ -1159,11 +1162,12 @@ export default createRule<Options, MessageIds>({

const fromToken = sourceCode.getLastToken(
node,
token => token.type === 'Identifier' && token.value === 'from',
token =>
token.type === AST_TOKEN_TYPES.Identifier && token.value === 'from',
)!;
const sourceToken = sourceCode.getLastToken(
node,
token => token.type === 'String',
token => token.type === AST_TOKEN_TYPES.String,
)!;
const semiToken = sourceCode.getLastToken(
node,
Expand Down Expand Up @@ -1345,7 +1349,7 @@ export default createRule<Options, MessageIds>({
if (
!(
node.consequent.length === 1 &&
node.consequent[0].type === 'BlockStatement'
node.consequent[0].type === AST_NODE_TYPES.BlockStatement
)
) {
const caseKeyword = sourceCode.getFirstToken(node)!;
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Expand Up @@ -287,8 +287,8 @@ export default util.createRule<Options, MessageIds>({
}

const abstract =
node.type === 'TSAbstractClassProperty' ||
node.type === 'TSAbstractMethodDefinition';
node.type === AST_NODE_TYPES.TSAbstractClassProperty ||
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition;

const scope =
'static' in node && node.static
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-empty-function.ts
@@ -1,6 +1,6 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/no-empty-function';
import * as util from '../util';
Expand Down Expand Up @@ -99,7 +99,7 @@ export default util.createRule<Options, MessageIds>({
const parent = node.parent;
if (
isBodyEmpty(node) &&
parent?.type === 'MethodDefinition' &&
parent?.type === AST_NODE_TYPES.MethodDefinition &&
parent.kind === 'constructor'
) {
const { accessibility } = parent;
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-inferrable-types.ts
@@ -1,6 +1,6 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';

Expand Down Expand Up @@ -164,7 +164,7 @@ export default util.createRule<Options, MessageIds>({
init.value instanceof RegExp;
const isRegExpNewCall =
init.type === AST_NODE_TYPES.NewExpression &&
init.callee.type === 'Identifier' &&
init.callee.type === AST_NODE_TYPES.Identifier &&
init.callee.name === 'RegExp';
const isRegExpCall = isFunctionCall(init, 'RegExp');

Expand Down
9 changes: 6 additions & 3 deletions packages/eslint-plugin/src/rules/no-unnecessary-qualifier.ts
@@ -1,4 +1,7 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import ts from 'typescript';
import * as tsutils from 'tsutils';
import * as util from '../util';
Expand Down Expand Up @@ -146,12 +149,12 @@ export default util.createRule({
function isPropertyAccessExpression(
node: TSESTree.Node,
): node is TSESTree.MemberExpression {
return node.type === 'MemberExpression' && !node.computed;
return node.type === AST_NODE_TYPES.MemberExpression && !node.computed;
}

function isEntityNameExpression(node: TSESTree.Node): boolean {
return (
node.type === 'Identifier' ||
node.type === AST_NODE_TYPES.Identifier ||
(isPropertyAccessExpression(node) &&
isEntityNameExpression(node.object))
);
Expand Down
16 changes: 8 additions & 8 deletions packages/eslint-plugin/src/rules/prefer-includes.ts
@@ -1,6 +1,6 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { getStaticValue } from 'eslint-utils';
import { AST as RegExpAST, parseRegExpLiteral } from 'regexpp';
Expand Down Expand Up @@ -204,13 +204,13 @@ export default createRule({
*fix(fixer) {
const argNode = callNode.arguments[0];
const needsParen =
argNode.type !== 'Literal' &&
argNode.type !== 'TemplateLiteral' &&
argNode.type !== 'Identifier' &&
argNode.type !== 'MemberExpression' &&
argNode.type !== 'OptionalMemberExpression' &&
argNode.type !== 'CallExpression' &&
argNode.type !== 'OptionalCallExpression';
argNode.type !== AST_NODE_TYPES.Literal &&
argNode.type !== AST_NODE_TYPES.TemplateLiteral &&
argNode.type !== AST_NODE_TYPES.Identifier &&
argNode.type !== AST_NODE_TYPES.MemberExpression &&
argNode.type !== AST_NODE_TYPES.OptionalMemberExpression &&
argNode.type !== AST_NODE_TYPES.CallExpression &&
argNode.type !== AST_NODE_TYPES.OptionalCallExpression;

yield fixer.removeRange([callNode.range[0], argNode.range[0]]);
if (needsParen) {
Expand Down
26 changes: 18 additions & 8 deletions packages/eslint-plugin/src/rules/prefer-readonly.ts
Expand Up @@ -16,12 +16,10 @@ type Options = [
];

const functionScopeBoundaries = [
'ArrowFunctionExpression',
'FunctionDeclaration',
'FunctionExpression',
'GetAccessor',
'MethodDefinition',
'SetAccessor',
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.MethodDefinition,
].join(', ');

export default util.createRule<Options, MessageIds>({
Expand Down Expand Up @@ -221,7 +219,13 @@ export default util.createRule<Options, MessageIds>({
);
}
},
[functionScopeBoundaries](node: TSESTree.Node): void {
[functionScopeBoundaries](
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.MethodDefinition,
): void {
if (isConstructor(node)) {
classScopeStack[classScopeStack.length - 1].enterConstructor(
parserServices.esTreeNodeToTSNodeMap.get<ts.ConstructorDeclaration>(
Expand All @@ -232,7 +236,13 @@ export default util.createRule<Options, MessageIds>({
classScopeStack[classScopeStack.length - 1].enterNonConstructor();
}
},
[`${functionScopeBoundaries}:exit`](node: TSESTree.Node): void {
[`${functionScopeBoundaries}:exit`](
armano2 marked this conversation as resolved.
Show resolved Hide resolved
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.MethodDefinition,
): void {
if (isConstructor(node)) {
classScopeStack[classScopeStack.length - 1].exitConstructor();
} else if (isFunctionScopeBoundaryInStack(node)) {
Expand Down
39 changes: 22 additions & 17 deletions packages/eslint-plugin/src/rules/prefer-string-starts-ends-with.ts
@@ -1,10 +1,14 @@
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
import {
isNotClosingParenToken,
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
getPropertyName,
getStaticValue,
isNotClosingParenToken,
} from 'eslint-utils';
import { RegExpParser, AST as RegExpAST } from 'regexpp';
import { AST as RegExpAST, RegExpParser } from 'regexpp';
import { createRule, getParserServices, getTypeName } from '../util';

const EQ_OPERATORS = /^[=!]=/;
Expand Down Expand Up @@ -94,7 +98,8 @@ export default createRule({
node: TSESTree.Node,
): node is TSESTree.BinaryExpression {
return (
node.type === 'BinaryExpression' && EQ_OPERATORS.test(node.operator)
node.type === AST_NODE_TYPES.BinaryExpression &&
EQ_OPERATORS.test(node.operator)
);
}

Expand Down Expand Up @@ -138,7 +143,7 @@ export default createRule({
node: TSESTree.Node,
expectedObjectNode: TSESTree.Node,
): boolean {
if (node.type === 'MemberExpression') {
if (node.type === AST_NODE_TYPES.MemberExpression) {
return (
getPropertyName(node, globalScope) === 'length' &&
isSameTokens(node.object, expectedObjectNode)
Expand Down Expand Up @@ -169,7 +174,7 @@ export default createRule({
expectedObjectNode: TSESTree.Node,
): boolean {
return (
node.type === 'BinaryExpression' &&
node.type === AST_NODE_TYPES.BinaryExpression &&
node.operator === '-' &&
isLengthExpression(node.left, expectedObjectNode) &&
isNumber(node.right, 1)
Expand Down Expand Up @@ -273,7 +278,7 @@ export default createRule({
negative: boolean,
): IterableIterator<TSESLint.RuleFix> {
// left is CallExpression or MemberExpression.
const leftNode = (node.left.type === 'CallExpression'
const leftNode = (node.left.type === AST_NODE_TYPES.CallExpression
? node.left.callee
: node.left) as TSESTree.MemberExpression;
const propertyRange = getPropertyRange(leftNode);
Expand Down Expand Up @@ -326,7 +331,7 @@ export default createRule({
])](node: TSESTree.MemberExpression): void {
let parentNode = node.parent!;
let indexNode: TSESTree.Node | null = null;
if (parentNode.type === 'CallExpression') {
if (parentNode.type === AST_NODE_TYPES.CallExpression) {
if (parentNode.arguments.length === 1) {
indexNode = parentNode.arguments[0];
}
Expand Down Expand Up @@ -411,7 +416,7 @@ export default createRule({
callNode.arguments.length !== 1 ||
!isEqualityComparison(parentNode) ||
parentNode.left !== callNode ||
parentNode.right.type !== 'BinaryExpression' ||
parentNode.right.type !== AST_NODE_TYPES.BinaryExpression ||
parentNode.right.operator !== '-' ||
!isLengthExpression(parentNode.right.left, node.object) ||
!isLengthExpression(parentNode.right.right, callNode.arguments[0]) ||
Expand Down Expand Up @@ -520,7 +525,7 @@ export default createRule({
// Don't fix if it can change the behavior.
if (
eqNode.operator.length === 2 &&
(eqNode.right.type !== 'Literal' ||
(eqNode.right.type !== AST_NODE_TYPES.Literal ||
typeof eqNode.right.value !== 'string')
) {
return null;
Expand All @@ -532,12 +537,12 @@ export default createRule({
} else {
const posNode = callNode.arguments[0];
const posNodeIsAbsolutelyValid =
(posNode.type === 'BinaryExpression' &&
(posNode.type === AST_NODE_TYPES.BinaryExpression &&
posNode.operator === '-' &&
isLengthExpression(posNode.left, node.object) &&
isLengthExpression(posNode.right, eqNode.right)) ||
(negativeIndexSupported &&
posNode.type === 'UnaryExpression' &&
posNode.type === AST_NODE_TYPES.UnaryExpression &&
posNode.operator === '-' &&
isLengthExpression(posNode.argument, eqNode.right));
if (!posNodeIsAbsolutelyValid) {
Expand Down Expand Up @@ -576,11 +581,11 @@ export default createRule({
*fix(fixer) {
const argNode = callNode.arguments[0];
const needsParen =
argNode.type !== 'Literal' &&
argNode.type !== 'TemplateLiteral' &&
argNode.type !== 'Identifier' &&
argNode.type !== 'MemberExpression' &&
argNode.type !== 'CallExpression';
argNode.type !== AST_NODE_TYPES.Literal &&
argNode.type !== AST_NODE_TYPES.TemplateLiteral &&
argNode.type !== AST_NODE_TYPES.Identifier &&
argNode.type !== AST_NODE_TYPES.MemberExpression &&
argNode.type !== AST_NODE_TYPES.CallExpression;

yield fixer.removeRange([callNode.range[0], argNode.range[0]]);
if (needsParen) {
Expand Down