Skip to content

Commit e4aa515

Browse files
yuitmhegazy
authored andcommitted
[Master] Fix 15179 missing comment in switch case clause (#16033)
* Fix emit comments after switch case clause * Update baselines * Add new tests and baselines
1 parent 7adfa8d commit e4aa515

17 files changed

+362
-16
lines changed

src/compiler/emitter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,22 @@ namespace ts {
20212021
rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)
20222022
);
20232023

2024+
// e.g:
2025+
// case 0: // Zero
2026+
// case 1: // One
2027+
// case 2: // two
2028+
// return "hi";
2029+
// If there is no statements, emitNodeWithComments of the parentNode which is caseClause will take care of trailing comment.
2030+
// So in example above, comment "// Zero" and "// One" will be emit in emitTrailingComments in emitNodeWithComments.
2031+
// However, for "case 2", because parentNode which is caseClause has an "end" property to be end of the statements (in this case return statement)
2032+
// comment "// two" will not be emitted in emitNodeWithComments.
2033+
// Therefore, we have to do the check here to emit such comment.
2034+
if (statements.length > 0) {
2035+
// We use emitTrailingCommentsOfPosition instead of emitLeadingCommentsOfPosition because leading comments is defined as comments before the node after newline character separating it from previous line
2036+
// Note: we can't use parentNode.end as such position includes statements.
2037+
emitTrailingCommentsOfPosition(statements.pos);
2038+
}
2039+
20242040
if (emitAsSingleStatement) {
20252041
write(" ");
20262042
emit(statements[0]);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [commentsAfterCaseClauses1.ts]
2+
function getSecurity(level) {
3+
switch(level){
4+
case 0: // Zero
5+
case 1: // one
6+
case 2: // two
7+
return "Hi";
8+
case 3: // three
9+
case 4 : // four
10+
return "hello";
11+
case 5: // five
12+
default: // default
13+
return "world";
14+
}
15+
}
16+
17+
//// [commentsAfterCaseClauses1.js]
18+
function getSecurity(level) {
19+
switch (level) {
20+
case 0: // Zero
21+
case 1: // one
22+
case 2:// two
23+
return "Hi";
24+
case 3: // three
25+
case 4:// four
26+
return "hello";
27+
case 5: // five
28+
default:// default
29+
return "world";
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/commentsAfterCaseClauses1.ts ===
2+
function getSecurity(level) {
3+
>getSecurity : Symbol(getSecurity, Decl(commentsAfterCaseClauses1.ts, 0, 0))
4+
>level : Symbol(level, Decl(commentsAfterCaseClauses1.ts, 0, 21))
5+
6+
switch(level){
7+
>level : Symbol(level, Decl(commentsAfterCaseClauses1.ts, 0, 21))
8+
9+
case 0: // Zero
10+
case 1: // one
11+
case 2: // two
12+
return "Hi";
13+
case 3: // three
14+
case 4 : // four
15+
return "hello";
16+
case 5: // five
17+
default: // default
18+
return "world";
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/commentsAfterCaseClauses1.ts ===
2+
function getSecurity(level) {
3+
>getSecurity : (level: any) => "Hi" | "hello" | "world"
4+
>level : any
5+
6+
switch(level){
7+
>level : any
8+
9+
case 0: // Zero
10+
>0 : 0
11+
12+
case 1: // one
13+
>1 : 1
14+
15+
case 2: // two
16+
>2 : 2
17+
18+
return "Hi";
19+
>"Hi" : "Hi"
20+
21+
case 3: // three
22+
>3 : 3
23+
24+
case 4 : // four
25+
>4 : 4
26+
27+
return "hello";
28+
>"hello" : "hello"
29+
30+
case 5: // five
31+
>5 : 5
32+
33+
default: // default
34+
return "world";
35+
>"world" : "world"
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [commentsAfterCaseClauses2.ts]
2+
function getSecurity(level) {
3+
switch(level){
4+
case 0: // Zero
5+
case 1: // one
6+
case 2: // two
7+
// Leading comments
8+
return "Hi";
9+
case 3: // three
10+
case 4: // four
11+
return "hello";
12+
case 5: // five
13+
default: // default
14+
return "world";
15+
// Comment After
16+
} /*Comment 1*/ // Comment After 1
17+
// Comment After 2
18+
}
19+
20+
//// [commentsAfterCaseClauses2.js]
21+
function getSecurity(level) {
22+
switch (level) {
23+
case 0: // Zero
24+
case 1: // one
25+
case 2:// two
26+
// Leading comments
27+
return "Hi";
28+
case 3: // three
29+
case 4:// four
30+
return "hello";
31+
case 5: // five
32+
default:// default
33+
return "world";
34+
} /*Comment 1*/ // Comment After 1
35+
// Comment After 2
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/commentsAfterCaseClauses2.ts ===
2+
function getSecurity(level) {
3+
>getSecurity : Symbol(getSecurity, Decl(commentsAfterCaseClauses2.ts, 0, 0))
4+
>level : Symbol(level, Decl(commentsAfterCaseClauses2.ts, 0, 21))
5+
6+
switch(level){
7+
>level : Symbol(level, Decl(commentsAfterCaseClauses2.ts, 0, 21))
8+
9+
case 0: // Zero
10+
case 1: // one
11+
case 2: // two
12+
// Leading comments
13+
return "Hi";
14+
case 3: // three
15+
case 4: // four
16+
return "hello";
17+
case 5: // five
18+
default: // default
19+
return "world";
20+
// Comment After
21+
} /*Comment 1*/ // Comment After 1
22+
// Comment After 2
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/compiler/commentsAfterCaseClauses2.ts ===
2+
function getSecurity(level) {
3+
>getSecurity : (level: any) => "Hi" | "hello" | "world"
4+
>level : any
5+
6+
switch(level){
7+
>level : any
8+
9+
case 0: // Zero
10+
>0 : 0
11+
12+
case 1: // one
13+
>1 : 1
14+
15+
case 2: // two
16+
>2 : 2
17+
18+
// Leading comments
19+
return "Hi";
20+
>"Hi" : "Hi"
21+
22+
case 3: // three
23+
>3 : 3
24+
25+
case 4: // four
26+
>4 : 4
27+
28+
return "hello";
29+
>"hello" : "hello"
30+
31+
case 5: // five
32+
>5 : 5
33+
34+
default: // default
35+
return "world";
36+
>"world" : "world"
37+
38+
// Comment After
39+
} /*Comment 1*/ // Comment After 1
40+
// Comment After 2
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [commentsAfterCaseClauses3.ts]
2+
function getSecurity(level) {
3+
switch(level){
4+
case 0: /*Zero*/
5+
case 1: /*One*/
6+
case 2: /*two*/
7+
// Leading comments
8+
return "Hi";
9+
case 3: /*three*/
10+
case 4: /*four*/
11+
return "hello";
12+
case 5: /*five*/
13+
default: /*six*/
14+
return "world";
15+
}
16+
17+
}
18+
19+
//// [commentsAfterCaseClauses3.js]
20+
function getSecurity(level) {
21+
switch (level) {
22+
case 0: /*Zero*/
23+
case 1: /*One*/
24+
case 2:/*two*/
25+
// Leading comments
26+
return "Hi";
27+
case 3: /*three*/
28+
case 4:/*four*/
29+
return "hello";
30+
case 5: /*five*/
31+
default:/*six*/
32+
return "world";
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/commentsAfterCaseClauses3.ts ===
2+
function getSecurity(level) {
3+
>getSecurity : Symbol(getSecurity, Decl(commentsAfterCaseClauses3.ts, 0, 0))
4+
>level : Symbol(level, Decl(commentsAfterCaseClauses3.ts, 0, 21))
5+
6+
switch(level){
7+
>level : Symbol(level, Decl(commentsAfterCaseClauses3.ts, 0, 21))
8+
9+
case 0: /*Zero*/
10+
case 1: /*One*/
11+
case 2: /*two*/
12+
// Leading comments
13+
return "Hi";
14+
case 3: /*three*/
15+
case 4: /*four*/
16+
return "hello";
17+
case 5: /*five*/
18+
default: /*six*/
19+
return "world";
20+
}
21+
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/commentsAfterCaseClauses3.ts ===
2+
function getSecurity(level) {
3+
>getSecurity : (level: any) => "Hi" | "hello" | "world"
4+
>level : any
5+
6+
switch(level){
7+
>level : any
8+
9+
case 0: /*Zero*/
10+
>0 : 0
11+
12+
case 1: /*One*/
13+
>1 : 1
14+
15+
case 2: /*two*/
16+
>2 : 2
17+
18+
// Leading comments
19+
return "Hi";
20+
>"Hi" : "Hi"
21+
22+
case 3: /*three*/
23+
>3 : 3
24+
25+
case 4: /*four*/
26+
>4 : 4
27+
28+
return "hello";
29+
>"hello" : "hello"
30+
31+
case 5: /*five*/
32+
>5 : 5
33+
34+
default: /*six*/
35+
return "world";
36+
>"world" : "world"
37+
}
38+
39+
}

0 commit comments

Comments
 (0)