From c689f2a0e725d96950f8ebb171af74063c2fbd1e Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sat, 6 May 2017 12:44:26 +1000 Subject: [PATCH] Fix module block, add enum initializers and fix type parameters (#1501) * fix(typescript): fix module block, add enum initializers and fix type parameters * fix(typescript): use printStatementSequence for TSModuleBlock * fix(type-params): move typeParameters out of printFunctionParams * refactor(type-params): move typeParameters out of printArgumentList --- src/printer.js | 93 ++++++++++--------- src/typescript-ast-nodes.js | 15 ++- .../__snapshots__/jsfmt.spec.js.snap | 43 +++++++-- .../call/__snapshots__/jsfmt.spec.js.snap | 4 + tests/typescript/custom/call/callSignature.ts | 2 + 5 files changed, 97 insertions(+), 60 deletions(-) diff --git a/src/printer.js b/src/printer.js index 4f0b5d4f..53074df0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -343,9 +343,7 @@ function genericPrintNoParens(path, options, print, args) { parts.push("async "); } - if (n.typeParameters) { - parts.push(path.call(print, "typeParameters")); - } + parts.push(printFunctionTypeParameters(path, options, print)); if (canPrintParamsWithoutParens(n)) { parts.push(path.call(print, "params", 0)); @@ -737,7 +735,7 @@ function genericPrintNoParens(path, options, print, args) { return concat([ path.call(print, "callee"), - path.call(print, "typeParameters"), + printFunctionTypeParameters(path, options, print), printArgumentsList(path, options, print) ]); } @@ -1077,15 +1075,13 @@ function genericPrintNoParens(path, options, print, args) { ]) ); case "NewExpression": - parts.push("new ", path.call(print, "callee")); + parts.push( + "new ", + path.call(print, "callee"), + printFunctionTypeParameters(path, options, print) + ); - if (n.typeParameters) { - parts.push(path.call(print, "typeParameters")); - } - - var args = n.arguments; - - if (args) { + if (n.arguments) { parts.push(printArgumentsList(path, options, print)); } @@ -1796,17 +1792,10 @@ function genericPrintNoParens(path, options, print, args) { parts.push("("); } - // With TypeScript `typeParameters` is an array of `TSTypeParameter` and - // with flow they are one `TypeParameterDeclaration` node. - if (n.type === 'TSFunctionType' && n.typeParameters) { - parts.push( - printTypeParameters(path, options, print, "typeParameters") - ); - } else { - parts.push(path.call(print, "typeParameters")); - } - - parts.push(printFunctionParams(path, print, options)); + parts.push( + printFunctionTypeParameters(path, options, print), + printFunctionParams(path, print, options) + ); // The returnType is not wrapped in a TypeAnnotation, so the colon // needs to be added separately. @@ -2275,7 +2264,11 @@ function genericPrintNoParens(path, options, print, args) { return concat(parts); case "TSEnumMember": - return path.call(print, "name") + parts.push(path.call(print, "name")); + if (n.initializer) { + parts.push(" = ", path.call(print, "initializer")); + } + return concat(parts); case "TSImportEqualsDeclaration": parts.push( softline, @@ -2312,7 +2305,9 @@ function genericPrintNoParens(path, options, print, args) { case "TSDeclareKeyword": return "declare" case "TSModuleBlock": - return concat(path.map(print, "body")) + return path.call(function(bodyPath) { + return printStatementSequence(bodyPath, options, print); + }, "body"); case "TSConstKeyword": return "const"; case "TSAbstractKeyword": @@ -2475,15 +2470,13 @@ function printMethod(path, options, print) { parts.push( key, - path.call(print, "value", "typeParameters"), - group( - concat([ - path.call(function(valuePath) { - return printFunctionParams(valuePath, print, options); - }, "value"), - path.call(p => printReturnType(p, print), "value") - ]) - ) + concat(path.call(valuePath => [ + printFunctionTypeParameters(valuePath, options, print), + group(concat([ + printFunctionParams(valuePath, print, options), + printReturnType(valuePath, print) + ])) + ], "value")) ); if (!node.value.body || node.value.body.length === 0) { @@ -2540,7 +2533,7 @@ function shouldGroupFirstArg(args) { function printArgumentsList(path, options, print) { var printed = path.map(print, "arguments"); - + var n = path.getValue(); if (printed.length === 0) { return concat([ "(", @@ -2627,6 +2620,19 @@ function printArgumentsList(path, options, print) { ); } +function printFunctionTypeParameters(path, options, print) { + const fun = path.getValue(); + // With TypeScript `typeParameters` is an array of `TSTypeParameter` and + // with flow they are one `TypeParameterDeclaration` node. + if (fun.type === "TSFunctionType") { + return printTypeParameters(path, options, print, "typeParameters") + } else if (fun.typeParameters) { + return path.call(print, "typeParameters"); + } else { + return ""; + } +} + function printFunctionParams(path, print, options, expandArg) { var fun = path.getValue(); // namedTypes.Function.assert(fun); @@ -2764,7 +2770,7 @@ function printFunctionDeclaration(path, print, options) { } parts.push( - path.call(print, "typeParameters"), + printFunctionTypeParameters(path, options, print), group( concat([ printFunctionParams(path, print, options), @@ -2802,11 +2808,8 @@ function printObjectMethod(path, options, print) { parts.push(key); } - if (objMethod.typeParameters) { - parts.push(path.call(print, "typeParameters")); - } - parts.push( + printFunctionTypeParameters(path, options, print), group( concat([ printFunctionParams(path, print, options), @@ -3111,7 +3114,10 @@ function printMemberChain(path, options, print) { node: node, printed: comments.printComments( path, - () => printArgumentsList(path, options, print), + () => concat([ + printFunctionTypeParameters(path, options, print), + printArgumentsList(path, options, print) + ]), options ) }); @@ -3138,7 +3144,10 @@ function printMemberChain(path, options, print) { // if handled inside of the recursive call. printedNodes.unshift({ node: path.getValue(), - printed: printArgumentsList(path, options, print) + printed: concat([ + printFunctionTypeParameters(path, options, print), + printArgumentsList(path, options, print) + ]) }); path.call(callee => rec(callee), "callee"); diff --git a/src/typescript-ast-nodes.js b/src/typescript-ast-nodes.js index 4bd882e9..0e141d7c 100644 --- a/src/typescript-ast-nodes.js +++ b/src/typescript-ast-nodes.js @@ -138,12 +138,11 @@ module.exports = function(fork) { def("TSImportEqualsDeclaration") .build("name", "moduleReference") - .field("name", def("Identifier")) - .field("moduleReference", def("TSExternalModuleReference")); - - def("TSImportEqualsDeclaration") .build("expression") - .field("expression", def("Literal")); + .field("name", def("Identifier")) + .field("moduleReference", def("TSExternalModuleReference")) + .field("expression", def("Literal")) + .bases("Declaration"); def("TSInterfaceDeclaration") .build("name", "members") @@ -152,12 +151,12 @@ module.exports = function(fork) { def("TSModuleDeclaration") .build("modifiers", "name", "body") - .bases("Node") + .bases("Declaration") .field("name", or(def("Identifier"), def("Literal"))); def("TSDeclareKeyword").build(); - def("TSModuleBlock").build("body"); + def("TSModuleBlock").build("body").bases("Node"); def("TSAbstractMethodDefinition").build().bases("Node"); @@ -169,7 +168,7 @@ module.exports = function(fork) { .build("expression") .field("expression", def("Identifier")) .bases("Node"); - + def("TSTypeParameter") .build("name") .field("name", def("Identifier")) diff --git a/tests/typescript/conformance/internalModules/importDeclarations/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/internalModules/importDeclarations/__snapshots__/jsfmt.spec.js.snap index 8de3d70a..424e4b67 100644 --- a/tests/typescript/conformance/internalModules/importDeclarations/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/internalModules/importDeclarations/__snapshots__/jsfmt.spec.js.snap @@ -24,6 +24,7 @@ var c = new B.a.C(); module B { export import a = A; + export class D extends a.C { id: number; } @@ -33,7 +34,8 @@ module A { export class C { name: string; - }export import b = B; + } + export import b = B; } var c: { name: string }; @@ -114,9 +116,11 @@ var p: M.D.Point;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ module A { export var x = "hello world"; + export class Point { constructor(public x: number, public y: number) {} - }export module B { + } + export module B { export interface Id { name: string } @@ -136,7 +140,9 @@ module X { export function Y() { return 42; - }export module Y { + } + + export module Y { export class Point { constructor(public x: number, public y: number) {} @@ -157,8 +163,11 @@ module K { export class L { constructor(public name: string) {} - }export module L { - export var y = 12;export interface Point { + } + + export module L { + export var y = 12; + export interface Point { x: number, y: number } @@ -232,6 +241,7 @@ module moduleA { } import alias = moduleA; + var p: alias.Point; var p: moduleA.Point; var p: { x: number, y: number }; @@ -244,10 +254,12 @@ module clodule { export interface Point { x: number, y: number - }var Point: Point = { x: 0, y: 0 }; + } + var Point: Point = { x: 0, y: 0 }; } import clolias = clodule; + var p: clolias.Point; var p: clodule.Point; var p: { x: number, y: number }; @@ -260,10 +272,12 @@ module fundule { export interface Point { x: number, y: number - }var Point: Point = { x: 0, y: 0 }; + } + var Point: Point = { x: 0, y: 0 }; } import funlias = fundule; + var p: funlias.Point; var p: fundule.Point; var p: { x: number, y: number }; @@ -300,14 +314,17 @@ import i = I; var V = 12; import v = V; + class C { name: string; } import c = C; + enum E { Red, Blue } import e = E; + interface I { id: number } @@ -353,14 +370,16 @@ module Z { // all errors imported modules conflict with local variables module A { - export var Point = { x: 0, y: 0 };export interface Point { + export var Point = { x: 0, y: 0 }; + export interface Point { x: number, y: number } } module B { - var A = { x: 0, y: 0 };import Point = A; + var A = { x: 0, y: 0 }; + import Point = A; } module X { @@ -370,13 +389,17 @@ module X { y: number } } + + export class Y { name: string; } } module Z { - import Y = X.Y;var Y = 12; + import Y = X.Y; + + var Y = 12; } `; diff --git a/tests/typescript/custom/call/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/custom/call/__snapshots__/jsfmt.spec.js.snap index 2ce79571..5ff97521 100644 --- a/tests/typescript/custom/call/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/custom/call/__snapshots__/jsfmt.spec.js.snap @@ -7,6 +7,8 @@ interface I { (arg: T); (arg: T): U; } + +Promise.all([]); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ interface I { (), @@ -15,4 +17,6 @@ interface I { (arg: T): U } +Promise.all([]); + `; diff --git a/tests/typescript/custom/call/callSignature.ts b/tests/typescript/custom/call/callSignature.ts index 54e805f9..24f05bf6 100644 --- a/tests/typescript/custom/call/callSignature.ts +++ b/tests/typescript/custom/call/callSignature.ts @@ -4,3 +4,5 @@ interface I { (arg: T); (arg: T): U; } + +Promise.all([]);