diff --git a/packages/eslint-plugin/src/rules/no-unused-vars.ts b/packages/eslint-plugin/src/rules/no-unused-vars.ts index 968c8d51b54..79709e1df1e 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.TSModuleBlock) { + 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/types/src/ts-estree.ts b/packages/types/src/ts-estree.ts index d2794f66268..e341088f201 100644 --- a/packages/types/src/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -428,11 +428,9 @@ export type MethodDefinition = export type Modifier = | TSAbstractKeyword | TSAsyncKeyword - | TSDeclareKeyword - | TSExportKeyword - | TSPublicKeyword | TSPrivateKeyword | TSProtectedKeyword + | TSPublicKeyword | TSReadonlyKeyword | TSStaticKeyword; export type ObjectLiteralElementLike = @@ -465,20 +463,8 @@ export type PrimaryExpression = | TemplateLiteral | ThisExpression | TSNullKeyword; -export type ProgramStatement = - | ClassDeclaration - | ExportAllDeclaration - | ExportDefaultDeclaration - | ExportNamedDeclaration - | ImportDeclaration - | Statement - | TSDeclareFunction - | TSEnumDeclaration - | TSExportAssignment - | TSImportEqualsDeclaration - | TSInterfaceDeclaration - | TSNamespaceExportDeclaration - | TSTypeAliasDeclaration; +/** 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; export type PropertyNameComputed = Expression; @@ -489,16 +475,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 @@ -1079,7 +1076,7 @@ export interface JSXOpeningElement extends BaseNode { typeParameters?: TSTypeParameterInstantiation; selfClosing: boolean; name: JSXTagNameExpression; - attributes: JSXAttribute[]; + attributes: (JSXAttribute | JSXSpreadAttribute)[]; } export interface JSXOpeningFragment extends BaseNode { @@ -1170,7 +1167,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[]; @@ -1188,7 +1185,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 { @@ -1271,7 +1268,7 @@ export interface TryStatement extends BaseNode { type: AST_NODE_TYPES.TryStatement; block: BlockStatement; handler: CatchClause | null; - finalizer: BlockStatement; + finalizer: BlockStatement | null; } export interface TSAbstractClassPropertyComputedName @@ -1502,13 +1499,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 7eb82ccbfe7..762df318e6d 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -1916,7 +1916,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 @@ -1927,7 +1927,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(), @@ -1964,7 +1964,7 @@ export class Converter { regex = null; } - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: regex, raw: node.text, @@ -1976,14 +1976,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', @@ -1997,7 +1997,7 @@ export class Converter { }); } - return this.createNode(node, { + return this.createNode(node, { type: AST_NODE_TYPES.Literal, value: null, raw: 'null',