2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5 }) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
9 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
14 var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
17 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
21 Object.defineProperty(exports, "__esModule", { value: true });
22 exports.Converter = exports.convertError = void 0;
23 // There's lots of funny stuff due to the typing of ts.Node
24 /* eslint-disable @typescript-eslint/no-explicit-any */
25 const ts = __importStar(require("typescript"));
26 const node_utils_1 = require("./node-utils");
27 const ts_estree_1 = require("./ts-estree");
28 const SyntaxKind = ts.SyntaxKind;
30 * Extends and formats a given error object
31 * @param error the error object
32 * @returns converted error object
34 function convertError(error) {
35 return node_utils_1.createError(error.file, error.start, error.message || error.messageText);
37 exports.convertError = convertError;
40 * Converts a TypeScript node into an ESTree node
41 * @param ast the full TypeScript AST
42 * @param options additional options for the conversion
43 * @returns the converted ESTreeNode
45 constructor(ast, options) {
46 this.esTreeNodeToTSNodeMap = new WeakMap();
47 this.tsNodeToESTreeNodeMap = new WeakMap();
48 this.allowPattern = false;
49 this.inTypeMode = false;
51 this.options = Object.assign({}, options);
55 esTreeNodeToTSNodeMap: this.esTreeNodeToTSNodeMap,
56 tsNodeToESTreeNodeMap: this.tsNodeToESTreeNodeMap,
60 return this.converter(this.ast);
63 * Converts a TypeScript node into an ESTree node.
64 * @param node the child ts.Node
65 * @param parent parentNode
66 * @param inTypeMode flag to determine if we are in typeMode
67 * @param allowPattern flag to determine if patterns are allowed
68 * @returns the converted ESTree node
70 converter(node, parent, inTypeMode, allowPattern) {
72 * Exit early for null and undefined
77 const typeMode = this.inTypeMode;
78 const pattern = this.allowPattern;
79 if (inTypeMode !== undefined) {
80 this.inTypeMode = inTypeMode;
82 if (allowPattern !== undefined) {
83 this.allowPattern = allowPattern;
85 const result = this.convertNode(node, (parent !== null && parent !== void 0 ? parent : node.parent));
86 this.registerTSNodeInNodeMap(node, result);
87 this.inTypeMode = typeMode;
88 this.allowPattern = pattern;
92 * Fixes the exports of the given ts.Node
93 * @param node the ts.Node
94 * @param result result
95 * @returns the ESTreeNode with fixed exports
97 fixExports(node, result) {
99 if (node.modifiers && node.modifiers[0].kind === SyntaxKind.ExportKeyword) {
101 * Make sure that original node is registered instead of export
103 this.registerTSNodeInNodeMap(node, result);
104 const exportKeyword = node.modifiers[0];
105 const nextModifier = node.modifiers[1];
106 const declarationIsDefault = nextModifier && nextModifier.kind === SyntaxKind.DefaultKeyword;
107 const varToken = declarationIsDefault
108 ? node_utils_1.findNextToken(nextModifier, this.ast, this.ast)
109 : node_utils_1.findNextToken(exportKeyword, this.ast, this.ast);
110 result.range[0] = varToken.getStart(this.ast);
111 result.loc = node_utils_1.getLocFor(result.range[0], result.range[1], this.ast);
112 if (declarationIsDefault) {
113 return this.createNode(node, {
114 type: ts_estree_1.AST_NODE_TYPES.ExportDefaultDeclaration,
116 range: [exportKeyword.getStart(this.ast), result.range[1]],
121 const isType = result.type === ts_estree_1.AST_NODE_TYPES.TSInterfaceDeclaration ||
122 result.type === ts_estree_1.AST_NODE_TYPES.TSTypeAliasDeclaration;
123 const isDeclare = result.declare === true;
124 return this.createNode(node, {
125 type: ts_estree_1.AST_NODE_TYPES.ExportNamedDeclaration,
129 exportKind: isType || isDeclare ? 'type' : 'value',
130 range: [exportKeyword.getStart(this.ast), result.range[1]],
137 * Register specific TypeScript node into map with first ESTree node provided
139 registerTSNodeInNodeMap(node, result) {
140 if (result && this.options.shouldPreserveNodeMaps) {
141 if (!this.tsNodeToESTreeNodeMap.has(node)) {
142 this.tsNodeToESTreeNodeMap.set(node, result);
147 * Converts a TypeScript node into an ESTree node.
148 * @param child the child ts.Node
149 * @param parent parentNode
150 * @returns the converted ESTree node
152 convertPattern(child, parent) {
153 return this.converter(child, parent, this.inTypeMode, true);
156 * Converts a TypeScript node into an ESTree node.
157 * @param child the child ts.Node
158 * @param parent parentNode
159 * @returns the converted ESTree node
161 convertChild(child, parent) {
162 return this.converter(child, parent, this.inTypeMode, false);
165 * Converts a TypeScript node into an ESTree node.
166 * @param child the child ts.Node
167 * @param parent parentNode
168 * @returns the converted ESTree node
170 convertType(child, parent) {
171 return this.converter(child, parent, true, false);
173 createNode(node, data) {
176 result.range = node_utils_1.getRange(
177 // this is completely valid, but TS hates it
181 result.loc = node_utils_1.getLocFor(result.range[0], result.range[1], this.ast);
183 if (result && this.options.shouldPreserveNodeMaps) {
184 this.esTreeNodeToTSNodeMap.set(result, node);
189 * Converts a child into a type annotation. This creates an intermediary
190 * TypeAnnotation node to match what Flow does.
191 * @param child The TypeScript AST node to convert.
192 * @param parent parentNode
193 * @returns The type annotation node.
195 convertTypeAnnotation(child, parent) {
196 // in FunctionType and ConstructorType typeAnnotation has 2 characters `=>` and in other places is just colon
197 const offset = parent.kind === SyntaxKind.FunctionType ||
198 parent.kind === SyntaxKind.ConstructorType
201 const annotationStartCol = child.getFullStart() - offset;
202 const loc = node_utils_1.getLocFor(annotationStartCol, child.end, this.ast);
204 type: ts_estree_1.AST_NODE_TYPES.TSTypeAnnotation,
206 range: [annotationStartCol, child.end],
207 typeAnnotation: this.convertType(child),
211 * Coverts body Nodes and add a directive field to StringLiterals
212 * @param nodes of ts.Node
213 * @param parent parentNode
214 * @returns Array of body statements
216 convertBodyExpressions(nodes, parent) {
217 let allowDirectives = node_utils_1.canContainDirective(parent);
220 const child = this.convertChild(statement);
221 if (allowDirectives) {
222 if ((child === null || child === void 0 ? void 0 : child.expression) &&
223 ts.isExpressionStatement(statement) &&
224 ts.isStringLiteral(statement.expression)) {
225 const raw = child.expression.raw;
226 child.directive = raw.slice(1, -1);
227 return child; // child can be null, but it's filtered below
230 allowDirectives = false;
233 return child; // child can be null, but it's filtered below
235 // filter out unknown nodes for now
236 .filter(statement => statement));
239 * Converts a ts.Node's typeArguments to TSTypeParameterInstantiation node
240 * @param typeArguments ts.NodeArray typeArguments
241 * @param node parent used to create this node
242 * @returns TypeParameterInstantiation node
244 convertTypeArgumentsToTypeParameters(typeArguments, node) {
245 const greaterThanToken = node_utils_1.findNextToken(typeArguments, this.ast, this.ast);
246 return this.createNode(node, {
247 type: ts_estree_1.AST_NODE_TYPES.TSTypeParameterInstantiation,
248 range: [typeArguments.pos - 1, greaterThanToken.end],
249 params: typeArguments.map(typeArgument => this.convertType(typeArgument)),
253 * Converts a ts.Node's typeParameters to TSTypeParameterDeclaration node
254 * @param typeParameters ts.Node typeParameters
255 * @returns TypeParameterDeclaration node
257 convertTSTypeParametersToTypeParametersDeclaration(typeParameters) {
258 const greaterThanToken = node_utils_1.findNextToken(typeParameters, this.ast, this.ast);
260 type: ts_estree_1.AST_NODE_TYPES.TSTypeParameterDeclaration,
261 range: [typeParameters.pos - 1, greaterThanToken.end],
262 loc: node_utils_1.getLocFor(typeParameters.pos - 1, greaterThanToken.end, this.ast),
263 params: typeParameters.map(typeParameter => this.convertType(typeParameter)),
267 * Converts an array of ts.Node parameters into an array of ESTreeNode params
268 * @param parameters An array of ts.Node params to be converted
269 * @returns an array of converted ESTreeNode params
271 convertParameters(parameters) {
272 if (!parameters || !parameters.length) {
275 return parameters.map(param => {
277 const convertedParam = this.convertChild(param);
278 if ((_a = param.decorators) === null || _a === void 0 ? void 0 : _a.length) {
279 convertedParam.decorators = param.decorators.map(el => this.convertChild(el));
281 return convertedParam;
285 * For nodes that are copied directly from the TypeScript AST into
286 * ESTree mostly as-is. The only difference is the addition of a type
287 * property instead of a kind property. Recursively copies all children.
290 if (node.kind === ts.SyntaxKind.JSDocFunctionType) {
291 throw node_utils_1.createError(this.ast, node.pos, 'JSDoc types can only be used inside documentation comments.');
293 const customType = `TS${SyntaxKind[node.kind]}`;
295 * If the "errorOnUnknownASTType" option is set to true, throw an error,
296 * otherwise fallback to just including the unknown type as-is.
298 if (this.options.errorOnUnknownASTType && !ts_estree_1.AST_NODE_TYPES[customType]) {
299 throw new Error(`Unknown AST_NODE_TYPE: "${customType}"`);
301 const result = this.createNode(node, {
304 if ('type' in node) {
305 result.typeAnnotation =
306 node.type && 'kind' in node.type && ts.isTypeNode(node.type)
307 ? this.convertTypeAnnotation(node.type, node)
310 if ('typeArguments' in node) {
311 result.typeParameters =
312 node.typeArguments && 'pos' in node.typeArguments
313 ? this.convertTypeArgumentsToTypeParameters(node.typeArguments, node)
316 if ('typeParameters' in node) {
317 result.typeParameters =
318 node.typeParameters && 'pos' in node.typeParameters
319 ? this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters)
322 if ('decorators' in node && node.decorators && node.decorators.length) {
323 result.decorators = node.decorators.map(el => this.convertChild(el));
326 .filter(([key]) => !/^(?:_children|kind|parent|pos|end|flags|modifierFlagsCache|jsDoc|type|typeArguments|typeParameters|decorators)$/.test(key))
327 .forEach(([key, value]) => {
328 if (Array.isArray(value)) {
329 result[key] = value.map(el => this.convertChild(el));
331 else if (value && typeof value === 'object' && value.kind) {
332 // need to check node[key].kind to ensure we don't try to convert a symbol
333 result[key] = this.convertChild(value);
342 * Converts a TypeScript JSX node.tagName into an ESTree node.name
343 * @param node the tagName object from a JSX ts.Node
345 * @returns the converted ESTree name object
347 convertJSXTagName(node, parent) {
350 case SyntaxKind.PropertyAccessExpression:
351 if (node.name.kind === SyntaxKind.PrivateIdentifier) {
352 // This is one of the few times where TS explicitly errors, and doesn't even gracefully handle the syntax.
353 // So we shouldn't ever get into this state to begin with.
354 throw new Error('Non-private identifier expected.');
356 result = this.createNode(node, {
357 type: ts_estree_1.AST_NODE_TYPES.JSXMemberExpression,
358 object: this.convertJSXTagName(node.expression, parent),
359 property: this.convertJSXTagName(node.name, parent),
362 case SyntaxKind.ThisKeyword:
363 result = this.createNode(node, {
364 type: ts_estree_1.AST_NODE_TYPES.JSXIdentifier,
368 case SyntaxKind.Identifier:
370 result = this.createNode(node, {
371 type: ts_estree_1.AST_NODE_TYPES.JSXIdentifier,
376 this.registerTSNodeInNodeMap(node, result);
380 * Applies the given TS modifiers to the given result object.
382 * @param modifiers original ts.Nodes from the node.modifiers array
383 * @returns the current result object will be mutated
384 * @deprecated This method adds not standardized `modifiers` property in nodes
386 applyModifiersToResult(result, modifiers) {
387 if (!modifiers || !modifiers.length) {
391 * Some modifiers are explicitly handled by applying them as
392 * boolean values on the result node. As well as adding them
393 * to the result, we remove them from the array, so that they
394 * are not handled twice.
396 const handledModifierIndices = {};
397 for (let i = 0; i < modifiers.length; i++) {
398 const modifier = modifiers[i];
399 switch (modifier.kind) {
401 * Ignore ExportKeyword and DefaultKeyword, they are handled
402 * via the fixExports utility function
404 case SyntaxKind.ExportKeyword:
405 case SyntaxKind.DefaultKeyword:
406 handledModifierIndices[i] = true;
408 case SyntaxKind.ConstKeyword:
410 handledModifierIndices[i] = true;
412 case SyntaxKind.DeclareKeyword:
413 result.declare = true;
414 handledModifierIndices[i] = true;
420 * If there are still valid modifiers available which have
421 * not been explicitly handled above, we just convert and
422 * add the modifiers array to the result node.
424 const remainingModifiers = modifiers.filter((_, i) => !handledModifierIndices[i]);
425 if (!remainingModifiers || !remainingModifiers.length) {
428 result.modifiers = remainingModifiers.map(el => this.convertChild(el));
431 * Uses the provided range location to adjust the location data of the given Node
432 * @param result The node that will have its location data mutated
433 * @param childRange The child node range used to expand location
435 fixParentLocation(result, childRange) {
436 if (childRange[0] < result.range[0]) {
437 result.range[0] = childRange[0];
438 result.loc.start = node_utils_1.getLineAndCharacterFor(result.range[0], this.ast);
440 if (childRange[1] > result.range[1]) {
441 result.range[1] = childRange[1];
442 result.loc.end = node_utils_1.getLineAndCharacterFor(result.range[1], this.ast);
446 * Converts a TypeScript node into an ESTree node.
447 * The core of the conversion logic:
448 * Identify and convert each relevant TypeScript SyntaxKind
449 * @param node the child ts.Node
450 * @param parent parentNode
451 * @returns the converted ESTree node
453 convertNode(node, parent) {
454 var _a, _b, _c, _d, _e, _f, _g, _h, _j;
456 case SyntaxKind.SourceFile: {
457 return this.createNode(node, {
458 type: ts_estree_1.AST_NODE_TYPES.Program,
459 body: this.convertBodyExpressions(node.statements, node),
460 sourceType: node.externalModuleIndicator ? 'module' : 'script',
461 range: [node.getStart(this.ast), node.endOfFileToken.end],
464 case SyntaxKind.Block: {
465 return this.createNode(node, {
466 type: ts_estree_1.AST_NODE_TYPES.BlockStatement,
467 body: this.convertBodyExpressions(node.statements, node),
470 case SyntaxKind.Identifier: {
471 return this.createNode(node, {
472 type: ts_estree_1.AST_NODE_TYPES.Identifier,
476 case SyntaxKind.WithStatement:
477 return this.createNode(node, {
478 type: ts_estree_1.AST_NODE_TYPES.WithStatement,
479 object: this.convertChild(node.expression),
480 body: this.convertChild(node.statement),
483 case SyntaxKind.ReturnStatement:
484 return this.createNode(node, {
485 type: ts_estree_1.AST_NODE_TYPES.ReturnStatement,
486 argument: this.convertChild(node.expression),
488 case SyntaxKind.LabeledStatement:
489 return this.createNode(node, {
490 type: ts_estree_1.AST_NODE_TYPES.LabeledStatement,
491 label: this.convertChild(node.label),
492 body: this.convertChild(node.statement),
494 case SyntaxKind.ContinueStatement:
495 return this.createNode(node, {
496 type: ts_estree_1.AST_NODE_TYPES.ContinueStatement,
497 label: this.convertChild(node.label),
499 case SyntaxKind.BreakStatement:
500 return this.createNode(node, {
501 type: ts_estree_1.AST_NODE_TYPES.BreakStatement,
502 label: this.convertChild(node.label),
505 case SyntaxKind.IfStatement:
506 return this.createNode(node, {
507 type: ts_estree_1.AST_NODE_TYPES.IfStatement,
508 test: this.convertChild(node.expression),
509 consequent: this.convertChild(node.thenStatement),
510 alternate: this.convertChild(node.elseStatement),
512 case SyntaxKind.SwitchStatement:
513 return this.createNode(node, {
514 type: ts_estree_1.AST_NODE_TYPES.SwitchStatement,
515 discriminant: this.convertChild(node.expression),
516 cases: node.caseBlock.clauses.map(el => this.convertChild(el)),
518 case SyntaxKind.CaseClause:
519 case SyntaxKind.DefaultClause:
520 return this.createNode(node, {
521 type: ts_estree_1.AST_NODE_TYPES.SwitchCase,
522 // expression is present in case only
523 test: node.kind === SyntaxKind.CaseClause
524 ? this.convertChild(node.expression)
526 consequent: node.statements.map(el => this.convertChild(el)),
529 case SyntaxKind.ThrowStatement:
530 return this.createNode(node, {
531 type: ts_estree_1.AST_NODE_TYPES.ThrowStatement,
532 argument: this.convertChild(node.expression),
534 case SyntaxKind.TryStatement:
535 return this.createNode(node, {
536 type: ts_estree_1.AST_NODE_TYPES.TryStatement,
537 block: this.convertChild(node.tryBlock),
538 handler: this.convertChild(node.catchClause),
539 finalizer: this.convertChild(node.finallyBlock),
541 case SyntaxKind.CatchClause:
542 return this.createNode(node, {
543 type: ts_estree_1.AST_NODE_TYPES.CatchClause,
544 param: node.variableDeclaration
545 ? this.convertChild(node.variableDeclaration.name)
547 body: this.convertChild(node.block),
550 case SyntaxKind.WhileStatement:
551 return this.createNode(node, {
552 type: ts_estree_1.AST_NODE_TYPES.WhileStatement,
553 test: this.convertChild(node.expression),
554 body: this.convertChild(node.statement),
557 * Unlike other parsers, TypeScript calls a "DoWhileStatement"
560 case SyntaxKind.DoStatement:
561 return this.createNode(node, {
562 type: ts_estree_1.AST_NODE_TYPES.DoWhileStatement,
563 test: this.convertChild(node.expression),
564 body: this.convertChild(node.statement),
566 case SyntaxKind.ForStatement:
567 return this.createNode(node, {
568 type: ts_estree_1.AST_NODE_TYPES.ForStatement,
569 init: this.convertChild(node.initializer),
570 test: this.convertChild(node.condition),
571 update: this.convertChild(node.incrementor),
572 body: this.convertChild(node.statement),
574 case SyntaxKind.ForInStatement:
575 return this.createNode(node, {
576 type: ts_estree_1.AST_NODE_TYPES.ForInStatement,
577 left: this.convertPattern(node.initializer),
578 right: this.convertChild(node.expression),
579 body: this.convertChild(node.statement),
581 case SyntaxKind.ForOfStatement:
582 return this.createNode(node, {
583 type: ts_estree_1.AST_NODE_TYPES.ForOfStatement,
584 left: this.convertPattern(node.initializer),
585 right: this.convertChild(node.expression),
586 body: this.convertChild(node.statement),
587 await: Boolean(node.awaitModifier &&
588 node.awaitModifier.kind === SyntaxKind.AwaitKeyword),
591 case SyntaxKind.FunctionDeclaration: {
592 const isDeclare = node_utils_1.hasModifier(SyntaxKind.DeclareKeyword, node);
593 const result = this.createNode(node, {
594 type: isDeclare || !node.body
595 ? ts_estree_1.AST_NODE_TYPES.TSDeclareFunction
596 : ts_estree_1.AST_NODE_TYPES.FunctionDeclaration,
597 id: this.convertChild(node.name),
598 generator: !!node.asteriskToken,
600 async: node_utils_1.hasModifier(SyntaxKind.AsyncKeyword, node),
601 params: this.convertParameters(node.parameters),
602 body: this.convertChild(node.body) || undefined,
604 // Process returnType
606 result.returnType = this.convertTypeAnnotation(node.type, node);
609 result.declare = true;
611 // Process typeParameters
612 if (node.typeParameters) {
613 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
616 * Semantically, decorators are not allowed on function declarations,
617 * but the TypeScript compiler will parse them and produce a valid AST,
618 * so we handle them here too.
620 if (node.decorators) {
621 result.decorators = node.decorators.map(el => this.convertChild(el));
624 return this.fixExports(node, result);
626 case SyntaxKind.VariableDeclaration: {
627 const result = this.createNode(node, {
628 type: ts_estree_1.AST_NODE_TYPES.VariableDeclarator,
629 id: this.convertPattern(node.name),
630 init: this.convertChild(node.initializer),
632 if (node.exclamationToken) {
633 result.definite = true;
636 result.id.typeAnnotation = this.convertTypeAnnotation(node.type, node);
637 this.fixParentLocation(result.id, result.id.typeAnnotation.range);
641 case SyntaxKind.VariableStatement: {
642 const result = this.createNode(node, {
643 type: ts_estree_1.AST_NODE_TYPES.VariableDeclaration,
644 declarations: node.declarationList.declarations.map(el => this.convertChild(el)),
645 kind: node_utils_1.getDeclarationKind(node.declarationList),
648 * Semantically, decorators are not allowed on variable declarations,
649 * but the TypeScript compiler will parse them and produce a valid AST,
650 * so we handle them here too.
652 if (node.decorators) {
653 result.decorators = node.decorators.map(el => this.convertChild(el));
655 if (node_utils_1.hasModifier(SyntaxKind.DeclareKeyword, node)) {
656 result.declare = true;
659 return this.fixExports(node, result);
661 // mostly for for-of, for-in
662 case SyntaxKind.VariableDeclarationList:
663 return this.createNode(node, {
664 type: ts_estree_1.AST_NODE_TYPES.VariableDeclaration,
665 declarations: node.declarations.map(el => this.convertChild(el)),
666 kind: node_utils_1.getDeclarationKind(node),
669 case SyntaxKind.ExpressionStatement:
670 return this.createNode(node, {
671 type: ts_estree_1.AST_NODE_TYPES.ExpressionStatement,
672 expression: this.convertChild(node.expression),
674 case SyntaxKind.ThisKeyword:
675 return this.createNode(node, {
676 type: ts_estree_1.AST_NODE_TYPES.ThisExpression,
678 case SyntaxKind.ArrayLiteralExpression: {
679 // TypeScript uses ArrayLiteralExpression in destructuring assignment, too
680 if (this.allowPattern) {
681 return this.createNode(node, {
682 type: ts_estree_1.AST_NODE_TYPES.ArrayPattern,
683 elements: node.elements.map(el => this.convertPattern(el)),
687 return this.createNode(node, {
688 type: ts_estree_1.AST_NODE_TYPES.ArrayExpression,
689 elements: node.elements.map(el => this.convertChild(el)),
693 case SyntaxKind.ObjectLiteralExpression: {
694 // TypeScript uses ObjectLiteralExpression in destructuring assignment, too
695 if (this.allowPattern) {
696 return this.createNode(node, {
697 type: ts_estree_1.AST_NODE_TYPES.ObjectPattern,
698 properties: node.properties.map(el => this.convertPattern(el)),
702 return this.createNode(node, {
703 type: ts_estree_1.AST_NODE_TYPES.ObjectExpression,
704 properties: node.properties.map(el => this.convertChild(el)),
708 case SyntaxKind.PropertyAssignment:
709 return this.createNode(node, {
710 type: ts_estree_1.AST_NODE_TYPES.Property,
711 key: this.convertChild(node.name),
712 value: this.converter(node.initializer, node, this.inTypeMode, this.allowPattern),
713 computed: node_utils_1.isComputedProperty(node.name),
718 case SyntaxKind.ShorthandPropertyAssignment: {
719 if (node.objectAssignmentInitializer) {
720 return this.createNode(node, {
721 type: ts_estree_1.AST_NODE_TYPES.Property,
722 key: this.convertChild(node.name),
723 value: this.createNode(node, {
724 type: ts_estree_1.AST_NODE_TYPES.AssignmentPattern,
725 left: this.convertPattern(node.name),
726 right: this.convertChild(node.objectAssignmentInitializer),
735 return this.createNode(node, {
736 type: ts_estree_1.AST_NODE_TYPES.Property,
737 key: this.convertChild(node.name),
738 value: this.convertChild(node.name),
746 case SyntaxKind.ComputedPropertyName:
747 return this.convertChild(node.expression);
748 case SyntaxKind.PropertyDeclaration: {
749 const isAbstract = node_utils_1.hasModifier(SyntaxKind.AbstractKeyword, node);
750 const result = this.createNode(node, {
752 ? ts_estree_1.AST_NODE_TYPES.TSAbstractClassProperty
753 : ts_estree_1.AST_NODE_TYPES.ClassProperty,
754 key: this.convertChild(node.name),
755 value: this.convertChild(node.initializer),
756 computed: node_utils_1.isComputedProperty(node.name),
757 static: node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node),
758 readonly: node_utils_1.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
759 declare: node_utils_1.hasModifier(SyntaxKind.DeclareKeyword, node),
762 result.typeAnnotation = this.convertTypeAnnotation(node.type, node);
764 if (node.decorators) {
765 result.decorators = node.decorators.map(el => this.convertChild(el));
767 const accessibility = node_utils_1.getTSNodeAccessibility(node);
769 result.accessibility = accessibility;
771 if ((node.name.kind === SyntaxKind.Identifier ||
772 node.name.kind === SyntaxKind.ComputedPropertyName) &&
773 node.questionToken) {
774 result.optional = true;
776 if (node.exclamationToken) {
777 result.definite = true;
779 if (result.key.type === ts_estree_1.AST_NODE_TYPES.Literal && node.questionToken) {
780 result.optional = true;
784 case SyntaxKind.GetAccessor:
785 case SyntaxKind.SetAccessor:
786 case SyntaxKind.MethodDeclaration: {
787 const method = this.createNode(node, {
789 ? ts_estree_1.AST_NODE_TYPES.TSEmptyBodyFunctionExpression
790 : ts_estree_1.AST_NODE_TYPES.FunctionExpression,
792 generator: !!node.asteriskToken,
794 async: node_utils_1.hasModifier(SyntaxKind.AsyncKeyword, node),
795 body: this.convertChild(node.body),
796 range: [node.parameters.pos - 1, node.end],
800 method.returnType = this.convertTypeAnnotation(node.type, node);
802 // Process typeParameters
803 if (node.typeParameters) {
804 method.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
805 this.fixParentLocation(method, method.typeParameters.range);
808 if (parent.kind === SyntaxKind.ObjectLiteralExpression) {
809 method.params = node.parameters.map(el => this.convertChild(el));
810 result = this.createNode(node, {
811 type: ts_estree_1.AST_NODE_TYPES.Property,
812 key: this.convertChild(node.name),
814 computed: node_utils_1.isComputedProperty(node.name),
815 method: node.kind === SyntaxKind.MethodDeclaration,
823 * Unlike in object literal methods, class method params can have decorators
825 method.params = this.convertParameters(node.parameters);
827 * TypeScript class methods can be defined as "abstract"
829 const methodDefinitionType = node_utils_1.hasModifier(SyntaxKind.AbstractKeyword, node)
830 ? ts_estree_1.AST_NODE_TYPES.TSAbstractMethodDefinition
831 : ts_estree_1.AST_NODE_TYPES.MethodDefinition;
832 result = this.createNode(node, {
833 type: methodDefinitionType,
834 key: this.convertChild(node.name),
836 computed: node_utils_1.isComputedProperty(node.name),
837 static: node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node),
840 if (node.decorators) {
841 result.decorators = node.decorators.map(el => this.convertChild(el));
843 const accessibility = node_utils_1.getTSNodeAccessibility(node);
845 result.accessibility = accessibility;
848 if (node.questionToken) {
849 result.optional = true;
851 if (node.kind === SyntaxKind.GetAccessor) {
854 else if (node.kind === SyntaxKind.SetAccessor) {
857 else if (!result.static &&
858 node.name.kind === SyntaxKind.StringLiteral &&
859 node.name.text === 'constructor' &&
860 result.type !== ts_estree_1.AST_NODE_TYPES.Property) {
861 result.kind = 'constructor';
865 // TypeScript uses this even for static methods named "constructor"
866 case SyntaxKind.Constructor: {
867 const lastModifier = node_utils_1.getLastModifier(node);
868 const constructorToken = (lastModifier && node_utils_1.findNextToken(lastModifier, node, this.ast)) ||
869 node.getFirstToken();
870 const constructor = this.createNode(node, {
872 ? ts_estree_1.AST_NODE_TYPES.TSEmptyBodyFunctionExpression
873 : ts_estree_1.AST_NODE_TYPES.FunctionExpression,
875 params: this.convertParameters(node.parameters),
879 body: this.convertChild(node.body),
880 range: [node.parameters.pos - 1, node.end],
882 // Process typeParameters
883 if (node.typeParameters) {
884 constructor.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
885 this.fixParentLocation(constructor, constructor.typeParameters.range);
887 // Process returnType
889 constructor.returnType = this.convertTypeAnnotation(node.type, node);
891 const constructorKey = this.createNode(node, {
892 type: ts_estree_1.AST_NODE_TYPES.Identifier,
894 range: [constructorToken.getStart(this.ast), constructorToken.end],
896 const isStatic = node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node);
897 const result = this.createNode(node, {
898 type: node_utils_1.hasModifier(SyntaxKind.AbstractKeyword, node)
899 ? ts_estree_1.AST_NODE_TYPES.TSAbstractMethodDefinition
900 : ts_estree_1.AST_NODE_TYPES.MethodDefinition,
905 kind: isStatic ? 'method' : 'constructor',
907 const accessibility = node_utils_1.getTSNodeAccessibility(node);
909 result.accessibility = accessibility;
913 case SyntaxKind.FunctionExpression: {
914 const result = this.createNode(node, {
915 type: ts_estree_1.AST_NODE_TYPES.FunctionExpression,
916 id: this.convertChild(node.name),
917 generator: !!node.asteriskToken,
918 params: this.convertParameters(node.parameters),
919 body: this.convertChild(node.body),
920 async: node_utils_1.hasModifier(SyntaxKind.AsyncKeyword, node),
923 // Process returnType
925 result.returnType = this.convertTypeAnnotation(node.type, node);
927 // Process typeParameters
928 if (node.typeParameters) {
929 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
933 case SyntaxKind.SuperKeyword:
934 return this.createNode(node, {
935 type: ts_estree_1.AST_NODE_TYPES.Super,
937 case SyntaxKind.ArrayBindingPattern:
938 return this.createNode(node, {
939 type: ts_estree_1.AST_NODE_TYPES.ArrayPattern,
940 elements: node.elements.map(el => this.convertPattern(el)),
942 // occurs with missing array elements like [,]
943 case SyntaxKind.OmittedExpression:
945 case SyntaxKind.ObjectBindingPattern:
946 return this.createNode(node, {
947 type: ts_estree_1.AST_NODE_TYPES.ObjectPattern,
948 properties: node.elements.map(el => this.convertPattern(el)),
950 case SyntaxKind.BindingElement: {
951 if (parent.kind === SyntaxKind.ArrayBindingPattern) {
952 const arrayItem = this.convertChild(node.name, parent);
953 if (node.initializer) {
954 return this.createNode(node, {
955 type: ts_estree_1.AST_NODE_TYPES.AssignmentPattern,
957 right: this.convertChild(node.initializer),
960 else if (node.dotDotDotToken) {
961 return this.createNode(node, {
962 type: ts_estree_1.AST_NODE_TYPES.RestElement,
972 if (node.dotDotDotToken) {
973 result = this.createNode(node, {
974 type: ts_estree_1.AST_NODE_TYPES.RestElement,
975 argument: this.convertChild((_a = node.propertyName) !== null && _a !== void 0 ? _a : node.name),
979 result = this.createNode(node, {
980 type: ts_estree_1.AST_NODE_TYPES.Property,
981 key: this.convertChild((_b = node.propertyName) !== null && _b !== void 0 ? _b : node.name),
982 value: this.convertChild(node.name),
983 computed: Boolean(node.propertyName &&
984 node.propertyName.kind === SyntaxKind.ComputedPropertyName),
986 shorthand: !node.propertyName,
990 if (node.initializer) {
991 result.value = this.createNode(node, {
992 type: ts_estree_1.AST_NODE_TYPES.AssignmentPattern,
993 left: this.convertChild(node.name),
994 right: this.convertChild(node.initializer),
995 range: [node.name.getStart(this.ast), node.initializer.end],
1001 case SyntaxKind.ArrowFunction: {
1002 const result = this.createNode(node, {
1003 type: ts_estree_1.AST_NODE_TYPES.ArrowFunctionExpression,
1006 params: this.convertParameters(node.parameters),
1007 body: this.convertChild(node.body),
1008 async: node_utils_1.hasModifier(SyntaxKind.AsyncKeyword, node),
1009 expression: node.body.kind !== SyntaxKind.Block,
1011 // Process returnType
1013 result.returnType = this.convertTypeAnnotation(node.type, node);
1015 // Process typeParameters
1016 if (node.typeParameters) {
1017 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1021 case SyntaxKind.YieldExpression:
1022 return this.createNode(node, {
1023 type: ts_estree_1.AST_NODE_TYPES.YieldExpression,
1024 delegate: !!node.asteriskToken,
1025 argument: this.convertChild(node.expression),
1027 case SyntaxKind.AwaitExpression:
1028 return this.createNode(node, {
1029 type: ts_estree_1.AST_NODE_TYPES.AwaitExpression,
1030 argument: this.convertChild(node.expression),
1032 // Template Literals
1033 case SyntaxKind.NoSubstitutionTemplateLiteral:
1034 return this.createNode(node, {
1035 type: ts_estree_1.AST_NODE_TYPES.TemplateLiteral,
1037 this.createNode(node, {
1038 type: ts_estree_1.AST_NODE_TYPES.TemplateElement,
1040 raw: this.ast.text.slice(node.getStart(this.ast) + 1, node.end - 1),
1048 case SyntaxKind.TemplateExpression: {
1049 const result = this.createNode(node, {
1050 type: ts_estree_1.AST_NODE_TYPES.TemplateLiteral,
1051 quasis: [this.convertChild(node.head)],
1054 node.templateSpans.forEach(templateSpan => {
1055 result.expressions.push(this.convertChild(templateSpan.expression));
1056 result.quasis.push(this.convertChild(templateSpan.literal));
1060 case SyntaxKind.TaggedTemplateExpression:
1061 return this.createNode(node, {
1062 type: ts_estree_1.AST_NODE_TYPES.TaggedTemplateExpression,
1063 typeParameters: node.typeArguments
1064 ? this.convertTypeArgumentsToTypeParameters(node.typeArguments, node)
1066 tag: this.convertChild(node.tag),
1067 quasi: this.convertChild(node.template),
1069 case SyntaxKind.TemplateHead:
1070 case SyntaxKind.TemplateMiddle:
1071 case SyntaxKind.TemplateTail: {
1072 const tail = node.kind === SyntaxKind.TemplateTail;
1073 return this.createNode(node, {
1074 type: ts_estree_1.AST_NODE_TYPES.TemplateElement,
1076 raw: this.ast.text.slice(node.getStart(this.ast) + 1, node.end - (tail ? 1 : 2)),
1083 case SyntaxKind.SpreadAssignment:
1084 case SyntaxKind.SpreadElement: {
1085 if (this.allowPattern) {
1086 return this.createNode(node, {
1087 type: ts_estree_1.AST_NODE_TYPES.RestElement,
1088 argument: this.convertPattern(node.expression),
1092 return this.createNode(node, {
1093 type: ts_estree_1.AST_NODE_TYPES.SpreadElement,
1094 argument: this.convertChild(node.expression),
1098 case SyntaxKind.Parameter: {
1101 if (node.dotDotDotToken) {
1102 parameter = result = this.createNode(node, {
1103 type: ts_estree_1.AST_NODE_TYPES.RestElement,
1104 argument: this.convertChild(node.name),
1107 else if (node.initializer) {
1108 parameter = this.convertChild(node.name);
1109 result = this.createNode(node, {
1110 type: ts_estree_1.AST_NODE_TYPES.AssignmentPattern,
1112 right: this.convertChild(node.initializer),
1114 if (node.modifiers) {
1115 // AssignmentPattern should not contain modifiers in range
1116 result.range[0] = parameter.range[0];
1117 result.loc = node_utils_1.getLocFor(result.range[0], result.range[1], this.ast);
1121 parameter = result = this.convertChild(node.name, parent);
1124 parameter.typeAnnotation = this.convertTypeAnnotation(node.type, node);
1125 this.fixParentLocation(parameter, parameter.typeAnnotation.range);
1127 if (node.questionToken) {
1128 if (node.questionToken.end > parameter.range[1]) {
1129 parameter.range[1] = node.questionToken.end;
1130 parameter.loc.end = node_utils_1.getLineAndCharacterFor(parameter.range[1], this.ast);
1132 parameter.optional = true;
1134 if (node.modifiers) {
1135 return this.createNode(node, {
1136 type: ts_estree_1.AST_NODE_TYPES.TSParameterProperty,
1137 accessibility: (_c = node_utils_1.getTSNodeAccessibility(node)) !== null && _c !== void 0 ? _c : undefined,
1138 readonly: node_utils_1.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
1139 static: node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node) || undefined,
1140 export: node_utils_1.hasModifier(SyntaxKind.ExportKeyword, node) || undefined,
1147 case SyntaxKind.ClassDeclaration:
1148 case SyntaxKind.ClassExpression: {
1149 const heritageClauses = (_d = node.heritageClauses) !== null && _d !== void 0 ? _d : [];
1150 const classNodeType = node.kind === SyntaxKind.ClassDeclaration
1151 ? ts_estree_1.AST_NODE_TYPES.ClassDeclaration
1152 : ts_estree_1.AST_NODE_TYPES.ClassExpression;
1153 const superClass = heritageClauses.find(clause => clause.token === SyntaxKind.ExtendsKeyword);
1154 const implementsClause = heritageClauses.find(clause => clause.token === SyntaxKind.ImplementsKeyword);
1155 const result = this.createNode(node, {
1156 type: classNodeType,
1157 id: this.convertChild(node.name),
1158 body: this.createNode(node, {
1159 type: ts_estree_1.AST_NODE_TYPES.ClassBody,
1161 range: [node.members.pos - 1, node.end],
1163 superClass: (superClass === null || superClass === void 0 ? void 0 : superClass.types[0]) ? this.convertChild(superClass.types[0].expression)
1167 if (superClass.types.length > 1) {
1168 throw node_utils_1.createError(this.ast, superClass.types[1].pos, 'Classes can only extend a single class.');
1170 if ((_e = superClass.types[0]) === null || _e === void 0 ? void 0 : _e.typeArguments) {
1171 result.superTypeParameters = this.convertTypeArgumentsToTypeParameters(superClass.types[0].typeArguments, superClass.types[0]);
1174 if (node.typeParameters) {
1175 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1177 if (implementsClause) {
1178 result.implements = implementsClause.types.map(el => this.convertChild(el));
1181 * TypeScript class declarations can be defined as "abstract"
1183 if (node_utils_1.hasModifier(SyntaxKind.AbstractKeyword, node)) {
1184 result.abstract = true;
1186 if (node_utils_1.hasModifier(SyntaxKind.DeclareKeyword, node)) {
1187 result.declare = true;
1189 if (node.decorators) {
1190 result.decorators = node.decorators.map(el => this.convertChild(el));
1192 const filteredMembers = node.members.filter(node_utils_1.isESTreeClassMember);
1193 if (filteredMembers.length) {
1194 result.body.body = filteredMembers.map(el => this.convertChild(el));
1196 // check for exports
1197 return this.fixExports(node, result);
1200 case SyntaxKind.ModuleBlock:
1201 return this.createNode(node, {
1202 type: ts_estree_1.AST_NODE_TYPES.TSModuleBlock,
1203 body: this.convertBodyExpressions(node.statements, node),
1205 case SyntaxKind.ImportDeclaration: {
1206 const result = this.createNode(node, {
1207 type: ts_estree_1.AST_NODE_TYPES.ImportDeclaration,
1208 source: this.convertChild(node.moduleSpecifier),
1210 importKind: 'value',
1212 if (node.importClause) {
1213 if (node.importClause.isTypeOnly) {
1214 result.importKind = 'type';
1216 if (node.importClause.name) {
1217 result.specifiers.push(this.convertChild(node.importClause));
1219 if (node.importClause.namedBindings) {
1220 switch (node.importClause.namedBindings.kind) {
1221 case SyntaxKind.NamespaceImport:
1222 result.specifiers.push(this.convertChild(node.importClause.namedBindings));
1224 case SyntaxKind.NamedImports:
1225 result.specifiers = result.specifiers.concat(node.importClause.namedBindings.elements.map(el => this.convertChild(el)));
1232 case SyntaxKind.NamespaceImport:
1233 return this.createNode(node, {
1234 type: ts_estree_1.AST_NODE_TYPES.ImportNamespaceSpecifier,
1235 local: this.convertChild(node.name),
1237 case SyntaxKind.ImportSpecifier:
1238 return this.createNode(node, {
1239 type: ts_estree_1.AST_NODE_TYPES.ImportSpecifier,
1240 local: this.convertChild(node.name),
1241 imported: this.convertChild((_f = node.propertyName) !== null && _f !== void 0 ? _f : node.name),
1243 case SyntaxKind.ImportClause:
1244 return this.createNode(node, {
1245 type: ts_estree_1.AST_NODE_TYPES.ImportDefaultSpecifier,
1246 local: this.convertChild(node.name),
1247 range: [node.getStart(this.ast), node.name.end],
1249 case SyntaxKind.ExportDeclaration:
1250 if (((_g = node.exportClause) === null || _g === void 0 ? void 0 : _g.kind) === SyntaxKind.NamedExports) {
1251 return this.createNode(node, {
1252 type: ts_estree_1.AST_NODE_TYPES.ExportNamedDeclaration,
1253 source: this.convertChild(node.moduleSpecifier),
1254 specifiers: node.exportClause.elements.map(el => this.convertChild(el)),
1255 exportKind: node.isTypeOnly ? 'type' : 'value',
1260 return this.createNode(node, {
1261 type: ts_estree_1.AST_NODE_TYPES.ExportAllDeclaration,
1262 source: this.convertChild(node.moduleSpecifier),
1263 exportKind: node.isTypeOnly ? 'type' : 'value',
1265 // note - for compat with 3.7.x, where node.exportClause is always undefined and
1266 // SyntaxKind.NamespaceExport does not exist yet (i.e. is undefined), this
1267 // cannot be shortened to an optional chain, or else you end up with
1268 // undefined === undefined, and the true path will hard error at runtime
1269 node.exportClause &&
1270 node.exportClause.kind === SyntaxKind.NamespaceExport
1271 ? this.convertChild(node.exportClause.name)
1275 case SyntaxKind.ExportSpecifier:
1276 return this.createNode(node, {
1277 type: ts_estree_1.AST_NODE_TYPES.ExportSpecifier,
1278 local: this.convertChild((_h = node.propertyName) !== null && _h !== void 0 ? _h : node.name),
1279 exported: this.convertChild(node.name),
1281 case SyntaxKind.ExportAssignment:
1282 if (node.isExportEquals) {
1283 return this.createNode(node, {
1284 type: ts_estree_1.AST_NODE_TYPES.TSExportAssignment,
1285 expression: this.convertChild(node.expression),
1289 return this.createNode(node, {
1290 type: ts_estree_1.AST_NODE_TYPES.ExportDefaultDeclaration,
1291 declaration: this.convertChild(node.expression),
1292 exportKind: 'value',
1296 case SyntaxKind.PrefixUnaryExpression:
1297 case SyntaxKind.PostfixUnaryExpression: {
1298 const operator = node_utils_1.getTextForTokenKind(node.operator);
1300 * ESTree uses UpdateExpression for ++/--
1302 if (operator === '++' || operator === '--') {
1303 return this.createNode(node, {
1304 type: ts_estree_1.AST_NODE_TYPES.UpdateExpression,
1306 prefix: node.kind === SyntaxKind.PrefixUnaryExpression,
1307 argument: this.convertChild(node.operand),
1311 return this.createNode(node, {
1312 type: ts_estree_1.AST_NODE_TYPES.UnaryExpression,
1314 prefix: node.kind === SyntaxKind.PrefixUnaryExpression,
1315 argument: this.convertChild(node.operand),
1319 case SyntaxKind.DeleteExpression:
1320 return this.createNode(node, {
1321 type: ts_estree_1.AST_NODE_TYPES.UnaryExpression,
1324 argument: this.convertChild(node.expression),
1326 case SyntaxKind.VoidExpression:
1327 return this.createNode(node, {
1328 type: ts_estree_1.AST_NODE_TYPES.UnaryExpression,
1331 argument: this.convertChild(node.expression),
1333 case SyntaxKind.TypeOfExpression:
1334 return this.createNode(node, {
1335 type: ts_estree_1.AST_NODE_TYPES.UnaryExpression,
1338 argument: this.convertChild(node.expression),
1340 case SyntaxKind.TypeOperator:
1341 return this.createNode(node, {
1342 type: ts_estree_1.AST_NODE_TYPES.TSTypeOperator,
1343 operator: node_utils_1.getTextForTokenKind(node.operator),
1344 typeAnnotation: this.convertChild(node.type),
1346 // Binary Operations
1347 case SyntaxKind.BinaryExpression: {
1348 // TypeScript uses BinaryExpression for sequences as well
1349 if (node_utils_1.isComma(node.operatorToken)) {
1350 const result = this.createNode(node, {
1351 type: ts_estree_1.AST_NODE_TYPES.SequenceExpression,
1354 const left = this.convertChild(node.left);
1355 if (left.type === ts_estree_1.AST_NODE_TYPES.SequenceExpression &&
1356 node.left.kind !== SyntaxKind.ParenthesizedExpression) {
1357 result.expressions = result.expressions.concat(left.expressions);
1360 result.expressions.push(left);
1362 result.expressions.push(this.convertChild(node.right));
1366 const type = node_utils_1.getBinaryExpressionType(node.operatorToken);
1367 if (this.allowPattern &&
1368 type === ts_estree_1.AST_NODE_TYPES.AssignmentExpression) {
1369 return this.createNode(node, {
1370 type: ts_estree_1.AST_NODE_TYPES.AssignmentPattern,
1371 left: this.convertPattern(node.left, node),
1372 right: this.convertChild(node.right),
1375 return this.createNode(node, {
1377 operator: node_utils_1.getTextForTokenKind(node.operatorToken.kind),
1378 left: this.converter(node.left, node, this.inTypeMode, type === ts_estree_1.AST_NODE_TYPES.AssignmentExpression),
1379 right: this.convertChild(node.right),
1383 case SyntaxKind.PropertyAccessExpression: {
1384 const object = this.convertChild(node.expression);
1385 const property = this.convertChild(node.name);
1386 const computed = false;
1387 const isLocallyOptional = node.questionDotToken !== undefined;
1388 // the optional expression should propagate up the member expression tree
1389 const isChildOptional = node_utils_1.isChildOptionalChain(node, object);
1390 if (isLocallyOptional || isChildOptional) {
1391 return this.createNode(node, {
1392 type: ts_estree_1.AST_NODE_TYPES.OptionalMemberExpression,
1396 optional: isLocallyOptional,
1400 return this.createNode(node, {
1401 type: ts_estree_1.AST_NODE_TYPES.MemberExpression,
1409 case SyntaxKind.ElementAccessExpression: {
1410 const object = this.convertChild(node.expression);
1411 const property = this.convertChild(node.argumentExpression);
1412 const computed = true;
1413 const isLocallyOptional = node.questionDotToken !== undefined;
1414 // the optional expression should propagate up the member expression tree
1415 const isChildOptional = node_utils_1.isChildOptionalChain(node, object);
1416 if (isLocallyOptional || isChildOptional) {
1417 return this.createNode(node, {
1418 type: ts_estree_1.AST_NODE_TYPES.OptionalMemberExpression,
1422 optional: isLocallyOptional,
1426 return this.createNode(node, {
1427 type: ts_estree_1.AST_NODE_TYPES.MemberExpression,
1435 case SyntaxKind.CallExpression: {
1436 if (node.expression.kind === SyntaxKind.ImportKeyword) {
1437 if (node.arguments.length !== 1) {
1438 throw node_utils_1.createError(this.ast, node.arguments.pos, 'Dynamic import must have one specifier as an argument.');
1440 return this.createNode(node, {
1441 type: ts_estree_1.AST_NODE_TYPES.ImportExpression,
1442 source: this.convertChild(node.arguments[0]),
1445 const callee = this.convertChild(node.expression);
1446 const args = node.arguments.map(el => this.convertChild(el));
1448 const isLocallyOptional = node.questionDotToken !== undefined;
1449 // the optional expression should propagate up the member expression tree
1450 const isChildOptional = node_utils_1.isChildOptionalChain(node, callee);
1451 if (isLocallyOptional || isChildOptional) {
1452 result = this.createNode(node, {
1453 type: ts_estree_1.AST_NODE_TYPES.OptionalCallExpression,
1456 optional: isLocallyOptional,
1460 result = this.createNode(node, {
1461 type: ts_estree_1.AST_NODE_TYPES.CallExpression,
1467 if (node.typeArguments) {
1468 result.typeParameters = this.convertTypeArgumentsToTypeParameters(node.typeArguments, node);
1472 case SyntaxKind.NewExpression: {
1473 // NOTE - NewExpression cannot have an optional chain in it
1474 const result = this.createNode(node, {
1475 type: ts_estree_1.AST_NODE_TYPES.NewExpression,
1476 callee: this.convertChild(node.expression),
1477 arguments: node.arguments
1478 ? node.arguments.map(el => this.convertChild(el))
1481 if (node.typeArguments) {
1482 result.typeParameters = this.convertTypeArgumentsToTypeParameters(node.typeArguments, node);
1486 case SyntaxKind.ConditionalExpression:
1487 return this.createNode(node, {
1488 type: ts_estree_1.AST_NODE_TYPES.ConditionalExpression,
1489 test: this.convertChild(node.condition),
1490 consequent: this.convertChild(node.whenTrue),
1491 alternate: this.convertChild(node.whenFalse),
1493 case SyntaxKind.MetaProperty: {
1494 return this.createNode(node, {
1495 type: ts_estree_1.AST_NODE_TYPES.MetaProperty,
1496 meta: this.createNode(
1497 // TODO: do we really want to convert it to Token?
1498 node.getFirstToken(), {
1499 type: ts_estree_1.AST_NODE_TYPES.Identifier,
1500 name: node_utils_1.getTextForTokenKind(node.keywordToken),
1502 property: this.convertChild(node.name),
1505 case SyntaxKind.Decorator: {
1506 return this.createNode(node, {
1507 type: ts_estree_1.AST_NODE_TYPES.Decorator,
1508 expression: this.convertChild(node.expression),
1512 case SyntaxKind.StringLiteral: {
1513 const result = this.createNode(node, {
1514 type: ts_estree_1.AST_NODE_TYPES.Literal,
1518 result.raw = this.ast.text.slice(result.range[0], result.range[1]);
1519 if ('name' in parent && parent.name === node) {
1520 result.value = node.text;
1523 result.value = node_utils_1.unescapeStringLiteralText(node.text);
1527 case SyntaxKind.NumericLiteral: {
1528 return this.createNode(node, {
1529 type: ts_estree_1.AST_NODE_TYPES.Literal,
1530 value: Number(node.text),
1531 raw: node.getText(),
1534 case SyntaxKind.BigIntLiteral: {
1535 const range = node_utils_1.getRange(node, this.ast);
1536 const rawValue = this.ast.text.slice(range[0], range[1]);
1537 const bigint = rawValue
1538 // remove suffix `n`
1540 // `BigInt` doesn't accept numeric separator
1541 // and `bigint` property should not include numeric separator
1543 const value = typeof BigInt !== 'undefined' ? BigInt(bigint) : null;
1544 return this.createNode(node, {
1545 type: ts_estree_1.AST_NODE_TYPES.Literal,
1548 bigint: value === null ? bigint : String(value),
1552 case SyntaxKind.RegularExpressionLiteral: {
1553 const pattern = node.text.slice(1, node.text.lastIndexOf('/'));
1554 const flags = node.text.slice(node.text.lastIndexOf('/') + 1);
1557 regex = new RegExp(pattern, flags);
1562 return this.createNode(node, {
1563 type: ts_estree_1.AST_NODE_TYPES.Literal,
1572 case SyntaxKind.TrueKeyword:
1573 return this.createNode(node, {
1574 type: ts_estree_1.AST_NODE_TYPES.Literal,
1578 case SyntaxKind.FalseKeyword:
1579 return this.createNode(node, {
1580 type: ts_estree_1.AST_NODE_TYPES.Literal,
1584 case SyntaxKind.NullKeyword: {
1585 if (this.inTypeMode) {
1586 return this.createNode(node, {
1587 type: ts_estree_1.AST_NODE_TYPES.TSNullKeyword,
1591 return this.createNode(node, {
1592 type: ts_estree_1.AST_NODE_TYPES.Literal,
1598 case SyntaxKind.EmptyStatement:
1599 return this.createNode(node, {
1600 type: ts_estree_1.AST_NODE_TYPES.EmptyStatement,
1602 case SyntaxKind.DebuggerStatement:
1603 return this.createNode(node, {
1604 type: ts_estree_1.AST_NODE_TYPES.DebuggerStatement,
1607 case SyntaxKind.JsxElement:
1608 return this.createNode(node, {
1609 type: ts_estree_1.AST_NODE_TYPES.JSXElement,
1610 openingElement: this.convertChild(node.openingElement),
1611 closingElement: this.convertChild(node.closingElement),
1612 children: node.children.map(el => this.convertChild(el)),
1614 case SyntaxKind.JsxFragment:
1615 return this.createNode(node, {
1616 type: ts_estree_1.AST_NODE_TYPES.JSXFragment,
1617 openingFragment: this.convertChild(node.openingFragment),
1618 closingFragment: this.convertChild(node.closingFragment),
1619 children: node.children.map(el => this.convertChild(el)),
1621 case SyntaxKind.JsxSelfClosingElement: {
1622 return this.createNode(node, {
1623 type: ts_estree_1.AST_NODE_TYPES.JSXElement,
1625 * Convert SyntaxKind.JsxSelfClosingElement to SyntaxKind.JsxOpeningElement,
1626 * TypeScript does not seem to have the idea of openingElement when tag is self-closing
1628 openingElement: this.createNode(node, {
1629 type: ts_estree_1.AST_NODE_TYPES.JSXOpeningElement,
1630 typeParameters: node.typeArguments
1631 ? this.convertTypeArgumentsToTypeParameters(node.typeArguments, node)
1634 name: this.convertJSXTagName(node.tagName, node),
1635 attributes: node.attributes.properties.map(el => this.convertChild(el)),
1636 range: node_utils_1.getRange(node, this.ast),
1638 closingElement: null,
1642 case SyntaxKind.JsxOpeningElement:
1643 return this.createNode(node, {
1644 type: ts_estree_1.AST_NODE_TYPES.JSXOpeningElement,
1645 typeParameters: node.typeArguments
1646 ? this.convertTypeArgumentsToTypeParameters(node.typeArguments, node)
1649 name: this.convertJSXTagName(node.tagName, node),
1650 attributes: node.attributes.properties.map(el => this.convertChild(el)),
1652 case SyntaxKind.JsxClosingElement:
1653 return this.createNode(node, {
1654 type: ts_estree_1.AST_NODE_TYPES.JSXClosingElement,
1655 name: this.convertJSXTagName(node.tagName, node),
1657 case SyntaxKind.JsxOpeningFragment:
1658 return this.createNode(node, {
1659 type: ts_estree_1.AST_NODE_TYPES.JSXOpeningFragment,
1661 case SyntaxKind.JsxClosingFragment:
1662 return this.createNode(node, {
1663 type: ts_estree_1.AST_NODE_TYPES.JSXClosingFragment,
1665 case SyntaxKind.JsxExpression: {
1666 const expression = node.expression
1667 ? this.convertChild(node.expression)
1668 : this.createNode(node, {
1669 type: ts_estree_1.AST_NODE_TYPES.JSXEmptyExpression,
1670 range: [node.getStart(this.ast) + 1, node.getEnd() - 1],
1672 if (node.dotDotDotToken) {
1673 return this.createNode(node, {
1674 type: ts_estree_1.AST_NODE_TYPES.JSXSpreadChild,
1679 return this.createNode(node, {
1680 type: ts_estree_1.AST_NODE_TYPES.JSXExpressionContainer,
1685 case SyntaxKind.JsxAttribute: {
1686 const attributeName = this.convertChild(node.name);
1687 attributeName.type = ts_estree_1.AST_NODE_TYPES.JSXIdentifier;
1688 return this.createNode(node, {
1689 type: ts_estree_1.AST_NODE_TYPES.JSXAttribute,
1690 name: attributeName,
1691 value: this.convertChild(node.initializer),
1695 * The JSX AST changed the node type for string literals
1696 * inside a JSX Element from `Literal` to `JSXText`. We
1697 * provide a flag to support both types until `Literal`
1698 * node type is deprecated in ESLint v5.
1700 case SyntaxKind.JsxText: {
1701 const start = node.getFullStart();
1702 const end = node.getEnd();
1703 if (this.options.useJSXTextNode) {
1704 return this.createNode(node, {
1705 type: ts_estree_1.AST_NODE_TYPES.JSXText,
1706 value: this.ast.text.slice(start, end),
1707 raw: this.ast.text.slice(start, end),
1708 range: [start, end],
1712 return this.createNode(node, {
1713 type: ts_estree_1.AST_NODE_TYPES.Literal,
1714 value: this.ast.text.slice(start, end),
1715 raw: this.ast.text.slice(start, end),
1716 range: [start, end],
1720 case SyntaxKind.JsxSpreadAttribute:
1721 return this.createNode(node, {
1722 type: ts_estree_1.AST_NODE_TYPES.JSXSpreadAttribute,
1723 argument: this.convertChild(node.expression),
1725 case SyntaxKind.QualifiedName: {
1726 return this.createNode(node, {
1727 type: ts_estree_1.AST_NODE_TYPES.TSQualifiedName,
1728 left: this.convertChild(node.left),
1729 right: this.convertChild(node.right),
1732 // TypeScript specific
1733 case SyntaxKind.TypeReference: {
1734 return this.createNode(node, {
1735 type: ts_estree_1.AST_NODE_TYPES.TSTypeReference,
1736 typeName: this.convertType(node.typeName),
1737 typeParameters: node.typeArguments
1738 ? this.convertTypeArgumentsToTypeParameters(node.typeArguments, node)
1742 case SyntaxKind.TypeParameter: {
1743 return this.createNode(node, {
1744 type: ts_estree_1.AST_NODE_TYPES.TSTypeParameter,
1745 name: this.convertType(node.name),
1746 constraint: node.constraint
1747 ? this.convertType(node.constraint)
1749 default: node.default ? this.convertType(node.default) : undefined,
1752 case SyntaxKind.ThisType:
1753 return this.createNode(node, {
1754 type: ts_estree_1.AST_NODE_TYPES.TSThisType,
1756 case SyntaxKind.AnyKeyword:
1757 case SyntaxKind.BigIntKeyword:
1758 case SyntaxKind.BooleanKeyword:
1759 case SyntaxKind.NeverKeyword:
1760 case SyntaxKind.NumberKeyword:
1761 case SyntaxKind.ObjectKeyword:
1762 case SyntaxKind.StringKeyword:
1763 case SyntaxKind.SymbolKeyword:
1764 case SyntaxKind.UnknownKeyword:
1765 case SyntaxKind.VoidKeyword:
1766 case SyntaxKind.UndefinedKeyword: {
1767 return this.createNode(node, {
1768 type: ts_estree_1.AST_NODE_TYPES[`TS${SyntaxKind[node.kind]}`],
1771 case SyntaxKind.NonNullExpression: {
1772 return this.createNode(node, {
1773 type: ts_estree_1.AST_NODE_TYPES.TSNonNullExpression,
1774 expression: this.convertChild(node.expression),
1777 case SyntaxKind.TypeLiteral: {
1778 return this.createNode(node, {
1779 type: ts_estree_1.AST_NODE_TYPES.TSTypeLiteral,
1780 members: node.members.map(el => this.convertChild(el)),
1783 case SyntaxKind.ArrayType: {
1784 return this.createNode(node, {
1785 type: ts_estree_1.AST_NODE_TYPES.TSArrayType,
1786 elementType: this.convertType(node.elementType),
1789 case SyntaxKind.IndexedAccessType: {
1790 return this.createNode(node, {
1791 type: ts_estree_1.AST_NODE_TYPES.TSIndexedAccessType,
1792 objectType: this.convertType(node.objectType),
1793 indexType: this.convertType(node.indexType),
1796 case SyntaxKind.ConditionalType: {
1797 return this.createNode(node, {
1798 type: ts_estree_1.AST_NODE_TYPES.TSConditionalType,
1799 checkType: this.convertType(node.checkType),
1800 extendsType: this.convertType(node.extendsType),
1801 trueType: this.convertType(node.trueType),
1802 falseType: this.convertType(node.falseType),
1805 case SyntaxKind.TypeQuery: {
1806 return this.createNode(node, {
1807 type: ts_estree_1.AST_NODE_TYPES.TSTypeQuery,
1808 exprName: this.convertType(node.exprName),
1811 case SyntaxKind.MappedType: {
1812 const result = this.createNode(node, {
1813 type: ts_estree_1.AST_NODE_TYPES.TSMappedType,
1814 typeParameter: this.convertType(node.typeParameter),
1816 if (node.readonlyToken) {
1817 if (node.readonlyToken.kind === SyntaxKind.ReadonlyKeyword) {
1818 result.readonly = true;
1821 result.readonly = node_utils_1.getTextForTokenKind(node.readonlyToken.kind);
1824 if (node.questionToken) {
1825 if (node.questionToken.kind === SyntaxKind.QuestionToken) {
1826 result.optional = true;
1829 result.optional = node_utils_1.getTextForTokenKind(node.questionToken.kind);
1833 result.typeAnnotation = this.convertType(node.type);
1837 case SyntaxKind.ParenthesizedExpression:
1838 return this.convertChild(node.expression, parent);
1839 case SyntaxKind.TypeAliasDeclaration: {
1840 const result = this.createNode(node, {
1841 type: ts_estree_1.AST_NODE_TYPES.TSTypeAliasDeclaration,
1842 id: this.convertChild(node.name),
1843 typeAnnotation: this.convertType(node.type),
1845 if (node_utils_1.hasModifier(SyntaxKind.DeclareKeyword, node)) {
1846 result.declare = true;
1848 // Process typeParameters
1849 if (node.typeParameters) {
1850 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1852 // check for exports
1853 return this.fixExports(node, result);
1855 case SyntaxKind.MethodSignature: {
1856 const result = this.createNode(node, {
1857 type: ts_estree_1.AST_NODE_TYPES.TSMethodSignature,
1858 computed: node_utils_1.isComputedProperty(node.name),
1859 key: this.convertChild(node.name),
1860 params: this.convertParameters(node.parameters),
1862 if (node_utils_1.isOptional(node)) {
1863 result.optional = true;
1866 result.returnType = this.convertTypeAnnotation(node.type, node);
1868 if (node_utils_1.hasModifier(SyntaxKind.ReadonlyKeyword, node)) {
1869 result.readonly = true;
1871 if (node.typeParameters) {
1872 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1874 const accessibility = node_utils_1.getTSNodeAccessibility(node);
1875 if (accessibility) {
1876 result.accessibility = accessibility;
1878 if (node_utils_1.hasModifier(SyntaxKind.ExportKeyword, node)) {
1879 result.export = true;
1881 if (node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node)) {
1882 result.static = true;
1886 case SyntaxKind.PropertySignature: {
1887 const result = this.createNode(node, {
1888 type: ts_estree_1.AST_NODE_TYPES.TSPropertySignature,
1889 optional: node_utils_1.isOptional(node) || undefined,
1890 computed: node_utils_1.isComputedProperty(node.name),
1891 key: this.convertChild(node.name),
1892 typeAnnotation: node.type
1893 ? this.convertTypeAnnotation(node.type, node)
1895 initializer: this.convertChild(node.initializer) || undefined,
1896 readonly: node_utils_1.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
1897 static: node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node) || undefined,
1898 export: node_utils_1.hasModifier(SyntaxKind.ExportKeyword, node) || undefined,
1900 const accessibility = node_utils_1.getTSNodeAccessibility(node);
1901 if (accessibility) {
1902 result.accessibility = accessibility;
1906 case SyntaxKind.IndexSignature: {
1907 const result = this.createNode(node, {
1908 type: ts_estree_1.AST_NODE_TYPES.TSIndexSignature,
1909 parameters: node.parameters.map(el => this.convertChild(el)),
1912 result.typeAnnotation = this.convertTypeAnnotation(node.type, node);
1914 if (node_utils_1.hasModifier(SyntaxKind.ReadonlyKeyword, node)) {
1915 result.readonly = true;
1917 const accessibility = node_utils_1.getTSNodeAccessibility(node);
1918 if (accessibility) {
1919 result.accessibility = accessibility;
1921 if (node_utils_1.hasModifier(SyntaxKind.ExportKeyword, node)) {
1922 result.export = true;
1924 if (node_utils_1.hasModifier(SyntaxKind.StaticKeyword, node)) {
1925 result.static = true;
1929 case SyntaxKind.ConstructorType:
1930 case SyntaxKind.FunctionType:
1931 case SyntaxKind.ConstructSignature:
1932 case SyntaxKind.CallSignature: {
1934 switch (node.kind) {
1935 case SyntaxKind.ConstructSignature:
1936 type = ts_estree_1.AST_NODE_TYPES.TSConstructSignatureDeclaration;
1938 case SyntaxKind.CallSignature:
1939 type = ts_estree_1.AST_NODE_TYPES.TSCallSignatureDeclaration;
1941 case SyntaxKind.FunctionType:
1942 type = ts_estree_1.AST_NODE_TYPES.TSFunctionType;
1944 case SyntaxKind.ConstructorType:
1946 type = ts_estree_1.AST_NODE_TYPES.TSConstructorType;
1949 const result = this.createNode(node, {
1951 params: this.convertParameters(node.parameters),
1954 result.returnType = this.convertTypeAnnotation(node.type, node);
1956 if (node.typeParameters) {
1957 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1961 case SyntaxKind.ExpressionWithTypeArguments: {
1962 const result = this.createNode(node, {
1963 type: parent && parent.kind === SyntaxKind.InterfaceDeclaration
1964 ? ts_estree_1.AST_NODE_TYPES.TSInterfaceHeritage
1965 : ts_estree_1.AST_NODE_TYPES.TSClassImplements,
1966 expression: this.convertChild(node.expression),
1968 if (node.typeArguments) {
1969 result.typeParameters = this.convertTypeArgumentsToTypeParameters(node.typeArguments, node);
1973 case SyntaxKind.InterfaceDeclaration: {
1974 const interfaceHeritageClauses = (_j = node.heritageClauses) !== null && _j !== void 0 ? _j : [];
1975 const result = this.createNode(node, {
1976 type: ts_estree_1.AST_NODE_TYPES.TSInterfaceDeclaration,
1977 body: this.createNode(node, {
1978 type: ts_estree_1.AST_NODE_TYPES.TSInterfaceBody,
1979 body: node.members.map(member => this.convertChild(member)),
1980 range: [node.members.pos - 1, node.end],
1982 id: this.convertChild(node.name),
1984 if (node.typeParameters) {
1985 result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1987 if (interfaceHeritageClauses.length > 0) {
1988 const interfaceExtends = [];
1989 const interfaceImplements = [];
1990 for (const heritageClause of interfaceHeritageClauses) {
1991 if (heritageClause.token === SyntaxKind.ExtendsKeyword) {
1992 for (const n of heritageClause.types) {
1993 interfaceExtends.push(this.convertChild(n, node));
1997 for (const n of heritageClause.types) {
1998 interfaceImplements.push(this.convertChild(n, node));
2002 if (interfaceExtends.length) {
2003 result.extends = interfaceExtends;
2005 if (interfaceImplements.length) {
2006 result.implements = interfaceImplements;
2010 * Semantically, decorators are not allowed on interface declarations,
2011 * but the TypeScript compiler will parse them and produce a valid AST,
2012 * so we handle them here too.
2014 if (node.decorators) {
2015 result.decorators = node.decorators.map(el => this.convertChild(el));
2017 if (node_utils_1.hasModifier(SyntaxKind.AbstractKeyword, node)) {
2018 result.abstract = true;
2020 if (node_utils_1.hasModifier(SyntaxKind.DeclareKeyword, node)) {
2021 result.declare = true;
2023 // check for exports
2024 return this.fixExports(node, result);
2026 case SyntaxKind.TypePredicate: {
2027 const result = this.createNode(node, {
2028 type: ts_estree_1.AST_NODE_TYPES.TSTypePredicate,
2029 asserts: node.assertsModifier !== undefined,
2030 parameterName: this.convertChild(node.parameterName),
2031 typeAnnotation: null,
2034 * Specific fix for type-guard location data
2037 result.typeAnnotation = this.convertTypeAnnotation(node.type, node);
2038 result.typeAnnotation.loc = result.typeAnnotation.typeAnnotation.loc;
2039 result.typeAnnotation.range =
2040 result.typeAnnotation.typeAnnotation.range;
2044 case SyntaxKind.ImportType:
2045 return this.createNode(node, {
2046 type: ts_estree_1.AST_NODE_TYPES.TSImportType,
2047 isTypeOf: !!node.isTypeOf,
2048 parameter: this.convertChild(node.argument),
2049 qualifier: this.convertChild(node.qualifier),
2050 typeParameters: node.typeArguments
2051 ? this.convertTypeArgumentsToTypeParameters(node.typeArguments, node)
2054 case SyntaxKind.EnumDeclaration: {
2055 const result = this.createNode(node, {
2056 type: ts_estree_1.AST_NODE_TYPES.TSEnumDeclaration,
2057 id: this.convertChild(node.name),
2058 members: node.members.map(el => this.convertChild(el)),
2060 // apply modifiers first...
2061 this.applyModifiersToResult(result, node.modifiers);
2063 * Semantically, decorators are not allowed on enum declarations,
2064 * but the TypeScript compiler will parse them and produce a valid AST,
2065 * so we handle them here too.
2067 if (node.decorators) {
2068 result.decorators = node.decorators.map(el => this.convertChild(el));
2070 // ...then check for exports
2071 return this.fixExports(node, result);
2073 case SyntaxKind.EnumMember: {
2074 const result = this.createNode(node, {
2075 type: ts_estree_1.AST_NODE_TYPES.TSEnumMember,
2076 id: this.convertChild(node.name),
2078 if (node.initializer) {
2079 result.initializer = this.convertChild(node.initializer);
2081 if (node.name.kind === ts.SyntaxKind.ComputedPropertyName) {
2082 result.computed = true;
2086 case SyntaxKind.ModuleDeclaration: {
2087 const result = this.createNode(node, {
2088 type: ts_estree_1.AST_NODE_TYPES.TSModuleDeclaration,
2089 id: this.convertChild(node.name),
2092 result.body = this.convertChild(node.body);
2094 // apply modifiers first...
2095 this.applyModifiersToResult(result, node.modifiers);
2096 if (node.flags & ts.NodeFlags.GlobalAugmentation) {
2097 result.global = true;
2099 // ...then check for exports
2100 return this.fixExports(node, result);
2102 // TypeScript specific types
2103 case SyntaxKind.OptionalType: {
2104 return this.createNode(node, {
2105 type: ts_estree_1.AST_NODE_TYPES.TSOptionalType,
2106 typeAnnotation: this.convertType(node.type),
2109 case SyntaxKind.ParenthesizedType: {
2110 return this.createNode(node, {
2111 type: ts_estree_1.AST_NODE_TYPES.TSParenthesizedType,
2112 typeAnnotation: this.convertType(node.type),
2115 case SyntaxKind.TupleType: {
2116 // In TS 4.0, the `elementTypes` property was changed to `elements`.
2117 // To support both at compile time, we cast to access the newer version
2118 // if the former does not exist.
2119 const elementTypes = node.elementTypes
2120 ? node.elementTypes.map(el => this.convertType(el))
2121 : node.elements.map((el) => this.convertType(el));
2122 return this.createNode(node, {
2123 type: ts_estree_1.AST_NODE_TYPES.TSTupleType,
2127 case SyntaxKind.UnionType: {
2128 return this.createNode(node, {
2129 type: ts_estree_1.AST_NODE_TYPES.TSUnionType,
2130 types: node.types.map(el => this.convertType(el)),
2133 case SyntaxKind.IntersectionType: {
2134 return this.createNode(node, {
2135 type: ts_estree_1.AST_NODE_TYPES.TSIntersectionType,
2136 types: node.types.map(el => this.convertType(el)),
2139 case SyntaxKind.RestType: {
2140 return this.createNode(node, {
2141 type: ts_estree_1.AST_NODE_TYPES.TSRestType,
2142 typeAnnotation: this.convertType(node.type),
2145 case SyntaxKind.AsExpression: {
2146 return this.createNode(node, {
2147 type: ts_estree_1.AST_NODE_TYPES.TSAsExpression,
2148 expression: this.convertChild(node.expression),
2149 typeAnnotation: this.convertType(node.type),
2152 case SyntaxKind.InferType: {
2153 return this.createNode(node, {
2154 type: ts_estree_1.AST_NODE_TYPES.TSInferType,
2155 typeParameter: this.convertType(node.typeParameter),
2158 case SyntaxKind.LiteralType: {
2159 return this.createNode(node, {
2160 type: ts_estree_1.AST_NODE_TYPES.TSLiteralType,
2161 literal: this.convertType(node.literal),
2164 case SyntaxKind.TypeAssertionExpression: {
2165 return this.createNode(node, {
2166 type: ts_estree_1.AST_NODE_TYPES.TSTypeAssertion,
2167 typeAnnotation: this.convertType(node.type),
2168 expression: this.convertChild(node.expression),
2171 case SyntaxKind.ImportEqualsDeclaration: {
2172 return this.createNode(node, {
2173 type: ts_estree_1.AST_NODE_TYPES.TSImportEqualsDeclaration,
2174 id: this.convertChild(node.name),
2175 moduleReference: this.convertChild(node.moduleReference),
2176 isExport: node_utils_1.hasModifier(SyntaxKind.ExportKeyword, node),
2179 case SyntaxKind.ExternalModuleReference: {
2180 return this.createNode(node, {
2181 type: ts_estree_1.AST_NODE_TYPES.TSExternalModuleReference,
2182 expression: this.convertChild(node.expression),
2185 case SyntaxKind.NamespaceExportDeclaration: {
2186 return this.createNode(node, {
2187 type: ts_estree_1.AST_NODE_TYPES.TSNamespaceExportDeclaration,
2188 id: this.convertChild(node.name),
2191 case SyntaxKind.AbstractKeyword: {
2192 return this.createNode(node, {
2193 type: ts_estree_1.AST_NODE_TYPES.TSAbstractKeyword,
2197 return this.deeplyCopy(node);
2201 exports.Converter = Converter;
2202 //# sourceMappingURL=convert.js.map