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
master
Lucas Azzola 2017-05-06 12:44:26 +10:00 committed by Christopher Chedeau
parent d3d5d57984
commit c689f2a0e7
5 changed files with 97 additions and 60 deletions

View File

@ -343,9 +343,7 @@ function genericPrintNoParens(path, options, print, args) {
parts.push("async "); parts.push("async ");
} }
if (n.typeParameters) { parts.push(printFunctionTypeParameters(path, options, print));
parts.push(path.call(print, "typeParameters"));
}
if (canPrintParamsWithoutParens(n)) { if (canPrintParamsWithoutParens(n)) {
parts.push(path.call(print, "params", 0)); parts.push(path.call(print, "params", 0));
@ -737,7 +735,7 @@ function genericPrintNoParens(path, options, print, args) {
return concat([ return concat([
path.call(print, "callee"), path.call(print, "callee"),
path.call(print, "typeParameters"), printFunctionTypeParameters(path, options, print),
printArgumentsList(path, options, print) printArgumentsList(path, options, print)
]); ]);
} }
@ -1077,15 +1075,13 @@ function genericPrintNoParens(path, options, print, args) {
]) ])
); );
case "NewExpression": case "NewExpression":
parts.push("new ", path.call(print, "callee")); parts.push(
"new ",
path.call(print, "callee"),
printFunctionTypeParameters(path, options, print)
);
if (n.typeParameters) { if (n.arguments) {
parts.push(path.call(print, "typeParameters"));
}
var args = n.arguments;
if (args) {
parts.push(printArgumentsList(path, options, print)); parts.push(printArgumentsList(path, options, print));
} }
@ -1796,17 +1792,10 @@ function genericPrintNoParens(path, options, print, args) {
parts.push("("); parts.push("(");
} }
// With TypeScript `typeParameters` is an array of `TSTypeParameter` and parts.push(
// with flow they are one `TypeParameterDeclaration` node. printFunctionTypeParameters(path, options, print),
if (n.type === 'TSFunctionType' && n.typeParameters) { printFunctionParams(path, print, options)
parts.push( );
printTypeParameters(path, options, print, "typeParameters")
);
} else {
parts.push(path.call(print, "typeParameters"));
}
parts.push(printFunctionParams(path, print, options));
// The returnType is not wrapped in a TypeAnnotation, so the colon // The returnType is not wrapped in a TypeAnnotation, so the colon
// needs to be added separately. // needs to be added separately.
@ -2275,7 +2264,11 @@ function genericPrintNoParens(path, options, print, args) {
return concat(parts); return concat(parts);
case "TSEnumMember": 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": case "TSImportEqualsDeclaration":
parts.push( parts.push(
softline, softline,
@ -2312,7 +2305,9 @@ function genericPrintNoParens(path, options, print, args) {
case "TSDeclareKeyword": case "TSDeclareKeyword":
return "declare" return "declare"
case "TSModuleBlock": case "TSModuleBlock":
return concat(path.map(print, "body")) return path.call(function(bodyPath) {
return printStatementSequence(bodyPath, options, print);
}, "body");
case "TSConstKeyword": case "TSConstKeyword":
return "const"; return "const";
case "TSAbstractKeyword": case "TSAbstractKeyword":
@ -2475,15 +2470,13 @@ function printMethod(path, options, print) {
parts.push( parts.push(
key, key,
path.call(print, "value", "typeParameters"), concat(path.call(valuePath => [
group( printFunctionTypeParameters(valuePath, options, print),
concat([ group(concat([
path.call(function(valuePath) { printFunctionParams(valuePath, print, options),
return printFunctionParams(valuePath, print, options); printReturnType(valuePath, print)
}, "value"), ]))
path.call(p => printReturnType(p, print), "value") ], "value"))
])
)
); );
if (!node.value.body || node.value.body.length === 0) { if (!node.value.body || node.value.body.length === 0) {
@ -2540,7 +2533,7 @@ function shouldGroupFirstArg(args) {
function printArgumentsList(path, options, print) { function printArgumentsList(path, options, print) {
var printed = path.map(print, "arguments"); var printed = path.map(print, "arguments");
var n = path.getValue();
if (printed.length === 0) { if (printed.length === 0) {
return concat([ 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) { function printFunctionParams(path, print, options, expandArg) {
var fun = path.getValue(); var fun = path.getValue();
// namedTypes.Function.assert(fun); // namedTypes.Function.assert(fun);
@ -2764,7 +2770,7 @@ function printFunctionDeclaration(path, print, options) {
} }
parts.push( parts.push(
path.call(print, "typeParameters"), printFunctionTypeParameters(path, options, print),
group( group(
concat([ concat([
printFunctionParams(path, print, options), printFunctionParams(path, print, options),
@ -2802,11 +2808,8 @@ function printObjectMethod(path, options, print) {
parts.push(key); parts.push(key);
} }
if (objMethod.typeParameters) {
parts.push(path.call(print, "typeParameters"));
}
parts.push( parts.push(
printFunctionTypeParameters(path, options, print),
group( group(
concat([ concat([
printFunctionParams(path, print, options), printFunctionParams(path, print, options),
@ -3111,7 +3114,10 @@ function printMemberChain(path, options, print) {
node: node, node: node,
printed: comments.printComments( printed: comments.printComments(
path, path,
() => printArgumentsList(path, options, print), () => concat([
printFunctionTypeParameters(path, options, print),
printArgumentsList(path, options, print)
]),
options options
) )
}); });
@ -3138,7 +3144,10 @@ function printMemberChain(path, options, print) {
// if handled inside of the recursive call. // if handled inside of the recursive call.
printedNodes.unshift({ printedNodes.unshift({
node: path.getValue(), node: path.getValue(),
printed: printArgumentsList(path, options, print) printed: concat([
printFunctionTypeParameters(path, options, print),
printArgumentsList(path, options, print)
])
}); });
path.call(callee => rec(callee), "callee"); path.call(callee => rec(callee), "callee");

View File

@ -138,12 +138,11 @@ module.exports = function(fork) {
def("TSImportEqualsDeclaration") def("TSImportEqualsDeclaration")
.build("name", "moduleReference") .build("name", "moduleReference")
.field("name", def("Identifier"))
.field("moduleReference", def("TSExternalModuleReference"));
def("TSImportEqualsDeclaration")
.build("expression") .build("expression")
.field("expression", def("Literal")); .field("name", def("Identifier"))
.field("moduleReference", def("TSExternalModuleReference"))
.field("expression", def("Literal"))
.bases("Declaration");
def("TSInterfaceDeclaration") def("TSInterfaceDeclaration")
.build("name", "members") .build("name", "members")
@ -152,12 +151,12 @@ module.exports = function(fork) {
def("TSModuleDeclaration") def("TSModuleDeclaration")
.build("modifiers", "name", "body") .build("modifiers", "name", "body")
.bases("Node") .bases("Declaration")
.field("name", or(def("Identifier"), def("Literal"))); .field("name", or(def("Identifier"), def("Literal")));
def("TSDeclareKeyword").build(); def("TSDeclareKeyword").build();
def("TSModuleBlock").build("body"); def("TSModuleBlock").build("body").bases("Node");
def("TSAbstractMethodDefinition").build().bases("Node"); def("TSAbstractMethodDefinition").build().bases("Node");
@ -169,7 +168,7 @@ module.exports = function(fork) {
.build("expression") .build("expression")
.field("expression", def("Identifier")) .field("expression", def("Identifier"))
.bases("Node"); .bases("Node");
def("TSTypeParameter") def("TSTypeParameter")
.build("name") .build("name")
.field("name", def("Identifier")) .field("name", def("Identifier"))

View File

@ -24,6 +24,7 @@ var c = new B.a.C();
module B { module B {
export import a = A; export import a = A;
export class D extends a.C { export class D extends a.C {
id: number; id: number;
} }
@ -33,7 +34,8 @@ module A {
export class C { export class C {
name: string; name: string;
}export import b = B; }
export import b = B;
} }
var c: { name: string }; var c: { name: string };
@ -114,9 +116,11 @@ var p: M.D.Point;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
module A { module A {
export var x = "hello world"; export var x = "hello world";
export class Point { export class Point {
constructor(public x: number, public y: number) {} constructor(public x: number, public y: number) {}
}export module B { }
export module B {
export interface Id { export interface Id {
name: string name: string
} }
@ -136,7 +140,9 @@ module X {
export function Y() { export function Y() {
return 42; return 42;
}export module Y { }
export module Y {
export class Point { export class Point {
constructor(public x: number, public y: number) {} constructor(public x: number, public y: number) {}
@ -157,8 +163,11 @@ module K {
export class L { export class L {
constructor(public name: string) {} 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, x: number,
y: number y: number
} }
@ -232,6 +241,7 @@ module moduleA {
} }
import alias = moduleA; import alias = moduleA;
var p: alias.Point; var p: alias.Point;
var p: moduleA.Point; var p: moduleA.Point;
var p: { x: number, y: number }; var p: { x: number, y: number };
@ -244,10 +254,12 @@ module clodule {
export interface Point { export interface Point {
x: number, x: number,
y: number y: number
}var Point: Point = { x: 0, y: 0 }; }
var Point: Point = { x: 0, y: 0 };
} }
import clolias = clodule; import clolias = clodule;
var p: clolias.Point; var p: clolias.Point;
var p: clodule.Point; var p: clodule.Point;
var p: { x: number, y: number }; var p: { x: number, y: number };
@ -260,10 +272,12 @@ module fundule {
export interface Point { export interface Point {
x: number, x: number,
y: number y: number
}var Point: Point = { x: 0, y: 0 }; }
var Point: Point = { x: 0, y: 0 };
} }
import funlias = fundule; import funlias = fundule;
var p: funlias.Point; var p: funlias.Point;
var p: fundule.Point; var p: fundule.Point;
var p: { x: number, y: number }; var p: { x: number, y: number };
@ -300,14 +314,17 @@ import i = I;
var V = 12; var V = 12;
import v = V; import v = V;
class C { class C {
name: string; name: string;
} }
import c = C; import c = C;
enum E { Red, Blue } enum E { Red, Blue }
import e = E; import e = E;
interface I { interface I {
id: number id: number
} }
@ -353,14 +370,16 @@ module Z {
// all errors imported modules conflict with local variables // all errors imported modules conflict with local variables
module A { module A {
export var Point = { x: 0, y: 0 };export interface Point { export var Point = { x: 0, y: 0 };
export interface Point {
x: number, x: number,
y: number y: number
} }
} }
module B { module B {
var A = { x: 0, y: 0 };import Point = A; var A = { x: 0, y: 0 };
import Point = A;
} }
module X { module X {
@ -370,13 +389,17 @@ module X {
y: number y: number
} }
} }
export class Y { export class Y {
name: string; name: string;
} }
} }
module Z { module Z {
import Y = X.Y;var Y = 12; import Y = X.Y;
var Y = 12;
} }
`; `;

View File

@ -7,6 +7,8 @@ interface I {
<T, U>(arg: T); <T, U>(arg: T);
<T, U>(arg: T): U; <T, U>(arg: T): U;
} }
Promise.all<void>([]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I { interface I {
(), (),
@ -15,4 +17,6 @@ interface I {
<T, U>(arg: T): U <T, U>(arg: T): U
} }
Promise.all<void>([]);
`; `;

View File

@ -4,3 +4,5 @@ interface I {
<T, U>(arg: T); <T, U>(arg: T);
<T, U>(arg: T): U; <T, U>(arg: T): U;
} }
Promise.all<void>([]);