TypeScript: correctly print nested namespaces (#1522)

* fix(typescript): correctly print nested namespaces

* test(typescript): update snapshots
master
Lucas Azzola 2017-05-06 14:14:07 +10:00 committed by Christopher Chedeau
parent b23c16d05d
commit b9a7549e47
7 changed files with 94 additions and 29 deletions

View File

@ -2290,15 +2290,30 @@ function genericPrintNoParens(path, options, print, args) {
")"
])
case "TSModuleDeclaration":
parts.push(
printTypeScriptModifiers(path, options, print),
"module ",
path.call(print, "name"),
" {",
indent(concat([line, group(path.call(print, "body"))])),
line,
"}"
);
var isExternalModule = namedTypes.Literal.check(n.name);
var parentIsDeclaration = path.getParentNode().type === "TSModuleDeclaration";
var bodyIsDeclaration = n.body.type === "TSModuleDeclaration";
if (parentIsDeclaration) {
parts.push(".");
} else {
parts.push(
printTypeScriptModifiers(path, options, print),
isExternalModule? "module " : "namespace "
);
}
parts.push(path.call(print, "name"));
if (bodyIsDeclaration) {
parts.push(path.call(print, "body"));
} else {
parts.push(
" {",
indent(concat([line, group(path.call(print, "body"))])),
line,
"}"
)
};
return concat(parts);
case "TSDeclareKeyword":

View File

@ -22,7 +22,7 @@ var c = new B.a.C();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// expected no error
module B {
namespace B {
export import a = A;
export class D extends a.C {
@ -30,7 +30,7 @@ module B {
}
}
module A {
namespace A {
export class C {
name: string;
@ -114,20 +114,20 @@ var p: { x: number; y: number; }
var p: M.D.Point;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// expect no errors here
module A {
namespace A {
export var x = "hello world";
export class Point {
constructor(public x: number, public y: number) {}
}
export module B {
export namespace B {
export interface Id {
name: string
}
}
}
module C {
namespace C {
export import a = A;
}
@ -136,13 +136,13 @@ var b: { x: number, y: number } = new C.a.Point(0, 0);
var c: { name: string };
var c: undefined.Id;
module X {
namespace X {
export function Y() {
return 42;
}
export module Y {
export namespace Y {
export class Point {
constructor(public x: number, public y: number) {}
@ -150,7 +150,7 @@ module X {
}
}
module Z {
namespace Z {
// 'y' should be a fundule here
export import y = X.Y;
@ -159,13 +159,13 @@ module Z {
var m: number = Z.y();
var n: { x: number, y: number } = new Z.y.Point(0, 0);
module K {
namespace K {
export class L {
constructor(public name: string) {}
}
export module L {
export namespace L {
export var y = 12;
export interface Point {
x: number,
@ -174,7 +174,7 @@ module K {
}
}
module M {
namespace M {
export import D = K.L;
}
@ -233,7 +233,7 @@ import funlias = fundule;
var p: funlias.Point;
var p: fundule.Point;
var p: { x: number; y: number; };~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
module moduleA {
namespace moduleA {
export class Point {
constructor(public x: number, public y: number) {}
@ -250,7 +250,7 @@ class clodule {
name: string;
}
module clodule {
namespace clodule {
export interface Point {
x: number,
y: number
@ -268,7 +268,7 @@ function fundule() {
return { x: 0, y: 0 };
}
module fundule {
namespace fundule {
export interface Point {
x: number,
y: number
@ -369,7 +369,7 @@ module Z {
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// all errors imported modules conflict with local variables
module A {
namespace A {
export var Point = { x: 0, y: 0 };
export interface Point {
x: number,
@ -377,13 +377,13 @@ module A {
}
}
module B {
namespace B {
var A = { x: 0, y: 0 };
import Point = A;
}
module X {
export module Y {
namespace X {
export namespace Y {
export interface Point {
x: number,
y: number
@ -396,7 +396,7 @@ module X {
}
}
module Z {
namespace Z {
import Y = X.Y;
var Y = 12;

View File

@ -11,7 +11,7 @@ declare module "B" {
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
module A {
namespace A {
export class A {}
}

View File

@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`moduleNamespace.ts 1`] = `
declare module "f" {}
namespace f {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare module "f" {
}
namespace f {
}
`;
exports[`nestedNamespace.ts 1`] = `
namespace X {
export namespace Y {
}
}
namespace X.Y {
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace X {
export namespace Y { }
}
namespace X.Y {
}
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "typescript" });

View File

@ -0,0 +1,3 @@
declare module "f" {}
namespace f {}

View File

@ -0,0 +1,9 @@
namespace X {
export namespace Y {
}
}
namespace X.Y {
}