Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes decaffeinate/decaffeinate#1171 Potentially add-variable-declarations and esnext could be smarter in this case, e.g. by declaring the variable outside the switch or even creating a block scope, but the easy thing is to just keep the declaration as `var`. Since `var` doesn't have a temporal dead zone, a declaration in any switch case will work for all switch cases.
- Loading branch information
1 parent
d12fa14
commit df09db7
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/form/declarations.block-scope/var-in-switch/_expected/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
switch (a) { | ||
case 1: | ||
var x = 1; | ||
console.log(x); | ||
break; | ||
case 2: | ||
x = 2; | ||
console.log(x); | ||
break; | ||
} |
5 changes: 5 additions & 0 deletions
5
test/form/declarations.block-scope/var-in-switch/_expected/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"declarations.block-scope": { | ||
"declarations": [] | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
test/form/declarations.block-scope/var-in-switch/_expected/warnings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
[ | ||
{ | ||
"node": { | ||
"type": "VariableDeclaration", | ||
"start": 27, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 3, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 3, | ||
"column": 14 | ||
} | ||
}, | ||
"declarations": [ | ||
{ | ||
"type": "VariableDeclarator", | ||
"start": 31, | ||
"end": 36, | ||
"loc": { | ||
"start": { | ||
"line": 3, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 3, | ||
"column": 13 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 31, | ||
"end": 32, | ||
"loc": { | ||
"start": { | ||
"line": 3, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 3, | ||
"column": 9 | ||
}, | ||
"identifierName": "x" | ||
}, | ||
"name": "x" | ||
}, | ||
"init": { | ||
"type": "NumericLiteral", | ||
"start": 35, | ||
"end": 36, | ||
"loc": { | ||
"start": { | ||
"line": 3, | ||
"column": 12 | ||
}, | ||
"end": { | ||
"line": 3, | ||
"column": 13 | ||
} | ||
}, | ||
"extra": { | ||
"rawValue": 1, | ||
"raw": "1" | ||
}, | ||
"value": 1 | ||
} | ||
} | ||
], | ||
"kind": "var" | ||
}, | ||
"type": "unsupported-declaration", | ||
"message": "'var' declaration cannot be converted to block scope" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"description": "does not convert `var` declarations within switch cases" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
switch (a) { | ||
case 1: | ||
var x = 1; | ||
console.log(x); | ||
break; | ||
case 2: | ||
x = 2; | ||
console.log(x); | ||
break; | ||
} |