diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 72b654e8671c..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 "{}"; } @@ -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 " ); @@ -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")]); diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap index 323078823ee0..fd08d6576331 100644 --- a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -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 } @@ -550,9 +546,7 @@ function b() { class global {} } -namespace global { - -} +namespace global {} function foo(global: number) {} @@ -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(); @@ -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(); @@ -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(); @@ -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_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 +936,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 +949,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/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/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 8e265f74c0b6..76ccd55b8a0f 100644 --- a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap @@ -26,17 +26,83 @@ global {} declare global {} =====================================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; +} +module X { + module X {} } -namespace global { +module X { + module X { + const x = 1; + } } -global { +namespace X {} + +namespace X { + const x = 1; +} + +namespace X { + namespace X {} } -declare global { +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; + } } ================================================================================ 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; + } +}