Skip to content

Commit badff46

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

File tree

8 files changed

+73
-15
lines changed

8 files changed

+73
-15
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.hasOwnBinding(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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function f() {
2+
const it = foo();
3+
4+
if (it === 1) {
5+
return "ok";
6+
} else if (it === 2) {
7+
bar();
8+
const it = baz();
9+
10+
if (it === 3) {
11+
return qux();
12+
}
13+
}
14+
}
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)