Skip to content

Commit

Permalink
fix(typescript-estree): correct issues in AST definition (#3083)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 22, 2021
1 parent 409bf0b commit 509a117
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
10 changes: 6 additions & 4 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Expand Up @@ -408,10 +408,12 @@ export default util.createRule<Options, MessageIds>({
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;
}
}
}

Expand Down
43 changes: 20 additions & 23 deletions packages/types/src/ts-estree.ts
Expand Up @@ -428,11 +428,9 @@ export type MethodDefinition =
export type Modifier =
| TSAbstractKeyword
| TSAsyncKeyword
| TSDeclareKeyword
| TSExportKeyword
| TSPublicKeyword
| TSPrivateKeyword
| TSProtectedKeyword
| TSPublicKeyword
| TSReadonlyKeyword
| TSStaticKeyword;
export type ObjectLiteralElementLike =
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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[];
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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[];
Expand Down
12 changes: 6 additions & 6 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -1916,7 +1916,7 @@ export class Converter {
// Literals

case SyntaxKind.StringLiteral: {
return this.createNode<TSESTree.Literal>(node, {
return this.createNode<TSESTree.StringLiteral>(node, {
type: AST_NODE_TYPES.Literal,
value:
parent.kind === SyntaxKind.JsxAttribute
Expand All @@ -1927,7 +1927,7 @@ export class Converter {
}

case SyntaxKind.NumericLiteral: {
return this.createNode<TSESTree.Literal>(node, {
return this.createNode<TSESTree.NumberLiteral>(node, {
type: AST_NODE_TYPES.Literal,
value: Number(node.text),
raw: node.getText(),
Expand Down Expand Up @@ -1964,7 +1964,7 @@ export class Converter {
regex = null;
}

return this.createNode<TSESTree.Literal>(node, {
return this.createNode<TSESTree.RegExpLiteral>(node, {
type: AST_NODE_TYPES.Literal,
value: regex,
raw: node.text,
Expand All @@ -1976,14 +1976,14 @@ export class Converter {
}

case SyntaxKind.TrueKeyword:
return this.createNode<TSESTree.Literal>(node, {
return this.createNode<TSESTree.BooleanLiteral>(node, {
type: AST_NODE_TYPES.Literal,
value: true,
raw: 'true',
});

case SyntaxKind.FalseKeyword:
return this.createNode<TSESTree.Literal>(node, {
return this.createNode<TSESTree.BooleanLiteral>(node, {
type: AST_NODE_TYPES.Literal,
value: false,
raw: 'false',
Expand All @@ -1997,7 +1997,7 @@ export class Converter {
});
}

return this.createNode<TSESTree.Literal>(node, {
return this.createNode<TSESTree.NullLiteral>(node, {
type: AST_NODE_TYPES.Literal,
value: null,
raw: 'null',
Expand Down

0 comments on commit 509a117

Please sign in to comment.