Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript-estree): correct issues in AST definition #3083

Merged
merged 6 commits into from Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -427,11 +427,9 @@ export type MethodDefinition =
export type Modifier =
| TSAbstractKeyword
| TSAsyncKeyword
| TSDeclareKeyword
| TSExportKeyword
| TSPublicKeyword
| TSPrivateKeyword
| TSProtectedKeyword
| TSPublicKeyword
| TSReadonlyKeyword
| TSStaticKeyword;
export type ObjectLiteralElementLike =
Expand Down Expand Up @@ -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;
/** 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 @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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[];
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really need to fix this - #2573

global?: boolean;
declare?: boolean;
modifiers?: Modifier[];
Expand Down
12 changes: 6 additions & 6 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -1921,7 +1921,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 @@ -1932,7 +1932,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 @@ -1969,7 +1969,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 @@ -1981,14 +1981,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 @@ -2002,7 +2002,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