var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
const ts = __importStar(require("typescript"));
const node_utils_1 = require("./node-utils");
const ts_estree_1 = require("./ts-estree");
+const version_check_1 = require("./version-check");
const SyntaxKind = ts.SyntaxKind;
/**
* Extends and formats a given error object
}
return result;
}
+ convertBindingNameWithTypeAnnotation(name, tsType, parent) {
+ const id = this.convertPattern(name);
+ if (tsType) {
+ id.typeAnnotation = this.convertTypeAnnotation(tsType, parent);
+ this.fixParentLocation(id, id.typeAnnotation.range);
+ }
+ return id;
+ }
/**
* Converts a child into a type annotation. This creates an intermediary
* TypeAnnotation node to match what Flow does.
*/
convertTypeAnnotation(child, parent) {
// in FunctionType and ConstructorType typeAnnotation has 2 characters `=>` and in other places is just colon
- const offset = parent.kind === SyntaxKind.FunctionType ||
- parent.kind === SyntaxKind.ConstructorType
+ const offset = (parent === null || parent === void 0 ? void 0 : parent.kind) === SyntaxKind.FunctionType ||
+ (parent === null || parent === void 0 ? void 0 : parent.kind) === SyntaxKind.ConstructorType
? 2
: 1;
const annotationStartCol = child.getFullStart() - offset;
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.CatchClause,
param: node.variableDeclaration
- ? this.convertChild(node.variableDeclaration.name)
+ ? this.convertBindingNameWithTypeAnnotation(node.variableDeclaration.name, node.variableDeclaration.type)
: null,
body: this.convertChild(node.block),
});
case SyntaxKind.VariableDeclaration: {
const result = this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.VariableDeclarator,
- id: this.convertPattern(node.name),
+ id: this.convertBindingNameWithTypeAnnotation(node.name, node.type, node),
init: this.convertChild(node.initializer),
});
if (node.exclamationToken) {
result.definite = true;
}
- if (node.type) {
- result.id.typeAnnotation = this.convertTypeAnnotation(node.type, node);
- this.fixParentLocation(result.id, result.id.typeAnnotation.range);
- }
return result;
}
case SyntaxKind.VariableStatement: {
local: this.convertChild(node.name),
imported: this.convertChild((_f = node.propertyName) !== null && _f !== void 0 ? _f : node.name),
});
- case SyntaxKind.ImportClause:
+ case SyntaxKind.ImportClause: {
+ const local = this.convertChild(node.name);
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.ImportDefaultSpecifier,
- local: this.convertChild(node.name),
- range: [node.getStart(this.ast), node.name.end],
+ local,
+ range: local.range,
});
+ }
case SyntaxKind.ExportDeclaration:
if (((_g = node.exportClause) === null || _g === void 0 ? void 0 : _g.kind) === SyntaxKind.NamedExports) {
return this.createNode(node, {
raw: 'false',
});
case SyntaxKind.NullKeyword: {
- if (this.inTypeMode) {
+ if (!version_check_1.typescriptVersionIsAtLeast['4.0'] && this.inTypeMode) {
+ // 4.0 started nesting null types inside a LiteralType node, but we still need to support pre-4.0
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.TSNullKeyword,
});
}
- else {
- return this.createNode(node, {
- type: ts_estree_1.AST_NODE_TYPES.Literal,
- value: null,
- raw: 'null',
- });
- }
+ return this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.Literal,
+ value: null,
+ raw: 'null',
+ });
}
case SyntaxKind.EmptyStatement:
return this.createNode(node, {
return this.fixExports(node, result);
}
// TypeScript specific types
- case SyntaxKind.OptionalType: {
- return this.createNode(node, {
- type: ts_estree_1.AST_NODE_TYPES.TSOptionalType,
- typeAnnotation: this.convertType(node.type),
- });
- }
case SyntaxKind.ParenthesizedType: {
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.TSParenthesizedType,
typeAnnotation: this.convertType(node.type),
});
}
- case SyntaxKind.TupleType: {
- // In TS 4.0, the `elementTypes` property was changed to `elements`.
- // To support both at compile time, we cast to access the newer version
- // if the former does not exist.
- const elementTypes = node.elementTypes
- ? node.elementTypes.map(el => this.convertType(el))
- : node.elements.map((el) => this.convertType(el));
- return this.createNode(node, {
- type: ts_estree_1.AST_NODE_TYPES.TSTupleType,
- elementTypes,
- });
- }
case SyntaxKind.UnionType: {
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.TSUnionType,
types: node.types.map(el => this.convertType(el)),
});
}
- case SyntaxKind.RestType: {
- return this.createNode(node, {
- type: ts_estree_1.AST_NODE_TYPES.TSRestType,
- typeAnnotation: this.convertType(node.type),
- });
- }
case SyntaxKind.AsExpression: {
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.TSAsExpression,
});
}
case SyntaxKind.LiteralType: {
- return this.createNode(node, {
- type: ts_estree_1.AST_NODE_TYPES.TSLiteralType,
- literal: this.convertType(node.literal),
- });
+ if (version_check_1.typescriptVersionIsAtLeast['4.0'] &&
+ node.literal.kind === SyntaxKind.NullKeyword) {
+ // 4.0 started nesting null types inside a LiteralType node
+ // but our AST is designed around the old way of null being a keyword
+ return this.createNode(node.literal, {
+ type: ts_estree_1.AST_NODE_TYPES.TSNullKeyword,
+ });
+ }
+ else {
+ return this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.TSLiteralType,
+ literal: this.convertType(node.literal),
+ });
+ }
}
case SyntaxKind.TypeAssertionExpression: {
return this.createNode(node, {
type: ts_estree_1.AST_NODE_TYPES.TSAbstractKeyword,
});
}
+ // Tuple
+ case SyntaxKind.TupleType: {
+ // In TS 4.0, the `elementTypes` property was changed to `elements`.
+ // To support both at compile time, we cast to access the newer version
+ // if the former does not exist.
+ const elementTypes = 'elementTypes' in node
+ ? node.elementTypes.map((el) => this.convertType(el))
+ : node.elements.map((el) => this.convertType(el));
+ return this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.TSTupleType,
+ elementTypes,
+ });
+ }
+ case SyntaxKind.NamedTupleMember: {
+ const member = this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.TSNamedTupleMember,
+ elementType: this.convertType(node.type, node),
+ label: this.convertChild(node.name, node),
+ optional: node.questionToken != null,
+ });
+ if (node.dotDotDotToken) {
+ // adjust the start to account for the "..."
+ member.range[0] = member.label.range[0];
+ member.loc.start = member.label.loc.start;
+ return this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.TSRestType,
+ typeAnnotation: member,
+ });
+ }
+ return member;
+ }
+ case SyntaxKind.OptionalType: {
+ return this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.TSOptionalType,
+ typeAnnotation: this.convertType(node.type),
+ });
+ }
+ case SyntaxKind.RestType: {
+ return this.createNode(node, {
+ type: ts_estree_1.AST_NODE_TYPES.TSRestType,
+ typeAnnotation: this.convertType(node.type),
+ });
+ }
default:
return this.deeplyCopy(node);
}