From c354d74600a36b1da071302dacc095e6f7f750a7 Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 25 Nov 2018 21:19:36 +0800 Subject: [PATCH 1/3] test: add tests --- .../__snapshots__/jsfmt.spec.js.snap | 74 +++++++++++++++++++ tests/typescript_module/keyword.ts | 31 ++++++++ 2 files changed, 105 insertions(+) create mode 100644 tests/typescript_module/keyword.ts diff --git a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap index 8e265f74c0b6..7d70725216d2 100644 --- a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap @@ -42,6 +42,80 @@ declare global { ================================================================================ `; +exports[`keyword.ts 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +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; + } +} + +=====================================output===================================== +namespace X { + +} + +namespace X { + const x = 1; +} + +namespace X { + namespace X { } +} + +namespace X { + namespace X { const x = 1; } +} + +namespace X { + +} + +namespace X { + const x = 1; +} + +namespace X { + namespace X { } +} + +namespace X { + namespace X { const x = 1; } +} + +================================================================================ +`; + exports[`namespace_function.ts 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript_module/keyword.ts b/tests/typescript_module/keyword.ts new file mode 100644 index 000000000000..d533a618796e --- /dev/null +++ b/tests/typescript_module/keyword.ts @@ -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; + } +} From 800fa1c21f1b9f6fe17a44f2d937c10797f3d2ab Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 25 Nov 2018 21:37:20 +0800 Subject: [PATCH 2/3] fix(typescript): print module/namespace correctly --- src/language-js/printer-estree.js | 3 +- .../compiler/__snapshots__/jsfmt.spec.js.snap | 30 +++++++-------- .../__snapshots__/jsfmt.spec.js.snap | 38 +++++++++---------- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 12 +++--- .../__snapshots__/jsfmt.spec.js.snap | 14 +++---- 7 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 72b654e8671c..926d0492f617 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3400,7 +3400,8 @@ function printPathNoParens(path, options, print, args) { if (!isGlobalDeclaration) { parts.push( - isExternalModule || /\smodule\s/.test(textBetweenNodeAndItsId) + isExternalModule || + /(^|\s)module(\s|$)/.test(textBetweenNodeAndItsId) ? "module " : "namespace " ); diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap index 323078823ee0..63b724b46b59 100644 --- a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -307,19 +307,19 @@ module T.U { // This needs to be emitted } =====================================output===================================== // @declaration: true -namespace M { - namespace P.Q { +module M { + module P.Q { } // This shouldnt be emitted } -namespace M { - export namespace R.S { +module M { + export module R.S { } //This should be emitted } -namespace T.U { +module T.U { // This needs to be emitted } @@ -816,8 +816,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(); @@ -826,7 +826,7 @@ namespace m1 { export var v2: c1; } - namespace m1_M2_private { + module m1_M2_private { export class c1 {} export function f1() { return new c1(); @@ -897,7 +897,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(); @@ -930,11 +930,11 @@ 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; } @@ -942,12 +942,12 @@ declare module "use_glo_M1_public" { } declare module "anotherParseError" { - namespace m2 { + module m2 { //declare module "abc" { //} } - namespace m2 { + module m2 { //module "abc2" { //} } @@ -955,9 +955,9 @@ declare module "anotherParseError" { //} } -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"); } 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 da66b7a51533..9f1f3f6d1299 100644 --- a/tests/typescript/conformance/internalModules/importDeclarations/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/internalModules/importDeclarations/__snapshots__/jsfmt.spec.js.snap @@ -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; } @@ -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; } @@ -146,19 +146,19 @@ 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; } @@ -166,12 +166,12 @@ namespace Z { 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; @@ -180,7 +180,7 @@ namespace K { } } -namespace M { +module M { export import D = K.L; } @@ -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) {} } @@ -275,7 +275,7 @@ class clodule { name: string; } -namespace clodule { +module clodule { export interface Point { x: number; y: number; @@ -293,7 +293,7 @@ function fundule() { return { x: 0, y: 0 }; } -namespace fundule { +module fundule { export interface Point { x: number; y: number; @@ -411,7 +411,7 @@ 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; @@ -419,13 +419,13 @@ namespace A { } } -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; @@ -437,7 +437,7 @@ namespace X { } } -namespace Z { +module Z { import Y = X.Y; var Y = 12; diff --git a/tests/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap index ef95c2d61cf4..4c825d8fafc3 100644 --- a/tests/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap @@ -17,7 +17,7 @@ declare module "B" { } =====================================output===================================== -namespace A { +module A { export class A {} } diff --git a/tests/typescript/custom/stability/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/custom/stability/__snapshots__/jsfmt.spec.js.snap index 389ba0ac5616..068d63a1e770 100644 --- a/tests/typescript/custom/stability/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/custom/stability/__snapshots__/jsfmt.spec.js.snap @@ -30,7 +30,7 @@ module m2 { } =====================================output===================================== -namespace m2 { +module m2 { function fn() { return 1; } @@ -42,7 +42,7 @@ namespace m2 { } } -namespace m2 { +module m2 { export function exports() { return 1; } diff --git a/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap index c9fe350c7b45..9587a45b1fa5 100644 --- a/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap @@ -48,7 +48,7 @@ module YYY4 { =====================================output===================================== // All of these should be an error -namespace Y3 { +module Y3 { public module Module { class A { s: string; @@ -63,14 +63,14 @@ namespace Y3 { } } -namespace Y4 { +module Y4 { public enum Color { Blue, Red } } -namespace YY3 { +module YY3 { private module Module { class A { s: string; @@ -78,14 +78,14 @@ namespace YY3 { } } -namespace YY4 { +module YY4 { private enum Color { Blue, Red } } -namespace YYY3 { +module YYY3 { static module Module { class A { s: string; @@ -93,7 +93,7 @@ namespace YYY3 { } } -namespace YYY4 { +module YYY4 { static enum Color { Blue, Red diff --git a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap index 7d70725216d2..df3087471f75 100644 --- a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap @@ -29,7 +29,7 @@ declare global {} namespace global { } -namespace global { +module global { } global { @@ -81,20 +81,20 @@ namespace X { } =====================================output===================================== -namespace X { +module X { } -namespace X { +module X { const x = 1; } -namespace X { - namespace X { } +module X { + module X { } } -namespace X { - namespace X { const x = 1; } +module X { + module X { const x = 1; } } namespace X { From dc11741b08b7c7e8550396d59a7e21dd69280201 Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 25 Nov 2018 21:38:50 +0800 Subject: [PATCH 3/3] fix(typescript): break/hug namespace/module body correctly --- src/language-js/printer-estree.js | 25 +++---------- .../compiler/__snapshots__/jsfmt.spec.js.snap | 12 ++----- .../module/__snapshots__/jsfmt.spec.js.snap | 14 +++----- .../__snapshots__/jsfmt.spec.js.snap | 36 ++++++++----------- 4 files changed, 25 insertions(+), 62 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 926d0492f617..e70e0c0efa01 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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); @@ -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 "{}"; } @@ -3413,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")]); diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap index 63b724b46b59..fd08d6576331 100644 --- a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -308,15 +308,11 @@ module T.U { // This needs to be emitted =====================================output===================================== // @declaration: true module M { - module P.Q { - - } // This shouldnt be emitted + module P.Q {} // This shouldnt be emitted } module M { - export module R.S { - - } //This should be emitted + export module R.S {} //This should be emitted } module T.U { @@ -550,9 +546,7 @@ function b() { class global {} } -namespace global { - -} +namespace global {} function foo(global: number) {} diff --git a/tests/typescript/custom/module/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/custom/module/__snapshots__/jsfmt.spec.js.snap index 1a4f917b24c5..c68beb942a72 100644 --- a/tests/typescript/custom/module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/custom/module/__snapshots__/jsfmt.spec.js.snap @@ -33,13 +33,9 @@ declare module "f" {} namespace f {} =====================================output===================================== -declare module "f" { - -} - -namespace f { +declare module "f" {} -} +namespace f {} ================================================================================ `; @@ -62,12 +58,10 @@ namespace X.Y { =====================================output===================================== namespace X { - export namespace Y { } + export namespace Y {} } -namespace X.Y { - -} +namespace X.Y {} ================================================================================ `; diff --git a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap index df3087471f75..76ccd55b8a0f 100644 --- a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap @@ -26,18 +26,10 @@ global {} declare global {} =====================================output===================================== -namespace global { - -} -module global { - -} -global { - -} -declare global { - -} +namespace global {} +module global {} +global {} +declare global {} ================================================================================ `; @@ -81,36 +73,36 @@ namespace X { } =====================================output===================================== -module X { - -} +module X {} module X { const x = 1; } module X { - module X { } + module X {} } module X { - module X { const x = 1; } + module X { + const x = 1; + } } -namespace X { - -} +namespace X {} namespace X { const x = 1; } namespace X { - namespace X { } + namespace X {} } namespace X { - namespace X { const x = 1; } + namespace X { + const x = 1; + } } ================================================================================