fix(35779): emit comments after trailing comma (#37887)

This commit is contained in:
Oleksandr T
2020-11-03 00:24:55 +02:00
committed by GitHub
parent ae81add083
commit 075477f9cf
66 changed files with 385 additions and 22 deletions

View File

@@ -4165,20 +4165,26 @@ namespace ts {
}
// Write a trailing comma, if requested.
const hasTrailingComma = (format & ListFormat.AllowTrailingComma) && children!.hasTrailingComma;
if (format & ListFormat.CommaDelimited && hasTrailingComma) {
writePunctuation(",");
const emitFlags = previousSibling ? getEmitFlags(previousSibling) : 0;
const skipTrailingComments = commentsDisabled || !!(emitFlags & EmitFlags.NoTrailingComments);
const hasTrailingComma = children?.hasTrailingComma && (format & ListFormat.AllowTrailingComma) && (format & ListFormat.CommaDelimited);
if (hasTrailingComma) {
if (previousSibling && !skipTrailingComments) {
emitTokenWithComment(SyntaxKind.CommaToken, previousSibling.end, writePunctuation, previousSibling);
}
else {
writePunctuation(",");
}
}
// Emit any trailing comment of the last element in the list
// i.e
// var array = [...
// 2
// /* end of element 2 */
// ];
if (previousSibling && format & ListFormat.DelimitersMask && previousSibling.end !== parentNode.end && !(getEmitFlags(previousSibling) & EmitFlags.NoTrailingComments)) {
emitLeadingCommentsOfPosition(previousSibling.end);
if (previousSibling && parentNode.end !== previousSibling.end && (format & ListFormat.DelimitersMask) && !skipTrailingComments) {
emitLeadingCommentsOfPosition(hasTrailingComma && children?.end ? children.end : previousSibling.end);
}
// Decrease the indent, if requested.

View File

@@ -1,5 +1,5 @@
//// [commentOnArrayElement1.ts]
var array = [
const array = [
/* element 1*/
1
/* end of element 1 */,

View File

@@ -1,6 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement1.ts ===
var array = [
>array : Symbol(array, Decl(commentOnArrayElement1.ts, 0, 3))
const array = [
>array : Symbol(array, Decl(commentOnArrayElement1.ts, 0, 5))
/* element 1*/
1

View File

@@ -1,5 +1,5 @@
=== tests/cases/compiler/commentOnArrayElement1.ts ===
var array = [
const array = [
>array : number[]
>[ /* element 1*/ 1 /* end of element 1 */, 2 /* end of element 2 */] : number[]

View File

@@ -0,0 +1,6 @@
//// [commentOnArrayElement10.ts]
const array = [,, /* comment */];
//// [commentOnArrayElement10.js]
var array = [, , /* comment */];

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/commentOnArrayElement10.ts ===
const array = [,, /* comment */];
>array : Symbol(array, Decl(commentOnArrayElement10.ts, 0, 5))

View File

@@ -0,0 +1,7 @@
=== tests/cases/compiler/commentOnArrayElement10.ts ===
const array = [,, /* comment */];
>array : any[]
>[,, /* comment */] : undefined[]
> : undefined
> : undefined

View File

@@ -0,0 +1,10 @@
//// [commentOnArrayElement11.ts]
const array = [
, /* comment */
];
//// [commentOnArrayElement11.js]
var array = [
, /* comment */
];

View File

@@ -0,0 +1,7 @@
=== tests/cases/compiler/commentOnArrayElement11.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement11.ts, 0, 5))
, /* comment */
];

View File

@@ -0,0 +1,10 @@
=== tests/cases/compiler/commentOnArrayElement11.ts ===
const array = [
>array : any[]
>[ , /* comment */] : undefined[]
, /* comment */
> : undefined
];

View File

@@ -0,0 +1,11 @@
//// [commentOnArrayElement12.ts]
const array = [
,, /* comment */
];
//// [commentOnArrayElement12.js]
var array = [
,
, /* comment */
];

View File

@@ -0,0 +1,7 @@
=== tests/cases/compiler/commentOnArrayElement12.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement12.ts, 0, 5))
,, /* comment */
];

View File

@@ -0,0 +1,11 @@
=== tests/cases/compiler/commentOnArrayElement12.ts ===
const array = [
>array : any[]
>[ ,, /* comment */] : undefined[]
,, /* comment */
> : undefined
> : undefined
];

View File

@@ -0,0 +1,6 @@
//// [commentOnArrayElement13.ts]
const array = [/* comment */];
//// [commentOnArrayElement13.js]
var array = [ /* comment */];

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/commentOnArrayElement13.ts ===
const array = [/* comment */];
>array : Symbol(array, Decl(commentOnArrayElement13.ts, 0, 5))

View File

@@ -0,0 +1,5 @@
=== tests/cases/compiler/commentOnArrayElement13.ts ===
const array = [/* comment */];
>array : any[]
>[/* comment */] : undefined[]

View File

@@ -0,0 +1,6 @@
//// [commentOnArrayElement14.ts]
const array = [1 /* comment */];
//// [commentOnArrayElement14.js]
var array = [1 /* comment */];

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/commentOnArrayElement14.ts ===
const array = [1 /* comment */];
>array : Symbol(array, Decl(commentOnArrayElement14.ts, 0, 5))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement14.ts ===
const array = [1 /* comment */];
>array : number[]
>[1 /* comment */] : number[]
>1 : 1

View File

@@ -0,0 +1,6 @@
//// [commentOnArrayElement15.ts]
const array = [/* comment */ 1 /* comment */];
//// [commentOnArrayElement15.js]
var array = [/* comment */ 1 /* comment */];

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/commentOnArrayElement15.ts ===
const array = [/* comment */ 1 /* comment */];
>array : Symbol(array, Decl(commentOnArrayElement15.ts, 0, 5))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement15.ts ===
const array = [/* comment */ 1 /* comment */];
>array : number[]
>[/* comment */ 1 /* comment */] : number[]
>1 : 1

View File

@@ -0,0 +1,16 @@
//// [commentOnArrayElement16.ts]
const array = [
// comment start
1,
2,
// comment end
];
//// [commentOnArrayElement16.js]
var array = [
// comment start
1,
2,
// comment end
];

View File

@@ -0,0 +1,10 @@
=== tests/cases/compiler/commentOnArrayElement16.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement16.ts, 0, 5))
// comment start
1,
2,
// comment end
];

