Skip to content

chore: enable prefer-ast-types-enum internal rule #1514

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

Merged
merged 12 commits into from
Jan 25, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 },
Copy link
Contributor Author

@G-Rath G-Rath Jan 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah haha hoisted by my own petard!

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,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/ban-ts-comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

interface Options {
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/ban-ts-ignore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

export default util.createRule({
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ type MessageIds = 'wrongIndentation';
type AppliedOptions = ExcludeKeys<
// slight hack to make interface work with Record<string, unknown>
RequireKeys<Pick<IndentConfig, keyof IndentConfig>, keyof IndentConfig>,
'VariableDeclarator'
AST_NODE_TYPES.VariableDeclarator
> & {
VariableDeclarator: 'off' | VariableDeclaratorObj;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/no-inferrable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default util.createRule<Options, MessageIds>({
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')
);
Expand All @@ -146,6 +147,7 @@ export default util.createRule<Options, MessageIds>({

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
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export default util.createRule<Options, MessageIds>({
// 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)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export default util.createRule<Options, MessageIds>({
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;

Expand All @@ -160,6 +161,7 @@ export default util.createRule<Options, MessageIds>({
break;

case ts.SyntaxKind.PropertyDeclaration:
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
report('Property');
break;

Expand Down
7 changes: 5 additions & 2 deletions packages/eslint-plugin/src/rules/triple-slash-reference.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand Down Expand Up @@ -92,7 +95,7 @@ export default util.createRule<Options, MessageIds>({
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);
Expand Down
9 changes: 9 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
},
Expand All @@ -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.',
},
Expand All @@ -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.',
},
Expand Down Expand Up @@ -172,6 +175,7 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
{
messageId: 'bannedTypeMessage',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
name: 'String',
customMessage: ' Use string instead.',
},
Expand All @@ -181,6 +185,7 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
{
messageId: 'bannedTypeMessage',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
name: 'String',
customMessage: ' Use string instead.',
},
Expand All @@ -199,6 +204,7 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
{
messageId: 'bannedTypeMessage',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
name: 'String',
customMessage: ' Use string instead.',
},
Expand All @@ -223,6 +229,7 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
{
messageId: 'bannedTypeMessage',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
name: 'String',
customMessage: ' Use string instead.',
},
Expand All @@ -232,6 +239,7 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
{
messageId: 'bannedTypeMessage',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
name: 'String',
customMessage: ' Use string instead.',
},
Expand All @@ -241,6 +249,7 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
{
messageId: 'bannedTypeMessage',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
name: 'String',
customMessage: ' Use string instead.',
},
Expand Down
39 changes: 26 additions & 13 deletions packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4586,7 +4586,7 @@ ruleTester.run('indent', rule, {
? bar
: baz
`,
options: [4, { ignoredNodes: ['ConditionalExpression'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.ConditionalExpression] }],
},
{
code: unIndent`
Expand All @@ -4596,7 +4596,7 @@ ruleTester.run('indent', rule, {
}
}
`,
options: [4, { ignoredNodes: ['ClassBody'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.ClassBody] }],
},
{
code: unIndent`
Expand All @@ -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,
],
},
],
},
{
Expand All @@ -4630,7 +4635,7 @@ ruleTester.run('indent', rule, {
foo
.bar
`,
options: [4, { ignoredNodes: ['MemberExpression'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.MemberExpression] }],
},
{
code: unIndent`
Expand All @@ -4655,7 +4660,7 @@ ruleTester.run('indent', rule, {
<Foo
bar="1" />
`,
options: [4, { ignoredNodes: ['JSXOpeningElement'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.JSXOpeningElement] }],
},
{
code: unIndent`
Expand All @@ -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,
],
},
],
},
{
Expand Down Expand Up @@ -4694,7 +4704,7 @@ ruleTester.run('indent', rule, {
valueIfFalse
);
`,
options: [4, { ignoredNodes: ['ConditionalExpression'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.ConditionalExpression] }],
},
{
code: unIndent`
Expand Down Expand Up @@ -4722,7 +4732,7 @@ ruleTester.run('indent', rule, {
? qux
: boop
`,
options: [4, { ignoredNodes: ['ConditionalExpression'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.ConditionalExpression] }],
},
{
code: unIndent`
Expand All @@ -4733,7 +4743,7 @@ ruleTester.run('indent', rule, {
} FROM THE_DATABASE
\`
`,
options: [4, { ignoredNodes: ['TemplateLiteral'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.TemplateLiteral] }],
},
{
code: unIndent`
Expand All @@ -4743,7 +4753,7 @@ ruleTester.run('indent', rule, {
Text
</foo>
`,
options: [4, { ignoredNodes: ['JSXOpeningElement'] }],
options: [4, { ignoredNodes: [AST_NODE_TYPES.JSXOpeningElement] }],
},
{
code: unIndent`
Expand All @@ -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`
Expand All @@ -4771,7 +4781,10 @@ ruleTester.run('indent', rule, {
`,
options: [
'tab',
{ ArrayExpression: 'first', ignoredNodes: ['CallExpression'] },
{
ArrayExpression: 'first',
ignoredNodes: [AST_NODE_TYPES.CallExpression],
},
],
},
{
Expand Down Expand Up @@ -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]),
},
{
Expand Down
Loading