diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts old mode 100644 new mode 100755 index 502329bbd84..d458e7e5ef5 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1863,7 +1863,9 @@ namespace ts { function emitModuleDeclaration(node: ModuleDeclaration) { emitModifiers(node, node.modifiers); - write(node.flags & NodeFlags.Namespace ? "namespace " : "module "); + if (~node.flags & NodeFlags.GlobalAugmentation) { + write(node.flags & NodeFlags.Namespace ? "namespace " : "module "); + } emit(node.name); let body = node.body; diff --git a/src/harness/unittests/printer.ts b/src/harness/unittests/printer.ts index 825bfddbb4a..2bbc19881a3 100644 --- a/src/harness/unittests/printer.ts +++ b/src/harness/unittests/printer.ts @@ -110,6 +110,29 @@ namespace ts { createSourceFile("source.ts", "", ScriptTarget.ES2015) )); + + printsCorrectly("emptyGlobalAugmentation", {}, printer => printer.printNode( + EmitHint.Unspecified, + createModuleDeclaration( + /*decorators*/ undefined, + /*modifiers*/ [createToken(SyntaxKind.DeclareKeyword)], + createIdentifier("global"), + createModuleBlock(emptyArray), + NodeFlags.GlobalAugmentation), + createSourceFile("source.ts", "", ScriptTarget.ES2015) + )); + + printsCorrectly("emptyGlobalAugmentationWithNoDeclareKeyword", {}, printer => printer.printNode( + EmitHint.Unspecified, + createModuleDeclaration( + /*decorators*/ undefined, + /*modifiers*/ undefined, + createIdentifier("global"), + createModuleBlock(emptyArray), + NodeFlags.GlobalAugmentation), + createSourceFile("source.ts", "", ScriptTarget.ES2015) + )); + // https://github.com/Microsoft/TypeScript/issues/15971 printsCorrectly("classWithOptionalMethodAndProperty", {}, printer => printer.printNode( EmitHint.Unspecified, diff --git a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js new file mode 100644 index 00000000000..cdbc9e5766e --- /dev/null +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js @@ -0,0 +1 @@ +declare global { } \ No newline at end of file diff --git a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js new file mode 100644 index 00000000000..a5315a0c0c4 --- /dev/null +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js @@ -0,0 +1 @@ +global { } \ No newline at end of file