fix(typescript): do not change `module` into `namespace` and break/hug their body correctly (#5551)

master
Ika 2018-11-26 13:34:35 +08:00 committed by GitHub
parent 99a3efaa7a
commit 3a5bbf5fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 155 additions and 86 deletions

View File

@ -1046,6 +1046,7 @@ function printPathNoParens(path, options, print, args) {
case "Import": case "Import":
return "import"; return "import";
case "TSModuleBlock":
case "BlockStatement": { case "BlockStatement": {
const naked = path.call(bodyPath => { const naked = path.call(bodyPath => {
return printStatementSequence(bodyPath, options, print); return printStatementSequence(bodyPath, options, print);
@ -1069,7 +1070,8 @@ function printPathNoParens(path, options, print, args) {
parent.type === "WhileStatement" || parent.type === "WhileStatement" ||
parent.type === "DoWhileStatement" || parent.type === "DoWhileStatement" ||
parent.type === "DoExpression" || parent.type === "DoExpression" ||
(parent.type === "CatchClause" && !parentParent.finalizer)) (parent.type === "CatchClause" && !parentParent.finalizer) ||
parent.type === "TSModuleDeclaration")
) { ) {
return "{}"; return "{}";
} }
@ -3400,7 +3402,8 @@ function printPathNoParens(path, options, print, args) {
if (!isGlobalDeclaration) { if (!isGlobalDeclaration) {
parts.push( parts.push(
isExternalModule || /\smodule\s/.test(textBetweenNodeAndItsId) isExternalModule ||
/(^|\s)module(\s|$)/.test(textBetweenNodeAndItsId)
? "module " ? "module "
: "namespace " : "namespace "
); );
@ -3412,32 +3415,13 @@ function printPathNoParens(path, options, print, args) {
if (bodyIsDeclaration) { if (bodyIsDeclaration) {
parts.push(path.call(print, "body")); parts.push(path.call(print, "body"));
} else if (n.body) { } else if (n.body) {
parts.push( parts.push(" ", group(path.call(print, "body")));
" {",
indent(
concat([
line,
path.call(
bodyPath =>
comments.printDanglingComments(bodyPath, options, true),
"body"
),
group(path.call(print, "body"))
])
),
line,
"}"
);
} else { } else {
parts.push(semi); parts.push(semi);
} }
return concat(parts); return concat(parts);
} }
case "TSModuleBlock":
return path.call(bodyPath => {
return printStatementSequence(bodyPath, options, print);
}, "body");
case "PrivateName": case "PrivateName":
return concat(["#", path.call(print, "id")]); return concat(["#", path.call(print, "id")]);

View File

@ -307,19 +307,15 @@ module T.U { // This needs to be emitted
} }
=====================================output===================================== =====================================output=====================================
// @declaration: true // @declaration: true
namespace M { module M {
namespace P.Q { module P.Q {} // This shouldnt be emitted
} // This shouldnt be emitted
} }
namespace M { module M {
export namespace R.S { export module R.S {} //This should be emitted
} //This should be emitted
} }
namespace T.U { module T.U {
// This needs to be emitted // This needs to be emitted
} }
@ -550,9 +546,7 @@ function b() {
class global {} class global {}
} }
namespace global { namespace global {}
}
function foo(global: number) {} function foo(global: number) {}
@ -816,8 +810,8 @@ module m2 {
} }
=====================================output===================================== =====================================output=====================================
//@declaration: true //@declaration: true
namespace m1 { module m1 {
export namespace m1_M1_public { export module m1_M1_public {
export class c1 {} export class c1 {}
export function f1() { export function f1() {
return new c1(); return new c1();
@ -826,7 +820,7 @@ namespace m1 {
export var v2: c1; export var v2: c1;
} }
namespace m1_M2_private { module m1_M2_private {
export class c1 {} export class c1 {}
export function f1() { export function f1() {
return new c1(); return new c1();
@ -897,7 +891,7 @@ namespace m1 {
//export import m1_im4_public = require("m1_M4_private"); //export import m1_im4_public = require("m1_M4_private");
} }
namespace glo_M1_public { module glo_M1_public {
export class c1 {} export class c1 {}
export function f1() { export function f1() {
return new c1(); return new c1();
@ -930,11 +924,11 @@ declare module "use_glo_M1_public" {
var use_glo_M2_public_v2_private: typeof use_glo_M2_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; var use_glo_M2_public_v3_private: () => use_glo_M2_public.c1;
namespace m2 { module m2 {
//import errorImport = require("glo_M2_public"); //import errorImport = require("glo_M2_public");
import nonerrorImport = glo_M1_public; import nonerrorImport = glo_M1_public;
namespace m5 { module m5 {
//import m5_errorImport = require("glo_M2_public"); //import m5_errorImport = require("glo_M2_public");
import m5_nonerrorImport = glo_M1_public; import m5_nonerrorImport = glo_M1_public;
} }
@ -942,12 +936,12 @@ declare module "use_glo_M1_public" {
} }
declare module "anotherParseError" { declare module "anotherParseError" {
namespace m2 { module m2 {
//declare module "abc" { //declare module "abc" {
//} //}
} }
namespace m2 { module m2 {
//module "abc2" { //module "abc2" {
//} //}
} }
@ -955,9 +949,9 @@ declare module "anotherParseError" {
//} //}
} }
namespace m2 { module m2 {
//import m3 = require("use_glo_M1_public"); //import m3 = require("use_glo_M1_public");
namespace m4 { module m4 {
var a = 10; var a = 10;
//import m2 = require("use_glo_M1_public"); //import m2 = require("use_glo_M1_public");
} }

View File

@ -28,14 +28,14 @@ var c = new B.a.C();
=====================================output===================================== =====================================output=====================================
// expected no error // expected no error
namespace 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;
} }
} }
namespace A { module A {
export class C { export class C {
name: string; name: string;
} }
@ -125,19 +125,19 @@ var p: M.D.Point;
=====================================output===================================== =====================================output=====================================
// expect no errors here // expect no errors here
namespace 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 namespace B { export module B {
export interface Id { export interface Id {
name: string; name: string;
} }
} }
} }
namespace C { module C {
export import a = A; export import a = A;
} }
@ -146,19 +146,19 @@ var b: { x: number; y: number } = new C.a.Point(0, 0);
var c: { name: string }; var c: { name: string };
var c: C.a.B.Id; var c: C.a.B.Id;
namespace X { module X {
export function Y() { export function Y() {
return 42; return 42;
} }
export namespace Y { export module Y {
export class Point { export class Point {
constructor(public x: number, public y: number) {} constructor(public x: number, public y: number) {}
} }
} }
} }
namespace Z { module Z {
// 'y' should be a fundule here // 'y' should be a fundule here
export import y = X.Y; export import y = X.Y;
} }
@ -166,12 +166,12 @@ namespace Z {
var m: number = Z.y(); var m: number = Z.y();
var n: { x: number; y: number } = new Z.y.Point(0, 0); var n: { x: number; y: number } = new Z.y.Point(0, 0);
namespace K { module K {
export class L { export class L {
constructor(public name: string) {} constructor(public name: string) {}
} }
export namespace L { export module L {
export var y = 12; export var y = 12;
export interface Point { export interface Point {
x: number; x: number;
@ -180,7 +180,7 @@ namespace K {
} }
} }
namespace M { module M {
export import D = K.L; export import D = K.L;
} }
@ -259,7 +259,7 @@ var p: funlias.Point;
var p: fundule.Point; var p: fundule.Point;
var p: { x: number; y: number; }; var p: { x: number; y: number; };
=====================================output===================================== =====================================output=====================================
namespace moduleA { module moduleA {
export class Point { export class Point {
constructor(public x: number, public y: number) {} constructor(public x: number, public y: number) {}
} }
@ -275,7 +275,7 @@ class clodule {
name: string; name: string;
} }
namespace clodule { module clodule {
export interface Point { export interface Point {
x: number; x: number;
y: number; y: number;
@ -293,7 +293,7 @@ function fundule() {
return { x: 0, y: 0 }; return { x: 0, y: 0 };
} }
namespace fundule { module fundule {
export interface Point { export interface Point {
x: number; x: number;
y: number; y: number;
@ -411,7 +411,7 @@ module Z {
=====================================output===================================== =====================================output=====================================
// all errors imported modules conflict with local variables // all errors imported modules conflict with local variables
namespace A { module A {
export var Point = { x: 0, y: 0 }; export var Point = { x: 0, y: 0 };
export interface Point { export interface Point {
x: number; x: number;
@ -419,13 +419,13 @@ namespace A {
} }
} }
namespace B { module B {
var A = { x: 0, y: 0 }; var A = { x: 0, y: 0 };
import Point = A; import Point = A;
} }
namespace X { module X {
export namespace Y { export module Y {
export interface Point { export interface Point {
x: number; x: number;
y: number; y: number;
@ -437,7 +437,7 @@ namespace X {
} }
} }
namespace Z { module Z {
import Y = X.Y; import Y = X.Y;
var Y = 12; var Y = 12;

View File

@ -17,7 +17,7 @@ declare module "B" {
} }
=====================================output===================================== =====================================output=====================================
namespace A { module A {
export class A {} export class A {}
} }

View File

@ -33,13 +33,9 @@ declare module "f" {}
namespace f {} namespace f {}
=====================================output===================================== =====================================output=====================================
declare module "f" { declare module "f" {}
} namespace f {}
namespace f {
}
================================================================================ ================================================================================
`; `;
@ -62,12 +58,10 @@ namespace X.Y {
=====================================output===================================== =====================================output=====================================
namespace X { namespace X {
export namespace Y { } export namespace Y {}
} }
namespace X.Y { namespace X.Y {}
}
================================================================================ ================================================================================
`; `;

View File

@ -30,7 +30,7 @@ module m2 {
} }
=====================================output===================================== =====================================output=====================================
namespace m2 { module m2 {
function fn() { function fn() {
return 1; return 1;
} }
@ -42,7 +42,7 @@ namespace m2 {
} }
} }
namespace m2 { module m2 {
export function exports() { export function exports() {
return 1; return 1;
} }

View File

@ -48,7 +48,7 @@ module YYY4 {
=====================================output===================================== =====================================output=====================================
// All of these should be an error // All of these should be an error
namespace Y3 { module Y3 {
public module Module { public module Module {
class A { class A {
s: string; s: string;
@ -63,14 +63,14 @@ namespace Y3 {
} }
} }
namespace Y4 { module Y4 {
public enum Color { public enum Color {
Blue, Blue,
Red Red
} }
} }
namespace YY3 { module YY3 {
private module Module { private module Module {
class A { class A {
s: string; s: string;
@ -78,14 +78,14 @@ namespace YY3 {
} }
} }
namespace YY4 { module YY4 {
private enum Color { private enum Color {
Blue, Blue,
Red Red
} }
} }
namespace YYY3 { module YYY3 {
static module Module { static module Module {
class A { class A {
s: string; s: string;
@ -93,7 +93,7 @@ namespace YYY3 {
} }
} }
namespace YYY4 { module YYY4 {
static enum Color { static enum Color {
Blue, Blue,
Red Red

View File

@ -26,17 +26,83 @@ global {}
declare global {} declare global {}
=====================================output===================================== =====================================output=====================================
namespace global { namespace global {}
module global {}
global {}
declare global {}
================================================================================
`;
exports[`keyword.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
module X {}
module X {
const x = 1;
} }
namespace global {
module X {
module X {}
} }
global {
module X {
module X {
const x = 1;
}
} }
declare global {
namespace X {}
namespace X {
const x = 1;
}
namespace X {
namespace X {}
}
namespace X {
namespace X {
const x = 1;
}
}
=====================================output=====================================
module X {}
module X {
const x = 1;
}
module X {
module X {}
}
module X {
module X {
const x = 1;
}
}
namespace X {}
namespace X {
const x = 1;
}
namespace X {
namespace X {}
}
namespace X {
namespace X {
const x = 1;
}
} }
================================================================================ ================================================================================

View File

@ -0,0 +1,31 @@
module X {}
module X {
const x = 1;
}
module X {
module X {}
}
module X {
module X {
const x = 1;
}
}
namespace X {}
namespace X {
const x = 1;
}
namespace X {
namespace X {}
}
namespace X {
namespace X {
const x = 1;
}
}