mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
[Master] Fix 15179 missing comment in switch case clause (#16033)
* Fix emit comments after switch case clause * Update baselines * Add new tests and baselines
This commit is contained in:
parent
7adfa8d854
commit
e4aa515191
@ -2021,6 +2021,22 @@ namespace ts {
|
||||
rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)
|
||||
);
|
||||
|
||||
// e.g:
|
||||
// case 0: // Zero
|
||||
// case 1: // One
|
||||
// case 2: // two
|
||||
// return "hi";
|
||||
// If there is no statements, emitNodeWithComments of the parentNode which is caseClause will take care of trailing comment.
|
||||
// So in example above, comment "// Zero" and "// One" will be emit in emitTrailingComments in emitNodeWithComments.
|
||||
// However, for "case 2", because parentNode which is caseClause has an "end" property to be end of the statements (in this case return statement)
|
||||
// comment "// two" will not be emitted in emitNodeWithComments.
|
||||
// Therefore, we have to do the check here to emit such comment.
|
||||
if (statements.length > 0) {
|
||||
// We use emitTrailingCommentsOfPosition instead of emitLeadingCommentsOfPosition because leading comments is defined as comments before the node after newline character separating it from previous line
|
||||
// Note: we can't use parentNode.end as such position includes statements.
|
||||
emitTrailingCommentsOfPosition(statements.pos);
|
||||
}
|
||||
|
||||
if (emitAsSingleStatement) {
|
||||
write(" ");
|
||||
emit(statements[0]);
|
||||
|
||||
31
tests/baselines/reference/commentsAfterCaseClauses1.js
Normal file
31
tests/baselines/reference/commentsAfterCaseClauses1.js
Normal file
@ -0,0 +1,31 @@
|
||||
//// [commentsAfterCaseClauses1.ts]
|
||||
function getSecurity(level) {
|
||||
switch(level){
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2: // two
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4 : // four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default: // default
|
||||
return "world";
|
||||
}
|
||||
}
|
||||
|
||||
//// [commentsAfterCaseClauses1.js]
|
||||
function getSecurity(level) {
|
||||
switch (level) {
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2:// two
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4:// four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default:// default
|
||||
return "world";
|
||||
}
|
||||
}
|
||||
20
tests/baselines/reference/commentsAfterCaseClauses1.symbols
Normal file
20
tests/baselines/reference/commentsAfterCaseClauses1.symbols
Normal file
@ -0,0 +1,20 @@
|
||||
=== tests/cases/compiler/commentsAfterCaseClauses1.ts ===
|
||||
function getSecurity(level) {
|
||||
>getSecurity : Symbol(getSecurity, Decl(commentsAfterCaseClauses1.ts, 0, 0))
|
||||
>level : Symbol(level, Decl(commentsAfterCaseClauses1.ts, 0, 21))
|
||||
|
||||
switch(level){
|
||||
>level : Symbol(level, Decl(commentsAfterCaseClauses1.ts, 0, 21))
|
||||
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2: // two
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4 : // four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default: // default
|
||||
return "world";
|
||||
}
|
||||
}
|
||||
37
tests/baselines/reference/commentsAfterCaseClauses1.types
Normal file
37
tests/baselines/reference/commentsAfterCaseClauses1.types
Normal file
@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/commentsAfterCaseClauses1.ts ===
|
||||
function getSecurity(level) {
|
||||
>getSecurity : (level: any) => "Hi" | "hello" | "world"
|
||||
>level : any
|
||||
|
||||
switch(level){
|
||||
>level : any
|
||||
|
||||
case 0: // Zero
|
||||
>0 : 0
|
||||
|
||||
case 1: // one
|
||||
>1 : 1
|
||||
|
||||
case 2: // two
|
||||
>2 : 2
|
||||
|
||||
return "Hi";
|
||||
>"Hi" : "Hi"
|
||||
|
||||
case 3: // three
|
||||
>3 : 3
|
||||
|
||||
case 4 : // four
|
||||
>4 : 4
|
||||
|
||||
return "hello";
|
||||
>"hello" : "hello"
|
||||
|
||||
case 5: // five
|
||||
>5 : 5
|
||||
|
||||
default: // default
|
||||
return "world";
|
||||
>"world" : "world"
|
||||
}
|
||||
}
|
||||
36
tests/baselines/reference/commentsAfterCaseClauses2.js
Normal file
36
tests/baselines/reference/commentsAfterCaseClauses2.js
Normal file
@ -0,0 +1,36 @@
|
||||
//// [commentsAfterCaseClauses2.ts]
|
||||
function getSecurity(level) {
|
||||
switch(level){
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2: // two
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4: // four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default: // default
|
||||
return "world";
|
||||
// Comment After
|
||||
} /*Comment 1*/ // Comment After 1
|
||||
// Comment After 2
|
||||
}
|
||||
|
||||
//// [commentsAfterCaseClauses2.js]
|
||||
function getSecurity(level) {
|
||||
switch (level) {
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2:// two
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4:// four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default:// default
|
||||
return "world";
|
||||
} /*Comment 1*/ // Comment After 1
|
||||
// Comment After 2
|
||||
}
|
||||
23
tests/baselines/reference/commentsAfterCaseClauses2.symbols
Normal file
23
tests/baselines/reference/commentsAfterCaseClauses2.symbols
Normal file
@ -0,0 +1,23 @@
|
||||
=== tests/cases/compiler/commentsAfterCaseClauses2.ts ===
|
||||
function getSecurity(level) {
|
||||
>getSecurity : Symbol(getSecurity, Decl(commentsAfterCaseClauses2.ts, 0, 0))
|
||||
>level : Symbol(level, Decl(commentsAfterCaseClauses2.ts, 0, 21))
|
||||
|
||||
switch(level){
|
||||
>level : Symbol(level, Decl(commentsAfterCaseClauses2.ts, 0, 21))
|
||||
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2: // two
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4: // four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default: // default
|
||||
return "world";
|
||||
// Comment After
|
||||
} /*Comment 1*/ // Comment After 1
|
||||
// Comment After 2
|
||||
}
|
||||
41
tests/baselines/reference/commentsAfterCaseClauses2.types
Normal file
41
tests/baselines/reference/commentsAfterCaseClauses2.types
Normal file
@ -0,0 +1,41 @@
|
||||
=== tests/cases/compiler/commentsAfterCaseClauses2.ts ===
|
||||
function getSecurity(level) {
|
||||
>getSecurity : (level: any) => "Hi" | "hello" | "world"
|
||||
>level : any
|
||||
|
||||
switch(level){
|
||||
>level : any
|
||||
|
||||
case 0: // Zero
|
||||
>0 : 0
|
||||
|
||||
case 1: // one
|
||||
>1 : 1
|
||||
|
||||
case 2: // two
|
||||
>2 : 2
|
||||
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
>"Hi" : "Hi"
|
||||
|
||||
case 3: // three
|
||||
>3 : 3
|
||||
|
||||
case 4: // four
|
||||
>4 : 4
|
||||
|
||||
return "hello";
|
||||
>"hello" : "hello"
|
||||
|
||||
case 5: // five
|
||||
>5 : 5
|
||||
|
||||
default: // default
|
||||
return "world";
|
||||
>"world" : "world"
|
||||
|
||||
// Comment After
|
||||
} /*Comment 1*/ // Comment After 1
|
||||
// Comment After 2
|
||||
}
|
||||
34
tests/baselines/reference/commentsAfterCaseClauses3.js
Normal file
34
tests/baselines/reference/commentsAfterCaseClauses3.js
Normal file
@ -0,0 +1,34 @@
|
||||
//// [commentsAfterCaseClauses3.ts]
|
||||
function getSecurity(level) {
|
||||
switch(level){
|
||||
case 0: /*Zero*/
|
||||
case 1: /*One*/
|
||||
case 2: /*two*/
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: /*three*/
|
||||
case 4: /*four*/
|
||||
return "hello";
|
||||
case 5: /*five*/
|
||||
default: /*six*/
|
||||
return "world";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//// [commentsAfterCaseClauses3.js]
|
||||
function getSecurity(level) {
|
||||
switch (level) {
|
||||
case 0: /*Zero*/
|
||||
case 1: /*One*/
|
||||
case 2:/*two*/
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: /*three*/
|
||||
case 4:/*four*/
|
||||
return "hello";
|
||||
case 5: /*five*/
|
||||
default:/*six*/
|
||||
return "world";
|
||||
}
|
||||
}
|
||||
22
tests/baselines/reference/commentsAfterCaseClauses3.symbols
Normal file
22
tests/baselines/reference/commentsAfterCaseClauses3.symbols
Normal file
@ -0,0 +1,22 @@
|
||||
=== tests/cases/compiler/commentsAfterCaseClauses3.ts ===
|
||||
function getSecurity(level) {
|
||||
>getSecurity : Symbol(getSecurity, Decl(commentsAfterCaseClauses3.ts, 0, 0))
|
||||
>level : Symbol(level, Decl(commentsAfterCaseClauses3.ts, 0, 21))
|
||||
|
||||
switch(level){
|
||||
>level : Symbol(level, Decl(commentsAfterCaseClauses3.ts, 0, 21))
|
||||
|
||||
case 0: /*Zero*/
|
||||
case 1: /*One*/
|
||||
case 2: /*two*/
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: /*three*/
|
||||
case 4: /*four*/
|
||||
return "hello";
|
||||
case 5: /*five*/
|
||||
default: /*six*/
|
||||
return "world";
|
||||
}
|
||||
|
||||
}
|
||||
39
tests/baselines/reference/commentsAfterCaseClauses3.types
Normal file
39
tests/baselines/reference/commentsAfterCaseClauses3.types
Normal file
@ -0,0 +1,39 @@
|
||||
=== tests/cases/compiler/commentsAfterCaseClauses3.ts ===
|
||||
function getSecurity(level) {
|
||||
>getSecurity : (level: any) => "Hi" | "hello" | "world"
|
||||
>level : any
|
||||
|
||||
switch(level){
|
||||
>level : any
|
||||
|
||||
case 0: /*Zero*/
|
||||
>0 : 0
|
||||
|
||||
case 1: /*One*/
|
||||
>1 : 1
|
||||
|
||||
case 2: /*two*/
|
||||
>2 : 2
|
||||
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
>"Hi" : "Hi"
|
||||
|
||||
case 3: /*three*/
|
||||
>3 : 3
|
||||
|
||||
case 4: /*four*/
|
||||
>4 : 4
|
||||
|
||||
return "hello";
|
||||
>"hello" : "hello"
|
||||
|
||||
case 5: /*five*/
|
||||
>5 : 5
|
||||
|
||||
default: /*six*/
|
||||
return "world";
|
||||
>"world" : "world"
|
||||
}
|
||||
|
||||
}
|
||||
@ -247,28 +247,28 @@ var TypeScript;
|
||||
var addChar = function (index) {
|
||||
var ch = value.charCodeAt(index);
|
||||
switch (ch) {
|
||||
case 0x09:
|
||||
case 0x09:// tab
|
||||
result += "\\t";
|
||||
break;
|
||||
case 0x0a:
|
||||
case 0x0a:// line feed
|
||||
result += "\\n";
|
||||
break;
|
||||
case 0x0b:
|
||||
case 0x0b:// vertical tab
|
||||
result += "\\v";
|
||||
break;
|
||||
case 0x0c:
|
||||
case 0x0c:// form feed
|
||||
result += "\\f";
|
||||
break;
|
||||
case 0x0d:
|
||||
case 0x0d:// carriage return
|
||||
result += "\\r";
|
||||
break;
|
||||
case 0x22:
|
||||
case 0x22:// double quote
|
||||
result += "\\\"";
|
||||
break;
|
||||
case 0x27:
|
||||
case 0x27:// single quote
|
||||
result += "\\\'";
|
||||
break;
|
||||
case 0x5c:
|
||||
case 0x5c:// Backslash
|
||||
result += "\\";
|
||||
break;
|
||||
default:
|
||||
|
||||
@ -912,20 +912,20 @@ var Formatting;
|
||||
Indenter.prototype.GetSpecialCaseIndentation = function (token, node) {
|
||||
var indentationInfo = null;
|
||||
switch (token.Token) {
|
||||
case AuthorTokenKind.atkLCurly:
|
||||
case AuthorTokenKind.atkLCurly:// { is not part of the tree
|
||||
indentationInfo = this.GetSpecialCaseIndentationForLCurly(node);
|
||||
return indentationInfo;
|
||||
case AuthorTokenKind.atkElse: // else is not part of the tree
|
||||
case AuthorTokenKind.atkRBrack:
|
||||
case AuthorTokenKind.atkRBrack:// ] is not part of the tree
|
||||
indentationInfo = node.GetNodeStartLineIndentation(this);
|
||||
return indentationInfo;
|
||||
case AuthorTokenKind.atkRCurly:
|
||||
case AuthorTokenKind.atkRCurly:// } is not part of the tree
|
||||
// if '}' is for a body-block, get indentation based on its parent.
|
||||
if (node.AuthorNode.Details.Kind == AuthorParseNodeKind.apnkBlock && node.AuthorNode.EdgeLabel == AuthorParseNodeEdge.apneBody)
|
||||
node = node.Parent;
|
||||
indentationInfo = node.GetNodeStartLineIndentation(this);
|
||||
return indentationInfo;
|
||||
case AuthorTokenKind.atkWhile:
|
||||
case AuthorTokenKind.atkWhile:// while (in do-while) is not part of the tree
|
||||
if (node.AuthorNode.Details.Kind == AuthorParseNodeKind.apnkDoWhile) {
|
||||
indentationInfo = node.GetNodeStartLineIndentation(this);
|
||||
return indentationInfo;
|
||||
|
||||
@ -35,7 +35,7 @@ var x = 10;
|
||||
switch (x) {
|
||||
case 1:
|
||||
case 2:
|
||||
default:
|
||||
default:// No issues.
|
||||
break;
|
||||
default: // Error; second 'default' clause.
|
||||
default: // Error; third 'default' clause.
|
||||
@ -43,12 +43,12 @@ switch (x) {
|
||||
x *= x;
|
||||
}
|
||||
switch (x) {
|
||||
default:
|
||||
default:// No issues.
|
||||
break;
|
||||
case 100:
|
||||
switch (x * x) {
|
||||
default: // No issues.
|
||||
default:
|
||||
default:// Error; second 'default' clause.
|
||||
break;
|
||||
case 10000:
|
||||
x /= x;
|
||||
|
||||
@ -17,7 +17,7 @@ var x = 10;
|
||||
switch (x) {
|
||||
case 1:
|
||||
case 2:
|
||||
default:
|
||||
default:// No issues.
|
||||
break;
|
||||
default: // Error; second 'default' clause.
|
||||
default: // Error; third 'default' clause.
|
||||
|
||||
14
tests/cases/compiler/commentsAfterCaseClauses1.ts
Normal file
14
tests/cases/compiler/commentsAfterCaseClauses1.ts
Normal file
@ -0,0 +1,14 @@
|
||||
function getSecurity(level) {
|
||||
switch(level){
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2: // two
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4 : // four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default: // default
|
||||
return "world";
|
||||
}
|
||||
}
|
||||
17
tests/cases/compiler/commentsAfterCaseClauses2.ts
Normal file
17
tests/cases/compiler/commentsAfterCaseClauses2.ts
Normal file
@ -0,0 +1,17 @@
|
||||
function getSecurity(level) {
|
||||
switch(level){
|
||||
case 0: // Zero
|
||||
case 1: // one
|
||||
case 2: // two
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: // three
|
||||
case 4: // four
|
||||
return "hello";
|
||||
case 5: // five
|
||||
default: // default
|
||||
return "world";
|
||||
// Comment After
|
||||
} /*Comment 1*/ // Comment After 1
|
||||
// Comment After 2
|
||||
}
|
||||
16
tests/cases/compiler/commentsAfterCaseClauses3.ts
Normal file
16
tests/cases/compiler/commentsAfterCaseClauses3.ts
Normal file
@ -0,0 +1,16 @@
|
||||
function getSecurity(level) {
|
||||
switch(level){
|
||||
case 0: /*Zero*/
|
||||
case 1: /*One*/
|
||||
case 2: /*two*/
|
||||
// Leading comments
|
||||
return "Hi";
|
||||
case 3: /*three*/
|
||||
case 4: /*four*/
|
||||
return "hello";
|
||||
case 5: /*five*/
|
||||
default: /*six*/
|
||||
return "world";
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user