View File

@@ -0,0 +1,15 @@
=== tests/cases/compiler/commentOnArrayElement16.ts ===
const array = [
>array : number[]
>[ // comment start 1, 2, // comment end] : number[]
// comment start
1,
>1 : 1
2,
>2 : 2
// comment end
];

View File

@@ -1,5 +1,5 @@
//// [commentOnArrayElement2.ts]
var array = [
const array = [
/* element 1*/
1 /* end of element 1 */,
2

View File

@@ -1,6 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement2.ts ===
var array = [
>array : Symbol(array, Decl(commentOnArrayElement2.ts, 0, 3))
const array = [
>array : Symbol(array, Decl(commentOnArrayElement2.ts, 0, 5))
/* element 1*/
1 /* end of element 1 */,

View File

@@ -1,5 +1,5 @@
=== tests/cases/compiler/commentOnArrayElement2.ts ===
var array = [
const array = [
>array : number[]
>[ /* element 1*/ 1 /* end of element 1 */, 2 /* end of element 2 */] : number[]

View File

@@ -1,5 +1,5 @@
//// [commentOnArrayElement3.ts]
var array = [
const array = [
/* element 1*/
1
/* end of element 1 */,
@@ -16,4 +16,5 @@ var array = [
2
/* end of element 2 */ ,
,
/* extra comment */
];

View File

@@ -1,6 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement3.ts ===
var array = [
>array : Symbol(array, Decl(commentOnArrayElement3.ts, 0, 3))
const array = [
>array : Symbol(array, Decl(commentOnArrayElement3.ts, 0, 5))
/* element 1*/
1

View File

@@ -1,5 +1,5 @@
=== tests/cases/compiler/commentOnArrayElement3.ts ===
var array = [
const array = [
>array : number[]
>[ /* element 1*/ 1 /* end of element 1 */, 2 /* end of element 2 */, , /* extra comment */] : number[]

View File

@@ -0,0 +1,14 @@
//// [commentOnArrayElement4.ts]
const array = [
/* element 1 */
1,
/* end of element 1 */
];
//// [commentOnArrayElement4.js]
var array = [
/* element 1 */
1,
/* end of element 1 */
];

View File

@@ -0,0 +1,9 @@
=== tests/cases/compiler/commentOnArrayElement4.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement4.ts, 0, 5))
/* element 1 */
1,
/* end of element 1 */
];

View File

@@ -0,0 +1,12 @@
=== tests/cases/compiler/commentOnArrayElement4.ts ===
const array = [
>array : number[]
>[ /* element 1 */ 1, /* end of element 1 */] : number[]
/* element 1 */
1,
>1 : 1
/* end of element 1 */
];

View File

@@ -0,0 +1,16 @@
//// [commentOnArrayElement5.ts]
const array = [
/* element 1 */
1,
/* end of element 1 */
/* extra comment */
];
//// [commentOnArrayElement5.js]
var array = [
/* element 1 */
1,
/* end of element 1 */
/* extra comment */
];

View File

@@ -0,0 +1,10 @@
=== tests/cases/compiler/commentOnArrayElement5.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement5.ts, 0, 5))
/* element 1 */
1,
/* end of element 1 */
/* extra comment */
];

View File

@@ -0,0 +1,13 @@
=== tests/cases/compiler/commentOnArrayElement5.ts ===
const array = [
>array : number[]
>[ /* element 1 */ 1, /* end of element 1 */ /* extra comment */] : number[]
/* element 1 */
1,
>1 : 1
/* end of element 1 */
/* extra comment */
];

View File

@@ -0,0 +1,6 @@
//// [commentOnArrayElement6.ts]
const array = [1, /* comment */];
//// [commentOnArrayElement6.js]
var array = [1, /* comment */];

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/commentOnArrayElement6.ts ===
const array = [1, /* comment */];
>array : Symbol(array, Decl(commentOnArrayElement6.ts, 0, 5))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement6.ts ===
const array = [1, /* comment */];
>array : number[]
>[1, /* comment */] : number[]
>1 : 1

View File

@@ -0,0 +1,6 @@
//// [commentOnArrayElement7.ts]
const array = [/* element 1 */ 1, /* end of element 1 */];
//// [commentOnArrayElement7.js]
var array = [/* element 1 */ 1, /* end of element 1 */];

View File

@@ -0,0 +1,4 @@
=== tests/cases/compiler/commentOnArrayElement7.ts ===
const array = [/* element 1 */ 1, /* end of element 1 */];
>array : Symbol(array, Decl(commentOnArrayElement7.ts, 0, 5))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/commentOnArrayElement7.ts ===
const array = [/* element 1 */ 1, /* end of element 1 */];
>array : number[]
>[/* element 1 */ 1, /* end of element 1 */] : number[]
>1 : 1

View File

@@ -0,0 +1,10 @@
//// [commentOnArrayElement8.ts]
const array = [
1, /* comment */
];
//// [commentOnArrayElement8.js]
var array = [
1, /* comment */
];

View File

@@ -0,0 +1,7 @@
=== tests/cases/compiler/commentOnArrayElement8.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement8.ts, 0, 5))
1, /* comment */
];

View File

@@ -0,0 +1,10 @@
=== tests/cases/compiler/commentOnArrayElement8.ts ===
const array = [
>array : number[]
>[ 1, /* comment */] : number[]
1, /* comment */
>1 : 1
];

View File

@@ -0,0 +1,10 @@
//// [commentOnArrayElement9.ts]
const array = [
/* element 1 */ 1, /* end of element 1 */
];
//// [commentOnArrayElement9.js]
var array = [
/* element 1 */ 1, /* end of element 1 */
];

View File

@@ -0,0 +1,7 @@
=== tests/cases/compiler/commentOnArrayElement9.ts ===
const array = [
>array : Symbol(array, Decl(commentOnArrayElement9.ts, 0, 5))
/* element 1 */ 1, /* end of element 1 */
];

View File

@@ -0,0 +1,10 @@
=== tests/cases/compiler/commentOnArrayElement9.ts ===
const array = [
>array : number[]
>[ /* element 1 */ 1, /* end of element 1 */] : number[]
/* element 1 */ 1, /* end of element 1 */
>1 : 1
];

View File

@@ -26,7 +26,7 @@ var x = {
\u0061: "ss",
a: {
c: 1,
"c": 56,
"c": 56, // Duplicate
}
};
var y = {

View File

@@ -1,4 +1,4 @@
var array = [
const array = [
/* element 1*/
1
/* end of element 1 */,

View File

@@ -0,0 +1 @@
const array = [,, /* comment */];

View File

@@ -0,0 +1,3 @@
const array = [
, /* comment */
];

View File

@@ -0,0 +1,3 @@
const array = [
,, /* comment */
];

View File

@@ -0,0 +1 @@
const array = [/* comment */];

View File

@@ -0,0 +1 @@
const array = [1 /* comment */];

View File

@@ -0,0 +1 @@
const array = [/* comment */ 1 /* comment */];

View File

@@ -0,0 +1,6 @@
const array = [
// comment start
1,
2,
// comment end
];

View File

@@ -1,4 +1,4 @@
var array = [
const array = [
/* element 1*/
1 /* end of element 1 */,
2

View File

@@ -1,4 +1,4 @@
var array = [
const array = [
/* element 1*/
1
/* end of element 1 */,

View File

@@ -0,0 +1,5 @@
const array = [
/* element 1 */
1,
/* end of element 1 */
];

View File

@@ -0,0 +1,6 @@
const array = [
/* element 1 */
1,
/* end of element 1 */
/* extra comment */
];

View File

@@ -0,0 +1 @@
const array = [1, /* comment */];

View File

@@ -0,0 +1 @@
const array = [/* element 1 */ 1, /* end of element 1 */];

View File

@@ -0,0 +1,3 @@
const array = [
1, /* comment */
];

View File

@@ -0,0 +1,3 @@
const array = [
/* element 1 */ 1, /* end of element 1 */
];