From 764af0e78106eb21f4f05d2f0eac73954de364c1 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 21 Sep 2017 11:05:40 -0700 Subject: [PATCH 1/5] Added tests and accepted baselines. --- src/harness/unittests/printer.ts | 23 +++++++++++++++++++ ...tsNodeCorrectly.emptyGlobalAugmentation.js | 1 + ...yGlobalAugmentationWithNoDeclareKeyword.js | 1 + 3 files changed, 25 insertions(+) create mode 100644 tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js create mode 100644 tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js 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..2a8c3b88e16 --- /dev/null +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js @@ -0,0 +1 @@ +declare module 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..4cc10f41f8f --- /dev/null +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js @@ -0,0 +1 @@ +module global { } \ No newline at end of file From 979295b21b10989328e851264a14460db49fadd7 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 21 Sep 2017 12:15:27 -0700 Subject: [PATCH 2/5] Force 'declare' modifiers and correct global augmentation emit. --- src/compiler/emitter.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 502329bbd84..f98533628d0 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1862,8 +1862,19 @@ namespace ts { } function emitModuleDeclaration(node: ModuleDeclaration) { - emitModifiers(node, node.modifiers); - write(node.flags & NodeFlags.Namespace ? "namespace " : "module "); + if (node.flags & NodeFlags.GlobalAugmentation) { + if (!hasModifier(node, ModifierFlags.Ambient)) { + // Always emit a 'declare' keyword in case it wasn't provided by a factory function call. + write("declare "); + } + else { + emitModifiers(node, node.modifiers); + } + } + else { + emitModifiers(node, node.modifiers); + write(node.flags & NodeFlags.Namespace ? "namespace " : "module "); + } emit(node.name); let body = node.body; From d30c3dcb4d06999ac3b28efb8659a0377bd2a9c1 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 21 Sep 2017 12:15:42 -0700 Subject: [PATCH 3/5] Accepted baselines. --- .../printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js | 2 +- ...NodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js index 2a8c3b88e16..cdbc9e5766e 100644 --- a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentation.js @@ -1 +1 @@ -declare module global { } \ No newline at end of file +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 index 4cc10f41f8f..cdbc9e5766e 100644 --- a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js @@ -1 +1 @@ -module global { } \ No newline at end of file +declare global { } \ No newline at end of file From ffa1ea72c12563ccbc0bb1ecce78dd5631b0e8fc Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 22 Sep 2017 00:12:37 -0700 Subject: [PATCH 4/5] Just always print out modifiers for global augmentations. Apparently, they don't always need them! --- src/compiler/emitter.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) mode change 100644 => 100755 src/compiler/emitter.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts old mode 100644 new mode 100755 index f98533628d0..d458e7e5ef5 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1862,17 +1862,8 @@ namespace ts { } function emitModuleDeclaration(node: ModuleDeclaration) { - if (node.flags & NodeFlags.GlobalAugmentation) { - if (!hasModifier(node, ModifierFlags.Ambient)) { - // Always emit a 'declare' keyword in case it wasn't provided by a factory function call. - write("declare "); - } - else { - emitModifiers(node, node.modifiers); - } - } - else { - emitModifiers(node, node.modifiers); + emitModifiers(node, node.modifiers); + if (~node.flags & NodeFlags.GlobalAugmentation) { write(node.flags & NodeFlags.Namespace ? "namespace " : "module "); } emit(node.name); From 555718ef32d9128e3dc7ec515c01682b409a5ff6 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 22 Sep 2017 00:13:31 -0700 Subject: [PATCH 5/5] Accepted baselines. --- ...NodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js index cdbc9e5766e..a5315a0c0c4 100644 --- a/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js +++ b/tests/baselines/reference/printerApi/printsNodeCorrectly.emptyGlobalAugmentationWithNoDeclareKeyword.js @@ -1 +1 @@ -declare global { } \ No newline at end of file +global { } \ No newline at end of file