Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do expressions transform for switch statements (#10070)
* lint fix * switch inside do expression * review comments * review comments * make min diff * adding more test * review changes * review changes * handle block statement and added more test cases * fix implementation and add more test cases * fix last swtich case * disable block fallthrough case * fix nitpick reviews
- Loading branch information
1 parent
be2cbde
commit 3e4a9d5
Showing
37 changed files
with
776 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...al-do-expressions/test/fixtures/do-expression-switch-case/.fallthrough-multiblock/exec.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,36 @@ | ||
// `blockExpressions` before `break` is hard to deal with | ||
// https://github.com/babel/babel/pull/10070#discussion_r296048106 | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
{ "a"; } | ||
{ "b"; }; | ||
case 1: | ||
{ "c"; } | ||
break; | ||
{ "d"; }; | ||
case 2: | ||
"a"; | ||
"b"; | ||
case 3: | ||
{} | ||
{ break; } | ||
case 4: | ||
{ "d"; } | ||
{ "e"; } | ||
case 5: | ||
"f"; | ||
case 6: | ||
{} | ||
case 7: | ||
} | ||
} | ||
|
||
expect(x(0)).toBe('c') | ||
expect(x(1)).toBe('c') | ||
expect(x(2)).toBeUndefined() | ||
expect(x(3)).toBeUndefined() | ||
expect(x(4)).toBe('f') | ||
expect(x(5)).toBe('f') | ||
expect(x(6)).toBeUndefined() | ||
expect(x(7)).toBeUndefined() |
25 changes: 25 additions & 0 deletions
25
...l-do-expressions/test/fixtures/do-expression-switch-case/.fallthrough-multiblock/input.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,25 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
{ "a"; } | ||
{ "b"; }; | ||
case 1: | ||
{ "c"; } | ||
break; | ||
{ "d"; }; | ||
case 2: | ||
"a"; | ||
"b"; | ||
case 3: | ||
{} | ||
{ break; } | ||
case 4: | ||
{ "a"; } | ||
{ "b"; } | ||
case 5: | ||
"c"; | ||
case 6: | ||
{} | ||
case 7: | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-do-expressions/test/fixtures/do-expression-switch-case/declaration-before-break/input.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,9 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
case 6: | ||
const b = 1; | ||
break; | ||
default: 'bar'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...do-expressions/test/fixtures/do-expression-switch-case/declaration-before-break/output.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,11 @@ | ||
const x = n => function () { | ||
switch (n) { | ||
case 0: | ||
case 6: | ||
const b = 1; | ||
return void 0; | ||
|
||
default: | ||
return 'bar'; | ||
} | ||
}(); |
7 changes: 7 additions & 0 deletions
7
...proposal-do-expressions/test/fixtures/do-expression-switch-case/empty-case-block/input.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,7 @@ | ||
const x = () => do { | ||
switch (new Date().getDay()) { | ||
case 0: | ||
break; | ||
default: "weekday 馃毚" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...roposal-do-expressions/test/fixtures/do-expression-switch-case/empty-case-block/output.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,9 @@ | ||
const x = () => function () { | ||
switch (new Date().getDay()) { | ||
case 0: | ||
return void 0; | ||
|
||
default: | ||
return "weekday 馃毚"; | ||
} | ||
}(); |
26 changes: 26 additions & 0 deletions
26
...sal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-block-last/exec.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,26 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: { | ||
'a'; | ||
} | ||
case 1: { | ||
'b'; | ||
break; | ||
} | ||
default: { | ||
'd'; | ||
} | ||
case 2: { | ||
'c'; | ||
} | ||
case 3: | ||
case 4: | ||
} | ||
} | ||
|
||
expect(x(0)).toBe('b'); | ||
expect(x(1)).toBe('b'); | ||
expect(x(2)).toBe('c'); | ||
expect(x(3)).toBeUndefined(); | ||
expect(x(4)).toBeUndefined(); | ||
expect(x(5)).toBe('c'); |
19 changes: 19 additions & 0 deletions
19
...al-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-block-last/input.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,19 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: { | ||
'a'; | ||
} | ||
case 1: { | ||
'b'; | ||
break; | ||
} | ||
default: { | ||
'd'; | ||
} | ||
case 2: { | ||
'c'; | ||
} | ||
case 3: | ||
case 4: | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...l-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-block-last/output.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,26 @@ | ||
const x = n => function () { | ||
switch (n) { | ||
case 0: | ||
{ | ||
'a'; | ||
} | ||
|
||
case 1: | ||
{ | ||
return 'b'; | ||
} | ||
|
||
default: | ||
{ | ||
'd'; | ||
} | ||
|
||
case 2: | ||
{ | ||
return 'c'; | ||
} | ||
|
||
case 3: | ||
case 4: | ||
} | ||
}(); |
30 changes: 30 additions & 0 deletions
30
...proposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-block/exec.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,30 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: { | ||
'a'; | ||
} | ||
case 1: { | ||
'b'; | ||
break; | ||
} | ||
case 2: { | ||
'c'; | ||
break; | ||
} | ||
case 3: { | ||
break; | ||
} | ||
case 4: { | ||
'd'; | ||
'e'; | ||
} | ||
default: 'f'; | ||
} | ||
} | ||
|
||
expect(x(0)).toBe('b'); | ||
expect(x(1)).toBe('b'); | ||
expect(x(2)).toBe('c'); | ||
expect(x(3)).toBeUndefined(); | ||
expect(x(4)).toBe('f'); | ||
expect(x(5)).toBe('f'); |
23 changes: 23 additions & 0 deletions
23
...roposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-block/input.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,23 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: { | ||
'a'; | ||
} | ||
case 1: { | ||
'b'; | ||
break; | ||
} | ||
case 2: { | ||
'c'; | ||
break; | ||
} | ||
case 3: { | ||
break; | ||
} | ||
case 4: { | ||
'd'; | ||
'e'; | ||
} | ||
default: 'f'; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...oposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-block/output.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,30 @@ | ||
const x = n => function () { | ||
switch (n) { | ||
case 0: | ||
{ | ||
'a'; | ||
} | ||
|
||
case 1: | ||
{ | ||
return 'b'; | ||
} | ||
|
||
case 2: | ||
{ | ||
return 'c'; | ||
} | ||
|
||
case 3: | ||
return void 0; | ||
|
||
case 4: | ||
{ | ||
'd'; | ||
'e'; | ||
} | ||
|
||
default: | ||
return 'f'; | ||
} | ||
}(); |
21 changes: 21 additions & 0 deletions
21
...-proposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-last/exec.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,21 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
'a'; | ||
case 1: | ||
'b'; | ||
break; | ||
default: 'd'; | ||
case 2: | ||
'c'; | ||
case 3: | ||
case 4: | ||
} | ||
} | ||
|
||
expect(x(0)).toBe('b'); | ||
expect(x(1)).toBe('b'); | ||
expect(x(2)).toBe('c'); | ||
expect(x(3)).toBeUndefined(); | ||
expect(x(4)).toBeUndefined(); | ||
expect(x(5)).toBe('c'); |
14 changes: 14 additions & 0 deletions
14
...proposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-last/input.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,14 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
'a'; | ||
case 1: | ||
'b'; | ||
break; | ||
default: 'd'; | ||
case 2: | ||
'c'; | ||
case 3: | ||
case 4: | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...roposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough-last/output.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,18 @@ | ||
const x = n => function () { | ||
switch (n) { | ||
case 0: | ||
'a'; | ||
|
||
case 1: | ||
return 'b'; | ||
|
||
default: | ||
'd'; | ||
|
||
case 2: | ||
return 'c'; | ||
|
||
case 3: | ||
case 4: | ||
} | ||
}(); |
25 changes: 25 additions & 0 deletions
25
...lugin-proposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough/exec.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,25 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
'a'; | ||
case 1: | ||
'b'; | ||
break; | ||
case 2: | ||
'c'; | ||
break; | ||
case 3: | ||
break; | ||
case 4: | ||
'd'; | ||
'e'; | ||
default: 'f'; | ||
} | ||
} | ||
|
||
expect(x(0)).toBe('b'); | ||
expect(x(1)).toBe('b'); | ||
expect(x(2)).toBe('c'); | ||
expect(x(3)).toBeUndefined(); | ||
expect(x(4)).toBe('f'); | ||
expect(x(5)).toBe('f'); |
18 changes: 18 additions & 0 deletions
18
...ugin-proposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough/input.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,18 @@ | ||
const x = (n) => do { | ||
switch (n) { | ||
case 0: | ||
'a'; | ||
case 1: | ||
'b'; | ||
break; | ||
case 2: | ||
'c'; | ||
break; | ||
case 3: | ||
break; | ||
case 4: | ||
'd'; | ||
'e'; | ||
default: 'f'; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...gin-proposal-do-expressions/test/fixtures/do-expression-switch-case/fallthrough/output.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,22 @@ | ||
const x = n => function () { | ||
switch (n) { | ||
case 0: | ||
'a'; | ||
|
||
case 1: | ||
return 'b'; | ||
|
||
case 2: | ||
return 'c'; | ||
|
||
case 3: | ||
return void 0; | ||
|
||
case 4: | ||
'd'; | ||
'e'; | ||
|
||
default: | ||
return 'f'; | ||
} | ||
}(); |
Oops, something went wrong.