Skip to content
Permalink
Browse files

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.
  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent 9de669f commit d738fa4eff0a5c4cfc9b30b1c0502f8d1e78d7b6
@@ -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;
@@ -65,13 +65,6 @@ function foo() {
},
{
code: `
@decorator()
function foo() {}
`,
options: [{ allow: ['decoratedFunctions'] }],
},
{
code: `
class Foo {
@decorator()
foo() {}
@@ -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: [
@@ -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'",
@@ -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;
}
@@ -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);
}
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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",
@@ -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',

0 comments on commit d738fa4

Please sign in to comment.