From fe74a6f157a1384bda1836cc95b7347755bfc4e3 Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 21 Feb 2021 15:43:18 +0100 Subject: [PATCH 1/5] fix: correct issues in AST definition --- packages/types/src/ts-estree.ts | 43 +++++++++++------------ packages/typescript-estree/src/convert.ts | 12 +++---- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/packages/types/src/ts-estree.ts b/packages/types/src/ts-estree.ts index 4dd89c962bef..449ea22d745d 100644 --- a/packages/types/src/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -427,11 +427,9 @@ export type MethodDefinition = export type Modifier = | TSAbstractKeyword | TSAsyncKeyword - | TSDeclareKeyword - | TSExportKeyword - | TSPublicKeyword | TSPrivateKeyword | TSProtectedKeyword + | TSPublicKeyword | TSReadonlyKeyword | TSStaticKeyword; export type ObjectLiteralElementLike = @@ -464,20 +462,8 @@ export type PrimaryExpression = | TemplateLiteral | ThisExpression | TSNullKeyword; -export type ProgramStatement = - | ClassDeclaration - | ExportAllDeclaration - | ExportDefaultDeclaration - | ExportNamedDeclaration - | ImportDeclaration - | Statement - | TSDeclareFunction - | TSEnumDeclaration - | TSExportAssignment - | TSImportEqualsDeclaration - | TSInterfaceDeclaration - | TSNamespaceExportDeclaration - | TSTypeAliasDeclaration; +/** @deprecated TODO: delete this in next major release */ +export type ProgramStatement = Statement; export type Property = PropertyComputedName | PropertyNonComputedName; export type PropertyName = PropertyNameComputed | PropertyNameNonComputed; export type PropertyNameComputed = Expression; @@ -488,16 +474,27 @@ export type PropertyNameNonComputed = export type Statement = | BlockStatement | BreakStatement + | ClassDeclaration | ContinueStatement | DebuggerStatement | DeclarationStatement | EmptyStatement + | ExportAllDeclaration + | ExportDefaultDeclaration + | ExportNamedDeclaration | ExpressionStatement | IfStatement | IterationStatement | ImportDeclaration | LabeledStatement + | TSDeclareFunction + | TSEnumDeclaration + | TSExportAssignment + | TSImportEqualsDeclaration + | TSInterfaceDeclaration | TSModuleBlock + | TSNamespaceExportDeclaration + | TSTypeAliasDeclaration | ReturnStatement | SwitchStatement | ThrowStatement @@ -1077,7 +1074,7 @@ export interface JSXOpeningElement extends BaseNode { typeParameters?: TSTypeParameterInstantiation; selfClosing: boolean; name: JSXTagNameExpression; - attributes: JSXAttribute[]; + attributes: (JSXAttribute | JSXSpreadAttribute)[]; } export interface JSXOpeningFragment extends BaseNode { @@ -1168,7 +1165,7 @@ export interface ObjectPattern extends BaseNode { export interface Program extends BaseNode { type: AST_NODE_TYPES.Program; - body: ProgramStatement[]; + body: Statement[]; sourceType: 'module' | 'script'; comments?: Comment[]; tokens?: Token[]; @@ -1186,7 +1183,7 @@ export interface PropertyNonComputedName extends PropertyBase { export interface RegExpLiteral extends LiteralBase { type: AST_NODE_TYPES.Literal; - value: RegExp; + value: RegExp | null; } export interface RestElement extends BaseNode { @@ -1269,7 +1266,7 @@ export interface TryStatement extends BaseNode { type: AST_NODE_TYPES.TryStatement; block: BlockStatement; handler: CatchClause | null; - finalizer: BlockStatement; + finalizer: BlockStatement | null; } export interface TSAbstractClassPropertyComputedName @@ -1496,13 +1493,13 @@ export interface TSMethodSignatureNonComputedName export interface TSModuleBlock extends BaseNode { type: AST_NODE_TYPES.TSModuleBlock; - body: ProgramStatement[]; + body: Statement[]; } export interface TSModuleDeclaration extends BaseNode { type: AST_NODE_TYPES.TSModuleDeclaration; id: Identifier | Literal; - body?: TSModuleBlock; + body?: TSModuleBlock | TSModuleDeclaration; global?: boolean; declare?: boolean; modifiers?: Modifier[]; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index f8819a33c9e3..11be4bb8dce9 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -1921,7 +1921,7 @@ export class Converter { // Literals case SyntaxKind.StringLiteral: { - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: parent.kind === SyntaxKind.JsxAttribute @@ -1932,7 +1932,7 @@ export class Converter { } case SyntaxKind.NumericLiteral: { - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: Number(node.text), raw: node.getText(), @@ -1969,7 +1969,7 @@ export class Converter { regex = null; } - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: regex, raw: node.text, @@ -1981,14 +1981,14 @@ export class Converter { } case SyntaxKind.TrueKeyword: - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: true, raw: 'true', }); case SyntaxKind.FalseKeyword: - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: false, raw: 'false', @@ -2002,7 +2002,7 @@ export class Converter { }); } - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: null, raw: 'null', From 34b9d6ad3f4d4f78ebd5f9710d946e669dcba059 Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 21 Feb 2021 15:54:00 +0100 Subject: [PATCH 2/5] fix: add missing type check to no-unused-vars --- packages/eslint-plugin/src/rules/no-unused-vars.ts | 10 ++++++---- packages/typescript-estree/src/convert.ts | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unused-vars.ts b/packages/eslint-plugin/src/rules/no-unused-vars.ts index 968c8d51b54e..1f614966893c 100644 --- a/packages/eslint-plugin/src/rules/no-unused-vars.ts +++ b/packages/eslint-plugin/src/rules/no-unused-vars.ts @@ -408,10 +408,12 @@ export default util.createRule({ return cached; } - for (const statement of node.body?.body ?? []) { - if (statement.type === AST_NODE_TYPES.TSExportAssignment) { - MODULE_DECL_CACHE.set(node, true); - return true; + if (node.body && node.body.type !== AST_NODE_TYPES.TSModuleDeclaration) { + for (const statement of node.body.body ?? []) { + if (statement.type === AST_NODE_TYPES.TSExportAssignment) { + MODULE_DECL_CACHE.set(node, true); + return true; + } } } diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 11be4bb8dce9..e0f175c27908 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -1932,7 +1932,7 @@ export class Converter { } case SyntaxKind.NumericLiteral: { - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: Number(node.text), raw: node.getText(), From cd42cf9d4ec8658892f112851caf4841037bc28d Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 21 Feb 2021 19:37:29 +0100 Subject: [PATCH 3/5] fix: improve type-check in no-unused-vars --- packages/eslint-plugin/src/rules/no-unused-vars.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unused-vars.ts b/packages/eslint-plugin/src/rules/no-unused-vars.ts index 1f614966893c..79709e1df1ee 100644 --- a/packages/eslint-plugin/src/rules/no-unused-vars.ts +++ b/packages/eslint-plugin/src/rules/no-unused-vars.ts @@ -408,8 +408,8 @@ export default util.createRule({ return cached; } - if (node.body && node.body.type !== AST_NODE_TYPES.TSModuleDeclaration) { - for (const statement of node.body.body ?? []) { + if (node.body && node.body.type === AST_NODE_TYPES.TSModuleBlock) { + for (const statement of node.body.body) { if (statement.type === AST_NODE_TYPES.TSExportAssignment) { MODULE_DECL_CACHE.set(node, true); return true; From ccd18e1793a5f18ffb093c9b4b14626bde1ee67b Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 21 Feb 2021 19:56:04 +0100 Subject: [PATCH 4/5] chore: update comment about ProgramStatement --- packages/types/src/ts-estree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/ts-estree.ts b/packages/types/src/ts-estree.ts index 449ea22d745d..83d9f3681b87 100644 --- a/packages/types/src/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -462,7 +462,7 @@ export type PrimaryExpression = | TemplateLiteral | ThisExpression | TSNullKeyword; -/** @deprecated TODO: delete this in next major release */ +/** @deprecated TODO: re-align this with EStree spec in next major release */ export type ProgramStatement = Statement; export type Property = PropertyComputedName | PropertyNonComputedName; export type PropertyName = PropertyNameComputed | PropertyNameNonComputed; From 51771378ea901ef9ca283100d215cafb9c3171bc Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 21 Feb 2021 20:12:02 +0100 Subject: [PATCH 5/5] chore: remove @deprecated from ProgramStatement --- packages/types/src/ts-estree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/ts-estree.ts b/packages/types/src/ts-estree.ts index 83d9f3681b87..6b29bb2efc91 100644 --- a/packages/types/src/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -462,7 +462,7 @@ export type PrimaryExpression = | TemplateLiteral | ThisExpression | TSNullKeyword; -/** @deprecated TODO: re-align this with EStree spec in next major release */ +/** TODO: re-align this with EStree spec in next major release */ export type ProgramStatement = Statement; export type Property = PropertyComputedName | PropertyNonComputedName; export type PropertyName = PropertyNameComputed | PropertyNameNonComputed;