Skip to content

Commit

Permalink
fix(typescript): do not change module into namespace and break/hu…
Browse files Browse the repository at this point in the history
…g their body correctly (#5551)
  • Loading branch information
ikatyang committed Nov 26, 2018
1 parent 99a3efa commit 3a5bbf5
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 86 deletions.
28 changes: 6 additions & 22 deletions src/language-js/printer-estree.js
Expand Up @@ -1046,6 +1046,7 @@ function printPathNoParens(path, options, print, args) {

case "Import":
return "import";
case "TSModuleBlock":
case "BlockStatement": {
const naked = path.call(bodyPath => {
return printStatementSequence(bodyPath, options, print);
Expand All @@ -1069,7 +1070,8 @@ function printPathNoParens(path, options, print, args) {
parent.type === "WhileStatement" ||
parent.type === "DoWhileStatement" ||
parent.type === "DoExpression" ||
(parent.type === "CatchClause" && !parentParent.finalizer))
(parent.type === "CatchClause" && !parentParent.finalizer) ||
parent.type === "TSModuleDeclaration")
) {
return "{}";
}
Expand Down Expand Up @@ -3400,7 +3402,8 @@ function printPathNoParens(path, options, print, args) {

if (!isGlobalDeclaration) {
parts.push(
isExternalModule || /\smodule\s/.test(textBetweenNodeAndItsId)
isExternalModule ||
/(^|\s)module(\s|$)/.test(textBetweenNodeAndItsId)
? "module "
: "namespace "
);
Expand All @@ -3412,32 +3415,13 @@ function printPathNoParens(path, options, print, args) {
if (bodyIsDeclaration) {
parts.push(path.call(print, "body"));
} else if (n.body) {
parts.push(
" {",
indent(
concat([
line,
path.call(
bodyPath =>
comments.printDanglingComments(bodyPath, options, true),
"body"
),
group(path.call(print, "body"))
])
),
line,
"}"
);
parts.push(" ", group(path.call(print, "body")));
} else {
parts.push(semi);
}

return concat(parts);
}
case "TSModuleBlock":
return path.call(bodyPath => {
return printStatementSequence(bodyPath, options, print);
}, "body");

case "PrivateName":
return concat(["#", path.call(print, "id")]);
Expand Down
38 changes: 16 additions & 22 deletions tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -307,19 +307,15 @@ module T.U { // This needs to be emitted
}
=====================================output=====================================
// @declaration: true
namespace M {
namespace P.Q {
} // This shouldnt be emitted
module M {
module P.Q {} // This shouldnt be emitted
}
namespace M {
export namespace R.S {
} //This should be emitted
module M {
export module R.S {} //This should be emitted
}
namespace T.U {
module T.U {
// This needs to be emitted
}
Expand Down Expand Up @@ -550,9 +546,7 @@ function b() {
class global {}
}
namespace global {
}
namespace global {}
function foo(global: number) {}
Expand Down Expand Up @@ -816,8 +810,8 @@ module m2 {
}
=====================================output=====================================
//@declaration: true
namespace m1 {
export namespace m1_M1_public {
module m1 {
export module m1_M1_public {
export class c1 {}
export function f1() {
return new c1();
Expand All @@ -826,7 +820,7 @@ namespace m1 {
export var v2: c1;
}
namespace m1_M2_private {
module m1_M2_private {
export class c1 {}
export function f1() {
return new c1();
Expand Down Expand Up @@ -897,7 +891,7 @@ namespace m1 {
//export import m1_im4_public = require("m1_M4_private");
}
namespace glo_M1_public {
module glo_M1_public {
export class c1 {}
export function f1() {
return new c1();
Expand Down Expand Up @@ -930,34 +924,34 @@ declare module "use_glo_M1_public" {
var use_glo_M2_public_v2_private: typeof use_glo_M2_public;
var use_glo_M2_public_v3_private: () => use_glo_M2_public.c1;
namespace m2 {
module m2 {
//import errorImport = require("glo_M2_public");
import nonerrorImport = glo_M1_public;
namespace m5 {
module m5 {
//import m5_errorImport = require("glo_M2_public");
import m5_nonerrorImport = glo_M1_public;
}
}
}
declare module "anotherParseError" {
namespace m2 {
module m2 {
//declare module "abc" {
//}
}
namespace m2 {
module m2 {
//module "abc2" {
//}
}
//module "abc3" {
//}
}
namespace m2 {
module m2 {
//import m3 = require("use_glo_M1_public");
namespace m4 {
module m4 {
var a = 10;
//import m2 = require("use_glo_M1_public");
}
Expand Down
Expand Up @@ -28,14 +28,14 @@ var c = new B.a.C();
=====================================output=====================================
// expected no error
namespace B {
module B {
export import a = A;
export class D extends a.C {
id: number;
}
}
namespace A {
module A {
export class C {
name: string;
}
Expand Down Expand Up @@ -125,19 +125,19 @@ var p: M.D.Point;
=====================================output=====================================
// expect no errors here
namespace A {
module A {
export var x = "hello world";
export class Point {
constructor(public x: number, public y: number) {}
}
export namespace B {
export module B {
export interface Id {
name: string;
}
}
}
namespace C {
module C {
export import a = A;
}
Expand All @@ -146,32 +146,32 @@ var b: { x: number; y: number } = new C.a.Point(0, 0);
var c: { name: string };
var c: C.a.B.Id;
namespace X {
module X {
export function Y() {
return 42;
}
export namespace Y {
export module Y {
export class Point {
constructor(public x: number, public y: number) {}
}
}
}
namespace Z {
module Z {
// 'y' should be a fundule here
export import y = X.Y;
}
var m: number = Z.y();
var n: { x: number; y: number } = new Z.y.Point(0, 0);
namespace K {
module K {
export class L {
constructor(public name: string) {}
}
export namespace L {
export module L {
export var y = 12;
export interface Point {
x: number;
Expand All @@ -180,7 +180,7 @@ namespace K {
}
}
namespace M {
module M {
export import D = K.L;
}
Expand Down Expand Up @@ -259,7 +259,7 @@ var p: funlias.Point;
var p: fundule.Point;
var p: { x: number; y: number; };
=====================================output=====================================
namespace moduleA {
module moduleA {
export class Point {
constructor(public x: number, public y: number) {}
}
Expand All @@ -275,7 +275,7 @@ class clodule {
name: string;
}
namespace clodule {
module clodule {
export interface Point {
x: number;
y: number;
Expand All @@ -293,7 +293,7 @@ function fundule() {
return { x: 0, y: 0 };
}
namespace fundule {
module fundule {
export interface Point {
x: number;
y: number;
Expand Down Expand Up @@ -411,21 +411,21 @@ module Z {
=====================================output=====================================
// all errors imported modules conflict with local variables
namespace A {
module A {
export var Point = { x: 0, y: 0 };
export interface Point {
x: number;
y: number;
}
}
namespace B {
module B {
var A = { x: 0, y: 0 };
import Point = A;
}
namespace X {
export namespace Y {
module X {
export module Y {
export interface Point {
x: number;
y: number;
Expand All @@ -437,7 +437,7 @@ namespace X {
}
}
namespace Z {
module Z {
import Y = X.Y;
var Y = 12;
Expand Down
Expand Up @@ -17,7 +17,7 @@ declare module "B" {
}
=====================================output=====================================
namespace A {
module A {
export class A {}
}
Expand Down
14 changes: 4 additions & 10 deletions tests/typescript/custom/module/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -33,13 +33,9 @@ declare module "f" {}
namespace f {}
=====================================output=====================================
declare module "f" {
}
namespace f {
declare module "f" {}
}
namespace f {}
================================================================================
`;
Expand All @@ -62,12 +58,10 @@ namespace X.Y {
=====================================output=====================================
namespace X {
export namespace Y { }
export namespace Y {}
}
namespace X.Y {
}
namespace X.Y {}
================================================================================
`;
Expand Up @@ -30,7 +30,7 @@ module m2 {
}
=====================================output=====================================
namespace m2 {
module m2 {
function fn() {
return 1;
}
Expand All @@ -42,7 +42,7 @@ namespace m2 {
}
}
namespace m2 {
module m2 {
export function exports() {
return 1;
}
Expand Down

0 comments on commit 3a5bbf5

Please sign in to comment.