• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/types';
2import * as ts from 'typescript';
3import { TSNode } from './ts-nodes';
4
5export interface EstreeToTsNodeTypes {
6  [AST_NODE_TYPES.ArrayExpression]: ts.ArrayLiteralExpression;
7  [AST_NODE_TYPES.ArrayPattern]:
8    | ts.ArrayLiteralExpression
9    | ts.ArrayBindingPattern;
10  [AST_NODE_TYPES.ArrowFunctionExpression]: ts.ArrowFunction;
11  [AST_NODE_TYPES.AssignmentExpression]: ts.BinaryExpression;
12  [AST_NODE_TYPES.AssignmentPattern]:
13    | ts.ShorthandPropertyAssignment
14    | ts.BindingElement
15    | ts.BinaryExpression
16    | ts.ParameterDeclaration;
17  [AST_NODE_TYPES.AwaitExpression]: ts.AwaitExpression;
18  [AST_NODE_TYPES.BinaryExpression]: ts.BinaryExpression;
19  [AST_NODE_TYPES.BlockStatement]: ts.Block;
20  [AST_NODE_TYPES.BreakStatement]: ts.BreakStatement;
21  [AST_NODE_TYPES.CallExpression]: ts.CallExpression;
22  [AST_NODE_TYPES.CatchClause]: ts.CatchClause;
23  [AST_NODE_TYPES.ChainExpression]:
24    | ts.CallExpression
25    | ts.PropertyAccessExpression
26    | ts.ElementAccessExpression
27    | ts.NonNullExpression;
28  [AST_NODE_TYPES.ClassBody]: ts.ClassDeclaration | ts.ClassExpression;
29  [AST_NODE_TYPES.ClassDeclaration]: ts.ClassDeclaration;
30  [AST_NODE_TYPES.ClassExpression]: ts.ClassExpression;
31  [AST_NODE_TYPES.ClassProperty]: ts.PropertyDeclaration;
32  [AST_NODE_TYPES.ConditionalExpression]: ts.ConditionalExpression;
33  [AST_NODE_TYPES.ContinueStatement]: ts.ContinueStatement;
34  [AST_NODE_TYPES.DebuggerStatement]: ts.DebuggerStatement;
35  [AST_NODE_TYPES.Decorator]: ts.Decorator;
36  [AST_NODE_TYPES.DoWhileStatement]: ts.DoStatement;
37  [AST_NODE_TYPES.EmptyStatement]: ts.EmptyStatement;
38  [AST_NODE_TYPES.ExportAllDeclaration]: ts.ExportDeclaration;
39  [AST_NODE_TYPES.ExportDefaultDeclaration]:
40    | ts.ExportAssignment
41    | ts.FunctionDeclaration
42    | ts.VariableStatement
43    | ts.ClassDeclaration
44    | ts.ClassExpression
45    | ts.TypeAliasDeclaration
46    | ts.InterfaceDeclaration
47    | ts.EnumDeclaration
48    | ts.ModuleDeclaration;
49  [AST_NODE_TYPES.ExportNamedDeclaration]:
50    | ts.ExportDeclaration
51    | ts.FunctionDeclaration
52    | ts.VariableStatement
53    | ts.ClassDeclaration
54    | ts.ClassExpression
55    | ts.TypeAliasDeclaration
56    | ts.InterfaceDeclaration
57    | ts.EnumDeclaration
58    | ts.ModuleDeclaration;
59  [AST_NODE_TYPES.ExportSpecifier]: ts.ExportSpecifier;
60  [AST_NODE_TYPES.ExpressionStatement]: ts.ExpressionStatement;
61  [AST_NODE_TYPES.ForInStatement]: ts.ForInStatement;
62  [AST_NODE_TYPES.ForOfStatement]: ts.ForOfStatement;
63  [AST_NODE_TYPES.ForStatement]: ts.ForStatement;
64  [AST_NODE_TYPES.FunctionDeclaration]: ts.FunctionDeclaration;
65  [AST_NODE_TYPES.FunctionExpression]:
66    | ts.FunctionExpression
67    | ts.ConstructorDeclaration
68    | ts.GetAccessorDeclaration
69    | ts.SetAccessorDeclaration
70    | ts.MethodDeclaration;
71  [AST_NODE_TYPES.Identifier]:
72    | ts.Identifier
73    | ts.ConstructorDeclaration
74    | ts.Token<ts.SyntaxKind.NewKeyword | ts.SyntaxKind.ImportKeyword>;
75  [AST_NODE_TYPES.IfStatement]: ts.IfStatement;
76  [AST_NODE_TYPES.ImportDeclaration]: ts.ImportDeclaration;
77  [AST_NODE_TYPES.ImportDefaultSpecifier]: ts.ImportClause;
78  [AST_NODE_TYPES.ImportExpression]: ts.CallExpression;
79  [AST_NODE_TYPES.ImportNamespaceSpecifier]: ts.NamespaceImport;
80  [AST_NODE_TYPES.ImportSpecifier]: ts.ImportSpecifier;
81  [AST_NODE_TYPES.JSXAttribute]: ts.JsxAttribute;
82  [AST_NODE_TYPES.JSXClosingElement]: ts.JsxClosingElement;
83  [AST_NODE_TYPES.JSXClosingFragment]: ts.JsxClosingFragment;
84  [AST_NODE_TYPES.JSXElement]: ts.JsxElement | ts.JsxSelfClosingElement;
85  [AST_NODE_TYPES.JSXEmptyExpression]: ts.JsxExpression;
86  [AST_NODE_TYPES.JSXExpressionContainer]: ts.JsxExpression;
87  [AST_NODE_TYPES.JSXFragment]: ts.JsxFragment;
88  [AST_NODE_TYPES.JSXIdentifier]: ts.Identifier | ts.ThisExpression;
89  [AST_NODE_TYPES.JSXOpeningElement]:
90    | ts.JsxOpeningElement
91    | ts.JsxSelfClosingElement;
92  [AST_NODE_TYPES.JSXOpeningFragment]: ts.JsxOpeningFragment;
93  [AST_NODE_TYPES.JSXSpreadAttribute]: ts.JsxSpreadAttribute;
94  [AST_NODE_TYPES.JSXSpreadChild]: ts.JsxExpression;
95  [AST_NODE_TYPES.JSXMemberExpression]: ts.PropertyAccessExpression;
96  [AST_NODE_TYPES.JSXText]: ts.JsxText;
97  [AST_NODE_TYPES.LabeledStatement]: ts.LabeledStatement;
98  [AST_NODE_TYPES.Literal]:
99    | ts.StringLiteral
100    | ts.NumericLiteral
101    | ts.RegularExpressionLiteral
102    | ts.JsxText
103    | ts.NullLiteral
104    | ts.BooleanLiteral
105    | ts.BigIntLiteral;
106  [AST_NODE_TYPES.LogicalExpression]: ts.BinaryExpression;
107  [AST_NODE_TYPES.MemberExpression]:
108    | ts.PropertyAccessExpression
109    | ts.ElementAccessExpression;
110  [AST_NODE_TYPES.MetaProperty]: ts.MetaProperty;
111  [AST_NODE_TYPES.MethodDefinition]:
112    | ts.GetAccessorDeclaration
113    | ts.SetAccessorDeclaration
114    | ts.MethodDeclaration
115    | ts.ConstructorDeclaration;
116  [AST_NODE_TYPES.NewExpression]: ts.NewExpression;
117  [AST_NODE_TYPES.ObjectExpression]: ts.ObjectLiteralExpression;
118  [AST_NODE_TYPES.ObjectPattern]:
119    | ts.ObjectLiteralExpression
120    | ts.ObjectBindingPattern;
121  [AST_NODE_TYPES.Program]: ts.SourceFile;
122  [AST_NODE_TYPES.Property]:
123    | ts.PropertyAssignment
124    | ts.ShorthandPropertyAssignment
125    | ts.GetAccessorDeclaration
126    | ts.SetAccessorDeclaration
127    | ts.MethodDeclaration
128    | ts.BindingElement;
129  [AST_NODE_TYPES.RestElement]:
130    | ts.BindingElement
131    | ts.SpreadAssignment
132    | ts.SpreadElement
133    | ts.ParameterDeclaration;
134  [AST_NODE_TYPES.ReturnStatement]: ts.ReturnStatement;
135  [AST_NODE_TYPES.SequenceExpression]: ts.BinaryExpression;
136  [AST_NODE_TYPES.SpreadElement]: ts.SpreadElement | ts.SpreadAssignment;
137  [AST_NODE_TYPES.Super]: ts.SuperExpression;
138  [AST_NODE_TYPES.SwitchCase]: ts.CaseClause | ts.DefaultClause;
139  [AST_NODE_TYPES.SwitchStatement]: ts.SwitchStatement;
140  [AST_NODE_TYPES.TaggedTemplateExpression]: ts.TaggedTemplateExpression;
141  [AST_NODE_TYPES.TemplateElement]:
142    | ts.NoSubstitutionTemplateLiteral
143    | ts.TemplateHead
144    | ts.TemplateMiddle
145    | ts.TemplateTail;
146  [AST_NODE_TYPES.TemplateLiteral]:
147    | ts.NoSubstitutionTemplateLiteral
148    | ts.TemplateExpression;
149  [AST_NODE_TYPES.ThisExpression]: ts.ThisExpression | ts.KeywordTypeNode;
150  [AST_NODE_TYPES.ThrowStatement]: ts.ThrowStatement;
151  [AST_NODE_TYPES.TryStatement]: ts.TryStatement;
152  [AST_NODE_TYPES.TSAbstractClassProperty]: ts.PropertyDeclaration;
153  [AST_NODE_TYPES.TSAbstractMethodDefinition]:
154    | ts.GetAccessorDeclaration
155    | ts.SetAccessorDeclaration
156    | ts.MethodDeclaration
157    | ts.ConstructorDeclaration;
158  [AST_NODE_TYPES.TSArrayType]: ts.ArrayTypeNode;
159  [AST_NODE_TYPES.TSAsExpression]: ts.AsExpression;
160  [AST_NODE_TYPES.TSCallSignatureDeclaration]: ts.PropertySignature;
161  [AST_NODE_TYPES.TSClassImplements]: ts.ExpressionWithTypeArguments;
162  [AST_NODE_TYPES.TSConditionalType]: ts.ConditionalTypeNode;
163  [AST_NODE_TYPES.TSConstructorType]: ts.ConstructorTypeNode;
164  [AST_NODE_TYPES.TSConstructSignatureDeclaration]:
165    | ts.ConstructorTypeNode
166    | ts.FunctionTypeNode
167    | ts.ConstructSignatureDeclaration
168    | ts.CallSignatureDeclaration;
169  [AST_NODE_TYPES.TSDeclareFunction]: ts.FunctionDeclaration;
170  [AST_NODE_TYPES.TSEnumDeclaration]: ts.EnumDeclaration;
171  [AST_NODE_TYPES.TSEnumMember]: ts.EnumMember;
172  [AST_NODE_TYPES.TSExportAssignment]: ts.ExportAssignment;
173  [AST_NODE_TYPES.TSExternalModuleReference]: ts.ExternalModuleReference;
174  [AST_NODE_TYPES.TSFunctionType]: ts.FunctionTypeNode;
175  [AST_NODE_TYPES.TSImportEqualsDeclaration]: ts.ImportEqualsDeclaration;
176  [AST_NODE_TYPES.TSImportType]: ts.ImportTypeNode;
177  [AST_NODE_TYPES.TSIndexedAccessType]: ts.IndexedAccessTypeNode;
178  [AST_NODE_TYPES.TSIndexSignature]: ts.IndexSignatureDeclaration;
179  [AST_NODE_TYPES.TSInferType]: ts.InferTypeNode;
180  [AST_NODE_TYPES.TSInterfaceDeclaration]: ts.InterfaceDeclaration;
181  [AST_NODE_TYPES.TSInterfaceBody]: ts.InterfaceDeclaration;
182  [AST_NODE_TYPES.TSInterfaceHeritage]: ts.ExpressionWithTypeArguments;
183  [AST_NODE_TYPES.TSIntersectionType]: ts.IntersectionTypeNode;
184  [AST_NODE_TYPES.TSLiteralType]: ts.LiteralTypeNode;
185  [AST_NODE_TYPES.TSMappedType]: ts.MappedTypeNode;
186  [AST_NODE_TYPES.TSMethodSignature]: ts.MethodSignature;
187  [AST_NODE_TYPES.TSModuleBlock]: ts.ModuleBlock;
188  [AST_NODE_TYPES.TSModuleDeclaration]: ts.ModuleDeclaration;
189  [AST_NODE_TYPES.TSNamedTupleMember]: ts.NamedTupleMember;
190  [AST_NODE_TYPES.TSNamespaceExportDeclaration]: ts.NamespaceExportDeclaration;
191  [AST_NODE_TYPES.TSNonNullExpression]: ts.NonNullExpression;
192  [AST_NODE_TYPES.TSOptionalType]: ts.OptionalTypeNode;
193  [AST_NODE_TYPES.TSParameterProperty]: ts.ParameterDeclaration;
194  [AST_NODE_TYPES.TSParenthesizedType]: ts.ParenthesizedTypeNode;
195  [AST_NODE_TYPES.TSPropertySignature]: ts.PropertySignature;
196  [AST_NODE_TYPES.TSQualifiedName]: ts.QualifiedName;
197  [AST_NODE_TYPES.TSRestType]:
198    | ts.RestTypeNode
199    // for consistency and following babel's choices, a named tuple member with a rest gets converted to a TSRestType
200    | ts.NamedTupleMember;
201  [AST_NODE_TYPES.TSThisType]: ts.ThisTypeNode;
202  [AST_NODE_TYPES.TSTupleType]: ts.TupleTypeNode;
203  [AST_NODE_TYPES.TSTemplateLiteralType]: ts.TemplateLiteralTypeNode;
204  [AST_NODE_TYPES.TSTypeAliasDeclaration]: ts.TypeAliasDeclaration;
205  [AST_NODE_TYPES.TSTypeAnnotation]: undefined;
206  [AST_NODE_TYPES.TSTypeAssertion]: ts.TypeAssertion;
207  [AST_NODE_TYPES.TSTypeLiteral]: ts.TypeLiteralNode;
208  [AST_NODE_TYPES.TSTypeOperator]: ts.TypeOperatorNode;
209  [AST_NODE_TYPES.TSTypeParameter]: ts.TypeParameterDeclaration;
210  [AST_NODE_TYPES.TSTypeParameterDeclaration]: undefined;
211  [AST_NODE_TYPES.TSTypeParameterInstantiation]:
212    | ts.TaggedTemplateExpression
213    | ts.ImportTypeNode
214    | ts.ExpressionWithTypeArguments
215    | ts.TypeReferenceNode
216    | ts.JsxOpeningElement
217    | ts.JsxSelfClosingElement
218    | ts.NewExpression
219    | ts.CallExpression;
220  [AST_NODE_TYPES.TSTypePredicate]: ts.TypePredicateNode;
221  [AST_NODE_TYPES.TSTypeQuery]: ts.TypeQueryNode;
222  [AST_NODE_TYPES.TSTypeReference]: ts.TypeReferenceNode;
223  [AST_NODE_TYPES.TSUnionType]: ts.UnionTypeNode;
224  [AST_NODE_TYPES.UpdateExpression]:
225    | ts.PrefixUnaryExpression
226    | ts.PostfixUnaryExpression;
227  [AST_NODE_TYPES.UnaryExpression]:
228    | ts.PrefixUnaryExpression
229    | ts.PostfixUnaryExpression
230    | ts.DeleteExpression
231    | ts.VoidExpression
232    | ts.TypeOfExpression;
233  [AST_NODE_TYPES.VariableDeclaration]:
234    | ts.VariableDeclarationList
235    | ts.VariableStatement;
236  [AST_NODE_TYPES.VariableDeclarator]: ts.VariableDeclaration;
237  [AST_NODE_TYPES.WhileStatement]: ts.WhileStatement;
238  [AST_NODE_TYPES.WithStatement]: ts.WithStatement;
239  [AST_NODE_TYPES.YieldExpression]: ts.YieldExpression;
240
241  // Added by parser
242  // Should be same as AST_NODE_TYPES.FunctionExpression
243  [AST_NODE_TYPES.TSEmptyBodyFunctionExpression]:
244    | ts.FunctionExpression
245    | ts.ConstructorDeclaration
246    | ts.GetAccessorDeclaration
247    | ts.SetAccessorDeclaration
248    | ts.MethodDeclaration;
249
250  // Keywords
251  [AST_NODE_TYPES.TSAbstractKeyword]: ts.Token<ts.SyntaxKind.AbstractKeyword>;
252  [AST_NODE_TYPES.TSNullKeyword]: ts.NullLiteral | ts.KeywordTypeNode;
253
254  [AST_NODE_TYPES.TSAnyKeyword]: ts.KeywordTypeNode;
255  [AST_NODE_TYPES.TSBigIntKeyword]: ts.KeywordTypeNode;
256  [AST_NODE_TYPES.TSBooleanKeyword]: ts.KeywordTypeNode;
257  [AST_NODE_TYPES.TSNeverKeyword]: ts.KeywordTypeNode;
258  [AST_NODE_TYPES.TSNumberKeyword]: ts.KeywordTypeNode;
259  [AST_NODE_TYPES.TSObjectKeyword]: ts.KeywordTypeNode;
260  [AST_NODE_TYPES.TSStringKeyword]: ts.KeywordTypeNode;
261  [AST_NODE_TYPES.TSSymbolKeyword]: ts.KeywordTypeNode;
262  [AST_NODE_TYPES.TSUnknownKeyword]: ts.KeywordTypeNode;
263  [AST_NODE_TYPES.TSVoidKeyword]: ts.KeywordTypeNode;
264  [AST_NODE_TYPES.TSUndefinedKeyword]: ts.KeywordTypeNode;
265
266  // Unused
267  [AST_NODE_TYPES.TSAsyncKeyword]: ts.Token<ts.SyntaxKind.AsyncKeyword>;
268  [AST_NODE_TYPES.TSDeclareKeyword]: ts.Token<ts.SyntaxKind.DeclareKeyword>;
269  [AST_NODE_TYPES.TSExportKeyword]: ts.Token<ts.SyntaxKind.ExportKeyword>;
270  [AST_NODE_TYPES.TSStaticKeyword]: ts.Token<ts.SyntaxKind.StaticKeyword>;
271  [AST_NODE_TYPES.TSPublicKeyword]: ts.Token<ts.SyntaxKind.PublicKeyword>;
272  [AST_NODE_TYPES.TSPrivateKeyword]: ts.Token<ts.SyntaxKind.PrivateKeyword>;
273  [AST_NODE_TYPES.TSProtectedKeyword]: ts.Token<ts.SyntaxKind.ProtectedKeyword>;
274  [AST_NODE_TYPES.TSReadonlyKeyword]: ts.Token<ts.SyntaxKind.ReadonlyKeyword>;
275}
276
277/**
278 * Maps TSESTree AST Node type to the expected TypeScript AST Node type(s).
279 * This mapping is based on the internal logic of the parser.
280 */
281export type TSESTreeToTSNode<T extends TSESTree.Node = TSESTree.Node> = Extract<
282  TSNode | ts.Token<ts.SyntaxKind.NewKeyword | ts.SyntaxKind.ImportKeyword>,
283  // if this errors, it means that one of the AST_NODE_TYPES is not defined in the above interface
284  EstreeToTsNodeTypes[T['type']]
285>;
286