diff --git a/src/index.js b/src/index.js index 84770971a9..d265f2c538 100755 --- a/src/index.js +++ b/src/index.js @@ -19,9 +19,11 @@ import type { Expression, File } from "./types"; import estreePlugin from "./plugins/estree"; import flowPlugin from "./plugins/flow"; import jsxPlugin from "./plugins/jsx"; +import typescriptPlugin from "./plugins/typescript"; plugins.estree = estreePlugin; plugins.flow = flowPlugin; plugins.jsx = jsxPlugin; +plugins.typescript = typescriptPlugin; export function parse(input: string, options?: Options): File { return getParser(options, input).parse(); @@ -53,7 +55,8 @@ function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class p === "estree" || p === "flow" || p === "jsx"); + let pluginList = pluginsFromOptions.filter((p) => + p === "estree" || p === "flow" || p === "jsx" || p === "typescript"); if (pluginList.indexOf("flow") >= 0) { // ensure flow plugin loads last @@ -61,6 +64,16 @@ function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class= 0 && pluginList.indexOf("typescript") >= 0) { + throw new Error("Cannot combine flow and typescript plugins."); + } + + if (pluginList.indexOf("typescript") >= 0) { + // ensure typescript plugin loads last + pluginList = pluginList.filter((plugin) => plugin !== "typescript"); + pluginList.push("typescript"); + } + if (pluginList.indexOf("estree") >= 0) { // ensure estree plugin loads first pluginList = pluginList.filter((plugin) => plugin !== "estree"); diff --git a/src/parser/expression.js b/src/parser/expression.js index 3aaa2faf08..29a75eae2c 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -294,7 +294,7 @@ export default class ExpressionParser extends LValParser { return this.parseSubscripts(expr, startPos, startLoc); } - parseSubscripts(base: N.Expression, startPos: number, startLoc: Position, noCalls?: ?boolean) { + parseSubscripts(base: N.Expression, startPos: number, startLoc: Position, noCalls?: ?boolean): N.Expression { const state = { stop: false }; do { base = this.parseSubscript(base, startPos, startLoc, noCalls, state); @@ -332,10 +332,7 @@ export default class ExpressionParser extends LValParser { this.expect(tt.bracketR); return this.finishNode(node, "MemberExpression"); } else if (this.eat(tt.parenL)) { - const possibleAsync = this.state.potentialArrowAt === base.start && - base.type === "Identifier" && - base.name === "async" && - !this.canInsertSemicolon(); + const possibleAsync = this.atPossibleAsync(base); node.callee = base; node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); @@ -363,23 +360,13 @@ export default class ExpressionParser extends LValParser { this.expect(tt.bracketR); return this.finishNode(node, "MemberExpression"); } else if (!noCalls && this.match(tt.parenL)) { - const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); + const possibleAsync = this.atPossibleAsync(base); this.next(); const node = this.startNodeAt(startPos, startLoc); node.callee = base; node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); - if (node.callee.type === "Import") { - if (node.arguments.length !== 1) { - this.raise(node.start, "import() requires exactly one argument"); - } - - const importArg = node.arguments[0]; - if (importArg && importArg.type === "SpreadElement") { - this.raise(importArg.start, "... is not allowed in import()"); - } - } - this.finishNode(node, "CallExpression"); + this.finishCallExpression(node); if (possibleAsync && this.shouldParseAsyncArrow()) { state.stop = true; @@ -399,6 +386,24 @@ export default class ExpressionParser extends LValParser { } } + atPossibleAsync(base: N.Expression): boolean { + return this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); + } + + finishCallExpression(node: N.CallExpression): N.CallExpression { + if (node.callee.type === "Import") { + if (node.arguments.length !== 1) { + this.raise(node.start, "import() requires exactly one argument"); + } + + const importArg = node.arguments[0]; + if (importArg && importArg.type === "SpreadElement") { + this.raise(importArg.start, "... is not allowed in import()"); + } + } + return this.finishNode(node, "CallExpression"); + } + parseCallExpressionArguments(close: TokenType, possibleAsyncArrow: boolean): $ReadOnlyArray { const elts = []; let innerParenStart; @@ -555,10 +560,7 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "NullLiteral"); case tt._true: case tt._false: - node = this.startNode(); - node.value = this.match(tt._true); - this.next(); - return this.finishNode(node, "BooleanLiteral"); + return this.parseBooleanLiteral(); case tt.parenL: return this.parseParenAndDistinguishExpression(canBeArrow); @@ -613,6 +615,13 @@ export default class ExpressionParser extends LValParser { } } + parseBooleanLiteral(): N.BooleanLiteral { + const node = this.startNode(); + node.value = this.match(tt._true); + this.next(); + return this.finishNode(node, "BooleanLiteral"); + } + parseMaybePrivateName(): N.PrivateName | N.Identifier { const isPrivate = this.eat(tt.hash); @@ -784,19 +793,20 @@ export default class ExpressionParser extends LValParser { } node.callee = this.parseNoCallExpr(); - const optional = this.eat(tt.questionDot); + if (this.eat(tt.questionDot)) node.optional = true; + this.parseNewArguments(node); + return this.finishNode(node, "NewExpression"); + } + parseNewArguments(node: N.NewExpression): void { if (this.eat(tt.parenL)) { - node.arguments = this.parseExprList(tt.parenR); - this.toReferencedList(node.arguments); + const args = this.parseExprList(tt.parenR); + this.toReferencedList(args); + // $FlowFixMe (parseExprList should be all non-null in this case) + node.arguments = args; } else { node.arguments = []; } - if (optional) { - node.optional = true; - } - - return this.finishNode(node, "NewExpression"); } // Parse template expression. @@ -978,19 +988,16 @@ export default class ExpressionParser extends LValParser { if (isPattern) this.unexpected(); prop.kind = "method"; prop.method = true; - this.parseMethod(prop, isGenerator, isAsync); - - return this.finishNode(prop, "ObjectMethod"); + return this.parseMethod(prop, isGenerator, isAsync, /* isConstructor */ false, "ObjectMethod"); } if (this.isGetterOrSetterMethod(prop, isPattern)) { if (isGenerator || isAsync) this.unexpected(); prop.kind = prop.key.name; this.parsePropertyName(prop); - this.parseMethod(prop); + this.parseMethod(prop, /* isGenerator */false, /* isAsync */ false, /* isConstructor */ false, "ObjectMethod"); this.checkGetterSetterParamCount(prop); - - return this.finishNode(prop, "ObjectMethod"); + return prop; } } @@ -1028,9 +1035,12 @@ export default class ExpressionParser extends LValParser { this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos); if (!node) this.unexpected(); + + // $FlowFixMe + return node; } - parsePropertyName(prop: N.ObjectOrClassMember): N.Expression { + parsePropertyName(prop: N.ObjectOrClassMember | N.TsNamedTypeElementBase): N.Expression { if (this.eat(tt.bracketL)) { prop.computed = true; prop.key = this.parseMaybeAssign(); @@ -1048,7 +1058,7 @@ export default class ExpressionParser extends LValParser { // Initialize empty function node. - initFunction(node: N.Function, isAsync: ?boolean): void { + initFunction(node: N.BodilessFunctionOrMethodBase, isAsync: ?boolean): void { node.id = null; node.generator = false; node.expression = false; @@ -1057,14 +1067,15 @@ export default class ExpressionParser extends LValParser { // Parse object or class method. - parseMethod(node: N.MethodLike, isGenerator?: boolean, isAsync?: boolean): N.MethodLike { + parseMethod(node: T, isGenerator: boolean, isAsync: boolean, isConstructor: boolean, type: string): T { const oldInMethod = this.state.inMethod; this.state.inMethod = node.kind || true; this.initFunction(node, isAsync); this.expect(tt.parenL); - node.params = this.parseBindingList(tt.parenR); + const allowModifiers = isConstructor; // For TypeScript parameter properties + node.params = this.parseBindingList(tt.parenR, /* allowEmpty */ false, allowModifiers); node.generator = !!isGenerator; - this.parseFunctionBody(node); + this.parseFunctionBodyAndFinish(node, type); this.state.inMethod = oldInMethod; return node; } @@ -1078,7 +1089,7 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "ArrowFunctionExpression"); } - isStrictBody(node: { body: N.BlockStatement }, isExpression?: boolean): boolean { + isStrictBody(node: { body: N.BlockStatement }, isExpression: ?boolean): boolean { if (!isExpression && node.body.directives.length) { for (const directive of node.body.directives) { if (directive.value.value === "use strict") { @@ -1090,8 +1101,14 @@ export default class ExpressionParser extends LValParser { return false; } + parseFunctionBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { + // $FlowIgnore (node is not bodiless if we get here) + this.parseFunctionBody(node, allowExpressionBody); + this.finishNode(node, type); + } + // Parse function body and check parameters. - parseFunctionBody(node: N.Function, allowExpression?: boolean): void { + parseFunctionBody(node: N.Function, allowExpression: ?boolean): void { const isExpression = allowExpression && !this.match(tt.braceL); const oldInAsync = this.state.inAsync; @@ -1181,26 +1198,33 @@ export default class ExpressionParser extends LValParser { parseIdentifier(liberal?: boolean): N.Identifier { const node = this.startNode(); + const name = this.parseIdentifierName(node.start, liberal); + node.name = name; + node.loc.identifierName = name; + return this.finishNode(node, "Identifier"); + } + + parseIdentifierName(pos: number, liberal?: boolean): string { if (!liberal) { this.checkReservedWord(this.state.value, this.state.start, !!this.state.type.keyword, false); } + let name: string; + if (this.match(tt.name)) { - node.name = this.state.value; + name = this.state.value; } else if (this.state.type.keyword) { - node.name = this.state.type.keyword; + name = this.state.type.keyword; } else { - this.unexpected(); + throw this.unexpected(); } - if (!liberal && node.name === "await" && this.state.inAsync) { - this.raise(node.start, "invalid use of await inside of an async function"); + if (!liberal && name === "await" && this.state.inAsync) { + this.raise(pos, "invalid use of await inside of an async function"); } - node.loc.identifierName = node.name; - this.next(); - return this.finishNode(node, "Identifier"); + return name; } checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { diff --git a/src/parser/lval.js b/src/parser/lval.js index 3c88a5b1c0..5c32af0aae 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -1,8 +1,8 @@ // @flow import { types as tt, type TokenType } from "../tokenizer/types"; -import type { Decorator, Expression, Identifier, Node, ObjectExpression, ObjectPattern, Pattern, RestElement, - SpreadElement } from "../types"; +import type { TSParameterProperty, Decorator, Expression, Identifier, Node, ObjectExpression, + ObjectPattern, Pattern, RestElement, SpreadElement } from "../types"; import type { Pos, Position } from "../util/location"; import { NodeUtils } from "./node"; @@ -167,8 +167,12 @@ export default class LValParser extends NodeUtils { } } - parseBindingList(close: TokenType, allowEmpty?: boolean): $ReadOnlyArray { - const elts = []; + parseBindingList( + close: TokenType, + allowEmpty?: boolean, + allowModifiers?: boolean + ): $ReadOnlyArray { + const elts: Array = []; let first = true; while (!this.eat(close)) { if (first) { @@ -193,17 +197,22 @@ export default class LValParser extends NodeUtils { while (this.match(tt.at)) { decorators.push(this.parseDecorator()); } - const left = this.parseMaybeDefault(); - if (decorators.length) { - left.decorators = decorators; - } - this.parseAssignableListItemTypes(left); - elts.push(this.parseMaybeDefault(left.start, left.loc.start, left)); + elts.push(this.parseAssignableListItem(allowModifiers, decorators)); } } return elts; } + parseAssignableListItem(allowModifiers: ?boolean, decorators: Decorator[]): Pattern | TSParameterProperty { + const left = this.parseMaybeDefault(); + this.parseAssignableListItemTypes(left); + const elt = this.parseMaybeDefault(left.start, left.loc.start, left); + if (decorators.length) { + left.decorators = decorators; + } + return elt; + } + parseAssignableListItemTypes(param: Pattern): Pattern { return param; } diff --git a/src/parser/node.js b/src/parser/node.js index cc8f514366..586322e313 100644 --- a/src/parser/node.js +++ b/src/parser/node.js @@ -55,6 +55,11 @@ export class NodeUtils extends UtilParser { return new Node(this, pos, loc); } + /** Start a new node with a previous node's location. */ + startNodeAtNode(type: NodeType): T { + return this.startNodeAt(type.start, type.loc.start); + } + // Finish an AST node, adding `type` and `end` properties. finishNode(node: T, type: string): T { diff --git a/src/parser/statement.js b/src/parser/statement.js index 2cbfa3b52c..a8ac0e1ea7 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -3,7 +3,7 @@ // @flow import * as N from "../types"; -import { types as tt, TokenType } from "../tokenizer/types"; +import { types as tt, type TokenType } from "../tokenizer/types"; import ExpressionParser from "./expression"; import type { Position } from "../util/location"; import { lineBreak } from "../util/whitespace"; @@ -15,7 +15,6 @@ const empty = []; const loopLabel = { kind: "loop" }, switchLabel = { kind: "switch" }; export default class StatementParser extends ExpressionParser { - // ### Statement parsing // Parse a program. Initializes the parser, reads any number of @@ -65,7 +64,10 @@ export default class StatementParser extends ExpressionParser { if (this.match(tt.at)) { this.parseDecorators(true); } + return this.parseStatementContent(declaration, topLevel); + } + parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement { const starttype = this.state.type; const node = this.startNode(); @@ -118,7 +120,13 @@ export default class StatementParser extends ExpressionParser { this.raise(this.state.start, "'import' and 'export' may appear only with 'sourceType: module'"); } } - return starttype === tt._import ? this.parseImport(node) : this.parseExport(node); + + this.next(); + if (starttype == tt._import) { + return this.parseImport(node); + } else { + return this.parseExport(node); + } case tt.name: if (this.state.value === "async") { @@ -511,7 +519,11 @@ export default class StatementParser extends ExpressionParser { parseBlockBody(node: N.BlockStatementLike, allowDirectives: ?boolean, topLevel: boolean, end: TokenType): void { const body = node.body = []; const directives = node.directives = []; + this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end); + } + // Undefined directives means that directives are not allowed. + parseBlockOrModuleBlockBody(body: N.Statement[], directives: ?N.Directive[], topLevel: boolean, end: TokenType): void { let parsedNonDirective = false; let oldStrict; let octalPosition; @@ -523,7 +535,7 @@ export default class StatementParser extends ExpressionParser { const stmt = this.parseStatement(true, topLevel); - if (allowDirectives && !parsedNonDirective && this.isValidDirective(stmt)) { + if (directives && !parsedNonDirective && this.isValidDirective(stmt)) { const directive = this.stmtToDirective(stmt); directives.push(directive); @@ -596,11 +608,15 @@ export default class StatementParser extends ExpressionParser { this.parseVarHead(decl); if (this.eat(tt.eq)) { decl.init = this.parseMaybeAssign(isFor); - } else if (kind === tt._const && !(this.match(tt._in) || this.isContextual("of"))) { - this.unexpected(); - } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(tt._in) || this.isContextual("of")))) { - this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value"); } else { + if (kind === tt._const && !(this.match(tt._in) || this.isContextual("of"))) { + // `const` with no initializer is allowed in TypeScript. It could be a declaration `const x: number;`. + if (!this.hasPlugin("typescript")) { + this.unexpected(); + } + } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(tt._in) || this.isContextual("of")))) { + this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value"); + } decl.init = null; } declarations.push(this.finishNode(decl, "VariableDeclarator")); @@ -641,14 +657,12 @@ export default class StatementParser extends ExpressionParser { } this.parseFunctionParams(node); - this.parseFunctionBody(node, allowExpressionBody); - + this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression", allowExpressionBody); this.state.inMethod = oldInMethod; - - return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); + return node; } - parseFunctionParams(node: N.NormalFunction): void { + parseFunctionParams(node: N.Function): void { this.expect(tt.parenL); node.params = this.parseBindingList(tt.parenR); } @@ -656,7 +670,7 @@ export default class StatementParser extends ExpressionParser { // Parse a class declaration or literal (depending on the // `isStatement` parameter). - parseClass(node: N.Class, isStatement: boolean, optionalId?: boolean): N.Class { + parseClass(node: T, isStatement: /* T === ClassDeclaration */boolean, optionalId?: boolean): T { this.next(); this.takeDecorators(node); this.parseClassId(node, isStatement, optionalId); @@ -687,8 +701,8 @@ export default class StatementParser extends ExpressionParser { this.state.inClass = true; const state = { hadConstructor: false }; - let decorators = []; - const classBody = this.startNode(); + let decorators: N.Decorator[] = []; + const classBody: N.ClassBody = this.startNode(); classBody.body = []; @@ -738,7 +752,6 @@ export default class StatementParser extends ExpressionParser { parseClassMember(classBody: N.ClassBody, member: N.ClassMember, state: { hadConstructor: boolean }): void { // Use the appropriate variable to represent `member` once a more specific type is known. const memberAny: any = member; - const methodOrProp: N.ClassMethod | N.ClassProperty = memberAny; const method: N.ClassMethod = memberAny; const prop: N.ClassProperty = memberAny; @@ -750,7 +763,7 @@ export default class StatementParser extends ExpressionParser { return; } - methodOrProp.static = false; + let isStatic = false; if (this.match(tt.name) && this.state.value === "static") { const key = this.parseIdentifier(true); // eats 'static' if (this.isClassMethod()) { @@ -758,19 +771,32 @@ export default class StatementParser extends ExpressionParser { method.kind = "method"; method.computed = false; method.key = key; - this.parseClassMethod(classBody, method, false, false); + method.static = false; + this.parseClassMethod(classBody, method, false, false, /* isConstructor */ false); return; } else if (this.isClassProperty()) { // a property named 'static' prop.computed = false; prop.key = key; + prop.static = false; classBody.body.push(this.parseClassProperty(prop)); return; } // otherwise something static - methodOrProp.static = true; + isStatic = true; } + this.parseClassMemberWithIsStatic(classBody, member, state, isStatic); + } + + parseClassMemberWithIsStatic(classBody: N.ClassBody, member: N.ClassMember, state: { hadConstructor: boolean }, isStatic: boolean) { + const memberAny: any = member; + const methodOrProp: N.ClassMethod | N.ClassProperty = memberAny; + const method: N.ClassMethod = memberAny; + const prop: N.ClassProperty = memberAny; + + methodOrProp.static = isStatic; + if (this.eat(tt.star)) { // a generator method.kind = "method"; @@ -781,36 +807,40 @@ export default class StatementParser extends ExpressionParser { if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) { this.raise(method.key.start, "Classes may not have static property named prototype"); } - this.parseClassMethod(classBody, method, true, false); + this.parseClassMethod(classBody, method, true, false, /* isConstructor */ false); return; } const isSimple = this.match(tt.name); - const key = this.parsePropertyName(methodOrProp); - if (!methodOrProp.computed && methodOrProp.static && (methodOrProp.key.name === "prototype" || methodOrProp.key.value === "prototype")) { - this.raise(methodOrProp.key.start, "Classes may not have static property named prototype"); - } + const key = this.parseClassPropertyName(methodOrProp); + + this.parsePostMemberNameModifiers(methodOrProp); + if (this.isClassMethod()) { // a normal method - if (this.isNonstaticConstructor(method)) { - if (state.hadConstructor) { - this.raise(key.start, "Duplicate constructor in the same class"); - } else if (method.decorators) { - this.raise(method.start, "You can't attach decorators to a class constructor"); - } - state.hadConstructor = true; + const isConstructor = this.isNonstaticConstructor(method); + if (isConstructor) { method.kind = "constructor"; } else { method.kind = "method"; } - this.parseClassMethod(classBody, method, false, false); - } else if (this.isClassProperty()) { - // a normal property - if (this.isNonstaticConstructor(prop)) { - this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'"); + + if (isConstructor) { + if (method.decorators) { + this.raise(method.start, "You can't attach decorators to a class constructor"); + } + + // TypeScript allows multiple overloaded constructor declarations. + if (state.hadConstructor && !this.hasPlugin("typescript")) { + this.raise(key.start, "Duplicate constructor in the same class"); + } + state.hadConstructor = true; } - classBody.body.push(this.parseClassProperty(prop)); + + this.parseClassMethod(classBody, method, false, false, isConstructor); + } else if (this.isClassProperty()) { + this.pushClassProperty(classBody, prop); } else if (isSimple && key.name === "async" && !this.isLineTerminator()) { // an async method const isGenerator = this.hasPlugin("asyncGenerators") && this.eat(tt.star); @@ -819,7 +849,7 @@ export default class StatementParser extends ExpressionParser { if (this.isNonstaticConstructor(method)) { this.raise(method.key.start, "Constructor can't be an async function"); } - this.parseClassMethod(classBody, method, isGenerator, true); + this.parseClassMethod(classBody, method, isGenerator, true, /* isConstructor */ false); } else if (isSimple && (key.name === "get" || key.name === "set") && !(this.isLineTerminator() && this.match(tt.star))) { // `get\n*` is an uninitialized property named 'get' followed by a generator. // a getter or setter method.kind = key.name; @@ -827,7 +857,7 @@ export default class StatementParser extends ExpressionParser { if (this.isNonstaticConstructor(method)) { this.raise(method.key.start, "Constructor can't have get/set modifier"); } - this.parseClassMethod(classBody, method, false, false); + this.parseClassMethod(classBody, method, false, false, /* isConstructor */ false); this.checkGetterSetterParamCount(method); } else if (this.isLineTerminator()) { // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token) @@ -840,6 +870,30 @@ export default class StatementParser extends ExpressionParser { } } + parseClassPropertyName(methodOrProp: N.ClassMethod | N.ClassProperty): N.Expression { + const key = this.parsePropertyName(methodOrProp); + if (!methodOrProp.computed && methodOrProp.static && (methodOrProp.key.name === "prototype" || methodOrProp.key.value === "prototype")) { + this.raise(methodOrProp.key.start, "Classes may not have static property named prototype"); + } + return key; + } + + pushClassProperty(classBody: N.ClassBody, prop: N.ClassProperty) { + if (this.isNonstaticConstructor(prop)) { + this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'"); + } + classBody.body.push(this.parseClassProperty(prop)); + } + + // Overridden in typescript.js + // eslint-disable-next-line no-unused-vars + parsePostMemberNameModifiers(methodOrProp: N.ClassMethod | N.ClassProperty): void {} + + // Overridden in typescript.js + parseAccessModifier(): ?N.Accessibility { + return undefined; + } + parsePrivateClassProperty(node: N.ClassPrivateProperty): N.ClassPrivateProperty { this.state.inClassProperty = true; @@ -854,9 +908,8 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "ClassPrivateProperty"); } - parseClassProperty(node: N.ClassProperty): N.ClassProperty { - const hasPlugin = this.hasPlugin("classProperties"); + const hasPlugin = this.hasPlugin("classProperties") || this.hasPlugin("typescript"); const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled."; if (!node.typeAnnotation && !hasPlugin) { this.raise(node.start, noPluginMsg); @@ -876,9 +929,8 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "ClassProperty"); } - parseClassMethod(classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, isAsync: boolean): void { - this.parseMethod(method, isGenerator, isAsync); - classBody.body.push(this.finishNode(method, "ClassMethod")); + parseClassMethod(classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, isAsync: boolean, isConstructor: boolean): void { + classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, "ClassMethod")); } parseClassId(node: N.Class, isStatement: boolean, optionalId: ?boolean): void { @@ -899,9 +951,8 @@ export default class StatementParser extends ExpressionParser { // Parses module export declaration. - parseExport(node: N.ExportNamedDeclaration): N.ExportNamedDeclaration { - this.eat(tt._export); - + // TODO: better type. Node is an N.AnyExport. + parseExport(node: N.Node): N.Node { // export * from '...' if (this.match(tt.star)) { const specifier = this.startNode(); @@ -949,7 +1000,6 @@ export default class StatementParser extends ExpressionParser { needsSemi = true; expr = this.parseMaybeAssign(); } - // $FlowFixMe node.declaration = expr; if (needsSemi) this.semicolon(); this.checkExport(node, true, true); @@ -1015,7 +1065,7 @@ export default class StatementParser extends ExpressionParser { || this.isContextual("async"); } - checkExport(node: N.ExportNamedDeclaration, checkNames: ?boolean, isDefault: ?boolean): void { + checkExport(node: N.ExportNamedDeclaration, checkNames: ?boolean, isDefault?: boolean): void { if (checkNames) { // Check for duplicate exports if (isDefault) { @@ -1119,9 +1169,7 @@ export default class StatementParser extends ExpressionParser { // Parses import declaration. - parseImport(node: N.ImportDeclaration): N.ImportDeclaration { - this.eat(tt._import); - + parseImport(node: N.Node): N.ImportDeclaration | N.TsImportEqualsDeclaration { // import '...' if (this.match(tt.string)) { node.specifiers = []; diff --git a/src/parser/util.js b/src/parser/util.js index 89ec41154e..7f7531ac27 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -33,6 +33,16 @@ export default class UtilParser extends Tokenizer { } } + // eat() for relational operators. + + eatRelational(op: "<" | ">"): boolean { + if (this.isRelational(op)) { + this.next(); + return true; + } + return false; + } + // Tests whether parsed token is a contextual keyword. isContextual(name: string): boolean { @@ -56,7 +66,11 @@ export default class UtilParser extends Tokenizer { canInsertSemicolon(): boolean { return this.match(tt.eof) || this.match(tt.braceR) || - lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)); + this.hasPrecedingLineBreak(); + } + + hasPrecedingLineBreak(): boolean { + return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)); } // TODO diff --git a/src/plugins/estree.js b/src/plugins/estree.js index 807fd9f340..8a96ed83b4 100644 --- a/src/plugins/estree.js +++ b/src/plugins/estree.js @@ -108,7 +108,7 @@ export default (superClass: Class): Class => class extends super } } - isStrictBody(node: { body: N.BlockStatement }, isExpression?: boolean): boolean { + isStrictBody(node: { body: N.BlockStatement }, isExpression: ?boolean): boolean { if (!isExpression && node.body.body.length > 0) { for (const directive of node.body.body) { if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") { @@ -158,15 +158,16 @@ export default (superClass: Class): Class => class extends super classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, - isAsync: boolean + isAsync: boolean, + isConstructor: boolean ): void { - this.parseMethod(method, isGenerator, isAsync); + this.parseMethod(method, isGenerator, isAsync, isConstructor, "MethodDefinition"); if (method.typeParameters) { // $FlowIgnore method.value.typeParameters = method.typeParameters; delete method.typeParameters; } - classBody.body.push(this.finishNode(method, "MethodDefinition")); + classBody.body.push(method); } parseExprAtom(refShorthandDefaultPos?: ?Pos): N.Expression { @@ -205,19 +206,21 @@ export default (superClass: Class): Class => class extends super return node; } - parseMethod( - node: N.MethodLike, - isGenerator?: boolean, - isAsync?: boolean - ): N.MethodLike { + parseMethod( + node: T, + isGenerator: boolean, + isAsync: boolean, + isConstructor: boolean, + type: string, + ): T { let funcNode = this.startNode(); funcNode.kind = node.kind; // provide kind, so super method correctly sets state - funcNode = super.parseMethod(funcNode, isGenerator, isAsync); + funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, "FunctionExpression"); delete funcNode.kind; // $FlowIgnore - node.value = this.finishNode(funcNode, "FunctionExpression"); + node.value = funcNode; - return node; + return this.finishNode(node, type); } parseObjectMethod( diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 164ea6ae41..49179bbead 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -178,7 +178,7 @@ export default (superClass: Class): Class => class extends super if (lookahead.value !== "type" && lookahead.value !== "typeof") { this.unexpected(null, "Imports within a `declare module` body must always be `import type` or `import typeof`"); } - + this.next(); this.parseImport(bodyNode); } else { this.expectContextual("declare", "Only declares and type imports are allowed inside declare module"); @@ -372,7 +372,7 @@ export default (superClass: Class): Class => class extends super // Type annotations - flowParseTypeParameter(): N.FlowTypeParameter { + flowParseTypeParameter(): N.TypeParameter { const node = this.startNode(); const variance = this.flowParseVariance(); @@ -390,7 +390,7 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameter"); } - flowParseTypeParameterDeclaration(): N.FlowTypeParameterDeclaration { + flowParseTypeParameterDeclaration(): N.TypeParameterDeclaration { const oldInType = this.state.inType; const node = this.startNode(); node.params = []; @@ -417,7 +417,7 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameterDeclaration"); } - flowParseTypeParameterInstantiation(): N.FlowTypeParameterInstantiation { + flowParseTypeParameterInstantiation(): N.TypeParameterInstantiation { const node = this.startNode(); const oldInType = this.state.inType; node.params = []; @@ -1007,21 +1007,18 @@ export default (superClass: Class): Class => class extends super // Overrides // ================================== - // plain function return types: function name(): string {} - parseFunctionBody(node: N.Function, allowExpression?: boolean): void { - if (this.match(tt.colon) && !allowExpression) { - // if allowExpression is true then we're parsing an arrow function and if - // there's a return type then it's been handled elsewhere + parseFunctionBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { + // For arrow functions, `parseArrow` handles the return type itself. + if (!allowExpressionBody && this.match(tt.colon)) { const typeNode = this.startNode(); - // $FlowFixMe (destructuring not yet supported) + // $FlowFixMe [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser(); - node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null; } - return super.parseFunctionBody(node, allowExpression); + super.parseFunctionBodyAndFinish(node, type, allowExpressionBody); } // interfaces @@ -1257,7 +1254,8 @@ export default (superClass: Class): Class => class extends super classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, - isAsync: boolean + isAsync: boolean, + isConstructor: boolean ): void { if (method.variance) { this.unexpected(method.variance.start); @@ -1267,7 +1265,7 @@ export default (superClass: Class): Class => class extends super method.typeParameters = this.flowParseTypeParameterDeclaration(); } - super.parseClassMethod(classBody, method, isGenerator, isAsync); + super.parseClassMethod(classBody, method, isGenerator, isAsync, isConstructor); } // parse a the super class type parameters and implements @@ -1278,7 +1276,7 @@ export default (superClass: Class): Class => class extends super } if (this.isContextual("implements")) { this.next(); - const implemented = node.implements = []; + const implemented: N.FlowClassImplements[] = node.implements = []; do { const node = this.startNode(); node.id = this.parseIdentifier(); @@ -1292,9 +1290,10 @@ export default (superClass: Class): Class => class extends super } } - parsePropertyName(node: N.ObjectOrClassMember): N.Identifier { + parsePropertyName(node: N.ObjectOrClassMember | N.TsNamedTypeElementBase): N.Identifier { const variance = this.flowParseVariance(); const key = super.parsePropertyName(node); + // $FlowIgnore ("variance" not defined on TsNamedTypeElementBase) node.variance = variance; return key; } @@ -1341,6 +1340,12 @@ export default (superClass: Class): Class => class extends super parseAssignableListItemTypes(param: N.Pattern): N.Pattern { if (this.eat(tt.question)) { + if (param.type !== "Identifier") { + throw this.raise( + param.start, + "A binding pattern parameter cannot be optional in an implementation signature."); + } + param.optional = true; } if (this.match(tt.colon)) { @@ -1439,7 +1444,7 @@ export default (superClass: Class): Class => class extends super } // parse function type parameters - function foo() {} - parseFunctionParams(node: N.NormalFunction): void { + parseFunctionParams(node: N.Function): void { if (this.isRelational("<")) { node.typeParameters = this.flowParseTypeParameterDeclaration(); } diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js new file mode 100644 index 0000000000..b185e6059c --- /dev/null +++ b/src/plugins/typescript.js @@ -0,0 +1,1717 @@ +// @flow + +import type { TokenType } from "../tokenizer/types"; +import { types as tt } from "../tokenizer/types"; +import { types as ct } from "../tokenizer/context"; +import * as N from "../types"; +import type { Pos, Position } from "../util/location"; +import Parser from "../parser"; + +type TsModifier = "readonly" | "abstract" | "static" | "public" | "private" | "protected"; + +function nonNull(x: ?T): T { + if (x == null) { + // $FlowIgnore + throw new Error(`Unexpected ${x} value.`); + } + return x; +} + +function assert(x: boolean): void { + if (!x) { + throw new Error("Assert fail"); + } +} + +type ParsingContext = + | "EnumMembers" + | "HeritageClauseElement" + | "TupleElementTypes" + | "TypeMembers" + | "TypeParametersOrArguments"; + +// Doesn't handle "void" or "null" because those are keywords, not identifiers. +function keywordTypeFromName(value: string): N.TsKeywordTypeType | typeof undefined { + switch (value) { + case "any": + return "TSAnyKeyword"; + case "boolean": + return "TSBooleanKeyword"; + case "never": + return "TSNeverKeyword"; + case "number": + return "TSNumberKeyword"; + case "object": + return "TSObjectKeyword"; + case "string": + return "TSStringKeyword"; + case "symbol": + return "TSSymbolKeyword"; + case "undefined": + return "TSUndefinedKeyword"; + default: + return undefined; + } +} + +export default (superClass: Class): Class => class extends superClass { + tsIsIdentifier(): boolean { + // TODO: actually a bit more complex in TypeScript, but shouldn't matter. + // See https://github.com/Microsoft/TypeScript/issues/15008 + return this.match(tt.name); + } + + tsNextTokenCanFollowModifier() { + // Note: TypeScript's implementation is much more complicated because + // more things are considered modifiers there. + // This implementation only handles modifiers not handled by babylon itself. And "static". + // TODO: Would be nice to avoid lookahead. Want a hasLineBreakUpNext() method... + this.next(); + return !this.hasPrecedingLineBreak() + && !this.match(tt.parenL) + && !this.match(tt.colon) + && !this.match(tt.eq) + && !this.match(tt.question); + } + + /** Parses a modifier matching one the given modifier names. */ + tsParseModifier(allowedModifiers: T[]): ?T { + if (!this.match(tt.name)) { + return undefined; + } + + const modifier = this.state.value; + if (allowedModifiers.indexOf(modifier) !== -1 + && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) { + return modifier; + } + return undefined; + } + + tsIsListTerminator(kind: ParsingContext): boolean { + switch (kind) { + case "EnumMembers": + case "TypeMembers": + return this.match(tt.braceR); + case "HeritageClauseElement": + return this.match(tt.braceL); + case "TupleElementTypes": + return this.match(tt.bracketR); + case "TypeParametersOrArguments": + return this.isRelational(">"); + } + + throw new Error("Unreachable"); + } + + tsParseList(kind: ParsingContext, parseElement: () => T): T[] { + const result: T[] = []; + while (!this.tsIsListTerminator(kind)) { + // Skipping "parseListElement" from the TS source since that's just for error handling. + result.push(parseElement()); + } + return result; + } + + tsParseDelimitedList(kind: ParsingContext, parseElement: () => T): T[] { + return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, /* expectSuccess */ true)); + } + + tsTryParseDelimitedList(kind: ParsingContext, parseElement: () => ?T): ?T[] { + return this.tsParseDelimitedListWorker(kind, parseElement, /* expectSuccess */ false); + } + + /** + * If !expectSuccess, returns undefined instead of failing to parse. + * If expectSuccess, parseElement should always return a defined value. + */ + tsParseDelimitedListWorker( + kind: ParsingContext, + parseElement: () => ?T, + expectSuccess: boolean): ?T[] { + + const result = []; + + while (true) { + if (this.tsIsListTerminator(kind)) { + break; + } + + const element = parseElement(); + if (element == null) { + return undefined; + } + result.push(element); + + if (this.eat(tt.comma)) { + continue; + } + + if (this.tsIsListTerminator(kind)) { + break; + } + + if (expectSuccess) { + // This will fail with an error about a missing comma + this.expect(tt.comma); + } + return undefined; + } + + return result; + } + + tsParseBracketedList( + kind: ParsingContext, + parseElement: () => T, + bracket: boolean, + skipFirstToken: boolean): T[] { + + if (!skipFirstToken) { + if (bracket) { + this.expect(tt.bracketL); + } else { + this.expectRelational("<"); + } + } + + const result = this.tsParseDelimitedList(kind, parseElement); + + if (bracket) { + this.expect(tt.bracketR); + } else { + this.expectRelational(">"); + } + + return result; + } + + tsParseEntityName(allowReservedWords: boolean): N.TsEntityName { + let entity: N.TsEntityName = this.parseIdentifier(); + while (this.eat(tt.dot)) { + const node: N.TsQualifiedName = this.startNodeAtNode(entity); + node.left = entity; + node.right = this.parseIdentifier(allowReservedWords); + entity = this.finishNode(node, "TSQualifiedName"); + } + return entity; + } + + tsParseTypeReference(): N.TsTypeReference { + const node: N.TsTypeReference = this.startNode(); + node.typeName = this.tsParseEntityName(/* allowReservedWords */ false); + if (!this.hasPrecedingLineBreak() && this.isRelational("<")) { + node.typeParameters = this.tsParseTypeArguments(); + } + return this.finishNode(node, "TSTypeReference"); + } + + tsParseThisTypePredicate(lhs: N.TsThisType): N.TsTypePredicate { + this.next(); + const node: N.TsTypePredicate = this.startNode(); + node.parameterName = lhs; + node.typeAnnotation = this.tsParseTypeAnnotation(/* eatColon */ false); + return this.finishNode(node, "TSTypePredicate"); + } + + tsParseThisTypeNode(): N.TsThisType { + const node: N.TsThisType = this.startNode(); + this.next(); + return this.finishNode(node, "TSThisType"); + } + + tsParseTypeQuery(): N.TsTypeQuery { + const node: N.TsTypeQuery = this.startNode(); + this.expect(tt._typeof); + node.exprName = this.tsParseEntityName(/* allowReservedWords */ true); + return this.finishNode(node, "TSTypeQuery"); + } + + tsParseTypeParameter(): N.TypeParameter { + const node: N.TypeParameter = this.startNode(); + node.name = this.parseIdentifierName(node.start); + if (this.eat(tt._extends)) { + node.constraint = this.tsParseType(); + } + + if (this.eat(tt.eq)) { + node.default = this.tsParseType(); + } + + return this.finishNode(node, "TypeParameter"); + } + + tsTryParseTypeParameters(): ?N.TypeParameterDeclaration { + if (this.eatRelational("<")) { + return this.tsParseTypeParameters(); + } + } + + tsParseTypeParameters(): N.TypeParameterDeclaration { + const node: N.TypeParameterDeclaration = this.startNode(); + node.params = this.tsParseBracketedList( + "TypeParametersOrArguments", + this.tsParseTypeParameter.bind(this), + /* bracket */ false, + /* skipFirstToken */ true); + return this.finishNode(node, "TypeParameterDeclaration"); + } + + // Note: In TypeScript implementation we must provide `yieldContext` and `awaitContext`, + // but here it's always false, because this is only used for types. + tsFillSignature(returnToken: TokenType, signature: N.TsSignatureDeclaration): void { + // Arrow fns *must* have return token (`=>`). Normal functions can omit it. + const returnTokenRequired = returnToken === tt.arrow; + signature.typeParameters = this.tsTryParseTypeParameters(); + this.expect(tt.parenL); + signature.parameters = this.tsParseBindingListForSignature(); + if (returnTokenRequired) { + signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken); + } else if (this.match(returnToken)) { + signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken); + } + } + + tsParseBindingListForSignature(): $ReadOnlyArray { + return this.parseBindingList(tt.parenR).map((pattern) => { + if (pattern.type !== "Identifier" && pattern.type !== "RestElement") { + throw this.unexpected(pattern.start, "Name in a signature must be an Identifier."); + } + return pattern; + }); + } + + tsParseTypeMemberSemicolon(): void { + if (!this.eat(tt.comma)) { + this.semicolon(); + } + } + + tsParseSignatureMember( + kind: "TSCallSignatureDeclaration" | "TSConstructSignatureDeclaration", + ): N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration { + const node: N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration = this.startNode(); + if (kind === "TSConstructSignatureDeclaration") { + this.expect(tt._new); + } + this.tsFillSignature(tt.colon, node); + this.tsParseTypeMemberSemicolon(); + return this.finishNode(node, kind); + } + + tsIsUnambiguouslyIndexSignature() { + this.next(); // Skip '{' + return this.eat(tt.name) && this.match(tt.colon); + } + + tsTryParseIndexSignature(node: N.TsIndexSignature): ?N.TsIndexSignature { + if (!(this.match(tt.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) { + return undefined; + } + + this.expect(tt.bracketL); + const id = this.parseIdentifier(); + this.expect(tt.colon); + id.typeAnnotation = this.tsParseTypeAnnotation(/* eatColon */ false); + this.expect(tt.bracketR); + node.parameters = [id]; + + const type = this.tsTryParseTypeAnnotation(); + if (type) node.typeAnnotation = type; + this.tsParseTypeMemberSemicolon(); + return this.finishNode(node, "TSIndexSignature"); + } + + tsParsePropertyOrMethodSignature( + node: N.TsPropertySignature | N.TsMethodSignature, + readonly: boolean): N.TsPropertySignature | N.TsMethodSignature { + + this.parsePropertyName(node); + if (this.eat(tt.question)) node.optional = true; + const nodeAny: any = node; + + if (!readonly && (this.match(tt.parenL) || this.isRelational("<"))) { + const method: N.TsMethodSignature = nodeAny; + this.tsFillSignature(tt.colon, method); + this.tsParseTypeMemberSemicolon(); + return this.finishNode(method, "TSMethodSignature"); + } else { + const property: N.TsPropertySignature = nodeAny; + if (readonly) property.readonly = true; + const type = this.tsTryParseTypeAnnotation(); + if (type) property.typeAnnotation = type; + this.tsParseTypeMemberSemicolon(); + return this.finishNode(property, "TSPropertySignature"); + } + } + + tsParseTypeMember(): N.TsTypeElement { + if (this.match(tt.parenL) || this.isRelational("<")) { + return this.tsParseSignatureMember("TSCallSignatureDeclaration"); + } + if (this.match(tt._new) && this.tsLookAhead(this.tsIsStartOfConstructSignature.bind(this))) { + return this.tsParseSignatureMember("TSConstructSignatureDeclaration"); + } + // Instead of fullStart, we create a node here. + const node: any = this.startNode(); + const readonly = !!this.tsParseModifier(["readonly"]); + + const idx = this.tsTryParseIndexSignature(node); + if (idx) { + if (readonly) node.readonly = true; + return idx; + } + return this.tsParsePropertyOrMethodSignature(node, readonly); + } + + tsIsStartOfConstructSignature() { + this.next(); + return this.match(tt.parenL) || this.isRelational("<"); + } + + tsParseTypeLiteral(): N.TsTypeLiteral { + const node: N.TsTypeLiteral = this.startNode(); + node.members = this.tsParseObjectTypeMembers(); + return this.finishNode(node, "TSTypeLiteral"); + } + + tsParseObjectTypeMembers(): $ReadOnlyArray { + this.expect(tt.braceL); + const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this)); + this.expect(tt.braceR); + return members; + } + + tsIsStartOfMappedType(): boolean { + this.next(); + if (this.isContextual("readonly")) { + this.next(); + } + if (!this.match(tt.bracketL)) { + return false; + } + this.next(); + if (!this.tsIsIdentifier()) { + return false; + } + this.next(); + return this.match(tt._in); + } + + tsParseMappedTypeParameter(): N.TypeParameter { + const node: N.TypeParameter = this.startNode(); + node.name = this.parseIdentifierName(node.start); + this.expect(tt._in); + node.constraint = this.tsParseType(); + return this.finishNode(node, "TypeParameter"); + } + + tsParseMappedType(): N.TsMappedType { + const node: N.TsMappedType = this.startNode(); + + this.expect(tt.braceL); + if (this.eatContextual("readonly")) { + node.readonly = true; + } + this.expect(tt.bracketL); + node.typeParameter = this.tsParseMappedTypeParameter(); + this.expect(tt.bracketR); + if (this.eat(tt.question)) { + node.optional = true; + } + node.typeAnnotation = this.tsTryParseType(); + this.semicolon(); + this.expect(tt.braceR); + + return this.finishNode(node, "TSMappedType"); + } + + tsParseTupleType(): N.TsTupleType { + const node: N.TsTupleType = this.startNode(); + node.elementTypes = this.tsParseBracketedList( + "TupleElementTypes", + this.tsParseType.bind(this), + /* bracket */true, + /* skipFirstToken */false); + return this.finishNode(node, "TSTupleType"); + } + + tsParseParenthesizedType(): N.TsParenthesizedType { + const node = this.startNode(); + this.expect(tt.parenL); + node.typeAnnotation = this.tsParseType(); + this.expect(tt.parenR); + return this.finishNode(node, "TSParenthesizedType"); + } + + tsParseFunctionOrConstructorType( + type: "TSFunctionType" | "TSConstructorType"): N.TsFunctionOrConstructorType { + + const node: N.TsFunctionOrConstructorType = this.startNode(); + if (type === "TSConstructorType") { + this.expect(tt._new); + } + this.tsFillSignature(tt.arrow, node); + return this.finishNode(node, type); + } + + tsParseLiteralTypeNode(): N.TsLiteralType { + const node: N.TsLiteralType = this.startNode(); + node.literal = (() => { + switch (this.state.type) { + case tt.num: + return this.parseLiteral(this.state.value, "NumericLiteral"); + case tt.string: + return this.parseLiteral(this.state.value, "StringLiteral"); + case tt._true: + case tt._false: + return this.parseBooleanLiteral(); + default: + throw this.unexpected(); + } + })(); + return this.finishNode(node, "TSLiteralType"); + } + + tsParseNonArrayType(): N.TsType { + switch (this.state.type) { + case tt.name: + case tt._void: + case tt._null: + const type = this.match(tt._void) + ? "TSVoidKeyword" + : this.match(tt._null) + ? "TSNullKeyword" + : keywordTypeFromName(this.state.value); + if (type !== undefined && this.lookahead().type !== tt.dot) { + const node: N.TsKeywordType = this.startNode(); + this.next(); + return this.finishNode(node, type); + } + return this.tsParseTypeReference(); + case tt.string: + case tt.num: + case tt._true: + case tt._false: + return this.tsParseLiteralTypeNode(); + case tt.plusMin: + if (this.state.value === "-") { + const node: N.TsLiteralType = this.startNode(); + this.next(); + if (!this.match(tt.num)) { + throw this.unexpected(); + } + node.literal = this.parseLiteral(-this.state.value, "NumericLiteral", node.start, node.loc.start); + return this.finishNode(node, "TSLiteralType"); + } + break; + case tt._this: + const thisKeyword = this.tsParseThisTypeNode(); + if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { + return this.tsParseThisTypePredicate(thisKeyword); + } else { + return thisKeyword; + } + case tt._typeof: + return this.tsParseTypeQuery(); + case tt.braceL: + return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) + ? this.tsParseMappedType() + : this.tsParseTypeLiteral(); + case tt.bracketL: + return this.tsParseTupleType(); + case tt.parenL: + return this.tsParseParenthesizedType(); + } + + throw this.unexpected(); + } + + tsParseArrayTypeOrHigher(): N.TsType { + let type = this.tsParseNonArrayType(); + while (!this.hasPrecedingLineBreak() && this.eat(tt.bracketL)) { + if (this.match(tt.bracketR)) { + const node: N.TsArrayType = this.startNodeAtNode(type); + node.elementType = type; + this.expect(tt.bracketR); + type = this.finishNode(node, "TSArrayType"); + } else { + const node: N.TsIndexedAccessType = this.startNodeAtNode(type); + node.objectType = type; + node.indexType = this.tsParseType(); + this.expect(tt.bracketR); + type = this.finishNode(node, "TSIndexedAccessType"); + } + } + return type; + } + + tsParseTypeOperator(operator: "keyof"): N.TsTypeOperator { + const node = this.startNode(); + this.expectContextual(operator); + node.operator = operator; + node.typeAnnotation = this.tsParseTypeOperatorOrHigher(); + return this.finishNode(node, "TSTypeOperator"); + } + + tsParseTypeOperatorOrHigher(): N.TsType { + if (this.isContextual("keyof")) { + return this.tsParseTypeOperator("keyof"); + } + return this.tsParseArrayTypeOrHigher(); + } + + tsParseUnionOrIntersectionType( + kind: "TSUnionType" | "TSIntersectionType", + parseConstituentType: () => N.TsType, + operator: TokenType): N.TsType { + + this.eat(operator); + let type = parseConstituentType(); + if (this.match(operator)) { + const types = [type]; + while (this.eat(operator)) { + types.push(parseConstituentType()); + } + const node: N.TsUnionType | N.TsIntersectionType = this.startNodeAtNode(type); + node.types = types; + type = this.finishNode(node, kind); + } + return type; + } + + tsParseIntersectionTypeOrHigher(): N.TsType { + return this.tsParseUnionOrIntersectionType( + "TSIntersectionType", + this.tsParseTypeOperatorOrHigher.bind(this), + tt.bitwiseAND); + } + + tsParseUnionTypeOrHigher() { + return this.tsParseUnionOrIntersectionType( + "TSUnionType", + this.tsParseIntersectionTypeOrHigher.bind(this), + tt.bitwiseOR); + } + + tsIsStartOfFunctionType() { + if (this.isRelational("<")) { + return true; + } + return this.match(tt.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this)); + } + + tsSkipParameterStart(): boolean { + if (this.match(tt.name) || this.match(tt._this)) { + this.next(); + return true; + } + return false; + } + + tsIsUnambiguouslyStartOfFunctionType(): boolean { + this.next(); + if (this.match(tt.parenR) || this.match(tt.ellipsis)) { + // ( ) + // ( ... + return true; + } + if (this.tsSkipParameterStart()) { + if (this.match(tt.colon) || this.match(tt.comma) || this.match(tt.question) || this.match(tt.eq)) { + // ( xxx : + // ( xxx , + // ( xxx ? + // ( xxx = + return true; + } + if (this.match(tt.parenR)) { + this.next(); + if (this.match(tt.arrow)) { + // ( xxx ) => + return true; + } + } + } + return false; + } + + tsParseTypeOrTypePredicateAnnotation(returnToken: TokenType): N.TypeAnnotation { + const t: N.TypeAnnotation = this.startNode(); + this.expect(returnToken); + + const typePredicateVariable = this.tsIsIdentifier() + && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); + + if (!typePredicateVariable) { + return this.tsParseTypeAnnotation(/* eatColon */ false, t); + } + + const type = this.tsParseTypeAnnotation(/* eatColon */ false); + + const node: N.TsTypePredicate = this.startNodeAtNode(typePredicateVariable); + node.parameterName = typePredicateVariable; + node.typeAnnotation = type; + t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); + return this.finishNode(t, "TypeAnnotation"); + } + + tsTryParseTypeOrTypePredicateAnnotation(): ?N.TypeAnnotation { + return this.match(tt.colon) ? this.tsParseTypeOrTypePredicateAnnotation(tt.colon) : undefined; + } + + tsTryParseTypeAnnotation(): ?N.TypeAnnotation { + return this.match(tt.colon) ? this.tsParseTypeAnnotation() : undefined; + } + + tsTryParseType(): ?N.TsType { + return this.eat(tt.colon) ? this.tsParseType() : undefined; + } + + tsParseTypePredicatePrefix(): ?N.Identifier { + const id = this.parseIdentifier(); + if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { + this.next(); + return id; + } + } + + tsParseTypeAnnotation(eatColon = true, t: N.TypeAnnotation = this.startNode()): N.TypeAnnotation { + if (eatColon) this.expect(tt.colon); + t.typeAnnotation = this.tsParseType(); + return this.finishNode(t, "TypeAnnotation"); + } + + tsParseType(): N.TsType { + // Need to set `state.inType` so that we don't parse JSX in a type context. + const oldInType = this.state.inType; + this.state.inType = true; + try { + if (this.tsIsStartOfFunctionType()) { + return this.tsParseFunctionOrConstructorType("TSFunctionType"); + } + if (this.match(tt._new)) { // As in `new () => Date` + return this.tsParseFunctionOrConstructorType("TSConstructorType"); + } + return this.tsParseUnionTypeOrHigher(); + } finally { + this.state.inType = oldInType; + } + } + + tsParseTypeAssertion(): N.TsTypeAssertion { + const node: N.TsTypeAssertion = this.startNode(); + node.typeAnnotation = this.tsParseType(); + this.expectRelational(">"); + node.expression = this.parseMaybeUnary(); + return this.finishNode(node, "TSTypeAssertion"); + } + + tsTryParseTypeArgumentsInExpression(): ?N.TypeParameterInstantiation { + return this.tsTryParseAndCatch(() => { + const res: N.TypeParameterInstantiation = this.startNode(); + this.expectRelational("<"); + const typeArguments = this.tsParseDelimitedList( + "TypeParametersOrArguments", + this.tsParseType.bind(this)); + this.expectRelational(">"); + res.params = typeArguments; + this.finishNode(res, "TypeParameterInstantiation"); + this.expect(tt.parenL); + return res; + }); + } + + tsParseHeritageClause(): $ReadOnlyArray { + return this.tsParseDelimitedList( + "HeritageClauseElement", + this.tsParseExpressionWithTypeArguments.bind(this)); + } + + tsParseExpressionWithTypeArguments(): N.TsExpressionWithTypeArguments { + const node: N.TsExpressionWithTypeArguments = this.startNode(); + // Note: TS uses parseLeftHandSideExpressionOrHigher, + // then has grammar errors later if it's not an EntityName. + node.expression = this.tsParseEntityName(/* allowReservedWords */ false); + if (this.isRelational("<")) { + node.typeParameters = this.tsParseTypeArguments(); + } + + return this.finishNode(node, "TSExpressionWithTypeArguments"); + } + + tsParseInterfaceDeclaration(node: N.TsInterfaceDeclaration): N.TsInterfaceDeclaration { + node.id = this.parseIdentifier(); + node.typeParameters = this.tsTryParseTypeParameters(); + if (this.eat(tt._extends)) { + node.extends = this.tsParseHeritageClause(); + } + const body: N.TSInterfaceBody = this.startNode(); + body.body = this.tsParseObjectTypeMembers(); + node.body = this.finishNode(body, "TSInterfaceBody"); + return this.finishNode(node, "TSInterfaceDeclaration"); + } + + tsParseTypeAliasDeclaration(node: N.TsTypeAliasDeclaration): N.TsTypeAliasDeclaration { + node.id = this.parseIdentifier(); + node.typeParameters = this.tsTryParseTypeParameters(); + this.expect(tt.eq); + node.typeAnnotation = this.tsParseType(); + this.semicolon(); + return this.finishNode(node, "TSTypeAliasDeclaration"); + } + + tsParseEnumMember(): N.TsEnumMember { + const node: N.TsEnumMember = this.startNode(); + // Computed property names are grammar errors in an enum, so accept just string literal or identifier. + node.id = this.match(tt.string) + ? this.parseLiteral(this.state.value, "StringLiteral") + : this.parseIdentifier(/* liberal */ true); + if (this.eat(tt.eq)) { + node.initializer = this.parseMaybeAssign(); + } + return this.finishNode(node, "TSEnumMember"); + } + + tsParseEnumDeclaration(node: N.TsEnumDeclaration, isConst: boolean): N.TsEnumDeclaration { + if (isConst) node.const = true; + node.id = this.parseIdentifier(); + this.expect(tt.braceL); + node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this)); + this.expect(tt.braceR); + return this.finishNode(node, "TSEnumDeclaration"); + } + + tsParseModuleBlock(): N.TsModuleBlock { + const node: N.TsModuleBlock = this.startNode(); + this.expect(tt.braceL); + // Inside of a module block is considered "top-level", meaning it can have imports and exports. + this.parseBlockOrModuleBlockBody( + node.body = [], /* directives */ undefined, /* topLevel */ true, /* end */ tt.braceR); + return this.finishNode(node, "TSModuleBlock"); + } + + tsParseModuleOrNamespaceDeclaration(node: N.TsModuleDeclaration): N.TsModuleDeclaration { + node.id = this.parseIdentifier(); + if (this.eat(tt.dot)) { + const inner = this.startNode(); + this.tsParseModuleOrNamespaceDeclaration(inner); + node.body = inner; + } else { + node.body = this.tsParseModuleBlock(); + } + return this.finishNode(node, "TSModuleDeclaration"); + } + + tsParseAmbientExternalModuleDeclaration(node: N.TsModuleDeclaration): N.TsModuleDeclaration { + if (this.isContextual("global")) { + node.global = true; + node.id = this.parseIdentifier(); + } else if (this.match(tt.string)) { + node.id = this.parseLiteral(this.state.value, "StringLiteral"); + } else { + this.unexpected(); + } + + if (this.match(tt.braceL)) { + node.body = this.tsParseModuleBlock(); + } else { + this.semicolon(); + } + + return this.finishNode(node, "TSModuleDeclaration"); + } + + tsParseImportEqualsDeclaration( + node: N.TsImportEqualsDeclaration, + isExport?: boolean): N.TsImportEqualsDeclaration { + + node.isExport = isExport || false; + node.id = this.parseIdentifier(); + this.expect(tt.eq); + node.moduleReference = this.tsParseModuleReference(); + this.semicolon(); + return this.finishNode(node, "TSImportEqualsDeclaration"); + } + + tsIsExternalModuleReference(): boolean { + return this.isContextual("require") && this.lookahead().type === tt.parenL; + } + + tsParseModuleReference(): N.TsModuleReference { + return this.tsIsExternalModuleReference() + ? this.tsParseExternalModuleReference() + : this.tsParseEntityName(/* allowReservedWords */ false); + } + + tsParseExternalModuleReference(): N.TsExternalModuleReference { + const node: N.TsExternalModuleReference = this.startNode(); + this.expectContextual("require"); + this.expect(tt.parenL); + if (!this.match(tt.string)) { + throw this.unexpected(); + } + node.expression = this.parseLiteral(this.state.value, "StringLiteral"); + this.expect(tt.parenR); + return this.finishNode(node, "TSExternalModuleReference"); + } + + // Utilities + + tsLookAhead(f: () => T): T { + const state = this.state.clone(); + const res = f(); + this.state = state; + return res; + } + + tsTryParseAndCatch(f: () => T): ?T { + const state = this.state.clone(); + try { + return f(); + } catch (e) { + if (e instanceof SyntaxError) { + this.state = state; + return undefined; + } + throw e; + } + } + + tsTryParse(f: () => ?T): ?T { + const state = this.state.clone(); + const result = f(); + if (result !== undefined && result !== false) { + return result; + } else { + this.state = state; + return undefined; + } + } + + nodeWithSamePosition(original: N.Node, type: string): T { + const node = this.startNodeAtNode(original); + node.type = type; + node.end = original.end; + node.loc.end = original.loc.end; + + if (original.leadingComments) node.leadingComments = original.leadingComments; + if (original.trailingComments) node.trailingComments = original.trailingComments; + if (original.innerComments) node.innerComments = original.innerComments; + + return node; + } + + tsTryParseDeclare(nany: any): ?N.Declaration { + switch (this.state.type) { + case tt._function: + this.next(); + return this.parseFunction(nany, /* isStatement */ true); + case tt._class: + return this.parseClass(nany, /* isStatement */ true, /* optionalId */ false); + case tt._const: + if (this.match(tt._const) && this.lookaheadIsContextual("enum")) { + // `const enum = 0;` not allowed because "enum" is a strict mode reserved word. + this.expect(tt._const); + this.expectContextual("enum"); + return this.tsParseEnumDeclaration(nany, /* isConst */ true); + } + // falls through + case tt._var: case tt._let: + return this.parseVarStatement(nany, this.state.type); + case tt.name: + const value = this.state.value; + if (value === "global") { + return this.tsParseAmbientExternalModuleDeclaration(nany); + } else { + return this.tsParseDeclaration(nany, value, /* next */ true); + } + } + } + + lookaheadIsContextual(name: string): boolean { + const l = this.lookahead(); + return l.type === tt.name && l.value === name; + } + + // Note: this won't be called unless the keyword is allowed in `shouldParseExportDeclaration`. + tsTryParseExportDeclaration(): ?N.Declaration { + return this.tsParseDeclaration(this.startNode(), this.state.value, /* next */ true); + } + + tsParseExpressionStatement(node: any, expr: N.Identifier): ?N.Declaration { + switch (expr.name) { + case "declare": + const declaration = this.tsTryParseDeclare(node); + if (declaration) { + declaration.declare = true; + return declaration; + } + break; + + case "global": + // `global { }` (with no `declare`) may appear inside an ambient module declaration. + // Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global". + if (this.match(tt.braceL)) { + const mod: N.TsModuleDeclaration = node; + mod.global = true; + mod.id = expr; + mod.body = this.tsParseModuleBlock(); + return this.finishNode(mod, "TSModuleDeclaration"); + } + break; + + default: + return this.tsParseDeclaration(node, expr.name, /* next */ false); + } + } + + // Common to tsTryParseDeclare, tsTryParseExportDeclaration, and tsParseExpressionStatement. + tsParseDeclaration(node: any, value: string, next: boolean): ?N.Declaration { + switch (value) { + case "abstract": + if (next || this.match(tt._class)) { + const cls: N.ClassDeclaration = node; + cls.abstract = true; + if (next) this.next(); + return this.parseClass(cls, /* isStatement */ true, /* optionalId */ false); + } + break; + + case "enum": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseEnumDeclaration(node, /* isConst */ false); + } + break; + + case "interface": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseInterfaceDeclaration(node); + } + break; + + case "module": + if (next) this.next(); + if (this.match(tt.string)) { + return this.tsParseAmbientExternalModuleDeclaration(node); + } else if (next || this.match(tt.name)) { + return this.tsParseModuleOrNamespaceDeclaration(node); + } + break; + + case "namespace": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseModuleOrNamespaceDeclaration(node); + } + break; + + case "type": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseTypeAliasDeclaration(node); + } + break; + } + } + + tsTryParseGenericAsyncArrowFunction(startPos: number, startLoc: Position): ?N.ArrowFunctionExpression { + const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => { + const node: N.ArrowFunctionExpression = this.startNodeAt(startPos, startLoc); + this.expectRelational("<"); + node.typeParameters = this.tsParseTypeParameters(); + // Don't use overloaded parseFunctionParams which would look for "<" again. + super.parseFunctionParams(node); + node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation(); + this.expect(tt.arrow); + return node; + }); + + if (!res) { + return undefined; + } + + res.id = null; + res.generator = false; + res.expression = true; // May be set again by parseFunctionBody. + res.async = true; + this.parseFunctionBody(res, true); + return this.finishNode(res, "ArrowFunctionExpression"); + } + + tsParseTypeArguments(): N.TypeParameterInstantiation { + const node = this.startNode(); + this.expectRelational("<"); + node.params = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); + this.expectRelational(">"); + return this.finishNode(node, "TypeParameterInstantiation"); + } + + // ====================================================== + // OVERRIDES + // ====================================================== + + parseAssignableListItem( + allowModifiers: ?boolean, + decorators: N.Decorator[]): N.Pattern | N.TSParameterProperty { + let accessibility: ?N.Accessibility; + let readonly = false; + if (allowModifiers) { + accessibility = this.parseAccessModifier(); + readonly = !!this.tsParseModifier(["readonly"]); + } + + const left = this.parseMaybeDefault(); + this.parseAssignableListItemTypes(left); + const elt = this.parseMaybeDefault(left.start, left.loc.start, left); + if (accessibility || readonly) { + const pp: N.TSParameterProperty = this.startNodeAtNode(elt); + if (decorators.length) { + pp.decorators = decorators; + } + if (accessibility) pp.accessibility = accessibility; + if (readonly) pp.readonly = readonly; + if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") { + throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern."); + } + pp.parameter = elt; + return this.finishNode(pp, "TSParameterProperty"); + } else { + if (decorators.length) { + left.decorators = decorators; + } + return elt; + } + } + + parseFunctionBodyAndFinish( + node: N.BodilessFunctionOrMethodBase, + type: string, + allowExpressionBody?: boolean): void { + // For arrow functions, `parseArrow` handles the return type itself. + if (!allowExpressionBody && this.match(tt.colon)) { + node.returnType = this.tsParseTypeOrTypePredicateAnnotation(tt.colon); + } + + const bodilessType = type === "FunctionDeclaration" + ? "TSDeclareFunction" + : type === "ClassMethod" + ? "TSDeclareMethod" + : undefined; + if (bodilessType && !this.match(tt.braceL) && this.isLineTerminator()) { + this.finishNode(node, bodilessType); + return; + } + + super.parseFunctionBodyAndFinish(node, type, allowExpressionBody); + } + + parseSubscript( + base: N.Expression, + startPos: number, + startLoc: Position, + noCalls: ?boolean, + state: { stop: boolean }): N.Expression { + + if (this.eat(tt.bang)) { + const nonNullExpression: N.TsNonNullExpression = this.startNodeAt(startPos, startLoc); + nonNullExpression.expression = base; + return this.finishNode(nonNullExpression, "TSNonNullExpression"); + } + + if (!noCalls && this.isRelational("<")) { + if (this.atPossibleAsync(base)) { + // Almost certainly this is a generic async function `async () => ... + // But it might be a call with a type argument `async();` + const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc); + if (asyncArrowFn) { + return asyncArrowFn; + } + } + + const node: N.CallExpression = this.startNodeAt(startPos, startLoc); + node.callee = base; + + // May be passing type arguments. But may just be the `<` operator. + const typeArguments = this.tsTryParseTypeArgumentsInExpression(); // Also eats the "(" + if (typeArguments) { + // possibleAsync always false here, because we would have handled it above. + // $FlowIgnore (won't be any undefined arguments) + node.arguments = this.parseCallExpressionArguments(tt.parenR, /* possibleAsync */ false); + node.typeParameters = typeArguments; + return this.finishCallExpression(node); + } + } + + return super.parseSubscript(base, startPos, startLoc, noCalls, state); + } + + parseNewArguments(node: N.NewExpression): void { + if (this.isRelational("<")) { // tsTryParseAndCatch is expensive, so avoid if not necessary. + // 99% certain this is `new C();`. But may be `new C < T;`, which is also legal. + const typeParameters = this.tsTryParseAndCatch(() => { + const args = this.tsParseTypeArguments(); + if (!this.match(tt.parenL)) this.unexpected(); + return args; + }); + if (typeParameters) { + node.typeParameters = typeParameters; + } + } + + super.parseNewArguments(node); + } + + parseExprOp( + left: N.Expression, + leftStartPos: number, + leftStartLoc: Position, + minPrec: number, + noIn: ?boolean) { + if (nonNull(tt._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.eatContextual("as")) { + const node: N.TsAsExpression = this.startNodeAt(leftStartPos, leftStartLoc); + node.expression = left; + node.typeAnnotation = this.tsParseType(); + this.finishNode(node, "TSAsExpression"); + return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn); + } + + return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn); + } + + // eslint-disable-next-line no-unused-vars + checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { + // Don't bother checking for TypeScript code. + // Strict mode words may be allowed as in `declare namespace N { const static: number; }`. + // And we have a type checker anyway, so don't bother having the parser do it. + } + + /* + Don't bother doing this check in TypeScript code because: + 1. We may have a nested export statement with the same name: + export const x = 0; + export namespace N { + export const x = 1; + } + 2. We have a type checker to warn us about this sort of thing. + */ + checkDuplicateExports() {} + + parseImport(node: N.Node): N.ImportDeclaration | N.TsImportEqualsDeclaration { + if (this.match(tt.name) && this.lookahead().type === tt.eq) { + return this.tsParseImportEqualsDeclaration(node); + } + return super.parseImport(node); + } + + parseExport(node: N.Node): N.Node { + if (this.match(tt._import)) { // `export import A = B;` + this.expect(tt._import); + return this.tsParseImportEqualsDeclaration(node, /* isExport */ true); + } else if (this.eat(tt.eq)) { // `export = x;` + const assign: N.TsExportAssignment = node; + assign.expression = this.parseExpression(); + this.semicolon(); + return this.finishNode(assign, "TSExportAssignment"); + } else if (this.eatContextual("as")) { // `export as namespace A;` + const decl: N.TsNamespaceExportDeclaration = node; + // See `parseNamespaceExportDeclaration` in TypeScript's own parser + this.expectContextual("namespace"); + decl.id = this.parseIdentifier(); + this.semicolon(); + return this.finishNode(decl, "TSNamespaceExportDeclaration"); + } else { + return super.parseExport(node); + } + } + + parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement { + if (this.state.type === tt._const) { + const ahead = this.lookahead(); + if (ahead.type === tt.name && ahead.value === "enum") { + const node: N.TsEnumDeclaration = this.startNode(); + this.expect(tt._const); + this.expectContextual("enum"); + return this.tsParseEnumDeclaration(node, /* isConst */ true); + } + } + return super.parseStatementContent(declaration, topLevel); + } + + parseAccessModifier(): ?N.Accessibility { + return this.tsParseModifier(["public", "protected", "private"]); + } + + parseClassMember(classBody: N.ClassBody, member: any, state: { hadConstructor: boolean }): void { + const accessibility = this.parseAccessModifier(); + if (accessibility) member.accessibility = accessibility; + + super.parseClassMember(classBody, member, state); + } + + parseClassMemberWithIsStatic( + classBody: N.ClassBody, + member: any, + state: { hadConstructor: boolean }, + isStatic: boolean): void { + const methodOrProp: N.ClassMethod | N.ClassProperty = member; + const prop: N.ClassProperty = member; + const propOrIdx: N.ClassProperty | N.TsIndexSignature = member; + + let abstract = false, readonly = false; + + const mod = this.tsParseModifier(["abstract", "readonly"]); + switch (mod) { + case "readonly": + readonly = true; + abstract = !!this.tsParseModifier(["abstract"]); + break; + case "abstract": + abstract = true; + readonly = !!this.tsParseModifier(["readonly"]); + break; + } + + if (abstract) methodOrProp.abstract = true; + if (readonly) propOrIdx.readonly = true; + + if (!abstract && !isStatic && !methodOrProp.accessibility) { + const idx = this.tsTryParseIndexSignature(member); + if (idx) { + classBody.body.push(idx); + return; + } + } + + if (readonly) { + // Must be a property (if not an index signature). + methodOrProp.static = isStatic; + this.parseClassPropertyName(prop); + this.parsePostMemberNameModifiers(methodOrProp); + this.pushClassProperty(classBody, prop); + return; + } + + super.parseClassMemberWithIsStatic(classBody, member, state, isStatic); + } + + parsePostMemberNameModifiers(methodOrProp: N.ClassMethod | N.ClassProperty): void { + const optional = this.eat(tt.question); + if (optional) methodOrProp.optional = true; + } + + // Note: The reason we do this in `parseExpressionStatement` and not `parseStatement` + // is that e.g. `type()` is valid JS, so we must try parsing that first. + // If it's really a type, we will parse `type` as the statement, and can correct it here + // by parsing the rest. + parseExpressionStatement(node: N.ExpressionStatement, expr: N.Expression): N.Statement { + const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined; + return decl || super.parseExpressionStatement(node, expr); + } + + // export type + // Should be true for anything parsed by `tsTryParseExportDeclaration`. + shouldParseExportDeclaration(): boolean { + if (this.match(tt.name)) { + switch (this.state.value) { + case "abstract": + case "declare": + case "enum": + case "interface": + case "module": + case "namespace": + case "type": + return true; + } + } + return super.shouldParseExportDeclaration(); + } + + // An apparent conditional expression could actually be an optional parameter in an arrow function. + parseConditional( + expr: N.Expression, + noIn: ?boolean, + startPos: number, + startLoc: Position, + refNeedsArrowPos?: ?Pos): N.Expression { + + // only do the expensive clone if there is a question mark + // and if we come from inside parens + if (!refNeedsArrowPos || !this.match(tt.question)) { + return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos); + } + + const state = this.state.clone(); + try { + return super.parseConditional(expr, noIn, startPos, startLoc); + } catch (err) { + if (!(err instanceof SyntaxError)) { + // istanbul ignore next: no such error is expected + throw err; + } + + this.state = state; + refNeedsArrowPos.start = err.pos || this.state.start; + return expr; + } + } + + // Note: These "type casts" are *not* valid TS expressions. + // But we parse them here and change them when completing the arrow function. + parseParenItem(node: N.Expression, startPos: number, startLoc: Position): N.Expression { + node = super.parseParenItem(node, startPos, startLoc); + if (this.eat(tt.question)) { + node.optional = true; + } + + if (this.match(tt.colon)) { + const typeCastNode: N.TypeCastExpression = this.startNodeAt(startPos, startLoc); + typeCastNode.expression = node; + typeCastNode.typeAnnotation = this.tsParseTypeAnnotation(); + + return this.finishNode(typeCastNode, "TypeCastExpression"); + } + + return node; + } + + parseExportDeclaration(node: N.ExportNamedDeclaration): ?N.Declaration { + // "export declare" is equivalent to just "export". + const isDeclare = this.eatContextual("declare"); + + let declaration: ?N.Declaration; + if (this.match(tt.name)) { + declaration = this.tsTryParseExportDeclaration(); + } + if (!declaration) { + declaration = super.parseExportDeclaration(node); + } + + if (declaration && isDeclare) { + declaration.declare = true; + } + + return declaration; + } + + parseClassId(node: N.Class, isStatement: boolean, optionalId: ?boolean): void { + if ((!isStatement || optionalId) && this.isContextual("implements")) { + return; + } + + super.parseClassId(...arguments); + const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters) node.typeParameters = typeParameters; + } + + parseClassProperty(node: N.ClassProperty): N.ClassProperty { + const type = this.tsTryParseTypeAnnotation(); + if (type) node.typeAnnotation = type; + return super.parseClassProperty(node); + } + + parseClassMethod( + classBody: N.ClassBody, + method: N.ClassMethod, + isGenerator: boolean, + isAsync: boolean, + isConstructor: boolean): void { + + const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters) method.typeParameters = typeParameters; + super.parseClassMethod(classBody, method, isGenerator, isAsync, isConstructor); + } + + parseClassSuper(node: N.Class): void { + super.parseClassSuper(node); + if (node.superClass && this.isRelational("<")) { + node.superTypeParameters = this.tsParseTypeArguments(); + } + if (this.eatContextual("implements")) { + node.implements = this.tsParseHeritageClause(); + } + } + + parseObjPropValue(prop: N.ObjectMember, ...args): void { + if (this.isRelational("<")) { + throw new Error("TODO"); + } + + super.parseObjPropValue(prop, ...args); + } + + parseFunctionParams(node: N.Function): void { + const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters) node.typeParameters = typeParameters; + super.parseFunctionParams(node); + } + + // `let x: number;` + parseVarHead(decl: N.VariableDeclarator): void { + super.parseVarHead(decl); + const type = this.tsTryParseTypeAnnotation(); + if (type) { + decl.id.typeAnnotation = type; + this.finishNode(decl.id, decl.id.type); // set end position to end of type + } + } + + // parse the return type of an async arrow function - let foo = (async (): number => {}); + parseAsyncArrowFromCallExpression( + node: N.ArrowFunctionExpression, + call: N.CallExpression): N.ArrowFunctionExpression { + + if (this.match(tt.colon)) { + node.returnType = this.tsParseTypeAnnotation(); + } + return super.parseAsyncArrowFromCallExpression(node, call); + } + + parseMaybeAssign(...args): N.Expression { + // Note: When the JSX plugin is on, type assertions (` x`) aren't valid syntax. + + let jsxError: ?SyntaxError; + + if (this.match(tt.jsxTagStart)) { + const context = this.curContext(); + assert(context === ct.j_oTag); + // Only time j_oTag is pushed is right after j_expr. + assert(this.state.context[this.state.context.length - 2] === ct.j_expr); + + // Prefer to parse JSX if possible. But may be an arrow fn. + const state = this.state.clone(); + try { + return super.parseMaybeAssign(...args); + } catch (err) { + if (!(err instanceof SyntaxError)) { + // istanbul ignore next: no such error is expected + throw err; + } + + this.state = state; + // Pop the context added by the jsxTagStart. + assert(this.curContext() === ct.j_oTag); + this.state.context.pop(); + assert(this.curContext() === ct.j_expr); + this.state.context.pop(); + jsxError = err; + } + } + + if (jsxError === undefined && !this.isRelational("<")) { + return super.parseMaybeAssign(...args); + } + + // Either way, we're looking at a '<': tt.jsxTagStart or relational. + + let arrowExpression; + let typeParameters: N.TypeParameterDeclaration; + const state = this.state.clone(); + this.next(); // skip the jsx start + try { + // This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`. + typeParameters = this.tsParseTypeParameters(); + arrowExpression = super.parseMaybeAssign(...args); + if (arrowExpression.type !== "ArrowFunctionExpression") { + this.unexpected(); // Go to the catch block (needs a SyntaxError). + } + } catch (err) { + if (!(err instanceof SyntaxError)) { + // istanbul ignore next: no such error is expected + throw err; + } + + if (jsxError) { + throw jsxError; + } + + // Try parsing a type cast instead of an arrow function. + // This will never happen outside of JSX. + // (Because in JSX the '<' should be a jsxTagStart and not a relational. + assert(!this.hasPlugin("jsx")); + // Parsing an arrow function failed, so try a type cast. + this.state = state; + // This will start with a type assertion (via parseMaybeUnary). + // But don't directly call `this.tsParseTypeAssertion` because we want to handle any binary after it. + return super.parseMaybeAssign(...args); + } + + // Correct TypeScript code should have at least 1 type parameter, but don't crash on bad code. + if (typeParameters && typeParameters.params.length !== 0) { + this.resetStartLocationFromNode(arrowExpression, typeParameters.params[0]); + } + arrowExpression.typeParameters = typeParameters; + return arrowExpression; + } + + // Handle type assertions + parseMaybeUnary(refShorthandDefaultPos?: ?Pos): N.Expression { + if (!this.hasPlugin("jsx") && this.eatRelational("<")) { + return this.tsParseTypeAssertion(); + } else { + return super.parseMaybeUnary(refShorthandDefaultPos); + } + } + + parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression { + if (this.match(tt.colon)) { + // This is different from how the TS parser does it. + // TS uses lookahead. Babylon parses it as a parenthesized expression and converts. + const state = this.state.clone(); + try { + const returnType = this.tsParseTypeOrTypePredicateAnnotation(tt.colon); + if (this.canInsertSemicolon()) this.unexpected(); + if (!this.match(tt.arrow)) this.unexpected(); + node.returnType = returnType; + } catch (err) { + if (err instanceof SyntaxError) { + this.state = state; + } else { + // istanbul ignore next: no such error is expected + throw err; + } + } + } + + return super.parseArrow(node); + } + + // Allow type annotations inside of a parameter list. + parseAssignableListItemTypes(param: N.Pattern) { + if (this.eat(tt.question)) { + if (param.type !== "Identifier") { + throw this.raise( + param.start, + "A binding pattern parameter cannot be optional in an implementation signature."); + } + + param.optional = true; + } + const type = this.tsTryParseTypeAnnotation(); + if (type) param.typeAnnotation = type; + return this.finishNode(param, param.type); + } + + toAssignable(node: N.Node, isBinding: ?boolean, contextDescription: string): N.Node { + switch (node.type) { + case "TypeCastExpression": + return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription); + case "TSParameterProperty": + return super.toAssignable(node, isBinding, contextDescription); + default: + return super.toAssignable(node, isBinding, contextDescription); + } + } + + checkLVal( + expr: N.Expression, + isBinding: ?boolean, + checkClashes: ?{ [key: string]: boolean }, + contextDescription: string): void { + switch (expr.type) { + case "TypeCastExpression": + // Allow "typecasts" to appear on the left of assignment expressions, + // because it may be in an arrow function. + // e.g. `const f = (foo: number = 0) => foo;` + return; + case "TSParameterProperty": + this.checkLVal(expr.parameter, isBinding, checkClashes, "parameter property"); + return; + default: + super.checkLVal(expr, isBinding, checkClashes, contextDescription); + return; + } + } + + parseBindingAtom(): N.Pattern { + switch (this.state.type) { + case tt._this: + // "this" may be the name of a parameter, so allow it. + return this.parseIdentifier(/* liberal */ true); + default: + return super.parseBindingAtom(); + } + } + + // === === === === === === === === === === === === === === === === + // Note: All below methods are duplicates of something in flow.js. + // Not sure what the best way to combine these is. + // === === === === === === === === === === === === === === === === + + isClassMethod(): boolean { + return this.isRelational("<") || super.isClassMethod(); + } + + isClassProperty(): boolean { + return this.match(tt.colon) || super.isClassProperty(); + } + + parseMaybeDefault(...args): N.Pattern { + const node = super.parseMaybeDefault(...args); + + if (node.type === "AssignmentPattern" + && node.typeAnnotation + && node.right.start < node.typeAnnotation.start) { + this.raise( + node.typeAnnotation.start, + "Type annotations must come before default assignments, " + + "e.g. instead of `age = 25: number` use `age: number = 25`"); + } + + return node; + } + + // ensure that inside types, we bypass the jsx parser plugin + readToken(code: number): void { + if (this.state.inType && (code === 62 || code === 60)) { + return this.finishOp(tt.relational, 1); + } else { + return super.readToken(code); + } + } + + toAssignableList( + exprList: N.Expression[], + isBinding: ?boolean, + contextDescription: string): $ReadOnlyArray { + + for (let i = 0; i < exprList.length; i++) { + const expr = exprList[i]; + if (expr && expr.type === "TypeCastExpression") { + exprList[i] = this.typeCastToParameter(expr); + } + } + return super.toAssignableList(exprList, isBinding, contextDescription); + } + + typeCastToParameter(node: N.TypeCastExpression): N.Node { + node.expression.typeAnnotation = node.typeAnnotation; + + return this.finishNodeAt( + node.expression, + node.expression.type, + node.typeAnnotation.end, + node.typeAnnotation.loc.end + ); + } + + toReferencedList(exprList: $ReadOnlyArray): $ReadOnlyArray { + for (let i = 0; i < exprList.length; i++) { + const expr = exprList[i]; + if (expr && expr._exprListItem && expr.type === "TypeCastExpression") { + this.raise(expr.start, "Did not expect a type annotation here."); + } + } + + return exprList; + } + + shouldParseArrow() { + return this.match(tt.colon) || super.shouldParseArrow(); + } + + shouldParseAsyncArrow(): boolean { + return this.match(tt.colon) || super.shouldParseAsyncArrow(); + } +}; diff --git a/src/tokenizer/context.js b/src/tokenizer/context.js index 7b7b97b6d7..67a62c9657 100644 --- a/src/tokenizer/context.js +++ b/src/tokenizer/context.js @@ -12,7 +12,7 @@ export class TokContext { token: string, isExpr?: boolean, preserveSpace?: boolean, - override?: Function, + override?: Function, // Takes a Tokenizer as a this-parameter, and returns void. ) { this.token = token; this.isExpr = !!isExpr; diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index 9854031814..a57684c74f 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -2,11 +2,10 @@ // @flow -import type { TokenType } from "./types"; import type { Options } from "../options"; import type { Position } from "../util/location"; import { isIdentifierStart, isIdentifierChar, isKeyword } from "../util/identifier"; -import { types as tt, keywords as keywordTypes } from "./types"; +import { types as tt, keywords as keywordTypes, type TokenType } from "./types"; import { type TokContext, types as ct } from "./context"; import LocationParser from "../parser/location"; import { SourceLocation } from "../util/location"; @@ -425,7 +424,7 @@ export default class Tokenizer extends LocationParser { this.state.pos += 2; return this.finishToken(tt.arrow); } - return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1); + return this.finishOp(code === 61 ? tt.eq : tt.bang, 1); } readToken_question() { // '?' @@ -531,7 +530,7 @@ export default class Tokenizer extends LocationParser { return this.readToken_eq_excl(code); case 126: // '~' - return this.finishOp(tt.prefix, 1); + return this.finishOp(tt.tilde, 1); } this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`); @@ -924,6 +923,11 @@ export default class Tokenizer extends LocationParser { return this.curContext() === ct.braceStatement; } + if (prevType === tt.relational) { + // `class C { ... }` + return true; + } + return !this.state.exprAllowed; } diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index 0a16182af8..307c1f248b 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -2,13 +2,11 @@ import type { Options } from "../options"; import * as N from "../types"; +import { Position } from "../util/location"; -import type { TokContext } from "./context"; +import { types as ct, type TokContext } from "./context"; import type { Token } from "./index"; -import type { TokenType } from "./types"; -import { Position } from "../util/location"; -import { types as ct } from "./context"; -import { types as tt } from "./types"; +import { types as tt, type TokenType } from "./types"; export default class State { init(options: Options, input: string): void { diff --git a/src/tokenizer/types.js b/src/tokenizer/types.js index 12d7ee1e93..c560a4ccbf 100644 --- a/src/tokenizer/types.js +++ b/src/tokenizer/types.js @@ -128,7 +128,8 @@ export const types: { [name: string]: TokenType } = { eq: new TokenType("=", { beforeExpr, isAssign }), assign: new TokenType("_=", { beforeExpr, isAssign }), incDec: new TokenType("++/--", { prefix, postfix, startsExpr }), - prefix: new TokenType("prefix", { beforeExpr, prefix, startsExpr }), + bang: new TokenType("!", { beforeExpr, prefix, startsExpr }), + tilde: new TokenType("~", { beforeExpr, prefix, startsExpr }), logicalOR: new BinopTokenType("||", 1), logicalAND: new BinopTokenType("&&", 2), bitwiseOR: new BinopTokenType("|", 3), diff --git a/src/types.js b/src/types.js index d4c1041ae8..0b6492c8b5 100644 --- a/src/types.js +++ b/src/types.js @@ -47,8 +47,16 @@ export type Pattern = export type Declaration = | VariableDeclaration | ClassDeclaration - | FunctionDeclaration; -export type DeclarationBase = NodeBase; + | FunctionDeclaration + | TsInterfaceDeclaration + | TsTypeAliasDeclaration + | TsEnumDeclaration + | TsModuleDeclaration; +export type DeclarationBase = NodeBase & { + // TypeScript allows declarations to be prefixed by `declare`. + //TODO: a FunctionDeclaration is never "declare", because it's a TSDeclareFunction instead. + declare?: true; +} // TODO: Not in spec export type HasDecorators = NodeBase & { @@ -60,6 +68,9 @@ export type Identifier = PatternBase & { name: string; __clone(): Identifier; + + // TypeScript only. Used in case of an optional parameter. + optional?: ?true; }; export type PrivateName = NodeBase & { @@ -122,16 +133,28 @@ export type Function = export type NormalFunction = FunctionDeclaration | FunctionExpression; -export type FunctionBase = HasDecorators & { +export type BodilessFunctionOrMethodBase = HasDecorators & { + // TODO: Remove this. Should not assign "id" to methods. + // https://github.com/babel/babylon/issues/535 id: ?Identifier; - params: $ReadOnlyArray; + + params: $ReadOnlyArray; body: BlockStatement; generator: boolean; async: boolean; - expression: boolean; // TODO: Not in spec - typeParameters?: ?FlowTypeParameterDeclaration; // TODO: Not in spec - returnType?: ?FlowTypeAnnotation; // TODO: Not in spec + // TODO: All not in spec + expression: boolean; + typeParameters?: ?TypeParameterDeclaration; + returnType?: ?TypeAnnotation; +} + +export type BodilessFunctionBase = BodilessFunctionOrMethodBase & { + id: ?Identifier; +} + +export type FunctionBase = BodilessFunctionBase & { + body: BlockStatement; }; // Statements @@ -271,7 +294,7 @@ export type ForOfStatement = ForInOfBase & { // Declarations -export type OptFunctionDeclaration = FunctionBase & DeclarationBase & HasDecorators & { +export type OptFunctionDeclaration = FunctionBase & DeclarationBase & { type: "FunctionDeclaration"; }; @@ -455,6 +478,7 @@ export type ConditionalExpression = NodeBase & { export type CallOrNewBase = NodeBase & { callee: Expression | Super | Import; arguments: Array; // TODO: $ReadOnlyArray + typeParameters?: ?TypeParameterInstantiation; // TODO: Not in spec }; export type CallExpression = CallOrNewBase & { @@ -463,6 +487,7 @@ export type CallExpression = CallOrNewBase & { export type NewExpression = CallOrNewBase & { type: "NewExpression"; + optional?: boolean; // TODO: Not in spec }; export type SequenceExpression = NodeBase & { @@ -495,10 +520,13 @@ export type TemplateElement = NodeBase & { // Patterns +// TypeScript access modifiers +export type Accessibility = "public" | "protected" | "private"; + export type PatternBase = HasDecorators & { - // Flow only: - optional?: true; - typeAnnotation?: ?FlowTypeAnnotation; + // TODO: All not in spec + // Flow/TypeScript only: + typeAnnotation?: ?TypeAnnotation; }; export type AssignmentProperty = ObjectProperty & { @@ -536,9 +564,10 @@ export type ClassBase = HasDecorators & { body: ClassBody; decorators: $ReadOnlyArray; - typeParameters?: ?FlowTypeParameterDeclaration; // TODO: Not in spec - superTypeParameters?: ?FlowTypeParameterInstantiation; // TODO: Not in spec - implements?: $ReadOnlyArray; + // TODO: All not in spec + typeParameters?: ?TypeParameterDeclaration; + superTypeParameters?: ?TypeParameterInstantiation; + implements?: ?$ReadOnlyArray | $ReadOnlyArray; }; export type ClassBody = NodeBase & { @@ -550,30 +579,30 @@ export type ClassMemberBase = NodeBase & HasDecorators & { static: boolean; computed: boolean; // TypeScript only: - access?: ?Accessibility; + accessibility?: ?Accessibility; abstract?: ?true; optional?: ?true; } -export type Accessibility = "public" | "protected" | "private"; - -export type ClassMember = ClassMethod | ClassProperty | ClassPrivateProperty; +export type ClassMember = ClassMethod | ClassProperty | ClassPrivateProperty | TsIndexSignature; -export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod; +export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod | TSDeclareMethod; export type MethodBase = FunctionBase & { - +kind?: MethodKind; + +kind: MethodKind; }; export type MethodKind = "constructor" | "method" | "get" | "set"; -export type ClassMethod = MethodBase & ClassMemberBase & { +export type ClassMethodOrDeclareMethodCommon = ClassMemberBase & { type: "ClassMethod"; key: Expression; kind: MethodKind; static: boolean; decorators: $ReadOnlyArray; +}; +export type ClassMethod = MethodBase & ClassMethodOrDeclareMethodCommon & { variance?: ?FlowVariance; // TODO: Not in spec }; @@ -582,7 +611,7 @@ export type ClassProperty = ClassMemberBase & { key: Expression; value: ?Expression; // TODO: Not in spec that this is nullable. - typeAnnotation?: ?FlowTypeAnnotation; // TODO: Not in spec + typeAnnotation?: ?TypeAnnotation; // TODO: Not in spec variance?: ?FlowVariance; // TODO: Not in spec // TypeScript only: (TODO: Not in spec) @@ -617,12 +646,13 @@ export type MetaProperty = NodeBase & { export type ModuleDeclaration = AnyImport | AnyExport; -export type AnyImport = ImportDeclaration; +export type AnyImport = ImportDeclaration | TsImportEqualsDeclaration; export type AnyExport = | ExportNamedDeclaration | ExportDefaultDeclaration - | ExportAllDeclaration; + | ExportAllDeclaration + | TsExportAssignment; export type ModuleSpecifier = NodeBase & { local: Identifier; @@ -670,7 +700,7 @@ export type ExportSpecifier = NodeBase & { export type ExportDefaultDeclaration = NodeBase & { type: "ExportDefaultDeclaration"; - declaration: OptFunctionDeclaration | OptClassDeclaration | Expression; + declaration: OptFunctionDeclaration | OptTSDeclareFunction | OptClassDeclaration | Expression; }; export type ExportAllDeclaration = NodeBase & { @@ -691,8 +721,38 @@ export type JSXOpeningElement = Node; export type JSXClosingElement = Node; export type JSXElement = Node; +// Flow/TypeScript common (TODO: Not in spec) + +export type TypeAnnotation = NodeBase & { + type: "TypeAnnotation"; + typeAnnotation: TsType | FlowTypeAnnotation; +} + +export type TypeParameterDeclaration = NodeBase & { + type: "TypeParameterDeclaration"; + params: $ReadOnlyArray; +}; + +export type TypeParameter = NodeBase & { + type: "TypeParameter"; + name: string; + constraint?: TsType; + default?: TsType; +}; + +export type TypeParameterInstantiation = NodeBase & { + type: "TypeParameterInstantiation"; + params: $ReadOnlyArray | $ReadOnlyArray; +}; + // Flow (TODO: Not in spec) +export type TypeCastExpression = NodeBase & { + type: "TypeCastExpression"; + expression: Expression; + typeAnnotation: TypeAnnotation; +}; + export type FlowType = Node; export type FlowPredicate = Node; export type FlowDeclare = Node; @@ -707,9 +767,6 @@ export type FlowDeclareInterface = Node; export type FlowInterface = Node; export type FlowInterfaceExtends = Node; export type FlowTypeAlias = Node; -export type FlowTypeParameter = Node; -export type FlowTypeParameterDeclaration = Node; -export type FlowTypeParameterInstantiation = Node; export type FlowObjectTypeIndexer = Node; export type FlowFunctionTypeAnnotation = Node; export type FlowObjectTypeProperty = Node; @@ -725,7 +782,6 @@ export type FlowTypeAnnotation = Node; export type FlowVariance = Node; export type FlowClassImplements = Node; - // estree export type EstreeProperty = NodeBase & { @@ -739,3 +795,362 @@ export type EstreeProperty = NodeBase & { variance?: ?FlowVariance; }; + +// === === === === +// TypeScript +// === === === === + +// Note: A type named `TsFoo` is based on TypeScript's `FooNode` type, +// defined in https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts +// Differences: +// * Change `NodeArray` to just `$ReadOnlyArray`. +// * Don't give nodes a "modifiers" list; use boolean flags instead, +// and only allow modifiers that are not considered errors. +// * A property named `type` must be renamed to `typeAnnotation` to avoid conflict with the node's type. +// * Sometimes TypeScript allows to parse something which will be a grammar error later; +// in babylon these cause exceptions, so the AST format is stricter. + +// ================ +// Misc +// ================ + +export type TSParameterProperty = HasDecorators & { + // Note: This has decorators instead of its parameter. + type: "TSParameterProperty"; + // At least one of `accessibility` or `readonly` must be set. + accessibility?: ?Accessibility; + readonly?: ?true; + parameter: Identifier | AssignmentPattern; +} + +export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { + type: "TSDeclareFunction"; +} + +export type TSDeclareFunction = OptTSDeclareFunction & { + id: Identifier; +}; + +export type TSDeclareMethod = BodilessFunctionOrMethodBase & ClassMethodOrDeclareMethodCommon & { + type: "TSDeclareMethod"; + +kind: MethodKind; +}; + +export type TsQualifiedName = NodeBase & { + type: "TSQualifiedName"; + left: TsEntityName; + right: Identifier; +} + +export type TsEntityName = Identifier | TsQualifiedName; + +export type TsSignatureDeclaration = + | TsCallSignatureDeclaration + | TsConstructSignatureDeclaration + | TsMethodSignature + | TsFunctionType + | TsConstructorType; + +export type TsSignatureDeclarationOrIndexSignatureBase = NodeBase & { + // Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions. + parameters: $ReadOnlyArray; + typeAnnotation: ?TypeAnnotation; +} + +export type TsSignatureDeclarationBase = TsSignatureDeclarationOrIndexSignatureBase & { + typeParameters: ?TypeParameterDeclaration; +} + +// ================ +// TypeScript type members (for type literal / interface / class) +// ================ + +export type TsTypeElement = + | TsCallSignatureDeclaration + | TsConstructSignatureDeclaration + | TsPropertySignature + | TsMethodSignature + | TsIndexSignature; + +export type TsCallSignatureDeclaration = TsSignatureDeclarationBase & { + type: "TSCallSignatureDeclaration"; +} + +export type TsConstructSignatureDeclaration = TsSignatureDeclarationBase & { + type: "TSConstructSignature"; +} + +export type TsNamedTypeElementBase = NodeBase & { + // Not using TypeScript's `PropertyName` here since we don't have a `ComputedPropertyName` node type. + // This is usually an Identifier but may be e.g. `Symbol.iterator` if `computed` is true. + key: Expression; + computed: boolean; + optional?: true; +} + +export type TsPropertySignature = TsNamedTypeElementBase & { + type: "TSPropertySignature"; + readonly?: true; + typeAnnotation?: TypeAnnotation; + initializer?: Expression; +}; + +export type TsMethodSignature = TsSignatureDeclarationBase & TsNamedTypeElementBase & { + type: "TSMethodSignature"; +} + +// *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional. +export type TsIndexSignature = TsSignatureDeclarationOrIndexSignatureBase & { + readonly?: true; + type: "TSIndexSignature"; + // Note: parameters.length must be 1. +}; + +// ================ +// TypeScript types +// ================ + +export type TsType = + | TsKeywordType + | TsThisType + | TsFunctionOrConstructorType + | TsTypeReference + | TsTypeQuery + | TsTypeLiteral + | TsArrayType + | TsTupleType + | TsUnionOrIntersectionType + | TsParenthesizedType + | TsTypeOperator + | TsIndexedAccessType + | TsMappedType + | TsLiteralType + // TODO: This probably shouldn't be included here. + | TsTypePredicate; + +export type TsTypeBase = NodeBase; + +export type TsKeywordTypeType = + | "TSAnyKeyword" + | "TSNumberKeyword" + | "TSObjectKeyword" + | "TSBooleanKeyword" + | "TSStringKeyword" + | "TSSymbolKeyword" + | "TSVoidKeyword" + | "TSUndefinedKeyword" + | "TSNullKeyword" + | "TSNeverKeyword"; +export type TsKeywordType = TsTypeBase & { + type: TsKeywordTypeType; +} + +export type TsThisType = TsTypeBase & { + type: "TSThisType" +}; + +export type TsFunctionOrConstructorType = TsFunctionType | TsConstructorType; + +export type TsFunctionType = TsTypeBase & TsSignatureDeclarationBase & { + type: "TSFunctionType"; + typeAnnotation: TypeAnnotation; // not optional +}; + +export type TsConstructorType = TsTypeBase & TsSignatureDeclarationBase & { + type: "TSConstructorType"; + typeAnnotation: TypeAnnotation; +}; + +export type TsTypeReference = TsTypeBase & { + type: "TSTypeReference"; + typeName: TsEntityName; + typeParameters?: TypeParameterInstantiation; +}; + +export type TsTypePredicate = TsTypeBase & { + type: "TSTypePredicate"; + parameterName: Identifier | TsThisType; + typeAnnotation: TypeAnnotation; +}; + +// `typeof` operator +export type TsTypeQuery = TsTypeBase & { + type: "TSTypeQuery"; + exprName: TsEntityName; +}; + +export type TsTypeLiteral = TsTypeBase & { + type: "TSTypeLiteral"; + members: $ReadOnlyArray; +}; + +export type TsArrayType = TsTypeBase & { + type: "TSArrayType"; + elementType: TsType; +}; + +export type TsTupleType = TsTypeBase & { + type: "TSTupleType"; + elementTypes: $ReadOnlyArray; +}; + +export type TsUnionOrIntersectionType = TsUnionType | TsIntersectionType; + +export type TsUnionOrIntersectionTypeBase = TsTypeBase & { + types: $ReadOnlyArray; +}; + +export type TsUnionType = TsUnionOrIntersectionTypeBase & { + type: "TSUnionType"; +}; + +export type TsIntersectionType = TsUnionOrIntersectionTypeBase & { + type: "TSIntersectionType"; +}; + +export type TsParenthesizedType = TsTypeBase & { + type: "TSParenthesizedType"; + typeAnnotation: TsType; +}; + +export type TsTypeOperator = TsTypeBase & { + type: "TSTypeOperator"; + operator: "keyof"; + typeAnnotation: TsType; +}; + +export type TsIndexedAccessType = TsTypeBase & { + type: "TSIndexedAccessType"; + objectType: TsType; + indexType: TsType; +}; + +export type TsMappedType = TsTypeBase & { + type: "TSMappedType"; + readonly?: true; + typeParameter: TypeParameter; + optional?: true; + typeAnnotation: ?TsType; +}; + +export type TsLiteralType = TsTypeBase & { + type: "TSLiteralType"; + literal: NumericLiteral | StringLiteral | BooleanLiteral; +}; + +// ================ +// TypeScript declarations +// ================ + +export type TsInterfaceDeclaration = DeclarationBase & { + type: "TSInterfaceDeclaration"; + id: Identifier; + typeParameters: ?TypeParameterDeclaration; + // TS uses "heritageClauses", but want this to resemble ClassBase. + extends?: $ReadOnlyArray; + body: TSInterfaceBody; +} + +export type TSInterfaceBody = NodeBase & { + type: "TSInterfaceBody"; + body: $ReadOnlyArray; +}; + +export type TsExpressionWithTypeArguments = TsTypeBase & { + type: "TSExpressionWithTypeArguments"; + expression: TsEntityName; + typeParameters?: TypeParameterInstantiation; +}; + +export type TsTypeAliasDeclaration = DeclarationBase & { + type: "TSTypeAliasDeclaration"; + id: Identifier; + typeParameters: ?TypeParameterDeclaration; + typeAnnotation: TsType; +}; + +export type TsEnumDeclaration = DeclarationBase & { + type: "TSEnumDeclaration"; + const?: true; + id: Identifier; + members: $ReadOnlyArray; +}; + +export type TsEnumMember = NodeBase & { + type: "TSEnumMemodulmber"; + id: Identifier | StringLiteral; + initializer?: Expression; +}; + +export type TsModuleDeclaration = DeclarationBase & { + type: "TSModuleDeclaration"; + global?: true; // In TypeScript, this is only available through `node.flags`. + id: TsModuleName; + body: TsNamespaceBody; +}; + +// `namespace A.B { }` is a namespace named `A` with another TsNamespaceDeclaration as its body. +export type TsNamespaceBody = TsModuleBlock | TsNamespaceDeclaration; + +export type TsModuleBlock = NodeBase & { + type: "TSModuleBlock"; + body: $ReadOnlyArray; +}; + +export type TsNamespaceDeclaration = TsModuleDeclaration & { + id: Identifier; + body: TsNamespaceBody; +}; + +export type TsModuleName = Identifier | StringLiteral; + +export type TsImportEqualsDeclaration = NodeBase & { + type: "TSImportEqualsDeclaration"; + isExport: boolean; + id: Identifier; + moduleReference: TsModuleReference; +}; + +export type TsModuleReference = TsEntityName | TsExternalModuleReference; + +export type TsExternalModuleReference = NodeBase & { + type: "TSExternalModuleReference"; + expression: StringLiteral; +}; + +// TypeScript's own parser uses ExportAssignment for both `export default` and `export =`. +// But for babylon, `export default` is an ExportDefaultDeclaration, +// so a TsExportAssignment is always `export =`. +export type TsExportAssignment = NodeBase & { + type: "TSExportAssignment"; + expression: Expression; +}; + +export type TsNamespaceExportDeclaration = NodeBase & { + type: "TSNamespaceExportDeclaration"; + id: Identifier; +}; + +// ================ +// TypeScript expressions +// ================ + +export type TsTypeAssertionLikeBase = NodeBase & { + expression: Expression; + typeAnnotation: TsType; +}; + +export type TsAsExpression = TsTypeAssertionLikeBase & { + type: "TSAsExpression"; +}; + +export type TsTypeAssertion = TsTypeAssertionLikeBase & { + type: "TSTypeAssertion"; + typeAnnotation: TsType; + expression: Expression; +}; + +export type TsNonNullExpression = NodeBase & { + type: "TSNonNullExpression"; + expression: Expression; +}; diff --git a/test/fixtures/flow/optional-type/6/actual.js b/test/fixtures/flow/optional-type/6/actual.js new file mode 100644 index 0000000000..d7fa2d78dc --- /dev/null +++ b/test/fixtures/flow/optional-type/6/actual.js @@ -0,0 +1 @@ +function f([]?, {}) {} diff --git a/test/fixtures/flow/optional-type/6/options.json b/test/fixtures/flow/optional-type/6/options.json new file mode 100644 index 0000000000..fb2831d618 --- /dev/null +++ b/test/fixtures/flow/optional-type/6/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A binding pattern parameter cannot be optional in an implementation signature. (1:11)" +} diff --git a/test/fixtures/typescript/arrow-function/annotated/actual.js b/test/fixtures/typescript/arrow-function/annotated/actual.js new file mode 100644 index 0000000000..ae22bd8d69 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/annotated/actual.js @@ -0,0 +1 @@ +(x: number): number => x; diff --git a/test/fixtures/typescript/arrow-function/annotated/expected.json b/test/fixtures/typescript/arrow-function/annotated/expected.json new file mode 100644 index 0000000000..4c9939e4a9 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/annotated/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 11, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 2, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js b/test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js new file mode 100644 index 0000000000..29986ed4d0 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js @@ -0,0 +1,2 @@ +async < 1; +async() == 0; diff --git a/test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json b/test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json new file mode 100644 index 0000000000..f04d318bc2 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json @@ -0,0 +1,235 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "left": { + "type": "Identifier", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "async" + }, + "name": "async" + }, + "operator": "<", + "right": { + "type": "NumericLiteral", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + }, + { + "type": "ExpressionStatement", + "start": 11, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 11, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "left": { + "type": "CallExpression", + "start": 11, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "callee": { + "type": "Identifier", + "start": 11, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "async" + }, + "name": "async" + }, + "arguments": [], + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + }, + "operator": "==", + "right": { + "type": "NumericLiteral", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/async-generic/actual.js b/test/fixtures/typescript/arrow-function/async-generic/actual.js new file mode 100644 index 0000000000..6295801168 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic/actual.js @@ -0,0 +1 @@ +async (a: T): T => a; diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json new file mode 100644 index 0000000000..86035a3967 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -0,0 +1,231 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": "T" + } + ] + }, + "params": [ + { + "type": "Identifier", + "start": 10, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 11, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": true, + "body": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "a" + }, + "name": "a" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/async/actual.js b/test/fixtures/typescript/arrow-function/async/actual.js new file mode 100644 index 0000000000..5f4fa39a1c --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async/actual.js @@ -0,0 +1 @@ +async (x?: number): any => x; diff --git a/test/fixtures/typescript/arrow-function/async/expected.json b/test/fixtures/typescript/arrow-function/async/expected.json new file mode 100644 index 0000000000..424ce9c58d --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async/expected.json @@ -0,0 +1,165 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 9, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 28 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/default-parameter-values/actual.js b/test/fixtures/typescript/arrow-function/default-parameter-values/actual.js new file mode 100644 index 0000000000..99e3245659 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/default-parameter-values/actual.js @@ -0,0 +1 @@ +(x: number = 0) => 0; diff --git a/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json new file mode 100644 index 0000000000..dd80d9124b --- /dev/null +++ b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json @@ -0,0 +1,172 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "AssignmentPattern", + "start": 1, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "Identifier", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 2, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + } + }, + "right": { + "type": "NumericLiteral", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "body": { + "type": "NumericLiteral", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/destructuring/actual.js b/test/fixtures/typescript/arrow-function/destructuring/actual.js new file mode 100644 index 0000000000..4bb8dcbff4 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/destructuring/actual.js @@ -0,0 +1 @@ +({ a = 0 }) => 0; diff --git a/test/fixtures/typescript/arrow-function/destructuring/expected.json b/test/fixtures/typescript/arrow-function/destructuring/expected.json new file mode 100644 index 0000000000..c48ece9678 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/destructuring/expected.json @@ -0,0 +1,197 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "ObjectPattern", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 3, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "method": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "a" + }, + "name": "a" + }, + "shorthand": true, + "value": { + "type": "AssignmentPattern", + "start": 3, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "left": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "a" + }, + "name": "a" + }, + "right": { + "type": "NumericLiteral", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + "extra": { + "shorthand": true + } + } + ] + } + ], + "body": { + "type": "NumericLiteral", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/actual.js b/test/fixtures/typescript/arrow-function/generic-tsx/actual.js new file mode 100644 index 0000000000..b9eea2b05a --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic-tsx/actual.js @@ -0,0 +1,2 @@ +// Same as `generic`. Verify that JSX doesn't change things. +(a: T): T => a; diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json new file mode 100644 index 0000000000..b61d4ea254 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -0,0 +1,269 @@ +{ + "type": "File", + "start": 0, + "end": 79, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 79, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 61, + "end": 79, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 62, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 70, + "end": 73, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "typeName": { + "type": "Identifier", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 65, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 66, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "name": "T", + "leadingComments": null + } + ], + "leadingComments": null + } + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Same as `generic`. Verify that JSX doesn't change things.", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 60 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Same as `generic`. Verify that JSX doesn't change things.", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 60 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/options.json b/test/fixtures/typescript/arrow-function/generic-tsx/options.json new file mode 100644 index 0000000000..6e5bbd52f8 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic-tsx/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["jsx", "typescript"] +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/generic/actual.js b/test/fixtures/typescript/arrow-function/generic/actual.js new file mode 100644 index 0000000000..110f583654 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic/actual.js @@ -0,0 +1 @@ +(a: T): T => a; diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json new file mode 100644 index 0000000000..6c5f53a23d --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -0,0 +1,231 @@ +{ + "type": "File", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 1, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "typeName": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 1, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 1, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "name": "T" + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/actual.js b/test/fixtures/typescript/arrow-function/optional-parameter/actual.js new file mode 100644 index 0000000000..4b8fea779c --- /dev/null +++ b/test/fixtures/typescript/arrow-function/optional-parameter/actual.js @@ -0,0 +1 @@ +(x?: number): any => x; \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json new file mode 100644 index 0000000000..0fa880dc0b --- /dev/null +++ b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json @@ -0,0 +1,165 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 12, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 3, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/predicate-types/actual.js b/test/fixtures/typescript/arrow-function/predicate-types/actual.js new file mode 100644 index 0000000000..5c26fdbc26 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/predicate-types/actual.js @@ -0,0 +1 @@ +(x: any): x is string => true; diff --git a/test/fixtures/typescript/arrow-function/predicate-types/expected.json b/test/fixtures/typescript/arrow-function/predicate-types/expected.json new file mode 100644 index 0000000000..db5590e5ba --- /dev/null +++ b/test/fixtures/typescript/arrow-function/predicate-types/expected.json @@ -0,0 +1,210 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 8, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 10, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "parameterName": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 21 + } + } + } + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 2, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 4, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + } + } + } + } + ], + "body": { + "type": "BooleanLiteral", + "start": 25, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "value": true + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/as/actual.js b/test/fixtures/typescript/cast/as/actual.js new file mode 100644 index 0000000000..4ecdb2197e --- /dev/null +++ b/test/fixtures/typescript/cast/as/actual.js @@ -0,0 +1,4 @@ +x as T; +x < y as boolean; // (x < y) as boolean; +x === 1 as number; // x === (1 as number); +x as any as T; diff --git a/test/fixtures/typescript/cast/as/expected.json b/test/fixtures/typescript/cast/as/expected.json new file mode 100644 index 0000000000..89db7218dd --- /dev/null +++ b/test/fixtures/typescript/cast/as/expected.json @@ -0,0 +1,526 @@ +{ + "type": "File", + "start": 0, + "end": 106, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 106, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "typeName": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + { + "type": "ExpressionStatement", + "start": 8, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 8, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 8, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "left": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + }, + "operator": "<", + "right": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 17, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " (x < y) as boolean;", + "start": 26, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 40 + } + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 49, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 49, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "left": { + "type": "Identifier", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null + }, + "operator": "===", + "right": { + "type": "TSAsExpression", + "start": 55, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "expression": { + "type": "NumericLiteral", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 60, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + }, + "leadingComments": null + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " (x < y) as boolean;", + "start": 26, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 40 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentLine", + "value": " x === (1 as number);", + "start": 68, + "end": 91, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 92, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 92, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 92, + "end": 100, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 8 + } + }, + "expression": { + "type": "Identifier", + "start": 92, + "end": 93, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 97, + "end": 100, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 8 + } + } + }, + "leadingComments": null + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 104, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "typeName": { + "type": "Identifier", + "start": 104, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "leadingComments": null + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " x === (1 as number);", + "start": 68, + "end": 91, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " (x < y) as boolean;", + "start": 26, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + { + "type": "CommentLine", + "value": " x === (1 as number);", + "start": 68, + "end": 91, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/false-positive/actual.js b/test/fixtures/typescript/cast/false-positive/actual.js new file mode 100644 index 0000000000..51f9677f39 --- /dev/null +++ b/test/fixtures/typescript/cast/false-positive/actual.js @@ -0,0 +1 @@ +f(x < 0, /a/); diff --git a/test/fixtures/typescript/cast/false-positive/expected.json b/test/fixtures/typescript/cast/false-positive/expected.json new file mode 100644 index 0000000000..d4088d6475 --- /dev/null +++ b/test/fixtures/typescript/cast/false-positive/expected.json @@ -0,0 +1,156 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "callee": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [ + { + "type": "BinaryExpression", + "start": 2, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "left": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "x" + }, + "name": "x" + }, + "operator": "<", + "right": { + "type": "NumericLiteral", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "RegExpLiteral", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "extra": { + "raw": "/a/" + }, + "pattern": "a", + "flags": "" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/need-parentheses/actual.js b/test/fixtures/typescript/cast/need-parentheses/actual.js new file mode 100644 index 0000000000..4c924ac9b0 --- /dev/null +++ b/test/fixtures/typescript/cast/need-parentheses/actual.js @@ -0,0 +1,3 @@ +( x).y; +(x as T).y; +x!.y; diff --git a/test/fixtures/typescript/cast/need-parentheses/expected.json b/test/fixtures/typescript/cast/need-parentheses/expected.json new file mode 100644 index 0000000000..92adbf3de4 --- /dev/null +++ b/test/fixtures/typescript/cast/need-parentheses/expected.json @@ -0,0 +1,346 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "object": { + "type": "TSTypeAssertion", + "start": 2, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "typeName": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "expression": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x" + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + }, + "property": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + }, + { + "type": "ExpressionStatement", + "start": 11, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "expression": { + "type": "MemberExpression", + "start": 11, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "object": { + "type": "TSAsExpression", + "start": 12, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "expression": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 11 + } + }, + "property": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + }, + { + "type": "ExpressionStatement", + "start": 23, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "expression": { + "type": "MemberExpression", + "start": 23, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "object": { + "type": "TSNonNullExpression", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "property": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 4 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js b/test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js new file mode 100644 index 0000000000..b89be6ad16 --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js @@ -0,0 +1 @@ +x!.y; diff --git a/test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json b/test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json new file mode 100644 index 0000000000..6ba14d5e7e --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json @@ -0,0 +1,114 @@ +{ + "type": "File", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + }, + "object": { + "type": "TSNonNullExpression", + "start": 0, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "property": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/null-assertion/actual.js b/test/fixtures/typescript/cast/null-assertion/actual.js new file mode 100644 index 0000000000..703cb104ad --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion/actual.js @@ -0,0 +1 @@ +x!; diff --git a/test/fixtures/typescript/cast/null-assertion/expected.json b/test/fixtures/typescript/cast/null-assertion/expected.json new file mode 100644 index 0000000000..5cf3ef60f4 --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion/expected.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "expression": { + "type": "TSNonNullExpression", + "start": 0, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/type-assertion-after-operator/actual.js b/test/fixtures/typescript/cast/type-assertion-after-operator/actual.js new file mode 100644 index 0000000000..7286cf709b --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-after-operator/actual.js @@ -0,0 +1 @@ +1 + 1; diff --git a/test/fixtures/typescript/cast/type-assertion-after-operator/expected.json b/test/fixtures/typescript/cast/type-assertion-after-operator/expected.json new file mode 100644 index 0000000000..4ab0e02125 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-after-operator/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "NumericLiteral", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "operator": "+", + "right": { + "type": "TSTypeAssertion", + "start": 5, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + "expression": { + "type": "NumericLiteral", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/type-assertion-before-operator/actual.js b/test/fixtures/typescript/cast/type-assertion-before-operator/actual.js new file mode 100644 index 0000000000..03f7f08051 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-before-operator/actual.js @@ -0,0 +1 @@ + 1 + 1; diff --git a/test/fixtures/typescript/cast/type-assertion-before-operator/expected.json b/test/fixtures/typescript/cast/type-assertion-before-operator/expected.json new file mode 100644 index 0000000000..9fa8bc989b --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-before-operator/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "TSTypeAssertion", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + "expression": { + "type": "NumericLiteral", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + "operator": "+", + "right": { + "type": "NumericLiteral", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/type-assertion/actual.js b/test/fixtures/typescript/cast/type-assertion/actual.js new file mode 100644 index 0000000000..742b94ba61 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion/actual.js @@ -0,0 +1 @@ + 1; diff --git a/test/fixtures/typescript/cast/type-assertion/expected.json b/test/fixtures/typescript/cast/type-assertion/expected.json new file mode 100644 index 0000000000..fa15bfa001 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion/expected.json @@ -0,0 +1,99 @@ +{ + "type": "File", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "TSTypeAssertion", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + "expression": { + "type": "NumericLiteral", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/abstract/actual.js b/test/fixtures/typescript/class/abstract/actual.js new file mode 100644 index 0000000000..97bbd960bc --- /dev/null +++ b/test/fixtures/typescript/class/abstract/actual.js @@ -0,0 +1,6 @@ +abstract class C {} +declare abstract class C {} +export abstract class C {} +// `export abstract class { }` is not valid. +// `export default abstract class C { }` is not valid. +// `abstract class` is not valid as an expression. diff --git a/test/fixtures/typescript/class/abstract/expected.json b/test/fixtures/typescript/class/abstract/expected.json new file mode 100644 index 0000000000..0c084659fb --- /dev/null +++ b/test/fixtures/typescript/class/abstract/expected.json @@ -0,0 +1,305 @@ +{ + "type": "File", + "start": 0, + "end": 225, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 225, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 17, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "body": [] + } + }, + { + "type": "ClassDeclaration", + "start": 20, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 43, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 45, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "body": [] + }, + "declare": true + }, + { + "type": "ExportNamedDeclaration", + "start": 48, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start": 55, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 70, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 23 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 72, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "body": [], + "leadingComments": null, + "trailingComments": null + }, + "trailingComments": null + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " `export abstract class { }` is not valid.", + "start": 75, + "end": 119, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 44 + } + } + }, + { + "type": "CommentLine", + "value": " `export default abstract class C { }` is not valid.", + "start": 120, + "end": 174, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 54 + } + } + }, + { + "type": "CommentLine", + "value": " `abstract class` is not valid as an expression.", + "start": 175, + "end": 225, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " `export abstract class { }` is not valid.", + "start": 75, + "end": 119, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 44 + } + } + }, + { + "type": "CommentLine", + "value": " `export default abstract class C { }` is not valid.", + "start": 120, + "end": 174, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 54 + } + } + }, + { + "type": "CommentLine", + "value": " `abstract class` is not valid as an expression.", + "start": 175, + "end": 225, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/constructor/actual.js b/test/fixtures/typescript/class/constructor/actual.js new file mode 100644 index 0000000000..e1621c489f --- /dev/null +++ b/test/fixtures/typescript/class/constructor/actual.js @@ -0,0 +1,5 @@ +class C { + constructor(x: number, y: number); + constructor(x: string, y: string); + constructor(x: any, y: any) {} +} diff --git a/test/fixtures/typescript/class/constructor/expected.json b/test/fixtures/typescript/class/constructor/expected.json new file mode 100644 index 0000000000..260165a33e --- /dev/null +++ b/test/fixtures/typescript/class/constructor/expected.json @@ -0,0 +1,506 @@ +{ + "type": "File", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 26, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 27, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + }, + { + "type": "Identifier", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 36 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 38, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + } + } + ] + }, + { + "type": "TSDeclareMethod", + "start": 53, + "end": 87, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 38 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 53, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 65, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 25 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 66, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 68, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 25 + } + } + } + } + }, + { + "type": "Identifier", + "start": 76, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 27 + }, + "end": { + "line": 3, + "column": 36 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 77, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 79, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 3, + "column": 36 + } + } + } + } + } + ] + }, + { + "type": "ClassMethod", + "start": 92, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 92, + "end": 103, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 104, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 16 + }, + "end": { + "line": 4, + "column": 22 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 105, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 22 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 107, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 22 + } + } + } + } + }, + { + "type": "Identifier", + "start": 112, + "end": 118, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 30 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 113, + "end": 118, + "loc": { + "start": { + "line": 4, + "column": 25 + }, + "end": { + "line": 4, + "column": 30 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 115, + "end": 118, + "loc": { + "start": { + "line": 4, + "column": 27 + }, + "end": { + "line": 4, + "column": 30 + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 32 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/declare/actual.js b/test/fixtures/typescript/class/declare/actual.js new file mode 100644 index 0000000000..4fe9d2b0cb --- /dev/null +++ b/test/fixtures/typescript/class/declare/actual.js @@ -0,0 +1,7 @@ +declare class C { + [x: string]: any; + x; + x: number; + f(); + f(): void; +} diff --git a/test/fixtures/typescript/class/declare/expected.json b/test/fixtures/typescript/class/declare/expected.json new file mode 100644 index 0000000000..ef56cc4a7c --- /dev/null +++ b/test/fixtures/typescript/class/declare/expected.json @@ -0,0 +1,389 @@ +{ + "type": "File", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 16, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "body": [ + { + "type": "TSIndexSignature", + "start": 22, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 33, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 35, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + } + } + } + }, + { + "type": "ClassProperty", + "start": 44, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 6 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 51, + "end": 61, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 52, + "end": 60, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 54, + "end": 60, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } + } + }, + "value": null + }, + { + "type": "TSDeclareMethod", + "start": 66, + "end": 70, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 8 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start": 75, + "end": 85, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 75, + "end": 76, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 78, + "end": 84, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 80, + "end": 84, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 13 + } + } + } + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-extends-implements/actual.js b/test/fixtures/typescript/class/expression-extends-implements/actual.js new file mode 100644 index 0000000000..77b5d3bb18 --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends-implements/actual.js @@ -0,0 +1,2 @@ +(class extends f() implements X.Y {}); +(class C extends f() implements X.Y {}); diff --git a/test/fixtures/typescript/class/expression-extends-implements/expected.json b/test/fixtures/typescript/class/expression-extends-implements/expected.json new file mode 100644 index 0000000000..ab219ddf0a --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends-implements/expected.json @@ -0,0 +1,546 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": null, + "superClass": { + "type": "CallExpression", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "callee": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 33, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "left": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 35, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "typeName": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "body": { + "type": "ClassBody", + "start": 40, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 45, + "end": 91, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "expression": { + "type": "ClassExpression", + "start": 46, + "end": 89, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "id": { + "type": "Identifier", + "start": 52, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 62, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "callee": { + "type": "Identifier", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 65, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeName": { + "type": "Identifier", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 80, + "end": 86, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 41 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 80, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "left": { + "type": "Identifier", + "start": 80, + "end": 81, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 36 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 82, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 37 + }, + "end": { + "line": 2, + "column": 38 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 83, + "end": 86, + "loc": { + "start": { + "line": 2, + "column": 38 + }, + "end": { + "line": 2, + "column": 41 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + } + }, + "typeName": { + "type": "Identifier", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "body": { + "type": "ClassBody", + "start": 87, + "end": 89, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 45 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-extends/actual.js b/test/fixtures/typescript/class/expression-extends/actual.js new file mode 100644 index 0000000000..b5054b0c75 --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends/actual.js @@ -0,0 +1,2 @@ +(class extends f() {}); +(class C extends f() {}); diff --git a/test/fixtures/typescript/class/expression-extends/expected.json b/test/fixtures/typescript/class/expression-extends/expected.json new file mode 100644 index 0000000000..3e7c74ca7e --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends/expected.json @@ -0,0 +1,316 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": null, + "superClass": { + "type": "CallExpression", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "callee": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "body": { + "type": "ClassBody", + "start": 22, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 27, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "expression": { + "type": "ClassExpression", + "start": 28, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 44, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "callee": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 47, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeName": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "body": { + "type": "ClassBody", + "start": 51, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 27 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-generic/actual.js b/test/fixtures/typescript/class/expression-generic/actual.js new file mode 100644 index 0000000000..94973f7a29 --- /dev/null +++ b/test/fixtures/typescript/class/expression-generic/actual.js @@ -0,0 +1,2 @@ +(class {}); +(class C {}); diff --git a/test/fixtures/typescript/class/expression-generic/expected.json b/test/fixtures/typescript/class/expression-generic/expected.json new file mode 100644 index 0000000000..6082a36e18 --- /dev/null +++ b/test/fixtures/typescript/class/expression-generic/expected.json @@ -0,0 +1,220 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "id": null, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": "T" + } + ] + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 10, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 15, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "ClassExpression", + "start": 16, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "name": "T" + } + ] + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 27, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 15 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-implements/actual.js b/test/fixtures/typescript/class/expression-implements/actual.js new file mode 100644 index 0000000000..7b5e192fdf --- /dev/null +++ b/test/fixtures/typescript/class/expression-implements/actual.js @@ -0,0 +1,2 @@ +(class implements X.Y {}); +(class C implements X.Y {}); diff --git a/test/fixtures/typescript/class/expression-implements/expected.json b/test/fixtures/typescript/class/expression-implements/expected.json new file mode 100644 index 0000000000..a3ec0e0b6e --- /dev/null +++ b/test/fixtures/typescript/class/expression-implements/expected.json @@ -0,0 +1,383 @@ +{ + "type": "File", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "superClass": null, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "left": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "body": { + "type": "ClassBody", + "start": 25, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 30, + "end": 61, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "expression": { + "type": "ClassExpression", + "start": 31, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 50, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "left": { + "type": "Identifier", + "start": 50, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 21 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 52, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 23 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 53, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeName": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "body": { + "type": "ClassBody", + "start": 57, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 30 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/extends-implements/actual.js b/test/fixtures/typescript/class/extends-implements/actual.js new file mode 100644 index 0000000000..960c58c5e6 --- /dev/null +++ b/test/fixtures/typescript/class/extends-implements/actual.js @@ -0,0 +1 @@ +class C extends f() implements X.Y {} diff --git a/test/fixtures/typescript/class/extends-implements/expected.json b/test/fixtures/typescript/class/extends-implements/expected.json new file mode 100644 index 0000000000..c8dee7a629 --- /dev/null +++ b/test/fixtures/typescript/class/extends-implements/expected.json @@ -0,0 +1,279 @@ +{ + "type": "File", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "callee": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 34, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 34, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "left": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 37 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 37, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "typeName": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "body": { + "type": "ClassBody", + "start": 41, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/extends/actual.js b/test/fixtures/typescript/class/extends/actual.js new file mode 100644 index 0000000000..c527424e5c --- /dev/null +++ b/test/fixtures/typescript/class/extends/actual.js @@ -0,0 +1 @@ +class C extends f() {} diff --git a/test/fixtures/typescript/class/extends/expected.json b/test/fixtures/typescript/class/extends/expected.json new file mode 100644 index 0000000000..d83c52adea --- /dev/null +++ b/test/fixtures/typescript/class/extends/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "callee": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "body": { + "type": "ClassBody", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/generic/actual.js b/test/fixtures/typescript/class/generic/actual.js new file mode 100644 index 0000000000..dbaeabda8c --- /dev/null +++ b/test/fixtures/typescript/class/generic/actual.js @@ -0,0 +1 @@ +class C {} diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json new file mode 100644 index 0000000000..2a84f7e0c1 --- /dev/null +++ b/test/fixtures/typescript/class/generic/expected.json @@ -0,0 +1,211 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 8, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 8, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "name": "T", + "constraint": { + "type": "TSObjectKeyword", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 27, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 29, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 30, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } + } + } + } + ] + } + } + ] + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/get-generic/actual.js b/test/fixtures/typescript/class/get-generic/actual.js new file mode 100644 index 0000000000..85739b3c2f --- /dev/null +++ b/test/fixtures/typescript/class/get-generic/actual.js @@ -0,0 +1,3 @@ +declare class C { + get(): void; +} diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json new file mode 100644 index 0000000000..d8733a2b3e --- /dev/null +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -0,0 +1,188 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 16, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 22, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "get" + }, + "name": "get" + }, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 26, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": "T" + } + ] + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/implements/actual.js b/test/fixtures/typescript/class/implements/actual.js new file mode 100644 index 0000000000..725bd441be --- /dev/null +++ b/test/fixtures/typescript/class/implements/actual.js @@ -0,0 +1 @@ +class C implements X.Y {} diff --git a/test/fixtures/typescript/class/implements/expected.json b/test/fixtures/typescript/class/implements/expected.json new file mode 100644 index 0000000000..4af4f8f4d0 --- /dev/null +++ b/test/fixtures/typescript/class/implements/expected.json @@ -0,0 +1,198 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "left": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "typeName": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "body": { + "type": "ClassBody", + "start": 26, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/index-signature/actual.js b/test/fixtures/typescript/class/index-signature/actual.js new file mode 100644 index 0000000000..a05c783338 --- /dev/null +++ b/test/fixtures/typescript/class/index-signature/actual.js @@ -0,0 +1,4 @@ +class C { + [x: string]: any; + readonly [x: string]: any; +} diff --git a/test/fixtures/typescript/class/index-signature/expected.json b/test/fixtures/typescript/class/index-signature/expected.json new file mode 100644 index 0000000000..3c2298fc51 --- /dev/null +++ b/test/fixtures/typescript/class/index-signature/expected.json @@ -0,0 +1,273 @@ +{ + "type": "File", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSIndexSignature", + "start": 14, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 25, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + } + } + } + }, + { + "type": "TSIndexSignature", + "start": 36, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "readonly": true, + "parameters": [ + { + "type": "Identifier", + "start": 46, + "end": 47, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 56, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 58, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 29 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/members-with-modifier-names/actual.js b/test/fixtures/typescript/class/members-with-modifier-names/actual.js new file mode 100644 index 0000000000..cbf422a383 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-modifier-names/actual.js @@ -0,0 +1,6 @@ +class C { + public(): void; + public static(): void; + readonly = 0; + async(): void; +} diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json new file mode 100644 index 0000000000..e61ec54c61 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -0,0 +1,382 @@ +{ + "type": "File", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "public" + }, + "name": "public" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + } + }, + { + "type": "TSDeclareMethod", + "start": 34, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "accessibility": "public", + "kind": "method", + "computed": false, + "key": { + "type": "Identifier", + "start": 41, + "end": 47, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 17 + }, + "identifierName": "static" + }, + "name": "static" + }, + "static": false, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 51, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 25 + } + } + } + } + }, + { + "type": "ClassProperty", + "start": 61, + "end": 74, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 61, + "end": 69, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 12 + }, + "identifierName": "readonly" + }, + "name": "readonly" + }, + "value": { + "type": "NumericLiteral", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "TSDeclareMethod", + "start": 79, + "end": 96, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 79, + "end": 84, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 9 + }, + "identifierName": "async" + }, + "name": "async" + }, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 85, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 12 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 85, + "end": 86, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 11 + } + }, + "name": "T" + } + ] + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 89, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 91, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 20 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/members-with-reserved-names/actual.js b/test/fixtures/typescript/class/members-with-reserved-names/actual.js new file mode 100644 index 0000000000..579d8c8998 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-reserved-names/actual.js @@ -0,0 +1,3 @@ +class C { + public delete(): void; +} diff --git a/test/fixtures/typescript/class/members-with-reserved-names/expected.json b/test/fixtures/typescript/class/members-with-reserved-names/expected.json new file mode 100644 index 0000000000..b23d374bce --- /dev/null +++ b/test/fixtures/typescript/class/members-with-reserved-names/expected.json @@ -0,0 +1,155 @@ +{ + "type": "File", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "accessibility": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "delete" + }, + "name": "delete" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-computed/actual.js b/test/fixtures/typescript/class/method-computed/actual.js new file mode 100644 index 0000000000..ce02ff3566 --- /dev/null +++ b/test/fixtures/typescript/class/method-computed/actual.js @@ -0,0 +1,4 @@ +class C { + [Symbol.iterator](): void; + [Symbol.iterator]?(): void; +} diff --git a/test/fixtures/typescript/class/method-computed/expected.json b/test/fixtures/typescript/class/method-computed/expected.json new file mode 100644 index 0000000000..4f2b905560 --- /dev/null +++ b/test/fixtures/typescript/class/method-computed/expected.json @@ -0,0 +1,291 @@ +{ + "type": "File", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 22, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + } + }, + { + "type": "TSDeclareMethod", + "start": 45, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 46, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 46, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 53, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 67, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-generic/actual.js b/test/fixtures/typescript/class/method-generic/actual.js new file mode 100644 index 0000000000..ceefb4d0ce --- /dev/null +++ b/test/fixtures/typescript/class/method-generic/actual.js @@ -0,0 +1,4 @@ +class C { + f(a: T, b?: T, ...c: T[]): T {} + [Symbol.iterator](): T {} +} diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json new file mode 100644 index 0000000000..99c4ee7ffe --- /dev/null +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -0,0 +1,615 @@ +{ + "type": "File", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 16, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "name": "T" + } + ] + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + { + "type": "Identifier", + "start": 25, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "b" + }, + "name": "b", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + { + "type": "RestElement", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "argument": { + "type": "Identifier", + "start": 35, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + }, + "identifierName": "c" + }, + "name": "c" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 36, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 38, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "elementType": { + "type": "TSTypeReference", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeName": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 42, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "typeName": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "body": { + "type": "BlockStatement", + "start": 46, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 53, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 54, + "end": 69, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 54, + "end": 60, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 61, + "end": 69, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 71, + "end": 73, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 71, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "name": "T" + } + ] + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 75, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "typeName": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "body": { + "type": "BlockStatement", + "start": 79, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-no-body/actual.js b/test/fixtures/typescript/class/method-no-body/actual.js new file mode 100644 index 0000000000..8641a976d7 --- /dev/null +++ b/test/fixtures/typescript/class/method-no-body/actual.js @@ -0,0 +1,4 @@ +class C { + f(); + f(): void; +} diff --git a/test/fixtures/typescript/class/method-no-body/expected.json b/test/fixtures/typescript/class/method-no-body/expected.json new file mode 100644 index 0000000000..71a82e87ca --- /dev/null +++ b/test/fixtures/typescript/class/method-no-body/expected.json @@ -0,0 +1,194 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start": 23, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 28, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-optional/actual.js b/test/fixtures/typescript/class/method-optional/actual.js new file mode 100644 index 0000000000..6c8694960d --- /dev/null +++ b/test/fixtures/typescript/class/method-optional/actual.js @@ -0,0 +1,3 @@ +class C { + m?(): void {} +} diff --git a/test/fixtures/typescript/class/method-optional/expected.json b/test/fixtures/typescript/class/method-optional/expected.json new file mode 100644 index 0000000000..c96beed6c2 --- /dev/null +++ b/test/fixtures/typescript/class/method-optional/expected.json @@ -0,0 +1,172 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "optional": true, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 25, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-readonly/actual.js b/test/fixtures/typescript/class/method-readonly/actual.js new file mode 100644 index 0000000000..393ccdb37b --- /dev/null +++ b/test/fixtures/typescript/class/method-readonly/actual.js @@ -0,0 +1,3 @@ +class C { + readonly m() {} +} diff --git a/test/fixtures/typescript/class/method-readonly/options.json b/test/fixtures/typescript/class/method-readonly/options.json new file mode 100644 index 0000000000..897764181d --- /dev/null +++ b/test/fixtures/typescript/class/method-readonly/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected ; (2:14)" +} diff --git a/test/fixtures/typescript/class/method-return-type/actual.js b/test/fixtures/typescript/class/method-return-type/actual.js new file mode 100644 index 0000000000..6c537e03f6 --- /dev/null +++ b/test/fixtures/typescript/class/method-return-type/actual.js @@ -0,0 +1,3 @@ +class C { + f(): void {} +} diff --git a/test/fixtures/typescript/class/method-return-type/expected.json b/test/fixtures/typescript/class/method-return-type/expected.json new file mode 100644 index 0000000000..483cb98a04 --- /dev/null +++ b/test/fixtures/typescript/class/method-return-type/expected.json @@ -0,0 +1,171 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 13 + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-with-newline-with-body/actual.js b/test/fixtures/typescript/class/method-with-newline-with-body/actual.js new file mode 100644 index 0000000000..65bd228eed --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-with-body/actual.js @@ -0,0 +1,6 @@ +class C +{ + m() + { + } +} diff --git a/test/fixtures/typescript/class/method-with-newline-with-body/expected.json b/test/fixtures/typescript/class/method-with-newline-with-body/expected.json new file mode 100644 index 0000000000..585a5b2920 --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-with-body/expected.json @@ -0,0 +1,141 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 22, + "end": 29, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-with-newline-without-body/actual.js b/test/fixtures/typescript/class/method-with-newline-without-body/actual.js new file mode 100644 index 0000000000..0a2f3719eb --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-without-body/actual.js @@ -0,0 +1,5 @@ +class C +{ + m() + n() +} diff --git a/test/fixtures/typescript/class/method-with-newline-without-body/expected.json b/test/fixtures/typescript/class/method-with-newline-without-body/expected.json new file mode 100644 index 0000000000..5252a1bbd5 --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-without-body/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 7 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "n" + }, + "name": "n" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/modifiers-accessors/actual.js b/test/fixtures/typescript/class/modifiers-accessors/actual.js new file mode 100644 index 0000000000..07b7d79c0c --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-accessors/actual.js @@ -0,0 +1,11 @@ +// Copy of modifiers-methods with 'get' +abstract class C { + abstract get a(); + static get s() { return 0; } + public abstract get pua(); + public static get pus() { return 0; } + + public get pu() { return 0; } + protected get po() { return 0; } + private get pi() { return 0; } +} diff --git a/test/fixtures/typescript/class/modifiers-accessors/expected.json b/test/fixtures/typescript/class/modifiers-accessors/expected.json new file mode 100644 index 0000000000..953516b92d --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-accessors/expected.json @@ -0,0 +1,673 @@ +{ + "type": "File", + "start": 0, + "end": 295, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 295, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 40, + "end": 295, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 57, + "end": 295, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 63, + "end": 80, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 76, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "a" + }, + "name": "a" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 85, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 32 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 96, + "end": 97, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + }, + "identifierName": "s" + }, + "name": "s" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 100, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 32 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 102, + "end": 111, + "loc": { + "start": { + "line": 4, + "column": 21 + }, + "end": { + "line": 4, + "column": 30 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 109, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 28 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "TSDeclareMethod", + "start": 118, + "end": 144, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 30 + } + }, + "accessibility": "public", + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 138, + "end": 141, + "loc": { + "start": { + "line": 5, + "column": 24 + }, + "end": { + "line": 5, + "column": 27 + }, + "identifierName": "pua" + }, + "name": "pua" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 149, + "end": 186, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 41 + } + }, + "accessibility": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 167, + "end": 170, + "loc": { + "start": { + "line": 6, + "column": 22 + }, + "end": { + "line": 6, + "column": 25 + }, + "identifierName": "pus" + }, + "name": "pus" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 173, + "end": 186, + "loc": { + "start": { + "line": 6, + "column": 28 + }, + "end": { + "line": 6, + "column": 41 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 175, + "end": 184, + "loc": { + "start": { + "line": 6, + "column": 30 + }, + "end": { + "line": 6, + "column": 39 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 182, + "end": 183, + "loc": { + "start": { + "line": 6, + "column": 37 + }, + "end": { + "line": 6, + "column": 38 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 192, + "end": 221, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 33 + } + }, + "accessibility": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 203, + "end": 205, + "loc": { + "start": { + "line": 8, + "column": 15 + }, + "end": { + "line": 8, + "column": 17 + }, + "identifierName": "pu" + }, + "name": "pu" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 208, + "end": 221, + "loc": { + "start": { + "line": 8, + "column": 20 + }, + "end": { + "line": 8, + "column": 33 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 210, + "end": 219, + "loc": { + "start": { + "line": 8, + "column": 22 + }, + "end": { + "line": 8, + "column": 31 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 217, + "end": 218, + "loc": { + "start": { + "line": 8, + "column": 29 + }, + "end": { + "line": 8, + "column": 30 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 226, + "end": 258, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 36 + } + }, + "accessibility": "protected", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 240, + "end": 242, + "loc": { + "start": { + "line": 9, + "column": 18 + }, + "end": { + "line": 9, + "column": 20 + }, + "identifierName": "po" + }, + "name": "po" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 245, + "end": 258, + "loc": { + "start": { + "line": 9, + "column": 23 + }, + "end": { + "line": 9, + "column": 36 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 247, + "end": 256, + "loc": { + "start": { + "line": 9, + "column": 25 + }, + "end": { + "line": 9, + "column": 34 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 254, + "end": 255, + "loc": { + "start": { + "line": 9, + "column": 32 + }, + "end": { + "line": 9, + "column": 33 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 263, + "end": 293, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 34 + } + }, + "accessibility": "private", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 275, + "end": 277, + "loc": { + "start": { + "line": 10, + "column": 16 + }, + "end": { + "line": 10, + "column": 18 + }, + "identifierName": "pi" + }, + "name": "pi" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 280, + "end": 293, + "loc": { + "start": { + "line": 10, + "column": 21 + }, + "end": { + "line": 10, + "column": 34 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 282, + "end": 291, + "loc": { + "start": { + "line": 10, + "column": 23 + }, + "end": { + "line": 10, + "column": 32 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 289, + "end": 290, + "loc": { + "start": { + "line": 10, + "column": 30 + }, + "end": { + "line": 10, + "column": 31 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'get'", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'get'", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/modifiers-methods-async/actual.js b/test/fixtures/typescript/class/modifiers-methods-async/actual.js new file mode 100644 index 0000000000..9632a64212 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-methods-async/actual.js @@ -0,0 +1,11 @@ +// Copy of modifiers-methods with 'async' +abstract class C { + abstract async a(); + static async s() {} + public abstract async pua(); + public static async pus() {} + + public async pu() {} + protected async po() {} + private async pi() {} +} diff --git a/test/fixtures/typescript/class/modifiers-methods-async/expected.json b/test/fixtures/typescript/class/modifiers-methods-async/expected.json new file mode 100644 index 0000000000..d35e4a6d64 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-methods-async/expected.json @@ -0,0 +1,493 @@ +{ + "type": "File", + "start": 0, + "end": 256, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 256, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 42, + "end": 256, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 59, + "end": 256, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 65, + "end": 84, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 80, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "a" + }, + "name": "a" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [] + }, + { + "type": "ClassMethod", + "start": 89, + "end": 108, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 102, + "end": 103, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 18 + }, + "identifierName": "s" + }, + "name": "s" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 106, + "end": 108, + "loc": { + "start": { + "line": 4, + "column": 21 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "TSDeclareMethod", + "start": 113, + "end": 141, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 32 + } + }, + "accessibility": "public", + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 135, + "end": 138, + "loc": { + "start": { + "line": 5, + "column": 26 + }, + "end": { + "line": 5, + "column": 29 + }, + "identifierName": "pua" + }, + "name": "pua" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [] + }, + { + "type": "ClassMethod", + "start": 146, + "end": 174, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 32 + } + }, + "accessibility": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 166, + "end": 169, + "loc": { + "start": { + "line": 6, + "column": 24 + }, + "end": { + "line": 6, + "column": 27 + }, + "identifierName": "pus" + }, + "name": "pus" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 172, + "end": 174, + "loc": { + "start": { + "line": 6, + "column": 30 + }, + "end": { + "line": 6, + "column": 32 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 180, + "end": 200, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 24 + } + }, + "accessibility": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 193, + "end": 195, + "loc": { + "start": { + "line": 8, + "column": 17 + }, + "end": { + "line": 8, + "column": 19 + }, + "identifierName": "pu" + }, + "name": "pu" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 198, + "end": 200, + "loc": { + "start": { + "line": 8, + "column": 22 + }, + "end": { + "line": 8, + "column": 24 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 205, + "end": 228, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 27 + } + }, + "accessibility": "protected", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 221, + "end": 223, + "loc": { + "start": { + "line": 9, + "column": 20 + }, + "end": { + "line": 9, + "column": 22 + }, + "identifierName": "po" + }, + "name": "po" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 226, + "end": 228, + "loc": { + "start": { + "line": 9, + "column": 25 + }, + "end": { + "line": 9, + "column": 27 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 233, + "end": 254, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "accessibility": "private", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 247, + "end": 249, + "loc": { + "start": { + "line": 10, + "column": 18 + }, + "end": { + "line": 10, + "column": 20 + }, + "identifierName": "pi" + }, + "name": "pi" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 252, + "end": 254, + "loc": { + "start": { + "line": 10, + "column": 23 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "body": [], + "directives": [] + } + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'async'", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'async'", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/modifiers-properties/actual.js b/test/fixtures/typescript/class/modifiers-properties/actual.js new file mode 100644 index 0000000000..ab1a705653 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-properties/actual.js @@ -0,0 +1,21 @@ +abstract class C { + readonly r; + readonly r2?: number; + abstract a; + static s; + + public pu; + protected po; + private pi; + + readonly abstract ra; + abstract readonly ar; + static readonly sr; + + public readonly pur; + public abstract pua; + public static pus; + public readonly abstract pura; + public abstract readonly puar; + public static readonly pusr; +} diff --git a/test/fixtures/typescript/class/modifiers-properties/expected.json b/test/fixtures/typescript/class/modifiers-properties/expected.json new file mode 100644 index 0000000000..ebdc3a5bcd --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-properties/expected.json @@ -0,0 +1,700 @@ +{ + "type": "File", + "start": 0, + "end": 396, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 21, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 396, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 21, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 396, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 21, + "column": 1 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 17, + "end": 396, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 21, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 23, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "r" + }, + "name": "r" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 39, + "end": 60, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 48, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "r2" + }, + "name": "r2" + }, + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 51, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 53, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 24 + } + } + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 65, + "end": 76, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 15 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 74, + "end": 75, + "loc": { + "start": { + "line": 4, + "column": 13 + }, + "end": { + "line": 4, + "column": 14 + }, + "identifierName": "a" + }, + "name": "a" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 81, + "end": 90, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 88, + "end": 89, + "loc": { + "start": { + "line": 5, + "column": 11 + }, + "end": { + "line": 5, + "column": 12 + }, + "identifierName": "s" + }, + "name": "s" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 96, + "end": 106, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 14 + } + }, + "accessibility": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 103, + "end": 105, + "loc": { + "start": { + "line": 7, + "column": 11 + }, + "end": { + "line": 7, + "column": 13 + }, + "identifierName": "pu" + }, + "name": "pu" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 111, + "end": 124, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 17 + } + }, + "accessibility": "protected", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 121, + "end": 123, + "loc": { + "start": { + "line": 8, + "column": 14 + }, + "end": { + "line": 8, + "column": 16 + }, + "identifierName": "po" + }, + "name": "po" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 129, + "end": 140, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 15 + } + }, + "accessibility": "private", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 137, + "end": 139, + "loc": { + "start": { + "line": 9, + "column": 12 + }, + "end": { + "line": 9, + "column": 14 + }, + "identifierName": "pi" + }, + "name": "pi" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 146, + "end": 167, + "loc": { + "start": { + "line": 11, + "column": 4 + }, + "end": { + "line": 11, + "column": 25 + } + }, + "abstract": true, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 164, + "end": 166, + "loc": { + "start": { + "line": 11, + "column": 22 + }, + "end": { + "line": 11, + "column": 24 + }, + "identifierName": "ra" + }, + "name": "ra" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 172, + "end": 193, + "loc": { + "start": { + "line": 12, + "column": 4 + }, + "end": { + "line": 12, + "column": 25 + } + }, + "abstract": true, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 190, + "end": 192, + "loc": { + "start": { + "line": 12, + "column": 22 + }, + "end": { + "line": 12, + "column": 24 + }, + "identifierName": "ar" + }, + "name": "ar" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 198, + "end": 217, + "loc": { + "start": { + "line": 13, + "column": 4 + }, + "end": { + "line": 13, + "column": 23 + } + }, + "readonly": true, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 214, + "end": 216, + "loc": { + "start": { + "line": 13, + "column": 20 + }, + "end": { + "line": 13, + "column": 22 + }, + "identifierName": "sr" + }, + "name": "sr" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 223, + "end": 243, + "loc": { + "start": { + "line": 15, + "column": 4 + }, + "end": { + "line": 15, + "column": 24 + } + }, + "accessibility": "public", + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 239, + "end": 242, + "loc": { + "start": { + "line": 15, + "column": 20 + }, + "end": { + "line": 15, + "column": 23 + }, + "identifierName": "pur" + }, + "name": "pur" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 248, + "end": 268, + "loc": { + "start": { + "line": 16, + "column": 4 + }, + "end": { + "line": 16, + "column": 24 + } + }, + "accessibility": "public", + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 264, + "end": 267, + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 23 + }, + "identifierName": "pua" + }, + "name": "pua" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 273, + "end": 291, + "loc": { + "start": { + "line": 17, + "column": 4 + }, + "end": { + "line": 17, + "column": 22 + } + }, + "accessibility": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 287, + "end": 290, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 21 + }, + "identifierName": "pus" + }, + "name": "pus" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 296, + "end": 326, + "loc": { + "start": { + "line": 18, + "column": 4 + }, + "end": { + "line": 18, + "column": 34 + } + }, + "accessibility": "public", + "abstract": true, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 321, + "end": 325, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 33 + }, + "identifierName": "pura" + }, + "name": "pura" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 331, + "end": 361, + "loc": { + "start": { + "line": 19, + "column": 4 + }, + "end": { + "line": 19, + "column": 34 + } + }, + "accessibility": "public", + "abstract": true, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 356, + "end": 360, + "loc": { + "start": { + "line": 19, + "column": 29 + }, + "end": { + "line": 19, + "column": 33 + }, + "identifierName": "puar" + }, + "name": "puar" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 366, + "end": 394, + "loc": { + "start": { + "line": 20, + "column": 4 + }, + "end": { + "line": 20, + "column": 32 + } + }, + "accessibility": "public", + "readonly": true, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 389, + "end": 393, + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 31 + }, + "identifierName": "pusr" + }, + "name": "pusr" + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js b/test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js new file mode 100644 index 0000000000..412241a327 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js @@ -0,0 +1,3 @@ +class C { + constructor(public []) {} +} diff --git a/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json b/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json new file mode 100644 index 0000000000..67137f2107 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A parameter property may not be declared using a binding pattern. (2:23)" +} diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js b/test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js new file mode 100644 index 0000000000..c5e8d315db --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js @@ -0,0 +1,3 @@ +class C { + constructor(@foo readonly x: number) {} +} diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json b/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json new file mode 100644 index 0000000000..e0ff119eb4 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json @@ -0,0 +1,239 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 43 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "TSParameterProperty", + "start": 40, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "decorators": [ + { + "type": "Decorator", + "start": 26, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "expression": { + "type": "Identifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "readonly": true, + "parameter": { + "type": "Identifier", + "start": 40, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 39 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 41, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 43, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 39 + } + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 51, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 43 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/options.json b/test/fixtures/typescript/class/parameter-properties-with-decorators/options.json new file mode 100644 index 0000000000..bb696a1918 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["typescript", "decorators"] +} diff --git a/test/fixtures/typescript/class/parameter-properties/actual.js b/test/fixtures/typescript/class/parameter-properties/actual.js new file mode 100644 index 0000000000..c9d6ec5927 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties/actual.js @@ -0,0 +1,11 @@ +class C { + constructor( + readonly r, + public pu: number, + protected po?, + private pi?: number, + public readonly pur, + // Also works on AssignmentPattern + readonly x = 0, + public y?: number = 0) {} +} diff --git a/test/fixtures/typescript/class/parameter-properties/expected.json b/test/fixtures/typescript/class/parameter-properties/expected.json new file mode 100644 index 0000000000..606cfe31be --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties/expected.json @@ -0,0 +1,575 @@ +{ + "type": "File", + "start": 0, + "end": 257, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 257, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 257, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 257, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 255, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 10, + "column": 33 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "TSParameterProperty", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "readonly": true, + "parameter": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "r" + }, + "name": "r" + } + }, + { + "type": "TSParameterProperty", + "start": 62, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "accessibility": "public", + "parameter": { + "type": "Identifier", + "start": 62, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 25 + }, + "identifierName": "pu" + }, + "name": "pu", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 64, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 25 + } + } + } + } + } + }, + { + "type": "TSParameterProperty", + "start": 92, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "accessibility": "protected", + "parameter": { + "type": "Identifier", + "start": 92, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 21 + }, + "identifierName": "po" + }, + "name": "po", + "optional": true + } + }, + { + "type": "TSParameterProperty", + "start": 113, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 27 + } + }, + "accessibility": "private", + "parameter": { + "type": "Identifier", + "start": 113, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 27 + }, + "identifierName": "pi" + }, + "name": "pi", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 116, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 19 + }, + "end": { + "line": 6, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 118, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 21 + }, + "end": { + "line": 6, + "column": 27 + } + } + } + } + } + }, + { + "type": "TSParameterProperty", + "start": 150, + "end": 153, + "loc": { + "start": { + "line": 7, + "column": 24 + }, + "end": { + "line": 7, + "column": 27 + } + }, + "accessibility": "public", + "readonly": true, + "parameter": { + "type": "Identifier", + "start": 150, + "end": 153, + "loc": { + "start": { + "line": 7, + "column": 24 + }, + "end": { + "line": 7, + "column": 27 + }, + "identifierName": "pur" + }, + "name": "pur" + } + }, + { + "type": "TSParameterProperty", + "start": 215, + "end": 220, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 22 + } + }, + "readonly": true, + "parameter": { + "type": "AssignmentPattern", + "start": 215, + "end": 220, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 22 + } + }, + "left": { + "type": "Identifier", + "start": 215, + "end": 216, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null + }, + "right": { + "type": "NumericLiteral", + "start": 219, + "end": 220, + "loc": { + "start": { + "line": 9, + "column": 21 + }, + "end": { + "line": 9, + "column": 22 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + }, + "leadingComments": null + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Also works on AssignmentPattern", + "start": 163, + "end": 197, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 42 + } + } + } + ] + }, + { + "type": "TSParameterProperty", + "start": 237, + "end": 251, + "loc": { + "start": { + "line": 10, + "column": 15 + }, + "end": { + "line": 10, + "column": 29 + } + }, + "accessibility": "public", + "parameter": { + "type": "AssignmentPattern", + "start": 237, + "end": 251, + "loc": { + "start": { + "line": 10, + "column": 15 + }, + "end": { + "line": 10, + "column": 29 + } + }, + "left": { + "type": "Identifier", + "start": 237, + "end": 247, + "loc": { + "start": { + "line": 10, + "column": 15 + }, + "end": { + "line": 10, + "column": 25 + }, + "identifierName": "y" + }, + "name": "y", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 239, + "end": 247, + "loc": { + "start": { + "line": 10, + "column": 17 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 241, + "end": 247, + "loc": { + "start": { + "line": 10, + "column": 19 + }, + "end": { + "line": 10, + "column": 25 + } + } + } + } + }, + "right": { + "type": "NumericLiteral", + "start": 250, + "end": 251, + "loc": { + "start": { + "line": 10, + "column": 28 + }, + "end": { + "line": 10, + "column": 29 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 253, + "end": 255, + "loc": { + "start": { + "line": 10, + "column": 31 + }, + "end": { + "line": 10, + "column": 33 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Also works on AssignmentPattern", + "start": 163, + "end": 197, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 42 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/properties/actual.js b/test/fixtures/typescript/class/properties/actual.js new file mode 100644 index 0000000000..8c0ad68965 --- /dev/null +++ b/test/fixtures/typescript/class/properties/actual.js @@ -0,0 +1,6 @@ +class C { + x; + x?; + x: number; + x: number = 1; +} diff --git a/test/fixtures/typescript/class/properties/expected.json b/test/fixtures/typescript/class/properties/expected.json new file mode 100644 index 0000000000..8cc2487abd --- /dev/null +++ b/test/fixtures/typescript/class/properties/expected.json @@ -0,0 +1,304 @@ +{ + "type": "File", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 14, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "optional": true, + "value": null + }, + { + "type": "ClassProperty", + "start": 29, + "end": 39, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 30, + "end": 38, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 44, + "end": 58, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 18 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 45, + "end": 53, + "loc": { + "start": { + "line": 5, + "column": 5 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + } + }, + "value": { + "type": "NumericLiteral", + "start": 56, + "end": 57, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 17 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/property-computed/actual.js b/test/fixtures/typescript/class/property-computed/actual.js new file mode 100644 index 0000000000..10ee099fa9 --- /dev/null +++ b/test/fixtures/typescript/class/property-computed/actual.js @@ -0,0 +1,4 @@ +class C { + [Symbol.iterator]: number; + [Symbol.iterator]?: number; +} diff --git a/test/fixtures/typescript/class/property-computed/expected.json b/test/fixtures/typescript/class/property-computed/expected.json new file mode 100644 index 0000000000..7978804e58 --- /dev/null +++ b/test/fixtures/typescript/class/property-computed/expected.json @@ -0,0 +1,281 @@ +{ + "type": "File", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 14, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 22, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 31, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 45, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 46, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 46, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 53, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 63, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/static/actual.js b/test/fixtures/typescript/class/static/actual.js new file mode 100644 index 0000000000..aca8d85ba7 --- /dev/null +++ b/test/fixtures/typescript/class/static/actual.js @@ -0,0 +1,6 @@ +class C { + static f(); + public static f(); + protected static f(); + private static f(); +} diff --git a/test/fixtures/typescript/class/static/expected.json b/test/fixtures/typescript/class/static/expected.json new file mode 100644 index 0000000000..f8a95e4d32 --- /dev/null +++ b/test/fixtures/typescript/class/static/expected.json @@ -0,0 +1,247 @@ +{ + "type": "File", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start": 30, + "end": 48, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "accessibility": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 19 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start": 53, + "end": 74, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "accessibility": "protected", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 70, + "end": 71, + "loc": { + "start": { + "line": 4, + "column": 21 + }, + "end": { + "line": 4, + "column": 22 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start": 79, + "end": 98, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 23 + } + }, + "accessibility": "private", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 94, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 19 + }, + "end": { + "line": 5, + "column": 20 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/const/no-initializer/actual.js b/test/fixtures/typescript/const/no-initializer/actual.js new file mode 100644 index 0000000000..8caae258b9 --- /dev/null +++ b/test/fixtures/typescript/const/no-initializer/actual.js @@ -0,0 +1 @@ +const x: number; diff --git a/test/fixtures/typescript/const/no-initializer/expected.json b/test/fixtures/typescript/const/no-initializer/expected.json new file mode 100644 index 0000000000..58ccff5e4a --- /dev/null +++ b/test/fixtures/typescript/const/no-initializer/expected.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 7, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + } + }, + "init": null + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/const/actual.js b/test/fixtures/typescript/declare/const/actual.js new file mode 100644 index 0000000000..f394dbd226 --- /dev/null +++ b/test/fixtures/typescript/declare/const/actual.js @@ -0,0 +1 @@ +declare const x: number, y: string; diff --git a/test/fixtures/typescript/declare/const/expected.json b/test/fixtures/typescript/declare/const/expected.json new file mode 100644 index 0000000000..2d0eb59bff --- /dev/null +++ b/test/fixtures/typescript/declare/const/expected.json @@ -0,0 +1,179 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 14, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 15, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + } + }, + "init": null + }, + { + "type": "VariableDeclarator", + "start": 25, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 25, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 34 + } + } + } + } + }, + "init": null + } + ], + "kind": "const", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/destructure/actual.js b/test/fixtures/typescript/declare/destructure/actual.js new file mode 100644 index 0000000000..7f46b962e9 --- /dev/null +++ b/test/fixtures/typescript/declare/destructure/actual.js @@ -0,0 +1 @@ +declare const { x, y }: { x: number, y: number }; diff --git a/test/fixtures/typescript/declare/destructure/expected.json b/test/fixtures/typescript/declare/destructure/expected.json new file mode 100644 index 0000000000..8d28d26e0b --- /dev/null +++ b/test/fixtures/typescript/declare/destructure/expected.json @@ -0,0 +1,354 @@ +{ + "type": "File", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "id": { + "type": "ObjectPattern", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "method": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + }, + "shorthand": true, + "value": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + }, + "extra": { + "shorthand": true + } + }, + { + "type": "ObjectProperty", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "method": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "y" + }, + "name": "y" + }, + "shorthand": true, + "value": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "y" + }, + "name": "y" + }, + "extra": { + "shorthand": true + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 22, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 24, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 26, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 27, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 35 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 38, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 46 + } + } + } + } + } + ] + } + } + }, + "init": null + } + ], + "kind": "const", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/interface/actual.js b/test/fixtures/typescript/declare/interface/actual.js new file mode 100644 index 0000000000..85d7623f14 --- /dev/null +++ b/test/fixtures/typescript/declare/interface/actual.js @@ -0,0 +1 @@ +declare interface I {} diff --git a/test/fixtures/typescript/declare/interface/expected.json b/test/fixtures/typescript/declare/interface/expected.json new file mode 100644 index 0000000000..2ddce3fec9 --- /dev/null +++ b/test/fixtures/typescript/declare/interface/expected.json @@ -0,0 +1,83 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 20, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "body": [] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/let/actual.js b/test/fixtures/typescript/declare/let/actual.js new file mode 100644 index 0000000000..3dc63502e9 --- /dev/null +++ b/test/fixtures/typescript/declare/let/actual.js @@ -0,0 +1 @@ +declare let x; diff --git a/test/fixtures/typescript/declare/let/expected.json b/test/fixtures/typescript/declare/let/expected.json new file mode 100644 index 0000000000..9644d592b1 --- /dev/null +++ b/test/fixtures/typescript/declare/let/expected.json @@ -0,0 +1,86 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": null + } + ], + "kind": "let", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/var/actual.js b/test/fixtures/typescript/declare/var/actual.js new file mode 100644 index 0000000000..c1a3d52a76 --- /dev/null +++ b/test/fixtures/typescript/declare/var/actual.js @@ -0,0 +1,2 @@ +declare var x; +declare var x: any; diff --git a/test/fixtures/typescript/declare/var/expected.json b/test/fixtures/typescript/declare/var/expected.json new file mode 100644 index 0000000000..a790e494e6 --- /dev/null +++ b/test/fixtures/typescript/declare/var/expected.json @@ -0,0 +1,168 @@ +{ + "type": "File", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": null + } + ], + "kind": "var", + "declare": true + }, + { + "type": "VariableDeclaration", + "start": 15, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 28, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 30, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + } + }, + "init": null + } + ], + "kind": "var", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/const/actual.js b/test/fixtures/typescript/enum/const/actual.js new file mode 100644 index 0000000000..6eb417807c --- /dev/null +++ b/test/fixtures/typescript/enum/const/actual.js @@ -0,0 +1 @@ +const enum E {} diff --git a/test/fixtures/typescript/enum/const/expected.json b/test/fixtures/typescript/enum/const/expected.json new file mode 100644 index 0000000000..4e0e48a25f --- /dev/null +++ b/test/fixtures/typescript/enum/const/expected.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "const": true, + "id": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/declare-const/actual.js b/test/fixtures/typescript/enum/declare-const/actual.js new file mode 100644 index 0000000000..4a3fc75be0 --- /dev/null +++ b/test/fixtures/typescript/enum/declare-const/actual.js @@ -0,0 +1 @@ +declare const enum E {} diff --git a/test/fixtures/typescript/enum/declare-const/expected.json b/test/fixtures/typescript/enum/declare-const/expected.json new file mode 100644 index 0000000000..cbb6b73653 --- /dev/null +++ b/test/fixtures/typescript/enum/declare-const/expected.json @@ -0,0 +1,69 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "const": true, + "id": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [], + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/declare/actual.js b/test/fixtures/typescript/enum/declare/actual.js new file mode 100644 index 0000000000..5218d402d3 --- /dev/null +++ b/test/fixtures/typescript/enum/declare/actual.js @@ -0,0 +1 @@ +declare enum E {} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/declare/expected.json b/test/fixtures/typescript/enum/declare/expected.json new file mode 100644 index 0000000000..9ca983fb14 --- /dev/null +++ b/test/fixtures/typescript/enum/declare/expected.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [], + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/export-const/actual.js b/test/fixtures/typescript/enum/export-const/actual.js new file mode 100644 index 0000000000..6b1e42773f --- /dev/null +++ b/test/fixtures/typescript/enum/export-const/actual.js @@ -0,0 +1 @@ +export const enum E {} diff --git a/test/fixtures/typescript/enum/export-const/expected.json b/test/fixtures/typescript/enum/export-const/expected.json new file mode 100644 index 0000000000..8d8043488c --- /dev/null +++ b/test/fixtures/typescript/enum/export-const/expected.json @@ -0,0 +1,85 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 7, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "const": true, + "id": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/export-declare-const/actual.js b/test/fixtures/typescript/enum/export-declare-const/actual.js new file mode 100644 index 0000000000..e6d28ba285 --- /dev/null +++ b/test/fixtures/typescript/enum/export-declare-const/actual.js @@ -0,0 +1 @@ +export declare const enum E {} diff --git a/test/fixtures/typescript/enum/export-declare-const/expected.json b/test/fixtures/typescript/enum/export-declare-const/expected.json new file mode 100644 index 0000000000..f83c34d4ac --- /dev/null +++ b/test/fixtures/typescript/enum/export-declare-const/expected.json @@ -0,0 +1,86 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "const": true, + "id": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [], + "declare": true + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/export/actual.js b/test/fixtures/typescript/enum/export/actual.js new file mode 100644 index 0000000000..5bda4c84bc --- /dev/null +++ b/test/fixtures/typescript/enum/export/actual.js @@ -0,0 +1 @@ +export enum E {} diff --git a/test/fixtures/typescript/enum/export/expected.json b/test/fixtures/typescript/enum/export/expected.json new file mode 100644 index 0000000000..a06a074797 --- /dev/null +++ b/test/fixtures/typescript/enum/export/expected.json @@ -0,0 +1,84 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 7, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-reserved-words/actual.js b/test/fixtures/typescript/enum/members-reserved-words/actual.js new file mode 100644 index 0000000000..06c4ab796c --- /dev/null +++ b/test/fixtures/typescript/enum/members-reserved-words/actual.js @@ -0,0 +1,4 @@ +enum E { + const, + default +} diff --git a/test/fixtures/typescript/enum/members-reserved-words/expected.json b/test/fixtures/typescript/enum/members-reserved-words/expected.json new file mode 100644 index 0000000000..110b72a0ac --- /dev/null +++ b/test/fixtures/typescript/enum/members-reserved-words/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "const" + }, + "name": "const" + } + }, + { + "type": "TSEnumMember", + "start": 24, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 24, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "default" + }, + "name": "default" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-strings/actual.js b/test/fixtures/typescript/enum/members-strings/actual.js new file mode 100644 index 0000000000..fe909aa5c4 --- /dev/null +++ b/test/fixtures/typescript/enum/members-strings/actual.js @@ -0,0 +1,4 @@ +enum E { + "foo", + "bar" = 1 +} diff --git a/test/fixtures/typescript/enum/members-strings/expected.json b/test/fixtures/typescript/enum/members-strings/expected.json new file mode 100644 index 0000000000..f5089fa23a --- /dev/null +++ b/test/fixtures/typescript/enum/members-strings/expected.json @@ -0,0 +1,158 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "id": { + "type": "StringLiteral", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "TSEnumMember", + "start": 24, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "id": { + "type": "StringLiteral", + "start": 24, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "extra": { + "rawValue": "bar", + "raw": "\"bar\"" + }, + "value": "bar" + }, + "initializer": { + "type": "NumericLiteral", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js new file mode 100644 index 0000000000..0c1f115f61 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js @@ -0,0 +1,3 @@ +enum E { + A = 0, +} diff --git a/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json new file mode 100644 index 0000000000..2ff7744992 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "initializer": { + "type": "NumericLiteral", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-trailing-comma/actual.js b/test/fixtures/typescript/enum/members-trailing-comma/actual.js new file mode 100644 index 0000000000..7db24478a7 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma/actual.js @@ -0,0 +1,3 @@ +enum E { + A, +} diff --git a/test/fixtures/typescript/enum/members-trailing-comma/expected.json b/test/fixtures/typescript/enum/members-trailing-comma/expected.json new file mode 100644 index 0000000000..ccf1e3313b --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma/expected.json @@ -0,0 +1,100 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members/actual.js b/test/fixtures/typescript/enum/members/actual.js new file mode 100644 index 0000000000..c0d9e76d28 --- /dev/null +++ b/test/fixtures/typescript/enum/members/actual.js @@ -0,0 +1,4 @@ +enum E { + A, + B = 0 +} diff --git a/test/fixtures/typescript/enum/members/expected.json b/test/fixtures/typescript/enum/members/expected.json new file mode 100644 index 0000000000..e2eb96a737 --- /dev/null +++ b/test/fixtures/typescript/enum/members/expected.json @@ -0,0 +1,152 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + } + }, + { + "type": "TSEnumMember", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "id": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "B" + }, + "name": "B" + }, + "initializer": { + "type": "NumericLiteral", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/as-namespace/actual.js b/test/fixtures/typescript/export/as-namespace/actual.js new file mode 100644 index 0000000000..b971448035 --- /dev/null +++ b/test/fixtures/typescript/export/as-namespace/actual.js @@ -0,0 +1 @@ +export as namespace A; diff --git a/test/fixtures/typescript/export/as-namespace/expected.json b/test/fixtures/typescript/export/as-namespace/expected.json new file mode 100644 index 0000000000..3b91561944 --- /dev/null +++ b/test/fixtures/typescript/export/as-namespace/expected.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSNamespaceExportDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/declare/actual.js b/test/fixtures/typescript/export/declare/actual.js new file mode 100644 index 0000000000..58d3f7937e --- /dev/null +++ b/test/fixtures/typescript/export/declare/actual.js @@ -0,0 +1,7 @@ +export declare const x: number; +export declare function f(): void; +export declare class C {} +export declare interface I {} +export declare type T = number; +export declare module M {} +export declare namespace N {} diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json new file mode 100644 index 0000000000..d20d41cbdd --- /dev/null +++ b/test/fixtures/typescript/export/declare/expected.json @@ -0,0 +1,547 @@ +{ + "type": "File", + "start": 0, + "end": 211, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 211, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 15, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 22, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 24, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 30 + } + } + } + } + }, + "init": null + } + ], + "kind": "const", + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 32, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSDeclareFunction", + "start": 47, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 56, + "end": 57, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 59, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 61, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 67, + "end": 92, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start": 82, + "end": 92, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 88, + "end": 89, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 90, + "end": 92, + "loc": { + "start": { + "line": 3, + "column": 23 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "body": [] + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 93, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 108, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 118, + "end": 119, + "loc": { + "start": { + "line": 4, + "column": 25 + }, + "end": { + "line": 4, + "column": 26 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 27 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "body": [] + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 123, + "end": 154, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 31 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSTypeAliasDeclaration", + "start": 138, + "end": 154, + "loc": { + "start": { + "line": 5, + "column": 15 + }, + "end": { + "line": 5, + "column": 31 + } + }, + "id": { + "type": "Identifier", + "start": 143, + "end": 144, + "loc": { + "start": { + "line": 5, + "column": 20 + }, + "end": { + "line": 5, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 147, + "end": 153, + "loc": { + "start": { + "line": 5, + "column": 24 + }, + "end": { + "line": 5, + "column": 30 + } + } + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 155, + "end": 181, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 26 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 170, + "end": 181, + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 177, + "end": 178, + "loc": { + "start": { + "line": 6, + "column": 22 + }, + "end": { + "line": 6, + "column": 23 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 179, + "end": 181, + "loc": { + "start": { + "line": 6, + "column": 24 + }, + "end": { + "line": 6, + "column": 26 + } + }, + "body": [] + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 182, + "end": 211, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 197, + "end": 211, + "loc": { + "start": { + "line": 7, + "column": 15 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 207, + "end": 208, + "loc": { + "start": { + "line": 7, + "column": 25 + }, + "end": { + "line": 7, + "column": 26 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 209, + "end": 211, + "loc": { + "start": { + "line": 7, + "column": 27 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "body": [] + }, + "declare": true + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/equals/actual.js b/test/fixtures/typescript/export/equals/actual.js new file mode 100644 index 0000000000..af251c676f --- /dev/null +++ b/test/fixtures/typescript/export/equals/actual.js @@ -0,0 +1 @@ +export = f; diff --git a/test/fixtures/typescript/export/equals/expected.json b/test/fixtures/typescript/export/equals/expected.json new file mode 100644 index 0000000000..9e0da1e567 --- /dev/null +++ b/test/fixtures/typescript/export/equals/expected.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSExportAssignment", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "f" + }, + "name": "f" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/nested-same-name/actual.js b/test/fixtures/typescript/export/nested-same-name/actual.js new file mode 100644 index 0000000000..e0c638df32 --- /dev/null +++ b/test/fixtures/typescript/export/nested-same-name/actual.js @@ -0,0 +1,4 @@ +export const x = 0; +export namespace N { + export const x = 1; +} diff --git a/test/fixtures/typescript/export/nested-same-name/expected.json b/test/fixtures/typescript/export/nested-same-name/expected.json new file mode 100644 index 0000000000..fb69187bd9 --- /dev/null +++ b/test/fixtures/typescript/export/nested-same-name/expected.json @@ -0,0 +1,274 @@ +{ + "type": "File", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 7, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "kind": "const" + } + }, + { + "type": "ExportNamedDeclaration", + "start": 20, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 27, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 39, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 45, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 52, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 58, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ], + "kind": "const" + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/annotated/actual.js b/test/fixtures/typescript/function/annotated/actual.js new file mode 100644 index 0000000000..139772fad0 --- /dev/null +++ b/test/fixtures/typescript/function/annotated/actual.js @@ -0,0 +1 @@ +function f(x?: T): T {} diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json new file mode 100644 index 0000000000..206cd1c3a3 --- /dev/null +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -0,0 +1,233 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 11, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": "T" + } + ] + }, + "params": [ + { + "type": "Identifier", + "start": 14, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "typeName": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "body": { + "type": "BlockStatement", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/anonymous/actual.js b/test/fixtures/typescript/function/anonymous/actual.js new file mode 100644 index 0000000000..395ce03c04 --- /dev/null +++ b/test/fixtures/typescript/function/anonymous/actual.js @@ -0,0 +1 @@ +const f = function(x?: T): T {}; diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json new file mode 100644 index 0000000000..ba875b73e8 --- /dev/null +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -0,0 +1,267 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "f" + }, + "name": "f" + }, + "init": { + "type": "FunctionExpression", + "start": 10, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "name": "T" + } + ] + }, + "params": [ + { + "type": "Identifier", + "start": 22, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 24, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeName": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 28, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "typeName": { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 31 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "body": { + "type": "BlockStatement", + "start": 32, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/declare/actual.js b/test/fixtures/typescript/function/declare/actual.js new file mode 100644 index 0000000000..f3a0c553cf --- /dev/null +++ b/test/fixtures/typescript/function/declare/actual.js @@ -0,0 +1,2 @@ +declare function f(): void; +declare function f(): T; diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json new file mode 100644 index 0000000000..1b115529a9 --- /dev/null +++ b/test/fixtures/typescript/function/declare/expected.json @@ -0,0 +1,218 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSDeclareFunction", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 22, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 26 + } + } + } + }, + "declare": true + }, + { + "type": "TSDeclareFunction", + "start": 28, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 45, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 47, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 47, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "name": "T" + } + ] + }, + "params": [], + "returnType": { + "type": "TypeAnnotation", + "start": 51, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 53, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "typeName": { + "type": "Identifier", + "start": 53, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/export-default/actual.js b/test/fixtures/typescript/function/export-default/actual.js new file mode 100644 index 0000000000..37e894cdaa --- /dev/null +++ b/test/fixtures/typescript/function/export-default/actual.js @@ -0,0 +1 @@ +export default function(x?: number): void; diff --git a/test/fixtures/typescript/function/export-default/expected.json b/test/fixtures/typescript/function/export-default/expected.json new file mode 100644 index 0000000000..26302894ff --- /dev/null +++ b/test/fixtures/typescript/function/export-default/expected.json @@ -0,0 +1,148 @@ +{ + "type": "File", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportDefaultDeclaration", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "declaration": { + "type": "TSDeclareFunction", + "start": 15, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 24, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 34 + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 37, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/overloads/actual.js b/test/fixtures/typescript/function/overloads/actual.js new file mode 100644 index 0000000000..2fb36ce1be --- /dev/null +++ b/test/fixtures/typescript/function/overloads/actual.js @@ -0,0 +1,2 @@ +export function f(x: number): number; +export function f(x: string): string; diff --git a/test/fixtures/typescript/function/overloads/expected.json b/test/fixtures/typescript/function/overloads/expected.json new file mode 100644 index 0000000000..04801d51b3 --- /dev/null +++ b/test/fixtures/typescript/function/overloads/expected.json @@ -0,0 +1,296 @@ +{ + "type": "File", + "start": 0, + "end": 75, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 75, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSDeclareFunction", + "start": 7, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "id": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 18, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 19, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 28, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 36 + } + } + } + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 38, + "end": 75, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSDeclareFunction", + "start": 45, + "end": 75, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "id": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 56, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 59, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 27 + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 66, + "end": 74, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 68, + "end": 74, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/pattern-parameters/actual.js b/test/fixtures/typescript/function/pattern-parameters/actual.js new file mode 100644 index 0000000000..d7fa2d78dc --- /dev/null +++ b/test/fixtures/typescript/function/pattern-parameters/actual.js @@ -0,0 +1 @@ +function f([]?, {}) {} diff --git a/test/fixtures/typescript/function/pattern-parameters/options.json b/test/fixtures/typescript/function/pattern-parameters/options.json new file mode 100644 index 0000000000..fb2831d618 --- /dev/null +++ b/test/fixtures/typescript/function/pattern-parameters/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A binding pattern parameter cannot be optional in an implementation signature. (1:11)" +} diff --git a/test/fixtures/typescript/function/predicate-types/actual.js b/test/fixtures/typescript/function/predicate-types/actual.js new file mode 100644 index 0000000000..374c71766c --- /dev/null +++ b/test/fixtures/typescript/function/predicate-types/actual.js @@ -0,0 +1,2 @@ +function f(x: any): x is boolean {} +(function(x: any): x is boolean {}) diff --git a/test/fixtures/typescript/function/predicate-types/expected.json b/test/fixtures/typescript/function/predicate-types/expected.json new file mode 100644 index 0000000000..6053c0ef64 --- /dev/null +++ b/test/fixtures/typescript/function/predicate-types/expected.json @@ -0,0 +1,393 @@ +{ + "type": "File", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 12, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 18, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 20, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "parameterName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 32 + } + } + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 33, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ExpressionStatement", + "start": 36, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "expression": { + "type": "FunctionExpression", + "start": 37, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 46, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 49, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 53, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 55, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "parameterName": { + "type": "Identifier", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 60, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 60, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } + } + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 68, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "body": [], + "directives": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 36 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/equals-require/actual.js b/test/fixtures/typescript/import/equals-require/actual.js new file mode 100644 index 0000000000..2c82fffdd9 --- /dev/null +++ b/test/fixtures/typescript/import/equals-require/actual.js @@ -0,0 +1 @@ +import a = require("a"); diff --git a/test/fixtures/typescript/import/equals-require/expected.json b/test/fixtures/typescript/import/equals-require/expected.json new file mode 100644 index 0000000000..3d3bcd0aa8 --- /dev/null +++ b/test/fixtures/typescript/import/equals-require/expected.json @@ -0,0 +1,102 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "isExport": false, + "id": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a" + }, + "moduleReference": { + "type": "TSExternalModuleReference", + "start": 11, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "expression": { + "type": "StringLiteral", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/equals/actual.js b/test/fixtures/typescript/import/equals/actual.js new file mode 100644 index 0000000000..4e414cfa82 --- /dev/null +++ b/test/fixtures/typescript/import/equals/actual.js @@ -0,0 +1 @@ +import A = B.C; diff --git a/test/fixtures/typescript/import/equals/expected.json b/test/fixtures/typescript/import/equals/expected.json new file mode 100644 index 0000000000..b8eec29841 --- /dev/null +++ b/test/fixtures/typescript/import/equals/expected.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "isExport": false, + "id": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "A" + }, + "name": "A" + }, + "moduleReference": { + "type": "TSQualifiedName", + "start": 11, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "B" + }, + "name": "B" + }, + "right": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "C" + }, + "name": "C" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/export-import-require/actual.js b/test/fixtures/typescript/import/export-import-require/actual.js new file mode 100644 index 0000000000..8b6df4184e --- /dev/null +++ b/test/fixtures/typescript/import/export-import-require/actual.js @@ -0,0 +1 @@ +export import a = require("a"); diff --git a/test/fixtures/typescript/import/export-import-require/expected.json b/test/fixtures/typescript/import/export-import-require/expected.json new file mode 100644 index 0000000000..548d031da4 --- /dev/null +++ b/test/fixtures/typescript/import/export-import-require/expected.json @@ -0,0 +1,102 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "isExport": true, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "a" + }, + "name": "a" + }, + "moduleReference": { + "type": "TSExternalModuleReference", + "start": 18, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "expression": { + "type": "StringLiteral", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/export-import/actual.js b/test/fixtures/typescript/import/export-import/actual.js new file mode 100644 index 0000000000..fb89f3bc50 --- /dev/null +++ b/test/fixtures/typescript/import/export-import/actual.js @@ -0,0 +1 @@ +export import A = B.C; diff --git a/test/fixtures/typescript/import/export-import/expected.json b/test/fixtures/typescript/import/export-import/expected.json new file mode 100644 index 0000000000..f9a877ad29 --- /dev/null +++ b/test/fixtures/typescript/import/export-import/expected.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "isExport": true, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "A" + }, + "name": "A" + }, + "moduleReference": { + "type": "TSQualifiedName", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "left": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "B" + }, + "name": "B" + }, + "right": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "C" + }, + "name": "C" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/not-top-level/actual.js b/test/fixtures/typescript/import/not-top-level/actual.js new file mode 100644 index 0000000000..b6278d2b78 --- /dev/null +++ b/test/fixtures/typescript/import/not-top-level/actual.js @@ -0,0 +1,3 @@ +declare module "m" { + import * as a from "a"; +} diff --git a/test/fixtures/typescript/import/not-top-level/expected.json b/test/fixtures/typescript/import/not-top-level/expected.json new file mode 100644 index 0000000000..88f8968e1f --- /dev/null +++ b/test/fixtures/typescript/import/not-top-level/expected.json @@ -0,0 +1,156 @@ +{ + "type": "File", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "StringLiteral", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 19, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ImportDeclaration", + "start": 25, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "local": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 44, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/call-signature/actual.js b/test/fixtures/typescript/interface/call-signature/actual.js new file mode 100644 index 0000000000..5d6b541719 --- /dev/null +++ b/test/fixtures/typescript/interface/call-signature/actual.js @@ -0,0 +1,3 @@ +interface I { + (x: number): void; +} diff --git a/test/fixtures/typescript/interface/call-signature/expected.json b/test/fixtures/typescript/interface/call-signature/expected.json new file mode 100644 index 0000000000..61e5359a15 --- /dev/null +++ b/test/fixtures/typescript/interface/call-signature/expected.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSCallSignatureDeclaration", + "start": 18, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 19, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 20, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 21 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/construct-signature/actual.js b/test/fixtures/typescript/interface/construct-signature/actual.js new file mode 100644 index 0000000000..d726e4c4ed --- /dev/null +++ b/test/fixtures/typescript/interface/construct-signature/actual.js @@ -0,0 +1,3 @@ +interface I { + new (x: number): void; +} diff --git a/test/fixtures/typescript/interface/construct-signature/expected.json b/test/fixtures/typescript/interface/construct-signature/expected.json new file mode 100644 index 0000000000..15e105e1d0 --- /dev/null +++ b/test/fixtures/typescript/interface/construct-signature/expected.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSConstructSignatureDeclaration", + "start": 18, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 23, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 24, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/export/actual.js b/test/fixtures/typescript/interface/export/actual.js new file mode 100644 index 0000000000..509789ea6f --- /dev/null +++ b/test/fixtures/typescript/interface/export/actual.js @@ -0,0 +1,2 @@ +export interface I {} +// `export default` does not work with interfaces diff --git a/test/fixtures/typescript/interface/export/expected.json b/test/fixtures/typescript/interface/export/expected.json new file mode 100644 index 0000000000..98373c9abf --- /dev/null +++ b/test/fixtures/typescript/interface/export/expected.json @@ -0,0 +1,138 @@ +{ + "type": "File", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 7, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "body": [], + "leadingComments": null, + "trailingComments": null + }, + "trailingComments": null + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " `export default` does not work with interfaces", + "start": 22, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " `export default` does not work with interfaces", + "start": 22, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/extends/actual.js b/test/fixtures/typescript/interface/extends/actual.js new file mode 100644 index 0000000000..8cf474f227 --- /dev/null +++ b/test/fixtures/typescript/interface/extends/actual.js @@ -0,0 +1 @@ +interface I extends X.Y {} diff --git a/test/fixtures/typescript/interface/extends/expected.json b/test/fixtures/typescript/interface/extends/expected.json new file mode 100644 index 0000000000..ef4064864f --- /dev/null +++ b/test/fixtures/typescript/interface/extends/expected.json @@ -0,0 +1,197 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "extends": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "left": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 23, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "typeName": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "Z" + }, + "name": "Z" + } + } + ] + } + } + ], + "body": { + "type": "TSInterfaceBody", + "start": 27, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/generic/actual.js b/test/fixtures/typescript/interface/generic/actual.js new file mode 100644 index 0000000000..62f2ede348 --- /dev/null +++ b/test/fixtures/typescript/interface/generic/actual.js @@ -0,0 +1 @@ +interface I {} diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json new file mode 100644 index 0000000000..d3f1878e05 --- /dev/null +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -0,0 +1,210 @@ +{ + "type": "File", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 12, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 12, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "name": "T", + "constraint": { + "type": "TSObjectKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 28 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 31, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 33, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 34, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 36, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 42 + } + } + } + } + } + ] + } + } + ] + }, + "body": { + "type": "TSInterfaceBody", + "start": 46, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/index-signature/actual.js b/test/fixtures/typescript/interface/index-signature/actual.js new file mode 100644 index 0000000000..2c676f629f --- /dev/null +++ b/test/fixtures/typescript/interface/index-signature/actual.js @@ -0,0 +1,3 @@ +interface I { + [s: string]: number; +} diff --git a/test/fixtures/typescript/interface/index-signature/expected.json b/test/fixtures/typescript/interface/index-signature/expected.json new file mode 100644 index 0000000000..e3fe435449 --- /dev/null +++ b/test/fixtures/typescript/interface/index-signature/expected.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSIndexSignature", + "start": 18, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "s" + }, + "name": "s", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 29, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-computed/actual.js b/test/fixtures/typescript/interface/method-computed/actual.js new file mode 100644 index 0000000000..688dc322af --- /dev/null +++ b/test/fixtures/typescript/interface/method-computed/actual.js @@ -0,0 +1,4 @@ +interface I { + [Symbol.iterator](): void; + [Symbol.iterator]?(): number; +} diff --git a/test/fixtures/typescript/interface/method-computed/expected.json b/test/fixtures/typescript/interface/method-computed/expected.json new file mode 100644 index 0000000000..d703d207c3 --- /dev/null +++ b/test/fixtures/typescript/interface/method-computed/expected.json @@ -0,0 +1,278 @@ +{ + "type": "File", + "start": 0, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 19, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + } + }, + { + "type": "TSMethodSignature", + "start": 49, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 50, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 69, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 71, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-generic/actual.js b/test/fixtures/typescript/interface/method-generic/actual.js new file mode 100644 index 0000000000..27f962006e --- /dev/null +++ b/test/fixtures/typescript/interface/method-generic/actual.js @@ -0,0 +1,3 @@ +interface I { + m(): T; +} diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json new file mode 100644 index 0000000000..1293957e3f --- /dev/null +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -0,0 +1,292 @@ +{ + "type": "File", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 20, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 20, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "name": "T", + "constraint": { + "type": "TSObjectKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 39, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 41, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 36 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 28 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 42, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + } + } + ] + } + } + ] + }, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 55, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "typeName": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-optional/actual.js b/test/fixtures/typescript/interface/method-optional/actual.js new file mode 100644 index 0000000000..9ceae32a53 --- /dev/null +++ b/test/fixtures/typescript/interface/method-optional/actual.js @@ -0,0 +1,3 @@ +interface I { + m?(): void; +} diff --git a/test/fixtures/typescript/interface/method-optional/expected.json b/test/fixtures/typescript/interface/method-optional/expected.json new file mode 100644 index 0000000000..3d52096664 --- /dev/null +++ b/test/fixtures/typescript/interface/method-optional/expected.json @@ -0,0 +1,148 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "optional": true, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-plain/actual.js b/test/fixtures/typescript/interface/method-plain/actual.js new file mode 100644 index 0000000000..8d4d83dcce --- /dev/null +++ b/test/fixtures/typescript/interface/method-plain/actual.js @@ -0,0 +1,4 @@ +interface I { + m(); + m(x?: number, ...y: number[]): void; +} diff --git a/test/fixtures/typescript/interface/method-plain/expected.json b/test/fixtures/typescript/interface/method-plain/expected.json new file mode 100644 index 0000000000..2ad1590a2a --- /dev/null +++ b/test/fixtures/typescript/interface/method-plain/expected.json @@ -0,0 +1,307 @@ +{ + "type": "File", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "parameters": [] + }, + { + "type": "TSMethodSignature", + "start": 27, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 40 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "parameters": [ + { + "type": "Identifier", + "start": 29, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 16 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 31, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } + } + } + }, + { + "type": "RestElement", + "start": 41, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "argument": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 45, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 47, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 56, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 39 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 58, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 39 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/modifiers/actual.js b/test/fixtures/typescript/interface/modifiers/actual.js new file mode 100644 index 0000000000..e5836883ef --- /dev/null +++ b/test/fixtures/typescript/interface/modifiers/actual.js @@ -0,0 +1,3 @@ +interface I { + readonly x: number; +} diff --git a/test/fixtures/typescript/interface/modifiers/expected.json b/test/fixtures/typescript/interface/modifiers/expected.json new file mode 100644 index 0000000000..64509cbce8 --- /dev/null +++ b/test/fixtures/typescript/interface/modifiers/expected.json @@ -0,0 +1,147 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x" + }, + "readonly": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 28, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/properties/actual.js b/test/fixtures/typescript/interface/properties/actual.js new file mode 100644 index 0000000000..94c10ef4f5 --- /dev/null +++ b/test/fixtures/typescript/interface/properties/actual.js @@ -0,0 +1,5 @@ +interface I { + x; + y: number; + z?: number; +} diff --git a/test/fixtures/typescript/interface/properties/expected.json b/test/fixtures/typescript/interface/properties/expected.json new file mode 100644 index 0000000000..71654d6806 --- /dev/null +++ b/test/fixtures/typescript/interface/properties/expected.json @@ -0,0 +1,243 @@ +{ + "type": "File", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + { + "type": "TSPropertySignature", + "start": 25, + "end": 35, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 40, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 15 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "z" + }, + "name": "z" + }, + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 42, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 6 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/property-computed/actual.js b/test/fixtures/typescript/interface/property-computed/actual.js new file mode 100644 index 0000000000..78f3ac4ab0 --- /dev/null +++ b/test/fixtures/typescript/interface/property-computed/actual.js @@ -0,0 +1,4 @@ +interface I { + [Symbol.iterator]: number; + [Symbol.iterator]?: number; +} diff --git a/test/fixtures/typescript/interface/property-computed/expected.json b/test/fixtures/typescript/interface/property-computed/expected.json new file mode 100644 index 0000000000..b859411d56 --- /dev/null +++ b/test/fixtures/typescript/interface/property-computed/expected.json @@ -0,0 +1,276 @@ +{ + "type": "File", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 19, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 35, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 49, + "end": 76, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 50, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 67, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 69, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/property-named-public/actual.js b/test/fixtures/typescript/interface/property-named-public/actual.js new file mode 100644 index 0000000000..ef6e1f3bf1 --- /dev/null +++ b/test/fixtures/typescript/interface/property-named-public/actual.js @@ -0,0 +1,3 @@ +interface I { + public: number; +} diff --git a/test/fixtures/typescript/interface/property-named-public/expected.json b/test/fixtures/typescript/interface/property-named-public/expected.json new file mode 100644 index 0000000000..1e6b5d8e56 --- /dev/null +++ b/test/fixtures/typescript/interface/property-named-public/expected.json @@ -0,0 +1,146 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "public" + }, + "name": "public" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 21, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/reserved-method-name/actual.js b/test/fixtures/typescript/interface/reserved-method-name/actual.js new file mode 100644 index 0000000000..76dff7a531 --- /dev/null +++ b/test/fixtures/typescript/interface/reserved-method-name/actual.js @@ -0,0 +1,3 @@ +interface I { + catch(): void; +} diff --git a/test/fixtures/typescript/interface/reserved-method-name/expected.json b/test/fixtures/typescript/interface/reserved-method-name/expected.json new file mode 100644 index 0000000000..e63d75c7dc --- /dev/null +++ b/test/fixtures/typescript/interface/reserved-method-name/expected.json @@ -0,0 +1,147 @@ +{ + "type": "File", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "catch" + }, + "name": "catch" + }, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 27, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 17 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/separators/actual.js b/test/fixtures/typescript/interface/separators/actual.js new file mode 100644 index 0000000000..f8e941cd41 --- /dev/null +++ b/test/fixtures/typescript/interface/separators/actual.js @@ -0,0 +1,6 @@ +interface Comma { x: number, y: number } +interface Semi { x: number; y: number } +interface Newline { + x: number + y: number +} diff --git a/test/fixtures/typescript/interface/separators/expected.json b/test/fixtures/typescript/interface/separators/expected.json new file mode 100644 index 0000000000..6470be82e4 --- /dev/null +++ b/test/fixtures/typescript/interface/separators/expected.json @@ -0,0 +1,559 @@ +{ + "type": "File", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "Comma" + }, + "name": "Comma" + }, + "body": { + "type": "TSInterfaceBody", + "start": 16, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 19, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 29, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 30, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } + } + } + } + ] + } + }, + { + "type": "TSInterfaceDeclaration", + "start": 41, + "end": 80, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "id": { + "type": "Identifier", + "start": 51, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "Semi" + }, + "name": "Semi" + }, + "body": { + "type": "TSInterfaceBody", + "start": 56, + "end": 80, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 58, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 59, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 61, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 69, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 69, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 70, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 72, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 37 + } + } + } + } + } + ] + } + }, + { + "type": "TSInterfaceDeclaration", + "start": 81, + "end": 130, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 91, + "end": 98, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 17 + }, + "identifierName": "Newline" + }, + "name": "Newline" + }, + "body": { + "type": "TSInterfaceBody", + "start": 99, + "end": 130, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 105, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 105, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 106, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 108, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 119, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 119, + "end": 120, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 120, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 5 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 122, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body-declare/actual.js b/test/fixtures/typescript/module-namespace/body-declare/actual.js new file mode 100644 index 0000000000..2723dc49e2 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-declare/actual.js @@ -0,0 +1,3 @@ +declare namespace N { + const x: number; +} diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json new file mode 100644 index 0000000000..31fb26a418 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -0,0 +1,165 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 20, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 26, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 33, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 19 + } + } + } + } + }, + "init": null + } + ], + "kind": "const" + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/actual.js b/test/fixtures/typescript/module-namespace/body-nested-declare/actual.js new file mode 100644 index 0000000000..8ed35530c9 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/actual.js @@ -0,0 +1,5 @@ +declare namespace A { + namespace B { + const x: number; + } +} diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json new file mode 100644 index 0000000000..1a3d715c76 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -0,0 +1,214 @@ +{ + "type": "File", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSModuleBlock", + "start": 20, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "TSModuleDeclaration", + "start": 26, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "B" + }, + "name": "B" + }, + "body": { + "type": "TSModuleBlock", + "start": 38, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 48, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 54, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 54, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 23 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 55, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 57, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } + } + } + }, + "init": null + } + ], + "kind": "const" + } + ] + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body-nested/actual.js b/test/fixtures/typescript/module-namespace/body-nested/actual.js new file mode 100644 index 0000000000..b03744fb9d --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested/actual.js @@ -0,0 +1,4 @@ +namespace A { + namespace B { + } +} diff --git a/test/fixtures/typescript/module-namespace/body-nested/expected.json b/test/fixtures/typescript/module-namespace/body-nested/expected.json new file mode 100644 index 0000000000..d9da87ef39 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested/expected.json @@ -0,0 +1,131 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSModuleDeclaration", + "start": 18, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "B" + }, + "name": "B" + }, + "body": { + "type": "TSModuleBlock", + "start": 30, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "body": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body/actual.js b/test/fixtures/typescript/module-namespace/body/actual.js new file mode 100644 index 0000000000..fbfadc1859 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body/actual.js @@ -0,0 +1,3 @@ +namespace N { + const x = 0; +} diff --git a/test/fixtures/typescript/module-namespace/body/expected.json b/test/fixtures/typescript/module-namespace/body/expected.json new file mode 100644 index 0000000000..478a7945b8 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body/expected.json @@ -0,0 +1,153 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 18, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 24, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "kind": "const" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/declare-shorthand/actual.js b/test/fixtures/typescript/module-namespace/declare-shorthand/actual.js new file mode 100644 index 0000000000..b6cc3a564c --- /dev/null +++ b/test/fixtures/typescript/module-namespace/declare-shorthand/actual.js @@ -0,0 +1 @@ +declare module "m"; diff --git a/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json b/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json new file mode 100644 index 0000000000..b03a91254c --- /dev/null +++ b/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "id": { + "type": "StringLiteral", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/global-in-module/actual.js b/test/fixtures/typescript/module-namespace/global-in-module/actual.js new file mode 100644 index 0000000000..8da25cacf8 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/global-in-module/actual.js @@ -0,0 +1,5 @@ +declare module "m" { + global { + var x: number; + } +} diff --git a/test/fixtures/typescript/module-namespace/global-in-module/expected.json b/test/fixtures/typescript/module-namespace/global-in-module/expected.json new file mode 100644 index 0000000000..1477ada618 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/global-in-module/expected.json @@ -0,0 +1,218 @@ +{ + "type": "File", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "StringLiteral", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 19, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "TSModuleDeclaration", + "start": 25, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "global": true, + "id": { + "type": "Identifier", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "global" + }, + "name": "global" + }, + "body": { + "type": "TSModuleBlock", + "start": 32, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 42, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 46, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 46, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 21 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 47, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 21 + } + } + } + } + }, + "init": null + } + ], + "kind": "var" + } + ] + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/head-declare/actual.js b/test/fixtures/typescript/module-namespace/head-declare/actual.js new file mode 100644 index 0000000000..d70669ebc7 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-declare/actual.js @@ -0,0 +1,4 @@ +declare namespace N.M {} +declare module M {} +declare module "m" {} +declare global {} diff --git a/test/fixtures/typescript/module-namespace/head-declare/expected.json b/test/fixtures/typescript/module-namespace/head-declare/expected.json new file mode 100644 index 0000000000..afe7354fd4 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-declare/expected.json @@ -0,0 +1,266 @@ +{ + "type": "File", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 22, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "body": [] + } + }, + "declare": true + }, + { + "type": "TSModuleDeclaration", + "start": 25, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "body": [] + }, + "declare": true + }, + { + "type": "TSModuleDeclaration", + "start": 45, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "id": { + "type": "StringLiteral", + "start": 60, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 64, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "body": [] + }, + "declare": true + }, + { + "type": "TSModuleDeclaration", + "start": 67, + "end": 84, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "global": true, + "id": { + "type": "Identifier", + "start": 75, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 14 + }, + "identifierName": "global" + }, + "name": "global" + }, + "body": { + "type": "TSModuleBlock", + "start": 82, + "end": 84, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "body": [] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/head-export/actual.js b/test/fixtures/typescript/module-namespace/head-export/actual.js new file mode 100644 index 0000000000..a3a6c205fa --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-export/actual.js @@ -0,0 +1,2 @@ +export namespace X.Y {} +export module X {} diff --git a/test/fixtures/typescript/module-namespace/head-export/expected.json b/test/fixtures/typescript/module-namespace/head-export/expected.json new file mode 100644 index 0000000000..8476ad7ecd --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-export/expected.json @@ -0,0 +1,196 @@ +{ + "type": "File", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 7, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "X" + }, + "name": "X" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "Y" + }, + "name": "Y" + }, + "body": { + "type": "TSModuleBlock", + "start": 21, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "body": [] + } + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 24, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 31, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "X" + }, + "name": "X" + }, + "body": { + "type": "TSModuleBlock", + "start": 40, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "body": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/head/actual.js b/test/fixtures/typescript/module-namespace/head/actual.js new file mode 100644 index 0000000000..f764e1bd26 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head/actual.js @@ -0,0 +1,4 @@ +namespace N {} +namespace M.N.O {} +module M {} +module "m" {} diff --git a/test/fixtures/typescript/module-namespace/head/expected.json b/test/fixtures/typescript/module-namespace/head/expected.json new file mode 100644 index 0000000000..c3f419f4ca --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head/expected.json @@ -0,0 +1,293 @@ +{ + "type": "File", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "TSModuleDeclaration", + "start": 15, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 29, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "O" + }, + "name": "O" + }, + "body": { + "type": "TSModuleBlock", + "start": 31, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "body": [] + } + } + } + }, + { + "type": "TSModuleDeclaration", + "start": 34, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 43, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "body": [] + } + }, + { + "type": "TSModuleDeclaration", + "start": 46, + "end": 59, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "id": { + "type": "StringLiteral", + "start": 53, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 10 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 57, + "end": 59, + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/options.json b/test/fixtures/typescript/options.json new file mode 100644 index 0000000000..fe9bffaa5e --- /dev/null +++ b/test/fixtures/typescript/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["typescript"] +} diff --git a/test/fixtures/typescript/tsx/brace-is-block/actual.js b/test/fixtures/typescript/tsx/brace-is-block/actual.js new file mode 100644 index 0000000000..dbe88dfe03 --- /dev/null +++ b/test/fixtures/typescript/tsx/brace-is-block/actual.js @@ -0,0 +1,3 @@ +// Regression test for tokenizer bug where the `{` after `` was considered a JSX interpolation. +class C extends D {} + diff --git a/test/fixtures/typescript/tsx/brace-is-block/expected.json b/test/fixtures/typescript/tsx/brace-is-block/expected.json new file mode 100644 index 0000000000..a264091a97 --- /dev/null +++ b/test/fixtures/typescript/tsx/brace-is-block/expected.json @@ -0,0 +1,250 @@ +{ + "type": "File", + "start": 0, + "end": 127, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 127, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 99, + "end": 122, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 105, + "end": 106, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C", + "leadingComments": null + }, + "superClass": { + "type": "Identifier", + "start": 115, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "D" + }, + "name": "D" + }, + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 116, + "end": 119, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 117, + "end": 118, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "typeName": { + "type": "Identifier", + "start": 117, + "end": 118, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "body": { + "type": "ClassBody", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "body": [] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Regression test for tokenizer bug where the `{` after `` was considered a JSX interpolation.", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 98 + } + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 123, + "end": 127, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "expression": { + "type": "JSXElement", + "start": 123, + "end": 127, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "openingElement": { + "type": "JSXOpeningElement", + "start": 123, + "end": 127, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "attributes": [], + "name": { + "type": "JSXIdentifier", + "start": 124, + "end": 125, + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "name": "C" + }, + "selfClosing": true + }, + "closingElement": null, + "children": [] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Regression test for tokenizer bug where the `{` after `` was considered a JSX interpolation.", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 98 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/tsx/options.json b/test/fixtures/typescript/tsx/options.json new file mode 100644 index 0000000000..3a319216ff --- /dev/null +++ b/test/fixtures/typescript/tsx/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["jsx", "typescript"], + "sourceType": "module" +} diff --git a/test/fixtures/typescript/type-alias/declare/actual.js b/test/fixtures/typescript/type-alias/declare/actual.js new file mode 100644 index 0000000000..301931812c --- /dev/null +++ b/test/fixtures/typescript/type-alias/declare/actual.js @@ -0,0 +1 @@ +declare type T = number; diff --git a/test/fixtures/typescript/type-alias/declare/expected.json b/test/fixtures/typescript/type-alias/declare/expected.json new file mode 100644 index 0000000000..342f2ca280 --- /dev/null +++ b/test/fixtures/typescript/type-alias/declare/expected.json @@ -0,0 +1,82 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/export/actual.js b/test/fixtures/typescript/type-alias/export/actual.js new file mode 100644 index 0000000000..0ffb0fcdd6 --- /dev/null +++ b/test/fixtures/typescript/type-alias/export/actual.js @@ -0,0 +1,2 @@ +export type T = number; +// `export default type` is not valid. diff --git a/test/fixtures/typescript/type-alias/export/expected.json b/test/fixtures/typescript/type-alias/export/expected.json new file mode 100644 index 0000000000..503ea72ce1 --- /dev/null +++ b/test/fixtures/typescript/type-alias/export/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSTypeAliasDeclaration", + "start": 7, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 16, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + "trailingComments": null + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " `export default type` is not valid.", + "start": 24, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " `export default type` is not valid.", + "start": 24, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/generic-complex/actual.js b/test/fixtures/typescript/type-alias/generic-complex/actual.js new file mode 100644 index 0000000000..9fb8928d4f --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic-complex/actual.js @@ -0,0 +1 @@ +type T = Array; diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json new file mode 100644 index 0000000000..f06680eaa4 --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -0,0 +1,275 @@ +{ + "type": "File", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 7, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "name": "U", + "constraint": { + "type": "TSObjectKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 26, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 28, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 29, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } + } + } + } + } + ] + } + } + ] + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 43, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 51 + } + }, + "typeName": { + "type": "Identifier", + "start": 43, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 48 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 48, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 48 + }, + "end": { + "line": 1, + "column": 51 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 50 + } + }, + "typeName": { + "type": "Identifier", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 50 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/generic/actual.js b/test/fixtures/typescript/type-alias/generic/actual.js new file mode 100644 index 0000000000..0e910abfc6 --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic/actual.js @@ -0,0 +1 @@ +type T = U; diff --git a/test/fixtures/typescript/type-alias/generic/expected.json b/test/fixtures/typescript/type-alias/generic/expected.json new file mode 100644 index 0000000000..d4ffab2901 --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic/expected.json @@ -0,0 +1,131 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": "U" + } + ] + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "typeName": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "U" + }, + "name": "U" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/plain/actual.js b/test/fixtures/typescript/type-alias/plain/actual.js new file mode 100644 index 0000000000..42126b96d0 --- /dev/null +++ b/test/fixtures/typescript/type-alias/plain/actual.js @@ -0,0 +1 @@ +type T = number; diff --git a/test/fixtures/typescript/type-alias/plain/expected.json b/test/fixtures/typescript/type-alias/plain/expected.json new file mode 100644 index 0000000000..bd7e4dd4ef --- /dev/null +++ b/test/fixtures/typescript/type-alias/plain/expected.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-arguments/call/actual.js b/test/fixtures/typescript/type-arguments/call/actual.js new file mode 100644 index 0000000000..3a4d08a538 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/call/actual.js @@ -0,0 +1,2 @@ +f(); +f(); diff --git a/test/fixtures/typescript/type-arguments/call/expected.json b/test/fixtures/typescript/type-arguments/call/expected.json new file mode 100644 index 0000000000..a644b276ea --- /dev/null +++ b/test/fixtures/typescript/type-arguments/call/expected.json @@ -0,0 +1,260 @@ +{ + "type": "File", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "callee": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [], + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 1, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "typeName": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + }, + { + "type": "ExpressionStatement", + "start": 8, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "expression": { + "type": "CallExpression", + "start": 8, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "callee": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [], + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "typeName": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + { + "type": "TSTypeReference", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js b/test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js new file mode 100644 index 0000000000..458a429660 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js @@ -0,0 +1 @@ +new A; diff --git a/test/fixtures/typescript/type-arguments/new-false-positive-2/options.json b/test/fixtures/typescript/type-arguments/new-false-positive-2/options.json new file mode 100644 index 0000000000..07d136e203 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive-2/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:8)" +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-arguments/new-false-positive/actual.js b/test/fixtures/typescript/type-arguments/new-false-positive/actual.js new file mode 100644 index 0000000000..f703174469 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive/actual.js @@ -0,0 +1 @@ +new A < T; diff --git a/test/fixtures/typescript/type-arguments/new-false-positive/expected.json b/test/fixtures/typescript/type-arguments/new-false-positive/expected.json new file mode 100644 index 0000000000..721f294462 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive/expected.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "left": { + "type": "NewExpression", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "callee": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "arguments": [] + }, + "operator": "<", + "right": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-arguments/new/actual.js b/test/fixtures/typescript/type-arguments/new/actual.js new file mode 100644 index 0000000000..d474bb65c3 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new/actual.js @@ -0,0 +1,2 @@ +new C(); +new C(); diff --git a/test/fixtures/typescript/type-arguments/new/expected.json b/test/fixtures/typescript/type-arguments/new/expected.json new file mode 100644 index 0000000000..333b5faa84 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new/expected.json @@ -0,0 +1,260 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "NewExpression", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "callee": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 5, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "arguments": [] + } + }, + { + "type": "ExpressionStatement", + "start": 12, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "expression": { + "type": "NewExpression", + "start": 12, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "callee": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + { + "type": "TSTypeReference", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "typeName": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ] + }, + "arguments": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/array/actual.js b/test/fixtures/typescript/types/array/actual.js new file mode 100644 index 0000000000..aff94656eb --- /dev/null +++ b/test/fixtures/typescript/types/array/actual.js @@ -0,0 +1 @@ +let arr: number[][]; diff --git a/test/fixtures/typescript/types/array/expected.json b/test/fixtures/typescript/types/array/expected.json new file mode 100644 index 0000000000..544df535c4 --- /dev/null +++ b/test/fixtures/typescript/types/array/expected.json @@ -0,0 +1,145 @@ +{ + "type": "File", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "arr" + }, + "name": "arr", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 7, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 9, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "elementType": { + "type": "TSArrayType", + "start": 9, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function-generic/actual.js b/test/fixtures/typescript/types/function-generic/actual.js new file mode 100644 index 0000000000..61d8ece714 --- /dev/null +++ b/test/fixtures/typescript/types/function-generic/actual.js @@ -0,0 +1 @@ +let f: (a: T) => T; diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json new file mode 100644 index 0000000000..11d92aed58 --- /dev/null +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -0,0 +1,261 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "f" + }, + "name": "f", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 8, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "T" + } + ] + }, + "parameters": [ + { + "type": "Identifier", + "start": 11, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 12, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeName": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 17, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function-in-generic/actual.js b/test/fixtures/typescript/types/function-in-generic/actual.js new file mode 100644 index 0000000000..6b9a571a7c --- /dev/null +++ b/test/fixtures/typescript/types/function-in-generic/actual.js @@ -0,0 +1 @@ +let x: Array<() => void>; diff --git a/test/fixtures/typescript/types/function-in-generic/expected.json b/test/fixtures/typescript/types/function-in-generic/expected.json new file mode 100644 index 0000000000..89e8e68bad --- /dev/null +++ b/test/fixtures/typescript/types/function-in-generic/expected.json @@ -0,0 +1,195 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 12, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "params": [ + { + "type": "TSFunctionType", + "start": 13, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 16, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + } + } + ] + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function-with-this/actual.js b/test/fixtures/typescript/types/function-with-this/actual.js new file mode 100644 index 0000000000..cd13ed098d --- /dev/null +++ b/test/fixtures/typescript/types/function-with-this/actual.js @@ -0,0 +1 @@ +let f: (this: number) => void; diff --git a/test/fixtures/typescript/types/function-with-this/expected.json b/test/fixtures/typescript/types/function-with-this/expected.json new file mode 100644 index 0000000000..fc64db43de --- /dev/null +++ b/test/fixtures/typescript/types/function-with-this/expected.json @@ -0,0 +1,194 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "f" + }, + "name": "f", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 8, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "this" + }, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 12, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 22, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 25, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 29 + } + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function/actual.js b/test/fixtures/typescript/types/function/actual.js new file mode 100644 index 0000000000..15173a1b65 --- /dev/null +++ b/test/fixtures/typescript/types/function/actual.js @@ -0,0 +1 @@ +let f: (a: number, b?: number, ...c: number[]) => void; diff --git a/test/fixtures/typescript/types/function/expected.json b/test/fixtures/typescript/types/function/expected.json new file mode 100644 index 0000000000..1a07ff6407 --- /dev/null +++ b/test/fixtures/typescript/types/function/expected.json @@ -0,0 +1,319 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 55 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 55 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 55 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 54 + }, + "identifierName": "f" + }, + "name": "f", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 8, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 9, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + } + }, + { + "type": "Identifier", + "start": 19, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "b" + }, + "name": "b", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 21, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 29 + } + } + } + } + }, + { + "type": "RestElement", + "start": 31, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "argument": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "c" + }, + "name": "c" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 35, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 37, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 43 + } + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 47, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 50, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 50 + }, + "end": { + "line": 1, + "column": 54 + } + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/indexed/actual.js b/test/fixtures/typescript/types/indexed/actual.js new file mode 100644 index 0000000000..4a5809fdd5 --- /dev/null +++ b/test/fixtures/typescript/types/indexed/actual.js @@ -0,0 +1 @@ +let x: T[K]; diff --git a/test/fixtures/typescript/types/indexed/expected.json b/test/fixtures/typescript/types/indexed/expected.json new file mode 100644 index 0000000000..0524607a50 --- /dev/null +++ b/test/fixtures/typescript/types/indexed/expected.json @@ -0,0 +1,179 @@ +{ + "type": "File", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "typeAnnotation": { + "type": "TSIndexedAccessType", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "objectType": { + "type": "TSTypeReference", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "indexType": { + "type": "TSTypeReference", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeName": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "K" + }, + "name": "K" + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/keywords/actual.js b/test/fixtures/typescript/types/keywords/actual.js new file mode 100644 index 0000000000..d2ddfb94bb --- /dev/null +++ b/test/fixtures/typescript/types/keywords/actual.js @@ -0,0 +1,10 @@ +let a: any; +let b: boolean; +let ne: never; +let nul: null; +let num: number; +let o: object; +let st: string; +let sy: symbol; +let u: undefined; +let v: void; diff --git a/test/fixtures/typescript/types/keywords/expected.json b/test/fixtures/typescript/types/keywords/expected.json new file mode 100644 index 0000000000..aef7589aae --- /dev/null +++ b/test/fixtures/typescript/types/keywords/expected.json @@ -0,0 +1,844 @@ +{ + "type": "File", + "start": 0, + "end": 152, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 10, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 152, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 10, + "column": 12 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 12, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "b" + }, + "name": "b", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 17, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 19, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 28, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 13 + }, + "identifierName": "ne" + }, + "name": "ne", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 34, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNeverKeyword", + "start": 36, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 43, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 47, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 47, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "nul" + }, + "name": "nul", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNullKeyword", + "start": 52, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 13 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 58, + "end": 74, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 62, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 62, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 15 + }, + "identifierName": "num" + }, + "name": "num", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 65, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 15 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 67, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 15 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 75, + "end": 89, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 79, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 79, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 13 + }, + "identifierName": "o" + }, + "name": "o", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 80, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 5 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSObjectKeyword", + "start": 82, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 13 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 90, + "end": 105, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 94, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 94, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 14 + }, + "identifierName": "st" + }, + "name": "st", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 96, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 6 + }, + "end": { + "line": 7, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 98, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 14 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 106, + "end": 121, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 110, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 110, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 14 + }, + "identifierName": "sy" + }, + "name": "sy", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 112, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 6 + }, + "end": { + "line": 8, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSSymbolKeyword", + "start": 114, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 14 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 122, + "end": 139, + "loc": { + "start": { + "line": 9, + "column": 0 + }, + "end": { + "line": 9, + "column": 17 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 126, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 126, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 16 + }, + "identifierName": "u" + }, + "name": "u", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 127, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 5 + }, + "end": { + "line": 9, + "column": 16 + } + }, + "typeAnnotation": { + "type": "TSUndefinedKeyword", + "start": 129, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 7 + }, + "end": { + "line": 9, + "column": 16 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 140, + "end": 152, + "loc": { + "start": { + "line": 10, + "column": 0 + }, + "end": { + "line": 10, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 144, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 144, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 11 + }, + "identifierName": "v" + }, + "name": "v", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 145, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 5 + }, + "end": { + "line": 10, + "column": 11 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 147, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 7 + }, + "end": { + "line": 10, + "column": 11 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-boolean/actual.js b/test/fixtures/typescript/types/literal-boolean/actual.js new file mode 100644 index 0000000000..d2cea8ee69 --- /dev/null +++ b/test/fixtures/typescript/types/literal-boolean/actual.js @@ -0,0 +1,2 @@ +let x: true; +let x: false; diff --git a/test/fixtures/typescript/types/literal-boolean/expected.json b/test/fixtures/typescript/types/literal-boolean/expected.json new file mode 100644 index 0000000000..6b0526b882 --- /dev/null +++ b/test/fixtures/typescript/types/literal-boolean/expected.json @@ -0,0 +1,228 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "literal": { + "type": "BooleanLiteral", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "value": true + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 13, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 17, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 18, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "literal": { + "type": "BooleanLiteral", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "value": false + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-number-negative/actual.js b/test/fixtures/typescript/types/literal-number-negative/actual.js new file mode 100644 index 0000000000..48e57ca110 --- /dev/null +++ b/test/fixtures/typescript/types/literal-number-negative/actual.js @@ -0,0 +1 @@ +let x: -1; diff --git a/test/fixtures/typescript/types/literal-number-negative/expected.json b/test/fixtures/typescript/types/literal-number-negative/expected.json new file mode 100644 index 0000000000..32eff1d795 --- /dev/null +++ b/test/fixtures/typescript/types/literal-number-negative/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "literal": { + "type": "NumericLiteral", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "extra": { + "rawValue": -1, + "raw": "-1" + }, + "value": -1 + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-number/actual.js b/test/fixtures/typescript/types/literal-number/actual.js new file mode 100644 index 0000000000..1e234e36b8 --- /dev/null +++ b/test/fixtures/typescript/types/literal-number/actual.js @@ -0,0 +1 @@ +let x: 0; diff --git a/test/fixtures/typescript/types/literal-number/expected.json b/test/fixtures/typescript/types/literal-number/expected.json new file mode 100644 index 0000000000..32e844e17f --- /dev/null +++ b/test/fixtures/typescript/types/literal-number/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "literal": { + "type": "NumericLiteral", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-string/actual.js b/test/fixtures/typescript/types/literal-string/actual.js new file mode 100644 index 0000000000..926a42ae7e --- /dev/null +++ b/test/fixtures/typescript/types/literal-string/actual.js @@ -0,0 +1 @@ +let x: "foo"; diff --git a/test/fixtures/typescript/types/literal-string/expected.json b/test/fixtures/typescript/types/literal-string/expected.json new file mode 100644 index 0000000000..842cf493b2 --- /dev/null +++ b/test/fixtures/typescript/types/literal-string/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "literal": { + "type": "StringLiteral", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/mapped/actual.js b/test/fixtures/typescript/types/mapped/actual.js new file mode 100644 index 0000000000..a3a1c9a3b4 --- /dev/null +++ b/test/fixtures/typescript/types/mapped/actual.js @@ -0,0 +1,2 @@ +let map: { [P in string]: number; }; +let map: { readonly [P in string]?: number; }; diff --git a/test/fixtures/typescript/types/mapped/expected.json b/test/fixtures/typescript/types/mapped/expected.json new file mode 100644 index 0000000000..441c47e824 --- /dev/null +++ b/test/fixtures/typescript/types/mapped/expected.json @@ -0,0 +1,290 @@ +{ + "type": "File", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "map" + }, + "name": "map", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 7, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "typeAnnotation": { + "type": "TSMappedType", + "start": 9, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "typeParameter": { + "type": "TypeParameter", + "start": 12, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "name": "P", + "constraint": { + "type": "TSStringKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 32 + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 37, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 41, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "id": { + "type": "Identifier", + "start": 41, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + }, + "identifierName": "map" + }, + "name": "map", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 44, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "typeAnnotation": { + "type": "TSMappedType", + "start": 46, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "readonly": true, + "typeParameter": { + "type": "TypeParameter", + "start": 58, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 32 + } + }, + "name": "P", + "constraint": { + "type": "TSStringKeyword", + "start": 63, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 32 + } + } + } + }, + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 73, + "end": 79, + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 42 + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/parenthesized/actual.js b/test/fixtures/typescript/types/parenthesized/actual.js new file mode 100644 index 0000000000..a8a9d2d955 --- /dev/null +++ b/test/fixtures/typescript/types/parenthesized/actual.js @@ -0,0 +1 @@ +type T = ({}); diff --git a/test/fixtures/typescript/types/parenthesized/expected.json b/test/fixtures/typescript/types/parenthesized/expected.json new file mode 100644 index 0000000000..b92fa9d5d7 --- /dev/null +++ b/test/fixtures/typescript/types/parenthesized/expected.json @@ -0,0 +1,97 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSParenthesizedType", + "start": 9, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 10, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "members": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/reference-generic-nested/actual.js b/test/fixtures/typescript/types/reference-generic-nested/actual.js new file mode 100644 index 0000000000..e38dfca9d5 --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic-nested/actual.js @@ -0,0 +1 @@ +let x: Array>; diff --git a/test/fixtures/typescript/types/reference-generic-nested/expected.json b/test/fixtures/typescript/types/reference-generic-nested/expected.json new file mode 100644 index 0000000000..a0e10cfcea --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic-nested/expected.json @@ -0,0 +1,213 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 12, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 13, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "params": [ + { + "type": "TSNumberKeyword", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } + } + } + ] + } + } + ] + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/reference-generic/actual.js b/test/fixtures/typescript/types/reference-generic/actual.js new file mode 100644 index 0000000000..4925bd4778 --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic/actual.js @@ -0,0 +1 @@ +let x: Array; diff --git a/test/fixtures/typescript/types/reference-generic/expected.json b/test/fixtures/typescript/types/reference-generic/expected.json new file mode 100644 index 0000000000..f9132c531e --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 12, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "params": [ + { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } + } + } + ] + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/reference/actual.js b/test/fixtures/typescript/types/reference/actual.js new file mode 100644 index 0000000000..330fb1d224 --- /dev/null +++ b/test/fixtures/typescript/types/reference/actual.js @@ -0,0 +1 @@ +let x: T; diff --git a/test/fixtures/typescript/types/reference/expected.json b/test/fixtures/typescript/types/reference/expected.json new file mode 100644 index 0000000000..6bec175d53 --- /dev/null +++ b/test/fixtures/typescript/types/reference/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/type-literal/actual.js b/test/fixtures/typescript/types/type-literal/actual.js new file mode 100644 index 0000000000..074c0b246a --- /dev/null +++ b/test/fixtures/typescript/types/type-literal/actual.js @@ -0,0 +1,2 @@ +let obj: { x: number }; +// Type literals have the same body syntax as interfaces, so see `interface` directory for that. diff --git a/test/fixtures/typescript/types/type-literal/expected.json b/test/fixtures/typescript/types/type-literal/expected.json new file mode 100644 index 0000000000..000ee5dad5 --- /dev/null +++ b/test/fixtures/typescript/types/type-literal/expected.json @@ -0,0 +1,216 @@ +{ + "type": "File", + "start": 0, + "end": 120, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 120, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "obj" + }, + "name": "obj", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 7, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 9, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 11, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 12, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + } + } + } + } + ] + } + } + }, + "init": null + } + ], + "kind": "let", + "trailingComments": [ + { + "type": "CommentLine", + "value": " Type literals have the same body syntax as interfaces, so see `interface` directory for that.", + "start": 24, + "end": 120, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Type literals have the same body syntax as interfaces, so see `interface` directory for that.", + "start": 24, + "end": 120, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/type-operator/actual.js b/test/fixtures/typescript/types/type-operator/actual.js new file mode 100644 index 0000000000..2027543c4e --- /dev/null +++ b/test/fixtures/typescript/types/type-operator/actual.js @@ -0,0 +1 @@ +let x: keyof T; diff --git a/test/fixtures/typescript/types/type-operator/expected.json b/test/fixtures/typescript/types/type-operator/expected.json new file mode 100644 index 0000000000..87635dfffa --- /dev/null +++ b/test/fixtures/typescript/types/type-operator/expected.json @@ -0,0 +1,148 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSTypeOperator", + "start": 7, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "operator": "keyof", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/typeof/actual.js b/test/fixtures/typescript/types/typeof/actual.js new file mode 100644 index 0000000000..eebcd19b13 --- /dev/null +++ b/test/fixtures/typescript/types/typeof/actual.js @@ -0,0 +1 @@ +let x: typeof y.z; diff --git a/test/fixtures/typescript/types/typeof/expected.json b/test/fixtures/typescript/types/typeof/expected.json new file mode 100644 index 0000000000..de41af47d4 --- /dev/null +++ b/test/fixtures/typescript/types/typeof/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSTypeQuery", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "exprName": { + "type": "TSQualifiedName", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "left": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "y" + }, + "name": "y" + }, + "right": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "z" + }, + "name": "z" + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/union-intersection/actual.js b/test/fixtures/typescript/types/union-intersection/actual.js new file mode 100644 index 0000000000..93f391fdc4 --- /dev/null +++ b/test/fixtures/typescript/types/union-intersection/actual.js @@ -0,0 +1,4 @@ +let union: number | null | undefined; +let intersection: number & string; +let precedence1: number | string & boolean; +let precedence2: number & string | boolean; diff --git a/test/fixtures/typescript/types/union-intersection/expected.json b/test/fixtures/typescript/types/union-intersection/expected.json new file mode 100644 index 0000000000..cc97fa9693 --- /dev/null +++ b/test/fixtures/typescript/types/union-intersection/expected.json @@ -0,0 +1,565 @@ +{ + "type": "File", + "start": 0, + "end": 160, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 43 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 160, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 43 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "union" + }, + "name": "union", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 9, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "typeAnnotation": { + "type": "TSUnionType", + "start": 11, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "TSNullKeyword", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "TSUndefinedKeyword", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 36 + } + } + } + ] + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 38, + "end": 72, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 42, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "id": { + "type": "Identifier", + "start": 42, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 33 + }, + "identifierName": "intersection" + }, + "name": "intersection", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 54, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "typeAnnotation": { + "type": "TSIntersectionType", + "start": 56, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 56, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "TSStringKeyword", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ] + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 73, + "end": 116, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 43 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 77, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "id": { + "type": "Identifier", + "start": 77, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 42 + }, + "identifierName": "precedence1" + }, + "name": "precedence1", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 88, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "typeAnnotation": { + "type": "TSUnionType", + "start": 90, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 90, + "end": 96, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } + }, + { + "type": "TSIntersectionType", + "start": 99, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "types": [ + { + "type": "TSStringKeyword", + "start": 99, + "end": 105, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + { + "type": "TSBooleanKeyword", + "start": 108, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] + } + ] + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 117, + "end": 160, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 43 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 121, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 42 + } + }, + "id": { + "type": "Identifier", + "start": 121, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 42 + }, + "identifierName": "precedence2" + }, + "name": "precedence2", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 132, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 42 + } + }, + "typeAnnotation": { + "type": "TSUnionType", + "start": 134, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 42 + } + }, + "types": [ + { + "type": "TSIntersectionType", + "start": 134, + "end": 149, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 32 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 134, + "end": 140, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 23 + } + } + }, + { + "type": "TSStringKeyword", + "start": 143, + "end": 149, + "loc": { + "start": { + "line": 4, + "column": 26 + }, + "end": { + "line": 4, + "column": 32 + } + } + } + ] + }, + { + "type": "TSBooleanKeyword", + "start": 152, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 35 + }, + "end": { + "line": 4, + "column": 42 + } + } + } + ] + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file