Skip to content
Permalink
Browse files

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

  • Loading branch information
G-Rath authored and bradzacher committed Jan 25, 2020
1 parent c3d0a3a commit afa7900285ad6d31d2acaff0107fbd19bad4ff06
@@ -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',
@@ -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',
@@ -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,
},
@@ -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;
}

@@ -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)) {
@@ -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;
};
@@ -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')
);
@@ -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
@@ -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)
) {
@@ -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;

@@ -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;

@@ -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<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);
@@ -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<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.',
},
@@ -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.',
},
@@ -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.',
},
@@ -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.',
},
@@ -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.',
},
@@ -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.',
},
@@ -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, {
<Foo
bar="1" />
`,
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
</foo>
`,
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]),
},
{

0 comments on commit afa7900

Please sign in to comment.
You can’t perform that action at this time.