Skip to content

Commit 509a117

Browse files
authored
fix(typescript-estree): correct issues in AST definition (typescript-eslint#3083)
1 parent 409bf0b commit 509a117

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

packages/eslint-plugin/src/rules/no-unused-vars.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,12 @@ export default util.createRule<Options, MessageIds>({
408408
return cached;
409409
}
410410

411-
for (const statement of node.body?.body ?? []) {
412-
if (statement.type === AST_NODE_TYPES.TSExportAssignment) {
413-
MODULE_DECL_CACHE.set(node, true);
414-
return true;
411+
if (node.body && node.body.type === AST_NODE_TYPES.TSModuleBlock) {
412+
for (const statement of node.body.body) {
413+
if (statement.type === AST_NODE_TYPES.TSExportAssignment) {
414+
MODULE_DECL_CACHE.set(node, true);
415+
return true;
416+
}
415417
}
416418
}
417419

packages/types/src/ts-estree.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,9 @@ export type MethodDefinition =
428428
export type Modifier =
429429
| TSAbstractKeyword
430430
| TSAsyncKeyword
431-
| TSDeclareKeyword
432-
| TSExportKeyword
433-
| TSPublicKeyword
434431
| TSPrivateKeyword
435432
| TSProtectedKeyword
433+
| TSPublicKeyword
436434
| TSReadonlyKeyword
437435
| TSStaticKeyword;
438436
export type ObjectLiteralElementLike =
@@ -465,20 +463,8 @@ export type PrimaryExpression =
465463
| TemplateLiteral
466464
| ThisExpression
467465
| TSNullKeyword;
468-
export type ProgramStatement =
469-
| ClassDeclaration
470-
| ExportAllDeclaration
471-
| ExportDefaultDeclaration
472-
| ExportNamedDeclaration
473-
| ImportDeclaration
474-
| Statement
475-
| TSDeclareFunction
476-
| TSEnumDeclaration
477-
| TSExportAssignment
478-
| TSImportEqualsDeclaration
479-
| TSInterfaceDeclaration
480-
| TSNamespaceExportDeclaration
481-
| TSTypeAliasDeclaration;
466+
/** TODO: re-align this with EStree spec in next major release */
467+
export type ProgramStatement = Statement;
482468
export type Property = PropertyComputedName | PropertyNonComputedName;
483469
export type PropertyName = PropertyNameComputed | PropertyNameNonComputed;
484470
export type PropertyNameComputed = Expression;
@@ -489,16 +475,27 @@ export type PropertyNameNonComputed =
489475
export type Statement =
490476
| BlockStatement
491477
| BreakStatement
478+
| ClassDeclaration
492479
| ContinueStatement
493480
| DebuggerStatement
494481
| DeclarationStatement
495482
| EmptyStatement
483+
| ExportAllDeclaration
484+
| ExportDefaultDeclaration
485+
| ExportNamedDeclaration
496486
| ExpressionStatement
497487
| IfStatement
498488
| IterationStatement
499489
| ImportDeclaration
500490
| LabeledStatement
491+
| TSDeclareFunction
492+
| TSEnumDeclaration
493+
| TSExportAssignment
494+
| TSImportEqualsDeclaration
495+
| TSInterfaceDeclaration
501496
| TSModuleBlock
497+
| TSNamespaceExportDeclaration
498+
| TSTypeAliasDeclaration
502499
| ReturnStatement
503500
| SwitchStatement
504501
| ThrowStatement
@@ -1079,7 +1076,7 @@ export interface JSXOpeningElement extends BaseNode {
10791076
typeParameters?: TSTypeParameterInstantiation;
10801077
selfClosing: boolean;
10811078
name: JSXTagNameExpression;
1082-
attributes: JSXAttribute[];
1079+
attributes: (JSXAttribute | JSXSpreadAttribute)[];
10831080
}
10841081

10851082
export interface JSXOpeningFragment extends BaseNode {
@@ -1170,7 +1167,7 @@ export interface ObjectPattern extends BaseNode {
11701167

11711168
export interface Program extends BaseNode {
11721169
type: AST_NODE_TYPES.Program;
1173-
body: ProgramStatement[];
1170+
body: Statement[];
11741171
sourceType: 'module' | 'script';
11751172
comments?: Comment[];
11761173
tokens?: Token[];
@@ -1188,7 +1185,7 @@ export interface PropertyNonComputedName extends PropertyBase {
11881185

11891186
export interface RegExpLiteral extends LiteralBase {
11901187
type: AST_NODE_TYPES.Literal;
1191-
value: RegExp;
1188+
value: RegExp | null;
11921189
}
11931190

11941191
export interface RestElement extends BaseNode {
@@ -1271,7 +1268,7 @@ export interface TryStatement extends BaseNode {
12711268
type: AST_NODE_TYPES.TryStatement;
12721269
block: BlockStatement;
12731270
handler: CatchClause | null;
1274-
finalizer: BlockStatement;
1271+
finalizer: BlockStatement | null;
12751272
}
12761273

12771274
export interface TSAbstractClassPropertyComputedName
@@ -1502,13 +1499,13 @@ export interface TSMethodSignatureNonComputedName
15021499

15031500
export interface TSModuleBlock extends BaseNode {
15041501
type: AST_NODE_TYPES.TSModuleBlock;
1505-
body: ProgramStatement[];
1502+
body: Statement[];
15061503
}
15071504

15081505
export interface TSModuleDeclaration extends BaseNode {
15091506
type: AST_NODE_TYPES.TSModuleDeclaration;
15101507
id: Identifier | Literal;
1511-
body?: TSModuleBlock;
1508+
body?: TSModuleBlock | TSModuleDeclaration;
15121509
global?: boolean;
15131510
declare?: boolean;
15141511
modifiers?: Modifier[];

packages/typescript-estree/src/convert.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,7 @@ export class Converter {
19161916
// Literals
19171917

19181918
case SyntaxKind.StringLiteral: {
1919-
return this.createNode<TSESTree.Literal>(node, {
1919+
return this.createNode<TSESTree.StringLiteral>(node, {
19201920
type: AST_NODE_TYPES.Literal,
19211921
value:
19221922
parent.kind === SyntaxKind.JsxAttribute
@@ -1927,7 +1927,7 @@ export class Converter {
19271927
}
19281928

19291929
case SyntaxKind.NumericLiteral: {
1930-
return this.createNode<TSESTree.Literal>(node, {
1930+
return this.createNode<TSESTree.NumberLiteral>(node, {
19311931
type: AST_NODE_TYPES.Literal,
19321932
value: Number(node.text),
19331933
raw: node.getText(),
@@ -1964,7 +1964,7 @@ export class Converter {
19641964
regex = null;
19651965
}
19661966

1967-
return this.createNode<TSESTree.Literal>(node, {
1967+
return this.createNode<TSESTree.RegExpLiteral>(node, {
19681968
type: AST_NODE_TYPES.Literal,
19691969
value: regex,
19701970
raw: node.text,
@@ -1976,14 +1976,14 @@ export class Converter {
19761976
}
19771977

19781978
case SyntaxKind.TrueKeyword:
1979-
return this.createNode<TSESTree.Literal>(node, {
1979+
return this.createNode<TSESTree.BooleanLiteral>(node, {
19801980
type: AST_NODE_TYPES.Literal,
19811981
value: true,
19821982
raw: 'true',
19831983
});
19841984

19851985
case SyntaxKind.FalseKeyword:
1986-
return this.createNode<TSESTree.Literal>(node, {
1986+
return this.createNode<TSESTree.BooleanLiteral>(node, {
19871987
type: AST_NODE_TYPES.Literal,
19881988
value: false,
19891989
raw: 'false',
@@ -1997,7 +1997,7 @@ export class Converter {
19971997
});
19981998
}
19991999

2000-
return this.createNode<TSESTree.Literal>(node, {
2000+
return this.createNode<TSESTree.NullLiteral>(node, {
20012001
type: AST_NODE_TYPES.Literal,
20022002
value: null,
20032003
raw: 'null',

0 commit comments

Comments
 (0)