Skip to content

Commit 94e0d95

Browse files
committed
Makes sure leading comments are emitting only once
1 parent c4b22d3 commit 94e0d95

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/compiler/emitter.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,14 +1954,17 @@ module ts {
19541954
}
19551955

19561956
function emitLeadingDeclarationComments(node: Node) {
1957-
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
1958-
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
1959-
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
1960-
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
1957+
// Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
1958+
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
1959+
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
1960+
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
1961+
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
1962+
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
1963+
}
19611964
}
19621965

19631966
function emitTrailingDeclarationComments(node: Node) {
1964-
// Emit the trailing declaration comments only if the parent's end doesnt match
1967+
// Emit the trailing comments only if the parent's end doesnt match
19651968
if (node.parent.kind === SyntaxKind.SourceFile || node.end !== node.parent.end) {
19661969
var trailingComments = getTrailingComments(currentSourceFile.text, node.end);
19671970
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/

tests/baselines/reference/commentsFunction.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ function blah3(a // trailing commen single line
7272
) {
7373
}
7474
lambdaFoo = function (a, b) { return a * b; }; // This is trailing comment
75-
/*leading comment*/ (
76-
/*leading comment*/ function () { return 0; }); // Needs to be wrapped in parens to be a valid expression (not declaration)
75+
/*leading comment*/ (function () { return 0; }); // Needs to be wrapped in parens to be a valid expression (not declaration)
7776
/*leading comment*/ (function () { return 0; }); //trailing comment
7877

7978

tests/baselines/reference/commentsOnObjectLiteral3.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ var v = {
2727
func: function () {
2828
},
2929
//PropertyName + CallSignature
30-
func1:
31-
//PropertyName + CallSignature
32-
function () {
30+
func1: function () {
3331
},
3432
//getter
3533
get a() {

0 commit comments

Comments
 (0)