From 4faf90c4d094ca29959134ab93319f811ecb2710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= Date: Sat, 23 Apr 2022 11:35:41 -0500 Subject: [PATCH 1/4] chore: remove invalid properties from ast nodes --- packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts | 2 -- .../ast-spec/src/declaration/TSInterfaceDeclaration/spec.ts | 2 -- packages/ast-spec/src/declaration/TSModuleDeclaration/spec.ts | 2 -- 3 files changed, 6 deletions(-) diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts b/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts index 1c1530d501ff..f59a99dee077 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts @@ -2,7 +2,6 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; import type { BaseNode } from '../../base/BaseNode'; import type { TSEnumMember } from '../../element/TSEnumMember/spec'; import type { Identifier } from '../../expression/Identifier/spec'; -import type { Modifier } from '../../unions/Modifier'; export interface TSEnumDeclaration extends BaseNode { type: AST_NODE_TYPES.TSEnumDeclaration; @@ -10,5 +9,4 @@ export interface TSEnumDeclaration extends BaseNode { members: TSEnumMember[]; const?: boolean; declare?: boolean; - modifiers?: Modifier[]; } diff --git a/packages/ast-spec/src/declaration/TSInterfaceDeclaration/spec.ts b/packages/ast-spec/src/declaration/TSInterfaceDeclaration/spec.ts index 1e95380c3cbb..26726b663f33 100644 --- a/packages/ast-spec/src/declaration/TSInterfaceDeclaration/spec.ts +++ b/packages/ast-spec/src/declaration/TSInterfaceDeclaration/spec.ts @@ -11,7 +11,5 @@ export interface TSInterfaceDeclaration extends BaseNode { id: Identifier; typeParameters?: TSTypeParameterDeclaration; extends?: TSInterfaceHeritage[]; - implements?: TSInterfaceHeritage[]; - abstract?: boolean; declare?: boolean; } diff --git a/packages/ast-spec/src/declaration/TSModuleDeclaration/spec.ts b/packages/ast-spec/src/declaration/TSModuleDeclaration/spec.ts index ac63e52a5938..d1bbd6e931e6 100644 --- a/packages/ast-spec/src/declaration/TSModuleDeclaration/spec.ts +++ b/packages/ast-spec/src/declaration/TSModuleDeclaration/spec.ts @@ -3,7 +3,6 @@ import type { BaseNode } from '../../base/BaseNode'; import type { Identifier } from '../../expression/Identifier/spec'; import type { TSModuleBlock } from '../../special/TSModuleBlock/spec'; import type { Literal } from '../../unions/Literal'; -import type { Modifier } from '../../unions/Modifier'; export interface TSModuleDeclaration extends BaseNode { type: AST_NODE_TYPES.TSModuleDeclaration; @@ -22,5 +21,4 @@ export interface TSModuleDeclaration extends BaseNode { | TSModuleDeclaration; global?: boolean; declare?: boolean; - modifiers?: Modifier[]; } From f87558ed5f8f0b644310bced6cbcd5de2f9d25ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= Date: Sat, 23 Apr 2022 11:41:31 -0500 Subject: [PATCH 2/4] chore: remove invalid code in scope-manager and typescript-estree --- .../src/referencer/TypeVisitor.ts | 1 - packages/typescript-estree/src/convert.ts | 85 +++---------------- 2 files changed, 12 insertions(+), 74 deletions(-) diff --git a/packages/scope-manager/src/referencer/TypeVisitor.ts b/packages/scope-manager/src/referencer/TypeVisitor.ts index e9abb40f4f42..7325f149d0f2 100644 --- a/packages/scope-manager/src/referencer/TypeVisitor.ts +++ b/packages/scope-manager/src/referencer/TypeVisitor.ts @@ -183,7 +183,6 @@ class TypeVisitor extends Visitor { } node.extends?.forEach(this.visit, this); - node.implements?.forEach(this.visit, this); this.visit(node.body); if (node.typeParameters) { diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index f0fb7d3cf58b..8d3f25a03c73 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -672,60 +672,6 @@ export class Converter { : node.elements.map(element => this.convertChild(element)); } - /** - * Applies the given TS modifiers to the given result object. - * @param result - * @param modifiers original ts.Nodes from the node.modifiers array - * @returns the current result object will be mutated - * @deprecated This method adds not standardized `modifiers` property in nodes - */ - private applyModifiersToResult( - result: TSESTree.TSEnumDeclaration | TSESTree.TSModuleDeclaration, - modifiers?: ts.ModifiersArray, - ): void { - if (!modifiers || !modifiers.length) { - return; - } - const remainingModifiers: TSESTree.Modifier[] = []; - /** - * Some modifiers are explicitly handled by applying them as - * boolean values on the result node. As well as adding them - * to the result, we remove them from the array, so that they - * are not handled twice. - */ - for (let i = 0; i < modifiers.length; i++) { - const modifier = modifiers[i]; - switch (modifier.kind) { - /** - * Ignore ExportKeyword and DefaultKeyword, they are handled - * via the fixExports utility function - */ - case SyntaxKind.ExportKeyword: - case SyntaxKind.DefaultKeyword: - break; - case SyntaxKind.ConstKeyword: - (result as any).const = true; - break; - case SyntaxKind.DeclareKeyword: - result.declare = true; - break; - default: - remainingModifiers.push( - this.convertChild(modifier) as TSESTree.Modifier, - ); - break; - } - } - /** - * If there are still valid modifiers available which have - * not been explicitly handled above, we just convert and - * add the modifiers array to the result node. - */ - if (remainingModifiers.length) { - result.modifiers = remainingModifiers; - } - } - /** * Uses the provided range location to adjust the location data of the given Node * @param result The node that will have its location data mutated @@ -2616,7 +2562,6 @@ export class Converter { if (interfaceHeritageClauses.length > 0) { const interfaceExtends: TSESTree.TSInterfaceHeritage[] = []; - const interfaceImplements: TSESTree.TSInterfaceHeritage[] = []; for (const heritageClause of interfaceHeritageClauses) { if (heritageClause.token === SyntaxKind.ExtendsKeyword) { @@ -2625,27 +2570,13 @@ export class Converter { this.convertChild(n, node) as TSESTree.TSInterfaceHeritage, ); } - } else { - for (const n of heritageClause.types) { - interfaceImplements.push( - this.convertChild(n, node) as TSESTree.TSInterfaceHeritage, - ); - } } } - - if (interfaceExtends.length) { + if (interfaceExtends.length > 0) { result.extends = interfaceExtends; } - - if (interfaceImplements.length) { - result.implements = interfaceImplements; - } } - if (hasModifier(SyntaxKind.AbstractKeyword, node)) { - result.abstract = true; - } if (hasModifier(SyntaxKind.DeclareKeyword, node)) { result.declare = true; } @@ -2693,7 +2624,13 @@ export class Converter { members: node.members.map(el => this.convertChild(el)), }); // apply modifiers first... - this.applyModifiersToResult(result, node.modifiers); + if (hasModifier(SyntaxKind.DeclareKeyword, node)) { + result.declare = true; + } + + if (hasModifier(SyntaxKind.ConstKeyword, node)) { + result.const = true; + } // ...then check for exports return this.fixExports(node, result); } @@ -2720,8 +2657,10 @@ export class Converter { if (node.body) { result.body = this.convertChild(node.body); } - // apply modifiers first... - this.applyModifiersToResult(result, node.modifiers); + + if (hasModifier(SyntaxKind.DeclareKeyword, node)) { + result.declare = true; + } if (node.flags & ts.NodeFlags.GlobalAugmentation) { result.global = true; } From 80578ac55e935ef160a6134c51c92b990b68c4f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= Date: Sat, 23 Apr 2022 11:42:42 -0500 Subject: [PATCH 3/4] chore: re-write snapshots that were using invalid properties --- .../basics/abstract-interface.src.ts.shot | 1 - .../enum-with-keywords.src.ts.shot | 121 ------------------ .../interface-implements.src.ts.shot | 37 ------ 3 files changed, 159 deletions(-) diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-interface.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-interface.src.ts.shot index 5d96c992f86f..bc674bcb4d15 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-interface.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/abstract-interface.src.ts.shot @@ -6,7 +6,6 @@ Object { Object { "assertions": Array [], "declaration": Object { - "abstract": true, "body": Object { "body": Array [], "loc": Object { diff --git a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/enum-with-keywords.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/enum-with-keywords.src.ts.shot index f21c928657d8..5bb1a9402e3b 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/enum-with-keywords.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/enum-with-keywords.src.ts.shot @@ -35,127 +35,6 @@ Object { }, }, "members": Array [], - "modifiers": Array [ - Object { - "loc": Object { - "end": Object { - "column": 14, - "line": 1, - }, - "start": Object { - "column": 7, - "line": 1, - }, - }, - "range": Array [ - 7, - 14, - ], - "type": "TSPrivateKeyword", - }, - Object { - "loc": Object { - "end": Object { - "column": 21, - "line": 1, - }, - "start": Object { - "column": 15, - "line": 1, - }, - }, - "range": Array [ - 15, - 21, - ], - "type": "TSPublicKeyword", - }, - Object { - "loc": Object { - "end": Object { - "column": 31, - "line": 1, - }, - "start": Object { - "column": 22, - "line": 1, - }, - }, - "range": Array [ - 22, - 31, - ], - "type": "TSProtectedKeyword", - }, - Object { - "loc": Object { - "end": Object { - "column": 38, - "line": 1, - }, - "start": Object { - "column": 32, - "line": 1, - }, - }, - "range": Array [ - 32, - 38, - ], - "type": "TSStaticKeyword", - }, - Object { - "loc": Object { - "end": Object { - "column": 47, - "line": 1, - }, - "start": Object { - "column": 39, - "line": 1, - }, - }, - "range": Array [ - 39, - 47, - ], - "type": "TSReadonlyKeyword", - }, - Object { - "loc": Object { - "end": Object { - "column": 56, - "line": 1, - }, - "start": Object { - "column": 48, - "line": 1, - }, - }, - "range": Array [ - 48, - 56, - ], - "type": "TSAbstractKeyword", - }, - Object { - "loc": Object { - "end": Object { - "column": 62, - "line": 1, - }, - "start": Object { - "column": 57, - "line": 1, - }, - }, - "range": Array [ - 57, - 62, - ], - "type": "TSAsyncKeyword", - }, - ], "range": Array [ 7, 72, diff --git a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-implements.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-implements.src.ts.shot index 748c52a5a1fb..65868666d64c 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-implements.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-implements.src.ts.shot @@ -40,43 +40,6 @@ Object { ], "type": "Identifier", }, - "implements": Array [ - Object { - "expression": Object { - "loc": Object { - "end": Object { - "column": 24, - "line": 1, - }, - "start": Object { - "column": 23, - "line": 1, - }, - }, - "name": "e", - "range": Array [ - 23, - 24, - ], - "type": "Identifier", - }, - "loc": Object { - "end": Object { - "column": 24, - "line": 1, - }, - "start": Object { - "column": 23, - "line": 1, - }, - }, - "range": Array [ - 23, - 24, - ], - "type": "TSInterfaceHeritage", - }, - ], "loc": Object { "end": Object { "column": 27, From 7ca78b15d48be745e46b5f2c4c43606b2902212b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= Date: Wed, 18 May 2022 09:47:29 -0500 Subject: [PATCH 4/4] feat: remove modifiers union from ast types --- packages/ast-spec/src/unions/Modifier.ts | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 packages/ast-spec/src/unions/Modifier.ts diff --git a/packages/ast-spec/src/unions/Modifier.ts b/packages/ast-spec/src/unions/Modifier.ts deleted file mode 100644 index 0922a52eca2e..000000000000 --- a/packages/ast-spec/src/unions/Modifier.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { TSAbstractKeyword } from '../token/TSAbstractKeyword/spec'; -import type { TSAsyncKeyword } from '../token/TSAsyncKeyword/spec'; -import type { TSPrivateKeyword } from '../token/TSPrivateKeyword/spec'; -import type { TSProtectedKeyword } from '../token/TSProtectedKeyword/spec'; -import type { TSPublicKeyword } from '../token/TSPublicKeyword/spec'; -import type { TSReadonlyKeyword } from '../token/TSReadonlyKeyword/spec'; -import type { TSStaticKeyword } from '../token/TSStaticKeyword/spec'; - -export type Modifier = - | TSAbstractKeyword - | TSAsyncKeyword - | TSPrivateKeyword - | TSProtectedKeyword - | TSPublicKeyword - | TSReadonlyKeyword - | TSStaticKeyword;