Skip to content

Commit d738fa4

Browse files
committed
fix: correct decorator traversal for AssignmentPattern (typescript-eslint#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.
1 parent 9de669f commit d738fa4

File tree

11 files changed

+14
-170
lines changed

11 files changed

+14
-170
lines changed

packages/eslint-plugin/src/rules/no-empty-function.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ export default util.createRule<Options, MessageIds>({
129129
): boolean {
130130
if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
131131
const decorators =
132-
node.type === AST_NODE_TYPES.FunctionDeclaration
133-
? node.decorators
134-
: node.parent?.type === AST_NODE_TYPES.MethodDefinition
132+
node.parent?.type === AST_NODE_TYPES.MethodDefinition
135133
? node.parent.decorators
136134
: undefined;
137135
return !!decorators && !!decorators.length;

packages/eslint-plugin/tests/rules/no-empty-function.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ function foo() {
6565
},
6666
{
6767
code: `
68-
@decorator()
69-
function foo() {}
70-
`,
71-
options: [{ allow: ['decoratedFunctions'] }],
72-
},
73-
{
74-
code: `
7568
class Foo {
7669
@decorator()
7770
foo() {}

packages/eslint-plugin/tests/rules/no-unused-vars.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,16 @@ export interface Event<T> {
749749
},
750750
],
751751
},
752+
// https://github.com/typescript-eslint/typescript-eslint/issues/2369
753+
`
754+
export default function (@Optional() value = []) {
755+
return value;
756+
}
757+
758+
function Optional() {
759+
return () => {};
760+
}
761+
`,
752762
],
753763

754764
invalid: [

packages/types/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"scripts": {
3131
"build": "tsc -b tsconfig.build.json",
3232
"clean": "tsc -b tsconfig.build.json --clean",
33+
"postclean": "rimraf dist",
3334
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
3435
"generate:lib": "../../node_modules/.bin/ts-node --files --transpile-only ../scope-manager/tools/generate-lib.ts",
3536
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",

packages/types/src/ts-estree.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,6 @@ export interface ForStatement extends BaseNode {
948948
export interface FunctionDeclaration extends FunctionDeclarationBase {
949949
type: AST_NODE_TYPES.FunctionDeclaration;
950950
body: BlockStatement;
951-
decorators?: Decorator[];
952951
}
953952

954953
export interface FunctionExpression extends FunctionDeclarationBase {
@@ -1343,7 +1342,6 @@ export interface TSEnumDeclaration extends BaseNode {
13431342
const?: boolean;
13441343
declare?: boolean;
13451344
modifiers?: Modifier[];
1346-
decorators?: Decorator[];
13471345
}
13481346

13491347
/**
@@ -1428,7 +1426,6 @@ export interface TSInterfaceDeclaration extends BaseNode {
14281426
typeParameters?: TSTypeParameterDeclaration;
14291427
extends?: TSInterfaceHeritage[];
14301428
implements?: TSInterfaceHeritage[];
1431-
decorators?: Decorator[];
14321429
abstract?: boolean;
14331430
declare?: boolean;
14341431
}

packages/typescript-estree/src/convert.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -839,17 +839,6 @@ export class Converter {
839839
);
840840
}
841841

842-
/**
843-
* Semantically, decorators are not allowed on function declarations,
844-
* but the TypeScript compiler will parse them and produce a valid AST,
845-
* so we handle them here too.
846-
*/
847-
if (node.decorators) {
848-
(result as any).decorators = node.decorators.map(el =>
849-
this.convertChild(el),
850-
);
851-
}
852-
853842
// check for exports
854843
return this.fixExports(node, result);
855844
}
@@ -2516,14 +2505,6 @@ export class Converter {
25162505
}
25172506
}
25182507

2519-
/**
2520-
* Semantically, decorators are not allowed on interface declarations,
2521-
* but the TypeScript compiler will parse them and produce a valid AST,
2522-
* so we handle them here too.
2523-
*/
2524-
if (node.decorators) {
2525-
result.decorators = node.decorators.map(el => this.convertChild(el));
2526-
}
25272508
if (hasModifier(SyntaxKind.AbstractKeyword, node)) {
25282509
result.abstract = true;
25292510
}
@@ -2575,14 +2556,6 @@ export class Converter {
25752556
});
25762557
// apply modifiers first...
25772558
this.applyModifiersToResult(result, node.modifiers);
2578-
/**
2579-
* Semantically, decorators are not allowed on enum declarations,
2580-
* but the TypeScript compiler will parse them and produce a valid AST,
2581-
* so we handle them here too.
2582-
*/
2583-
if (node.decorators) {
2584-
result.decorators = node.decorators.map(el => this.convertChild(el));
2585-
}
25862559
// ...then check for exports
25872560
return this.fixExports(node, result);
25882561
}

packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-enum-declaration.src.ts.shot

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,6 @@ exports[`typescript errorRecovery decorator-on-enum-declaration.src 1`] = `
44
Object {
55
"body": Array [
66
Object {
7-
"decorators": Array [
8-
Object {
9-
"expression": Object {
10-
"loc": Object {
11-
"end": Object {
12-
"column": 4,
13-
"line": 1,
14-
},
15-
"start": Object {
16-
"column": 1,
17-
"line": 1,
18-
},
19-
},
20-
"name": "dec",
21-
"range": Array [
22-
1,
23-
4,
24-
],
25-
"type": "Identifier",
26-
},
27-
"loc": Object {
28-
"end": Object {
29-
"column": 4,
30-
"line": 1,
31-
},
32-
"start": Object {
33-
"column": 0,
34-
"line": 1,
35-
},
36-
},
37-
"range": Array [
38-
0,
39-
4,
40-
],
41-
"type": "Decorator",
42-
},
43-
],
447
"id": Object {
458
"loc": Object {
469
"end": Object {

packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-function.src.ts.shot

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,6 @@ Object {
2323
],
2424
"type": "BlockStatement",
2525
},
26-
"decorators": Array [
27-
Object {
28-
"expression": Object {
29-
"loc": Object {
30-
"end": Object {
31-
"column": 4,
32-
"line": 1,
33-
},
34-
"start": Object {
35-
"column": 1,
36-
"line": 1,
37-
},
38-
},
39-
"name": "dec",
40-
"range": Array [
41-
1,
42-
4,
43-
],
44-
"type": "Identifier",
45-
},
46-
"loc": Object {
47-
"end": Object {
48-
"column": 4,
49-
"line": 1,
50-
},
51-
"start": Object {
52-
"column": 0,
53-
"line": 1,
54-
},
55-
},
56-
"range": Array [
57-
0,
58-
4,
59-
],
60-
"type": "Decorator",
61-
},
62-
],
6326
"expression": false,
6427
"generator": false,
6528
"id": Object {

packages/typescript-estree/tests/snapshots/typescript/errorRecovery/decorator-on-interface-declaration.src.ts.shot

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,6 @@ Object {
2222
],
2323
"type": "TSInterfaceBody",
2424
},
25-
"decorators": Array [
26-
Object {
27-
"expression": Object {
28-
"arguments": Array [],
29-
"callee": Object {
30-
"loc": Object {
31-
"end": Object {
32-
"column": 5,
33-
"line": 1,
34-
},
35-
"start": Object {
36-
"column": 1,
37-
"line": 1,
38-
},
39-
},
40-
"name": "deco",
41-
"range": Array [
42-
1,
43-
5,
44-
],
45-
"type": "Identifier",
46-
},
47-
"loc": Object {
48-
"end": Object {
49-
"column": 7,
50-
"line": 1,
51-
},
52-
"start": Object {
53-
"column": 1,
54-
"line": 1,
55-
},
56-
},
57-
"optional": false,
58-
"range": Array [
59-
1,
60-
7,
61-
],
62-
"type": "CallExpression",
63-
},
64-
"loc": Object {
65-
"end": Object {
66-
"column": 7,
67-
"line": 1,
68-
},
69-
"start": Object {
70-
"column": 0,
71-
"line": 1,
72-
},
73-
},
74-
"range": Array [
75-
0,
76-
7,
77-
],
78-
"type": "Decorator",
79-
},
80-
],
8125
"id": Object {
8226
"loc": Object {
8327
"end": Object {

packages/visitor-keys/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"scripts": {
3131
"build": "tsc -b tsconfig.build.json",
3232
"clean": "tsc -b tsconfig.build.json --clean",
33+
"postclean": "rimraf dist",
3334
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
3435
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
3536
"test": "jest --coverage",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy