Skip to content

fix: correct decorator traversal for AssignmentPattern #2375

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 1 commit into from
Aug 9, 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
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-empty-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ export default util.createRule<Options, MessageIds>({
): boolean {
if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
const decorators =
node.type === AST_NODE_TYPES.FunctionDeclaration
? node.decorators
: node.parent?.type === AST_NODE_TYPES.MethodDefinition
node.parent?.type === AST_NODE_TYPES.MethodDefinition
? node.parent.decorators
: undefined;
return !!decorators && !!decorators.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ 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 Down
7 changes: 0 additions & 7 deletions packages/eslint-plugin/tests/rules/no-empty-function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ function foo() {
},
{
code: `
@decorator()
function foo() {}
`,
options: [{ allow: ['decoratedFunctions'] }],
},
{
code: `
class Foo {
@decorator()
foo() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,6 @@ 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,
},
Expand Down Expand Up @@ -1047,7 +1046,6 @@ 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,
},
Expand All @@ -1067,7 +1065,6 @@ 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,
},
Expand All @@ -1087,7 +1084,6 @@ 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,
},
Expand All @@ -1107,7 +1103,6 @@ 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,
},
Expand All @@ -1127,7 +1122,6 @@ 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,
},
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,16 @@ export interface Event<T> {
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2369
`
export default function (@Optional() value = []) {
return value;
}

function Optional() {
return () => {};
}
`,
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scripts": {
"build": "tsc -b tsconfig.build.json",
"clean": "tsc -b tsconfig.build.json --clean",
"postclean": "rimraf dist",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"generate:lib": "../../node_modules/.bin/ts-node --files --transpile-only ../scope-manager/tools/generate-lib.ts",
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
Expand Down
16 changes: 15 additions & 1 deletion packages/types/src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ enum AST_NODE_TYPES {
FunctionExpression = 'FunctionExpression',
Identifier = 'Identifier',
IfStatement = 'IfStatement',
Import = 'Import',
ImportDeclaration = 'ImportDeclaration',
ImportDefaultSpecifier = 'ImportDefaultSpecifier',
ImportExpression = 'ImportExpression',
Expand Down Expand Up @@ -163,3 +162,18 @@ enum AST_NODE_TYPES {
}

export { AST_NODE_TYPES };

// Below is a special type-only test which ensures that we don't accidentally leave unused keys in this enum
// eslint-disable-next-line import/first -- purposely down here to colocate it with this hack of a test
import type { Node } from './ts-estree';

type GetKeys<T extends AST_NODE_TYPES> = keyof Extract<Node, { type: T }>;
type AllKeys = {
readonly [T in AST_NODE_TYPES]: GetKeys<T>;
};
type TakesString<T extends Record<string, string>> = T;
// @ts-expect-error: purposely unused
type _Test =
// forcing the test onto a new line so it isn't covered by the expect error
// If there are any enum members that don't have a corresponding TSESTree.Node, then this line will error with "Type 'string | number | symbol' is not assignable to type 'string'."
void | TakesString<AllKeys>;
3 changes: 0 additions & 3 deletions packages/types/src/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,6 @@ export interface ForStatement extends BaseNode {
export interface FunctionDeclaration extends FunctionDeclarationBase {
type: AST_NODE_TYPES.FunctionDeclaration;
body: BlockStatement;
decorators?: Decorator[];
}

export interface FunctionExpression extends FunctionDeclarationBase {
Expand Down Expand Up @@ -1341,7 +1340,6 @@ export interface TSEnumDeclaration extends BaseNode {
const?: boolean;
declare?: boolean;
modifiers?: Modifier[];
decorators?: Decorator[];
}

/**
Expand Down Expand Up @@ -1426,7 +1424,6 @@ export interface TSInterfaceDeclaration extends BaseNode {
typeParameters?: TSTypeParameterDeclaration;
extends?: TSInterfaceHeritage[];
implements?: TSInterfaceHeritage[];
decorators?: Decorator[];
abstract?: boolean;
declare?: boolean;
}
Expand Down
27 changes: 0 additions & 27 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,17 +839,6 @@ export class Converter {
);
}

/**
* Semantically, decorators are not allowed on function declarations,
* but the TypeScript compiler will parse them and produce a valid AST,
* so we handle them here too.
*/
if (node.decorators) {
(result as any).decorators = node.decorators.map(el =>
this.convertChild(el),
);
}

// check for exports
return this.fixExports(node, result);
}
Expand Down Expand Up @@ -2514,14 +2503,6 @@ export class Converter {
}
}

/**
* Semantically, decorators are not allowed on interface declarations,
* but the TypeScript compiler will parse them and produce a valid AST,
* so we handle them here too.
*/
if (node.decorators) {
result.decorators = node.decorators.map(el => this.convertChild(el));
}
if (hasModifier(SyntaxKind.AbstractKeyword, node)) {
result.abstract = true;
}
Expand Down Expand Up @@ -2573,14 +2554,6 @@ export class Converter {
});
// apply modifiers first...
this.applyModifiersToResult(result, node.modifiers);
/**
* Semantically, decorators are not allowed on enum declarations,
* but the TypeScript compiler will parse them and produce a valid AST,
* so we handle them here too.
*/
if (node.decorators) {
result.decorators = node.decorators.map(el => this.convertChild(el));
}
// ...then check for exports
return this.fixExports(node, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export interface EstreeToTsNodeTypes {
| ts.ConstructorDeclaration
| ts.Token<ts.SyntaxKind.NewKeyword | ts.SyntaxKind.ImportKeyword>;
[AST_NODE_TYPES.IfStatement]: ts.IfStatement;
[AST_NODE_TYPES.Import]: ts.ImportExpression;
[AST_NODE_TYPES.ImportDeclaration]: ts.ImportDeclaration;
[AST_NODE_TYPES.ImportDefaultSpecifier]: ts.ImportClause;
[AST_NODE_TYPES.ImportExpression]: ts.CallExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,6 @@ exports[`typescript errorRecovery decorator-on-enum-declaration.src 1`] = `
Object {
"body": Array [
Object {
"decorators": Array [
Object {
"expression": Object {
"loc": Object {
"end": Object {
"column": 4,
"line": 1,
},
"start": Object {
"column": 1,
"line": 1,
},
},
"name": "dec",
"range": Array [
1,
4,
],
"type": "Identifier",
},
"loc": Object {
"end": Object {
"column": 4,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
4,
],
"type": "Decorator",
},
],
"id": Object {
"loc": Object {
"end": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,6 @@ Object {
],
"type": "BlockStatement",
},
"decorators": Array [
Object {
"expression": Object {
"loc": Object {
"end": Object {
"column": 4,
"line": 1,
},
"start": Object {
"column": 1,
"line": 1,
},
},
"name": "dec",
"range": Array [
1,
4,
],
"type": "Identifier",
},
"loc": Object {
"end": Object {
"column": 4,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
4,
],
"type": "Decorator",
},
],
"expression": false,
"generator": false,
"id": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,6 @@ Object {
],
"type": "TSInterfaceBody",
},
"decorators": Array [
Object {
"expression": Object {
"arguments": Array [],
"callee": Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 1,
"line": 1,
},
},
"name": "deco",
"range": Array [
1,
5,
],
"type": "Identifier",
},
"loc": Object {
"end": Object {
"column": 7,
"line": 1,
},
"start": Object {
"column": 1,
"line": 1,
},
},
"optional": false,
"range": Array [
1,
7,
],
"type": "CallExpression",
},
"loc": Object {
"end": Object {
"column": 7,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
7,
],
"type": "Decorator",
},
],
"id": Object {
"loc": Object {
"end": Object {
Expand Down
1 change: 1 addition & 0 deletions packages/visitor-keys/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scripts": {
"build": "tsc -b tsconfig.build.json",
"clean": "tsc -b tsconfig.build.json --clean",
"postclean": "rimraf dist",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
"test": "jest --coverage",
Expand Down
Loading