Skip to content

Commit

Permalink
fix: correct decorator traversal for AssignmentPattern (#2375)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- Removed decorators property from several Nodes that could never semantically have them (FunctionDeclaration, TSEnumDeclaration, and TSInterfaceDeclaration)
- Removed AST_NODE_TYPES.Import. This is a minor breaking change as the node type that used this was removed ages ago.
  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent 9de669f commit d738fa4
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 170 deletions.
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-empty-function.ts
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
7 changes: 0 additions & 7 deletions packages/eslint-plugin/tests/rules/no-empty-function.test.ts
Expand Up @@ -65,13 +65,6 @@ function foo() {
},
{
code: `
@decorator()
function foo() {}
`,
options: [{ allow: ['decoratedFunctions'] }],
},
{
code: `
class Foo {
@decorator()
foo() {}
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
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
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
3 changes: 0 additions & 3 deletions packages/types/src/ts-estree.ts
Expand Up @@ -948,7 +948,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 @@ -1343,7 +1342,6 @@ export interface TSEnumDeclaration extends BaseNode {
const?: boolean;
declare?: boolean;
modifiers?: Modifier[];
decorators?: Decorator[];
}

/**
Expand Down Expand Up @@ -1428,7 +1426,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
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 @@ -2516,14 +2505,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 @@ -2575,14 +2556,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
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
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
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
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
1 change: 1 addition & 0 deletions packages/visitor-keys/src/visitor-keys.ts
Expand Up @@ -22,6 +22,7 @@ const additionalKeys: AdditionalKeys = {
// Additional Properties.
ArrayPattern: ['decorators', 'elements', 'typeAnnotation'],
ArrowFunctionExpression: ['typeParameters', 'params', 'returnType', 'body'],
AssignmentPattern: ['decorators', 'left', 'right', 'typeAnnotation'],
CallExpression: ['callee', 'typeParameters', 'arguments'],
ClassDeclaration: [
'decorators',
Expand Down

0 comments on commit d738fa4

Please sign in to comment.