From d738fa4eff0a5c4cfc9b30b1c0502f8d1e78d7b6 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sat, 8 Aug 2020 17:42:34 -0700 Subject: [PATCH] fix: correct decorator traversal for AssignmentPattern (#2375) 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. --- .../src/rules/no-empty-function.ts | 4 +- .../tests/rules/no-empty-function.test.ts | 7 --- .../tests/rules/no-unused-vars.test.ts | 10 ++++ packages/types/package.json | 1 + packages/types/src/ts-estree.ts | 3 - packages/typescript-estree/src/convert.ts | 27 --------- .../decorator-on-enum-declaration.src.ts.shot | 37 ------------ .../decorator-on-function.src.ts.shot | 37 ------------ ...rator-on-interface-declaration.src.ts.shot | 56 ------------------- packages/visitor-keys/package.json | 1 + packages/visitor-keys/src/visitor-keys.ts | 1 + 11 files changed, 14 insertions(+), 170 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-empty-function.ts b/packages/eslint-plugin/src/rules/no-empty-function.ts index 7f5a03a8c39..246680c60e3 100644 --- a/packages/eslint-plugin/src/rules/no-empty-function.ts +++ b/packages/eslint-plugin/src/rules/no-empty-function.ts @@ -129,9 +129,7 @@ export default util.createRule({ ): 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; diff --git a/packages/eslint-plugin/tests/rules/no-empty-function.test.ts b/packages/eslint-plugin/tests/rules/no-empty-function.test.ts index 258ef3e0ff5..b840643f01a 100644 --- a/packages/eslint-plugin/tests/rules/no-empty-function.test.ts +++ b/packages/eslint-plugin/tests/rules/no-empty-function.test.ts @@ -65,13 +65,6 @@ function foo() { }, { code: ` -@decorator() -function foo() {} - `, - options: [{ allow: ['decoratedFunctions'] }], - }, - { - code: ` class Foo { @decorator() foo() {} diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts index 6baafcf9d29..166022cee20 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts @@ -749,6 +749,16 @@ export interface Event { }, ], }, + // https://github.com/typescript-eslint/typescript-eslint/issues/2369 + ` +export default function (@Optional() value = []) { + return value; +} + +function Optional() { + return () => {}; +} + `, ], invalid: [ diff --git a/packages/types/package.json b/packages/types/package.json index a2deae04ccb..b4894e594eb 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -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'", diff --git a/packages/types/src/ts-estree.ts b/packages/types/src/ts-estree.ts index 89c2f832596..14cc90e5857 100644 --- a/packages/types/src/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -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 { @@ -1343,7 +1342,6 @@ export interface TSEnumDeclaration extends BaseNode { const?: boolean; declare?: boolean; modifiers?: Modifier[]; - decorators?: Decorator[]; } /** @@ -1428,7 +1426,6 @@ export interface TSInterfaceDeclaration extends BaseNode { typeParameters?: TSTypeParameterDeclaration; extends?: TSInterfaceHeritage[]; implements?: TSInterfaceHeritage[]; - decorators?: Decorator[]; abstract?: boolean; declare?: boolean; } diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 7522943694c..0b83a4cb965 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -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); } @@ -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; } @@ -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); } diff --git a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-enum-declaration.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-enum-declaration.src.ts.shot index 46050b8b2fc..5e2931242c1 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-enum-declaration.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-enum-declaration.src.ts.shot @@ -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 { diff --git a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-function.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-function.src.ts.shot index f85170d6f47..2fd5ad0bea3 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-function.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-function.src.ts.shot @@ -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 { diff --git a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-interface-declaration.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-interface-declaration.src.ts.shot index 86aefaeb5b0..ef5878c412f 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-interface-declaration.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-interface-declaration.src.ts.shot @@ -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 { diff --git a/packages/visitor-keys/package.json b/packages/visitor-keys/package.json index 6923dda7431..ba029b5876e 100644 --- a/packages/visitor-keys/package.json +++ b/packages/visitor-keys/package.json @@ -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", diff --git a/packages/visitor-keys/src/visitor-keys.ts b/packages/visitor-keys/src/visitor-keys.ts index a8434dcbd4e..a279aef46bf 100644 --- a/packages/visitor-keys/src/visitor-keys.ts +++ b/packages/visitor-keys/src/visitor-keys.ts @@ -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',