Skip to content

Commit aba197c

Browse files
committed
Fix issue microsoft#5810 doubled comment on functions in array literals
1 parent 1e64f16 commit aba197c

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

src/compiler/emitter.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -4280,9 +4280,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
42804280

42814281
// TODO (yuisu) : we should not have special cases to condition emitting comments
42824282
// but have one place to fix check for these conditions.
4283-
if (node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature &&
4284-
node.parent && node.parent.kind !== SyntaxKind.PropertyAssignment &&
4285-
node.parent.kind !== SyntaxKind.CallExpression) {
4283+
if (node.kind !== SyntaxKind.MethodDeclaration &&
4284+
node.kind !== SyntaxKind.MethodSignature &&
4285+
node.parent &&
4286+
node.parent.kind !== SyntaxKind.PropertyAssignment &&
4287+
node.parent.kind !== SyntaxKind.CallExpression &&
4288+
node.parent.kind !== SyntaxKind.ArrayLiteralExpression) {
42864289
// 1. Methods will emit the comments as part of emitting method declaration
42874290

42884291
// 2. If the function is a property of object literal, emitting leading-comments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [arrayLiteralComments.ts]
2+
var testArrayWithFunc = [
3+
// Function comment
4+
function() {
5+
let x = 1;
6+
},
7+
// String comment
8+
'1',
9+
// Numeric comment
10+
2,
11+
// Object comment
12+
{ a: 1 },
13+
// Array comment
14+
[1, 2, 3]
15+
]
16+
17+
//// [arrayLiteralComments.js]
18+
var testArrayWithFunc = [
19+
// Function comment
20+
function () {
21+
var x = 1;
22+
},
23+
// String comment
24+
'1',
25+
// Numeric comment
26+
2,
27+
// Object comment
28+
{ a: 1 },
29+
// Array comment
30+
[1, 2, 3]
31+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/arrayLiteralComments.ts ===
2+
var testArrayWithFunc = [
3+
>testArrayWithFunc : Symbol(testArrayWithFunc, Decl(arrayLiteralComments.ts, 0, 3))
4+
5+
// Function comment
6+
function() {
7+
let x = 1;
8+
>x : Symbol(x, Decl(arrayLiteralComments.ts, 3, 11))
9+
10+
},
11+
// String comment
12+
'1',
13+
// Numeric comment
14+
2,
15+
// Object comment
16+
{ a: 1 },
17+
>a : Symbol(a, Decl(arrayLiteralComments.ts, 10, 5))
18+
19+
// Array comment
20+
[1, 2, 3]
21+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/arrayLiteralComments.ts ===
2+
var testArrayWithFunc = [
3+
>testArrayWithFunc : ((() => void) | string | number | { a: number; } | number[])[]
4+
>[ // Function comment function() { let x = 1; }, // String comment '1', // Numeric comment 2, // Object comment { a: 1 }, // Array comment [1, 2, 3]] : ((() => void) | string | number | { a: number; } | number[])[]
5+
6+
// Function comment
7+
function() {
8+
>function() { let x = 1; } : () => void
9+
10+
let x = 1;
11+
>x : number
12+
>1 : number
13+
14+
},
15+
// String comment
16+
'1',
17+
>'1' : string
18+
19+
// Numeric comment
20+
2,
21+
>2 : number
22+
23+
// Object comment
24+
{ a: 1 },
25+
>{ a: 1 } : { a: number; }
26+
>a : number
27+
>1 : number
28+
29+
// Array comment
30+
[1, 2, 3]
31+
>[1, 2, 3] : number[]
32+
>1 : number
33+
>2 : number
34+
>3 : number
35+
36+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var testArrayWithFunc = [
2+
// Function comment
3+
function() {
4+
let x = 1;
5+
},
6+
// String comment
7+
'1',
8+
// Numeric comment
9+
2,
10+
// Object comment
11+
{ a: 1 },
12+
// Array comment
13+
[1, 2, 3]
14+
]

0 commit comments

Comments
 (0)