From a49ce91e00c356d1a9e28c788a2fad56c003c944 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 7 Sep 2016 16:59:22 -0700 Subject: [PATCH 1/2] Only emit comment only once in module declaration with identifier path name --- src/compiler/transformers/ts.ts | 25 ++++++++++++++++++- ...espaceDeclarationWithIdentifierPathName.js | 20 +++++++++++++++ ...eDeclarationWithIdentifierPathName.symbols | 11 ++++++++ ...aceDeclarationWithIdentifierPathName.types | 11 ++++++++ ...espaceDeclarationWithIdentifierPathName.ts | 6 +++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.js create mode 100644 tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.symbols create mode 100644 tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.types create mode 100644 tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index efb247ba663..1912370998b 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2859,7 +2859,6 @@ namespace ts { const moduleBlock = getInnerMostModuleDeclarationFromDottedModule(node).body; statementsLocation = moveRangePos(moduleBlock.statements, -1); } - addRange(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceContainerName; @@ -2872,6 +2871,30 @@ namespace ts { /*location*/ blockLocation, /*multiLine*/ true ); + + // namespace hello.hi.world { + // function foo() {} + // + // // TODO, blah + // } + // + // should be emitted as + // + // var hello; + // (function (hello) { + // var hi; + // (function (hi) { + // var world; + // (function (world) { + // function foo() { } + // // TODO, blah + // })(world = hi.world || (hi.world = {})); + // })(hi = hello.hi || (hello.hi = {})); + // })(hello || (hello = {})); + // so if the block is a transformed module declaration, turn off the comment emit + if (body.kind !== SyntaxKind.ModuleBlock) { + setNodeEmitFlags(block, block.emitFlags | NodeEmitFlags.NoComments); + } return block; } diff --git a/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.js b/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.js new file mode 100644 index 00000000000..6f99037a61d --- /dev/null +++ b/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.js @@ -0,0 +1,20 @@ +//// [commentInNamespaceDeclarationWithIdentifierPathName.ts] +namespace hello.hi.world +{ + function foo() {} + + // TODO, blah +} + +//// [commentInNamespaceDeclarationWithIdentifierPathName.js] +var hello; +(function (hello) { + var hi; + (function (hi) { + var world; + (function (world) { + function foo() { } + // TODO, blah + })(world = hi.world || (hi.world = {})); + })(hi = hello.hi || (hello.hi = {})); +})(hello || (hello = {})); diff --git a/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.symbols b/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.symbols new file mode 100644 index 00000000000..6db54887737 --- /dev/null +++ b/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.symbols @@ -0,0 +1,11 @@ +=== tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts === +namespace hello.hi.world +>hello : Symbol(hello, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 0, 0)) +>hi : Symbol(hi, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 0, 16)) +>world : Symbol(world, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 0, 19)) +{ + function foo() {} +>foo : Symbol(foo, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 1, 1)) + + // TODO, blah +} diff --git a/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.types b/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.types new file mode 100644 index 00000000000..6a5064e2700 --- /dev/null +++ b/tests/baselines/reference/commentInNamespaceDeclarationWithIdentifierPathName.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts === +namespace hello.hi.world +>hello : typeof hello +>hi : typeof hi +>world : typeof world +{ + function foo() {} +>foo : () => void + + // TODO, blah +} diff --git a/tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts b/tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts new file mode 100644 index 00000000000..a5a32e53502 --- /dev/null +++ b/tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts @@ -0,0 +1,6 @@ +namespace hello.hi.world +{ + function foo() {} + + // TODO, blah +} \ No newline at end of file From b193426c7b12ce73331aea2019b718f22f06dc87 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Fri, 16 Sep 2016 15:17:52 -0700 Subject: [PATCH 2/2] Address PR: Update comment --- src/compiler/transformers/ts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 1912370998b..4de60322e93 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2891,7 +2891,7 @@ namespace ts { // })(world = hi.world || (hi.world = {})); // })(hi = hello.hi || (hello.hi = {})); // })(hello || (hello = {})); - // so if the block is a transformed module declaration, turn off the comment emit + // We only want to emit comment on the namespace which contains block body itself, not the containing namespaces. if (body.kind !== SyntaxKind.ModuleBlock) { setNodeEmitFlags(block, block.emitFlags | NodeEmitFlags.NoComments); }