Skip to content

Commit 1e720c1

Browse files
committed
Allow sibling matches to use it
1 parent 350454b commit 1e720c1

File tree

12 files changed

+113
-23
lines changed

12 files changed

+113
-23
lines changed

src/index.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ export default function (babel) {
10091009
}, null);
10101010
}
10111011

1012-
function prependDeclaration(path, id, init, kind = "const") {
1012+
function insertDeclarationBefore(path, id, init, kind = "const") {
10131013
path.insertBefore(t.variableDeclaration(kind, [
10141014
t.variableDeclarator(id, init)
10151015
]));
@@ -1581,19 +1581,23 @@ export default function (babel) {
15811581
const { discriminant } = path.node;
15821582
const alias = path.node.alias || t.identifier("it");
15831583

1584-
let argRef;
1585-
if (t.isIdentifier(discriminant) && !containsAliasReference(path, alias)) {
1586-
argRef = discriminant;
1587-
} else {
1588-
argRef = alias;
1589-
if (path.scope.hasOwnBinding(argRef.name)) {
1590-
throw path.buildCodeFrameError("Cannot re-use `it` binding in the same scope. Try a new shorthand name (eg; `match foo as x:`).");
1591-
}
1592-
prependDeclaration(path, argRef, discriminant);
1593-
}
1594-
1584+
const argRef = t.isIdentifier(discriminant) && !containsAliasReference(path, alias)
1585+
? discriminant
1586+
: alias;
15951587
const matchBody = transformMatchCases(argRef, path.get("cases"));
1588+
15961589
path.replaceWith(matchBody);
1590+
1591+
// insert eg; `const it = foo()` above the body,
1592+
// possibly wrapping both in an anonymous block.
1593+
if (argRef !== discriminant) {
1594+
if (path.scope.hasBinding(alias.name)) {
1595+
// wrap in anonymous block
1596+
path.replaceWith(t.blockStatement([path.node]));
1597+
path = path.get("body.0");
1598+
}
1599+
insertDeclarationBefore(path, argRef, discriminant);
1600+
}
15971601
},
15981602

15991603
});

test/fixtures/match/illegal-sibling-no-as/options.json

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
f() ->
2+
match foo():
3+
| 1:
4+
"ok"
5+
| 2:
6+
bar()
7+
match baz():
8+
| 3:
9+
qux()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function f() {
2+
const it = foo();
3+
4+
if (it === 1) {
5+
return "ok";
6+
} else if (it === 2) {
7+
bar();
8+
{
9+
const it = baz();
10+
11+
if (it === 3) {
12+
return qux();
13+
}
14+
}
15+
}
16+
}

test/fixtures/match/nested-it/expected.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ function _hasProps(obj) { if (obj == null) return false; if (typeof obj !== "obj
33
const it = foo();
44

55
if (it === 1) {
6-
const it = it;
6+
{
7+
const it = it;
78

8-
if (it === 2) {
9-
it;
10-
}
9+
if (it === 2) {
10+
it;
11+
}
12+
}
1113
} else if (_hasProps(it, "x")) {
1214
const { x } = it;
13-
const it = x;
15+
{
16+
const it = x;
1417

15-
if (it === 2) {
16-
it;
17-
}
18+
if (it === 2) {
19+
it;
20+
}
21+
}
1822
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
it = "good"
2+
f() ->
3+
match "bad":
4+
| true: true
5+
6+
it

test/fixtures/match/shadow-it/exec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
it = "good"
2+
f() ->
3+
match "bad":
4+
| true: true
5+
6+
it
7+
8+
assert.equal("good", f())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const it = "good";
2+
3+
function f() {
4+
{
5+
const it = "bad";
6+
7+
if (it === true) {
8+
true;
9+
}
10+
}
11+
return it;
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
f() ->
2+
match foo():
3+
| 1: "ok"
4+
5+
match bar():
6+
| 2: "oops"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function f() {
2+
const it = foo();
3+
4+
if (it === 1) {
5+
"ok";
6+
}
7+
8+
{
9+
const it = bar();
10+
11+
if (it === 2) {
12+
return "oops";
13+
}
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const it = foo();
2+
3+
if (it === 1) {
4+
"ok";
5+
}
6+
7+
{
8+
const it = bar();
9+
10+
if (it === 2) {
11+
"oops";
12+
}
13+
}

0 commit comments

Comments
 (0)