From afa7900285ad6d31d2acaff0107fbd19bad4ff06 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 25 Jan 2020 15:26:09 +1300 Subject: [PATCH] chore: enable `prefer-ast-types-enum` internal rule (#1514) --- .eslintrc.js | 1 + .../src/rules/prefer-ast-types-enum.ts | 4 +- .../tests/rules/prefer-ast-types-enum.test.ts | 8 ++- .../eslint-plugin/src/rules/ban-ts-comment.ts | 3 +- .../eslint-plugin/src/rules/ban-ts-ignore.ts | 3 +- .../src/rules/indent-new-do-not-use/index.ts | 2 +- .../src/rules/no-inferrable-types.ts | 2 + .../eslint-plugin/src/rules/no-type-alias.ts | 1 + .../src/rules/no-unused-vars-experimental.ts | 2 + .../src/rules/triple-slash-reference.ts | 7 ++- .../tests/rules/ban-types.test.ts | 9 +++ .../tests/rules/indent/indent-eslint.test.ts | 39 ++++++++---- .../tests/rules/indent/indent.test.ts | 63 ++++++++++--------- .../rules/no-unused-vars-experimental.test.ts | 7 +++ .../rules/no-useless-constructor.test.ts | 3 +- .../experimental-utils/src/ts-eslint/Scope.ts | 7 ++- packages/parser/src/parser.ts | 2 +- packages/parser/tests/lib/basics.ts | 9 ++- .../tests/ast-alignment/utils.ts | 14 ++--- .../tests/lib/visitor-keys.ts | 2 +- 20 files changed, 122 insertions(+), 66 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3271a33e83ed..39ca61de5168 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -124,6 +124,7 @@ module.exports = { // Internal repo rules // '@typescript-eslint/internal/no-typescript-default-import': 'error', + '@typescript-eslint/internal/prefer-ast-types-enum': 'error', }, parserOptions: { sourceType: 'module', diff --git a/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts b/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts index 09398ea11dff..8a5ec63bb4eb 100755 --- a/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts +++ b/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts @@ -1,15 +1,15 @@ import { AST_NODE_TYPES, AST_TOKEN_TYPES, - ESLintUtils, TSESTree, } from '@typescript-eslint/experimental-utils'; +import { createRule } from '../util'; const isStringLiteral = ( node: TSESTree.Literal, ): node is TSESTree.StringLiteral => typeof node.value === 'string'; -export = ESLintUtils.RuleCreator(name => name)({ +export default createRule({ name: __filename, meta: { type: 'problem', diff --git a/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts b/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts index 52cab8b69c08..2688e035b59c 100644 --- a/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts +++ b/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts @@ -1,3 +1,7 @@ +import { + AST_NODE_TYPES, + AST_TOKEN_TYPES, +} from '@typescript-eslint/experimental-utils'; import rule from '../../src/rules/prefer-ast-types-enum'; import { RuleTester, batchedSingleLineTests } from '../RuleTester'; @@ -36,12 +40,12 @@ node.type === AST_TOKEN_TYPES.Keyword `, errors: [ { - data: { enumName: 'AST_NODE_TYPES', literal: 'Literal' }, + data: { enumName: 'AST_NODE_TYPES', literal: AST_NODE_TYPES.Literal }, messageId: 'preferEnum', line: 2, }, { - data: { enumName: 'AST_TOKEN_TYPES', literal: 'Keyword' }, + data: { enumName: 'AST_TOKEN_TYPES', literal: AST_TOKEN_TYPES.Keyword }, messageId: 'preferEnum', line: 3, }, diff --git a/packages/eslint-plugin/src/rules/ban-ts-comment.ts b/packages/eslint-plugin/src/rules/ban-ts-comment.ts index 6370ef029f58..54e2394415bb 100644 --- a/packages/eslint-plugin/src/rules/ban-ts-comment.ts +++ b/packages/eslint-plugin/src/rules/ban-ts-comment.ts @@ -1,3 +1,4 @@ +import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils'; import * as util from '../util'; interface Options { @@ -60,7 +61,7 @@ export default util.createRule<[Options], MessageIds>({ const comments = sourceCode.getAllComments(); comments.forEach(comment => { - if (comment.type !== 'Line') { + if (comment.type !== AST_TOKEN_TYPES.Line) { return; } diff --git a/packages/eslint-plugin/src/rules/ban-ts-ignore.ts b/packages/eslint-plugin/src/rules/ban-ts-ignore.ts index a00b4a4f67d0..7551df8d0224 100644 --- a/packages/eslint-plugin/src/rules/ban-ts-ignore.ts +++ b/packages/eslint-plugin/src/rules/ban-ts-ignore.ts @@ -1,3 +1,4 @@ +import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils'; import * as util from '../util'; export default util.createRule({ @@ -27,7 +28,7 @@ export default util.createRule({ const comments = sourceCode.getAllComments(); comments.forEach(comment => { - if (comment.type !== 'Line') { + if (comment.type !== AST_TOKEN_TYPES.Line) { return; } if (tsIgnoreRegExp.test(comment.value)) { diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts index 6427439e7b81..923423dbc146 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts @@ -245,7 +245,7 @@ type MessageIds = 'wrongIndentation'; type AppliedOptions = ExcludeKeys< // slight hack to make interface work with Record RequireKeys, keyof IndentConfig>, - 'VariableDeclarator' + AST_NODE_TYPES.VariableDeclarator > & { VariableDeclarator: 'off' | VariableDeclaratorObj; }; diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 057958da0d4c..898a3294fd7a 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -125,6 +125,7 @@ export default util.createRule({ case AST_NODE_TYPES.TSBooleanKeyword: return ( hasUnaryPrefix(init, '!') || + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum isFunctionCall(init, 'Boolean') || isLiteral(init, 'boolean') ); @@ -146,6 +147,7 @@ export default util.createRule({ case AST_NODE_TYPES.TSStringKeyword: return ( + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum isFunctionCall(init, 'String') || isLiteral(init, 'string') || init.type === AST_NODE_TYPES.TemplateLiteral diff --git a/packages/eslint-plugin/src/rules/no-type-alias.ts b/packages/eslint-plugin/src/rules/no-type-alias.ts index d8e22e9d2196..597127561638 100644 --- a/packages/eslint-plugin/src/rules/no-type-alias.ts +++ b/packages/eslint-plugin/src/rules/no-type-alias.ts @@ -260,6 +260,7 @@ export default util.createRule({ // tuple types checkAndReport(allowTupleTypes!, isTopLevel, type, 'Tuple Types'); } else if ( + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type.node.type.endsWith('Keyword') || aliasTypes.has(type.node.type) ) { diff --git a/packages/eslint-plugin/src/rules/no-unused-vars-experimental.ts b/packages/eslint-plugin/src/rules/no-unused-vars-experimental.ts index 0c1ac212380a..920a23199251 100644 --- a/packages/eslint-plugin/src/rules/no-unused-vars-experimental.ts +++ b/packages/eslint-plugin/src/rules/no-unused-vars-experimental.ts @@ -141,6 +141,7 @@ export default util.createRule({ case ts.SyntaxKind.ImportSpecifier: // a namespace import is NOT used, but the default import is used case ts.SyntaxKind.NamespaceImport: + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum report('Import'); break; @@ -160,6 +161,7 @@ export default util.createRule({ break; case ts.SyntaxKind.PropertyDeclaration: + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum report('Property'); break; diff --git a/packages/eslint-plugin/src/rules/triple-slash-reference.ts b/packages/eslint-plugin/src/rules/triple-slash-reference.ts index 6669fe940bcb..1bb16c229a11 100644 --- a/packages/eslint-plugin/src/rules/triple-slash-reference.ts +++ b/packages/eslint-plugin/src/rules/triple-slash-reference.ts @@ -1,5 +1,8 @@ +import { + AST_TOKEN_TYPES, + TSESTree, +} from '@typescript-eslint/experimental-utils'; import * as util from '../util'; -import { TSESTree } from '@typescript-eslint/experimental-utils'; type Options = [ { @@ -92,7 +95,7 @@ export default util.createRule({ const commentsBefore = sourceCode.getCommentsBefore(programNode); commentsBefore.forEach(comment => { - if (comment.type !== 'Line') { + if (comment.type !== AST_TOKEN_TYPES.Line) { return; } const referenceResult = referenceRegExp.exec(comment.value); diff --git a/packages/eslint-plugin/tests/rules/ban-types.test.ts b/packages/eslint-plugin/tests/rules/ban-types.test.ts index 416c71a1e7a9..bbf2ab29db48 100644 --- a/packages/eslint-plugin/tests/rules/ban-types.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-types.test.ts @@ -96,6 +96,7 @@ ruleTester.run('ban-types', rule, { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -112,6 +113,7 @@ ruleTester.run('ban-types', rule, { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -128,6 +130,7 @@ ruleTester.run('ban-types', rule, { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -172,6 +175,7 @@ class Foo extends Bar implements Baz { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -181,6 +185,7 @@ class Foo extends Bar implements Baz { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -199,6 +204,7 @@ class Foo extends Bar implements Baz { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -223,6 +229,7 @@ class Foo extends Bar implements Baz { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -232,6 +239,7 @@ class Foo extends Bar implements Baz { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, @@ -241,6 +249,7 @@ class Foo extends Bar implements Baz { { messageId: 'bannedTypeMessage', data: { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum name: 'String', customMessage: ' Use string instead.', }, diff --git a/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts b/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts index e27a037074f2..d726a5e2c95f 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts @@ -4586,7 +4586,7 @@ ruleTester.run('indent', rule, { ? bar : baz `, - options: [4, { ignoredNodes: ['ConditionalExpression'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.ConditionalExpression] }], }, { code: unIndent` @@ -4596,7 +4596,7 @@ ruleTester.run('indent', rule, { } } `, - options: [4, { ignoredNodes: ['ClassBody'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.ClassBody] }], }, { code: unIndent` @@ -4608,7 +4608,12 @@ ruleTester.run('indent', rule, { `, options: [ 4, - { ignoredNodes: ['ClassBody', AST_NODE_TYPES.BlockStatement] }, + { + ignoredNodes: [ + AST_NODE_TYPES.ClassBody, + AST_NODE_TYPES.BlockStatement, + ], + }, ], }, { @@ -4630,7 +4635,7 @@ ruleTester.run('indent', rule, { foo .bar `, - options: [4, { ignoredNodes: ['MemberExpression'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.MemberExpression] }], }, { code: unIndent` @@ -4655,7 +4660,7 @@ ruleTester.run('indent', rule, { `, - options: [4, { ignoredNodes: ['JSXOpeningElement'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.JSXOpeningElement] }], }, { code: unIndent` @@ -4666,7 +4671,12 @@ ruleTester.run('indent', rule, { `, options: [ 4, - { ignoredNodes: ['JSXElement', AST_NODE_TYPES.JSXOpeningElement] }, + { + ignoredNodes: [ + AST_NODE_TYPES.JSXElement, + AST_NODE_TYPES.JSXOpeningElement, + ], + }, ], }, { @@ -4694,7 +4704,7 @@ ruleTester.run('indent', rule, { valueIfFalse ); `, - options: [4, { ignoredNodes: ['ConditionalExpression'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.ConditionalExpression] }], }, { code: unIndent` @@ -4722,7 +4732,7 @@ ruleTester.run('indent', rule, { ? qux : boop `, - options: [4, { ignoredNodes: ['ConditionalExpression'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.ConditionalExpression] }], }, { code: unIndent` @@ -4733,7 +4743,7 @@ ruleTester.run('indent', rule, { } FROM THE_DATABASE \` `, - options: [4, { ignoredNodes: ['TemplateLiteral'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.TemplateLiteral] }], }, { code: unIndent` @@ -4743,7 +4753,7 @@ ruleTester.run('indent', rule, { Text `, - options: [4, { ignoredNodes: ['JSXOpeningElement'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.JSXOpeningElement] }], }, { code: unIndent` @@ -4760,7 +4770,7 @@ ruleTester.run('indent', rule, { y = 2; var z; `, - options: ['tab', { ignoredNodes: ['VariableDeclarator'] }], + options: ['tab', { ignoredNodes: [AST_NODE_TYPES.VariableDeclarator] }], }, { code: unIndent` @@ -4771,7 +4781,10 @@ ruleTester.run('indent', rule, { `, options: [ 'tab', - { ArrayExpression: 'first', ignoredNodes: ['CallExpression'] }, + { + ArrayExpression: 'first', + ignoredNodes: [AST_NODE_TYPES.CallExpression], + }, ], }, { @@ -9453,7 +9466,7 @@ ruleTester.run('indent', rule, { } } `, - options: [4, { ignoredNodes: ['ClassBody'] }], + options: [4, { ignoredNodes: [AST_NODE_TYPES.ClassBody] }], errors: expectedErrors([3, 4, 0, AST_TOKEN_TYPES.Identifier]), }, { diff --git a/packages/eslint-plugin/tests/rules/indent/indent.test.ts b/packages/eslint-plugin/tests/rules/indent/indent.test.ts index fca5629655b7..fe88d3de17a0 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent.test.ts @@ -1,4 +1,7 @@ -import { TSESLint } from '@typescript-eslint/experimental-utils'; +import { + AST_NODE_TYPES, + TSESLint, +} from '@typescript-eslint/experimental-utils'; import { RuleTester } from '../../RuleTester'; import rule from '../../../src/rules/indent'; import { @@ -18,7 +21,7 @@ function nonTsTestCase(example: TemplateStringsArray): string { const individualNodeTests = [ { - node: 'ClassDeclaration', + node: AST_NODE_TYPES.ClassDeclaration, code: [ ` abstract class Foo { @@ -31,7 +34,7 @@ abstract class Foo { ], }, { - node: 'TSAbstractClassProperty', + node: AST_NODE_TYPES.TSAbstractClassProperty, code: [ ` class Foo { @@ -45,7 +48,7 @@ class Foo { ], }, { - node: 'TSAbstractMethodDefinition', + node: AST_NODE_TYPES.TSAbstractMethodDefinition, code: [ ` class Foo { @@ -59,7 +62,7 @@ class Foo { ], }, { - node: 'TSArrayType', + node: AST_NODE_TYPES.TSArrayType, code: [ ` type foo = ArrType[]; @@ -67,7 +70,7 @@ type foo = ArrType[]; ], }, { - node: 'TSAsExpression', + node: AST_NODE_TYPES.TSAsExpression, code: [ ` const foo = {} as { @@ -92,7 +95,7 @@ const foo = {} as ], }, { - node: 'TSConditionalType', + node: AST_NODE_TYPES.TSConditionalType, code: [ nonTsTestCase` const Foo = T @@ -129,7 +132,7 @@ type Foo = T extends string ? { ], }, { - node: 'TSConstructorType', + node: AST_NODE_TYPES.TSConstructorType, code: [ ` type Constructor = new ( @@ -153,7 +156,7 @@ interface Foo { ], }, { - node: 'TSDeclareFunction', + node: AST_NODE_TYPES.TSDeclareFunction, code: [ ` declare function foo() : { @@ -164,7 +167,7 @@ declare function foo() : { ], }, { - node: 'TSEmptyBodyFunctionExpression', + node: AST_NODE_TYPES.TSEmptyBodyFunctionExpression, code: [ ` class Foo { @@ -190,7 +193,7 @@ enum Foo { ], }, { - node: 'TSExportAssignment', + node: AST_NODE_TYPES.TSExportAssignment, code: [ ` export = { @@ -201,7 +204,7 @@ export = { ], }, { - node: 'TSFunctionType', + node: AST_NODE_TYPES.TSFunctionType, code: [ ` const foo: () => void = () => ({ @@ -242,7 +245,7 @@ const foo: ({ ], }, { - node: 'TSImportType', + node: AST_NODE_TYPES.TSImportType, code: [ ` const foo: import("bar") = { @@ -261,7 +264,7 @@ const foo: import( ], }, { - node: 'TSIndexedAccessType', + node: AST_NODE_TYPES.TSIndexedAccessType, code: [ nonTsTestCase` const Foo = Bar[ @@ -276,7 +279,7 @@ type Foo = Bar[ ], }, { - node: 'TSIndexSignature', + node: AST_NODE_TYPES.TSIndexSignature, code: [ ` type Foo = { @@ -289,7 +292,7 @@ type Foo = { ], }, { - node: 'TSInferType', + node: AST_NODE_TYPES.TSInferType, code: [ ` type Foo = T extends string @@ -315,7 +318,7 @@ interface Foo { ], }, { - node: 'TSInterfaceHeritage', + node: AST_NODE_TYPES.TSInterfaceHeritage, code: [ ` interface Foo extends Bar { @@ -329,7 +332,7 @@ interface Foo extends Bar { ], }, { - node: 'TSIntersectionType', + node: AST_NODE_TYPES.TSIntersectionType, code: [ ` type Foo = "string" & { @@ -355,7 +358,7 @@ import foo = require( }, // TSLiteralType { - node: 'TSMappedType', + node: AST_NODE_TYPES.TSMappedType, code: [ ` type Partial = { @@ -383,7 +386,7 @@ type Partial = { ], }, { - node: 'TSMethodSignature', + node: AST_NODE_TYPES.TSMethodSignature, code: [ ` interface Foo { @@ -411,7 +414,7 @@ declare module "foo" { ], }, { - node: 'TSNonNullExpression', + node: AST_NODE_TYPES.TSNonNullExpression, code: [ nonTsTestCase` const foo = a @@ -426,7 +429,7 @@ const foo = a! ], }, { - node: 'TSParameterProperty', + node: AST_NODE_TYPES.TSParameterProperty, code: [ ` class Foo { @@ -444,7 +447,7 @@ class Foo { ], }, { - node: 'TSParenthesizedType', + node: AST_NODE_TYPES.TSParenthesizedType, code: [ ` const x: Array<( @@ -468,7 +471,7 @@ const x: Array<( }, // TSPlusToken - tested in TSMappedType { - node: 'TSPropertySignature', + node: AST_NODE_TYPES.TSPropertySignature, code: [ ` interface Foo { @@ -482,7 +485,7 @@ interface Foo { ], }, { - node: 'TSQualifiedName', + node: AST_NODE_TYPES.TSQualifiedName, code: [ ` const a: Foo.bar = { @@ -510,7 +513,7 @@ const a: Foo. }, // TSQuestionToken - tested in TSMappedType { - node: 'TSRestType', + node: AST_NODE_TYPES.TSRestType, code: [ ` type foo = [ @@ -521,7 +524,7 @@ type foo = [ ], }, { - node: 'TSThisType', + node: AST_NODE_TYPES.TSThisType, code: [ ` declare class MyArray extends Array { @@ -534,7 +537,7 @@ declare class MyArray extends Array { ], }, { - node: 'TSTupleType', + node: AST_NODE_TYPES.TSTupleType, code: [ nonTsTestCase` const foo = [ @@ -569,7 +572,7 @@ type foo = [ // TSTypeAnnotation - tested in everything.. // TSTypeLiteral - tested in everything.. { - node: 'TSTypeOperator', + node: AST_NODE_TYPES.TSTypeOperator, code: [ ` type T = keyof { @@ -600,7 +603,7 @@ function foo< }, // TSTypeReference - tested in everything.. { - node: 'TSUnionType', + node: AST_NODE_TYPES.TSUnionType, code: [ ` type Foo = string | { diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars-experimental.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars-experimental.test.ts index 6cb2a24e5a3a..335b3986437e 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars-experimental.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars-experimental.test.ts @@ -543,6 +543,7 @@ export class Foo { messageId: 'unusedWithIgnorePattern', data: { name: 'foo', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Property', pattern: DEFAULT_IGNORED_REGEX, }, @@ -970,6 +971,7 @@ export function foo([[a]], used) { messageId: 'unusedWithIgnorePattern', data: { name: 'foo', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Import', pattern: DEFAULT_IGNORED_REGEX, }, @@ -1044,6 +1046,7 @@ console.log(named); messageId: 'unusedWithIgnorePattern', data: { name: 'defaultImp', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Import', pattern: DEFAULT_IGNORED_REGEX, }, @@ -1063,6 +1066,7 @@ console.log(named); messageId: 'unusedWithIgnorePattern', data: { name: 'defaultImp', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Import', pattern: DEFAULT_IGNORED_REGEX, }, @@ -1082,6 +1086,7 @@ console.log(defaultImp); messageId: 'unusedWithIgnorePattern', data: { name: 'named', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Import', pattern: DEFAULT_IGNORED_REGEX, }, @@ -1101,6 +1106,7 @@ console.log(defaultImp); messageId: 'unusedWithIgnorePattern', data: { name: 'named', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Import', pattern: DEFAULT_IGNORED_REGEX, }, @@ -1120,6 +1126,7 @@ console.log(named1); messageId: 'unusedWithIgnorePattern', data: { name: 'named2', + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum type: 'Import', pattern: DEFAULT_IGNORED_REGEX, }, diff --git a/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts b/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts index 2a5d97a4cf88..49b362f4d9f7 100644 --- a/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts +++ b/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts @@ -1,3 +1,4 @@ +import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; import rule from '../../src/rules/no-useless-constructor'; import { RuleTester } from '../RuleTester'; @@ -13,7 +14,7 @@ const ruleTester = new RuleTester({ // eslint-disable-next-line @typescript-eslint/no-explicit-any const error: any = { message: 'Useless constructor.', - type: 'MethodDefinition', + type: AST_NODE_TYPES.MethodDefinition, }; ruleTester.run('no-useless-constructor', rule, { diff --git a/packages/experimental-utils/src/ts-eslint/Scope.ts b/packages/experimental-utils/src/ts-eslint/Scope.ts index 6a85042f2b89..03c6bc663952 100644 --- a/packages/experimental-utils/src/ts-eslint/Scope.ts +++ b/packages/experimental-utils/src/ts-eslint/Scope.ts @@ -65,7 +65,12 @@ namespace Scope { } export type DefinitionType = - | { type: 'CatchClause'; node: TSESTree.CatchClause; parent: null } + | { + // eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum + type: 'CatchClause'; + node: TSESTree.CatchClause; + parent: null; + } | { type: 'ClassName'; node: TSESTree.ClassDeclaration | TSESTree.ClassExpression; diff --git a/packages/parser/src/parser.ts b/packages/parser/src/parser.ts index 46878a9cdea7..86469cea2552 100644 --- a/packages/parser/src/parser.ts +++ b/packages/parser/src/parser.ts @@ -99,7 +99,7 @@ export function parseForESLint( enter(node) { switch (node.type) { // Function#body cannot be null in ESTree spec. - case 'FunctionExpression': + case AST_NODE_TYPES.FunctionExpression: if (!node.body) { // eslint-disable-next-line @typescript-eslint/no-explicit-any node.type = `TSEmptyBody${node.type}` as any; diff --git a/packages/parser/tests/lib/basics.ts b/packages/parser/tests/lib/basics.ts index c06e8e57be12..b1ab1d9a96a8 100644 --- a/packages/parser/tests/lib/basics.ts +++ b/packages/parser/tests/lib/basics.ts @@ -1,4 +1,7 @@ -import { TSESLint } from '@typescript-eslint/experimental-utils'; +import { + AST_NODE_TYPES, + TSESLint, +} from '@typescript-eslint/experimental-utils'; import fs from 'fs'; import glob from 'glob'; import * as parser from '../../src/parser'; @@ -61,7 +64,7 @@ export const Price: React.SFC = function Price(props) {} endLine: 2, line: 2, message: 'called on React.SFC', - nodeType: 'TSTypeReference', + nodeType: AST_NODE_TYPES.TSTypeReference, ruleId: 'test', severity: 2, }, @@ -71,7 +74,7 @@ export const Price: React.SFC = function Price(props) {} endLine: 2, line: 2, message: 'called on PriceProps', - nodeType: 'TSTypeReference', + nodeType: AST_NODE_TYPES.TSTypeReference, ruleId: 'test', severity: 2, }, diff --git a/packages/typescript-estree/tests/ast-alignment/utils.ts b/packages/typescript-estree/tests/ast-alignment/utils.ts index 1ad1ba3bba17..002a4c66dcbd 100644 --- a/packages/typescript-estree/tests/ast-alignment/utils.ts +++ b/packages/typescript-estree/tests/ast-alignment/utils.ts @@ -157,7 +157,7 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any { * ts-estree: TSAbstractClassProperty */ if (node.abstract) { - node.type = 'TSAbstractClassProperty'; + node.type = AST_NODE_TYPES.TSAbstractClassProperty; delete node.abstract; } /** @@ -170,20 +170,20 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any { } }, TSExpressionWithTypeArguments(node, parent: any) { - if (parent.type === 'TSInterfaceDeclaration') { - node.type = 'TSInterfaceHeritage'; + if (parent.type === AST_NODE_TYPES.TSInterfaceDeclaration) { + node.type = AST_NODE_TYPES.TSInterfaceHeritage; } else if ( - parent.type === 'ClassExpression' || - parent.type === 'ClassDeclaration' + parent.type === AST_NODE_TYPES.ClassExpression || + parent.type === AST_NODE_TYPES.ClassDeclaration ) { - node.type = 'TSClassImplements'; + node.type = AST_NODE_TYPES.TSClassImplements; } }, /** * @see https://github.com/prettier/prettier/issues/5817 */ FunctionExpression(node: any, parent: any) { - if (parent.typeParameters && parent.type === 'Property') { + if (parent.typeParameters && parent.type === AST_NODE_TYPES.Property) { node.typeParameters = parent.typeParameters; delete parent.typeParameters; } diff --git a/packages/typescript-estree/tests/lib/visitor-keys.ts b/packages/typescript-estree/tests/lib/visitor-keys.ts index 6d16e90d00bf..aceb5b75eb5c 100644 --- a/packages/typescript-estree/tests/lib/visitor-keys.ts +++ b/packages/typescript-estree/tests/lib/visitor-keys.ts @@ -6,7 +6,7 @@ import { visitorKeys } from '../../src/visitor-keys'; //------------------------------------------------------------------------------ const astTypes = Object.keys(AST_NODE_TYPES); -astTypes.push('TSEmptyBodyFunctionExpression'); // node created by parser.ts +astTypes.push(AST_NODE_TYPES.TSEmptyBodyFunctionExpression); // node created by parser.ts //------------------------------------------------------------------------------ // Tests