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

feat(eslint-plugin-internal): [prefer-ast-types-enum] add DefinitionType enum #3916

Merged
merged 2 commits into from Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 change: 1 addition & 0 deletions packages/eslint-plugin-internal/package.json
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@types/prettier": "*",
"@typescript-eslint/experimental-utils": "4.31.2",
"@typescript-eslint/scope-manager": "^4.31.2",
"prettier": "*"
}
}
@@ -1,7 +1,7 @@
import {
TSESTree,
ESLintUtils,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule } from '../util';

Expand All @@ -24,6 +24,7 @@ const BANNED_PROPERTIES = [
fixWith: 'getDeclarations()',
},
{
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
type: 'Type',
property: 'symbol',
fixWith: 'getSymbol()',
Expand Down
Expand Up @@ -3,6 +3,7 @@ import {
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DefinitionType } from '@typescript-eslint/scope-manager';
import { createRule } from '../util';

const isStringLiteral = (
Expand All @@ -17,7 +18,7 @@ export default createRule({
category: 'Best Practices',
recommended: 'error',
description:
'Ensures consistent usage of AST_NODE_TYPES & AST_TOKEN_TYPES enums.',
'Ensures consistent usage of `AST_NODE_TYPES`, `AST_TOKEN_TYPES` and `DefinitionType` enums.',
},
messages: {
preferEnum: 'Prefer {{ enumName }}.{{ literal }} over raw literal',
Expand All @@ -28,7 +29,7 @@ export default createRule({
defaultOptions: [],
create(context) {
const report = (
enumName: 'AST_NODE_TYPES' | 'AST_TOKEN_TYPES',
enumName: 'AST_NODE_TYPES' | 'AST_TOKEN_TYPES' | 'DefinitionType',
literal: TSESTree.StringLiteral,
): void =>
context.report({
Expand All @@ -44,7 +45,7 @@ export default createRule({
if (
node.parent?.type === AST_NODE_TYPES.TSEnumMember &&
node.parent.parent?.type === AST_NODE_TYPES.TSEnumDeclaration &&
['AST_NODE_TYPES', 'AST_TOKEN_TYPES'].includes(
['AST_NODE_TYPES', 'AST_TOKEN_TYPES', 'DefinitionType'].includes(
node.parent.parent.id.name,
)
) {
Expand All @@ -64,6 +65,10 @@ export default createRule({
if (Object.prototype.hasOwnProperty.call(AST_TOKEN_TYPES, value)) {
report('AST_TOKEN_TYPES', node);
}

if (Object.prototype.hasOwnProperty.call(DefinitionType, value)) {
report('DefinitionType', node);
}
},
};
},
Expand Down
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum */
import rule from '../../src/rules/no-poorly-typed-ts-props';
import { RuleTester, getFixturesRootDir } from '../RuleTester';
import { getFixturesRootDir, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
Expand Up @@ -2,8 +2,9 @@ import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
} from '@typescript-eslint/experimental-utils';
import { DefinitionType } from '@typescript-eslint/scope-manager';
import rule from '../../src/rules/prefer-ast-types-enum';
import { RuleTester, batchedSingleLineTests } from '../RuleTester';
import { batchedSingleLineTests, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand All @@ -17,6 +18,7 @@ ruleTester.run('prefer-ast-types-enum', rule, {
"node.type === 'constructor';",
'node.type === AST_NODE_TYPES.Literal;',
'node.type === AST_TOKEN_TYPES.Keyword;',
'node.type === DefinitionType.Parameter;',
'node.type === 1;',
`
enum MY_ENUM {
Expand All @@ -33,10 +35,12 @@ ruleTester.run('prefer-ast-types-enum', rule, {
code: `
node.type === 'Literal';
node.type === 'Keyword';
node.type === 'Parameter';
`,
output: `
node.type === AST_NODE_TYPES.Literal;
node.type === AST_TOKEN_TYPES.Keyword;
node.type === DefinitionType.Parameter;
`,
errors: [
{
Expand All @@ -49,6 +53,11 @@ node.type === AST_TOKEN_TYPES.Keyword;
messageId: 'preferEnum',
line: 3,
},
{
data: { enumName: 'DefinitionType', literal: DefinitionType.Parameter },
messageId: 'preferEnum',
line: 4,
},
],
}),
});
@@ -1,7 +1,8 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DefinitionType } from '@typescript-eslint/scope-manager';
import * as util from '../util';
import {
checkFunctionExpressionReturnType,
Expand Down Expand Up @@ -294,11 +295,12 @@ export default util.createRule<Options, MessageIds>({
for (const definition of variable.defs) {
// cases we don't care about in this rule
if (
definition.type === 'ImplicitGlobalVariable' ||
definition.type === 'ImportBinding' ||
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
definition.type === 'CatchClause' ||
definition.type === 'Parameter'
[
DefinitionType.ImplicitGlobalVariable,
DefinitionType.ImportBinding,
DefinitionType.CatchClause,
DefinitionType.Parameter,
].includes(definition.type)
) {
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-inferrable-types.ts
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum */
import {
AST_NODE_TYPES,
TSESTree,
Expand Down Expand Up @@ -129,7 +130,6 @@ 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 @@ -151,7 +151,6 @@ 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
13 changes: 8 additions & 5 deletions packages/eslint-plugin/src/rules/no-shadow.ts
@@ -1,9 +1,9 @@
import {
TSESTree,
TSESLint,
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { ScopeType } from '@typescript-eslint/scope-manager';
import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager';
import * as util from '../util';

type MessageIds = 'noShadow';
Expand Down Expand Up @@ -82,7 +82,10 @@ export default util.createRule<Options, MessageIds>({
* Check if variable is a `this` parameter.
*/
function isThisParam(variable: TSESLint.Scope.Variable): boolean {
return variable.defs[0].type === 'Parameter' && variable.name === 'this';
return (
variable.defs[0].type === DefinitionType.Parameter &&
variable.name === 'this'
);
}

function isTypeValueShadow(
Expand Down Expand Up @@ -276,7 +279,7 @@ export default util.createRule<Options, MessageIds>({
inner &&
outer[0] < inner[0] &&
inner[1] < outer[1] &&
((innerDef.type === 'FunctionName' &&
((innerDef.type === DefinitionType.FunctionName &&
innerDef.node.type === AST_NODE_TYPES.FunctionExpression) ||
innerDef.node.type === AST_NODE_TYPES.ClassExpression) &&
outerScope === innerScope.upper
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
@@ -1,5 +1,6 @@
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
Expand Down Expand Up @@ -278,8 +279,7 @@ export default util.createRule<Options, MessageIds>({
reportError(type.node, type.compositionType, isTopLevel, 'Generics');
}
} else if (
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
type.node.type.endsWith('Keyword') ||
type.node.type.endsWith(AST_TOKEN_TYPES.Keyword) ||
aliasTypes.has(type.node.type) ||
(type.node.type === AST_NODE_TYPES.TSTypeOperator &&
(type.node.operator === 'keyof' ||
Expand Down
@@ -1,5 +1,4 @@
/* eslint-disable no-fallthrough */

/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum, no-fallthrough */
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import * as util from '../util';
Expand Down Expand Up @@ -161,7 +160,6 @@ 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
13 changes: 7 additions & 6 deletions packages/eslint-plugin/src/rules/no-use-before-define.ts
Expand Up @@ -3,6 +3,7 @@ import {
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DefinitionType } from '@typescript-eslint/scope-manager';
import * as util from '../util';

const SENTINEL_TYPE =
Expand Down Expand Up @@ -44,14 +45,14 @@ function parseOptions(options: string | Config | null): Required<Config> {
* Checks whether or not a given variable is a function declaration.
*/
function isFunction(variable: TSESLint.Scope.Variable): boolean {
return variable.defs[0].type === 'FunctionName';
return variable.defs[0].type === DefinitionType.FunctionName;
}

/**
* Checks whether or not a given variable is a type declaration.
*/
function isTypedef(variable: TSESLint.Scope.Variable): boolean {
return variable.defs[0].type === 'Type';
return variable.defs[0].type === DefinitionType.Type;
}

/**
Expand All @@ -62,7 +63,7 @@ function isOuterEnum(
reference: TSESLint.Scope.Reference,
): boolean {
return (
variable.defs[0].type == 'TSEnumName' &&
variable.defs[0].type == DefinitionType.TSEnumName &&
variable.scope.variableScope !== reference.from.variableScope
);
}
Expand All @@ -75,7 +76,7 @@ function isOuterClass(
reference: TSESLint.Scope.Reference,
): boolean {
return (
variable.defs[0].type === 'ClassName' &&
variable.defs[0].type === DefinitionType.ClassName &&
variable.scope.variableScope !== reference.from.variableScope
);
}
Expand All @@ -88,7 +89,7 @@ function isOuterVariable(
reference: TSESLint.Scope.Reference,
): boolean {
return (
variable.defs[0].type === 'Variable' &&
variable.defs[0].type === DefinitionType.Variable &&
variable.scope.variableScope !== reference.from.variableScope
);
}
Expand Down Expand Up @@ -142,7 +143,7 @@ function isClassRefInClassDecorator(
variable: TSESLint.Scope.Variable,
reference: TSESLint.Scope.Reference,
): boolean {
if (variable.defs[0].type !== 'ClassName') {
if (variable.defs[0].type !== DefinitionType.ClassName) {
return false;
}

Expand Down
12 changes: 2 additions & 10 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum */
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule, {
MessageIds,
Options,
TYPE_KEYWORDS,
} from '../../src/rules/ban-types';
import { objectReduceKey } from '../../src/util';
import { RuleTester, noFormat } from '../RuleTester';
import { noFormat, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down Expand Up @@ -163,7 +164,6 @@ 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 @@ -180,7 +180,6 @@ 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 @@ -197,7 +196,6 @@ 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 @@ -242,7 +240,6 @@ 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 @@ -252,7 +249,6 @@ 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 @@ -271,7 +267,6 @@ 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 @@ -296,7 +291,6 @@ 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 @@ -306,7 +300,6 @@ 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 @@ -316,7 +309,6 @@ 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
5 changes: 3 additions & 2 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum */
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule, { MessageIds, Options } from '../../src/rules/naming-convention';
import {
PredefinedFormatsString,
selectorTypeToMessageString,
Selector,
selectorTypeToMessageString,
} from '../../src/rules/naming-convention-utils';
import { RuleTester, getFixturesRootDir } from '../RuleTester';
import { getFixturesRootDir, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down