Skip to content

Commit 350454b

Browse files
committed
Prevent sibling 'it'
1 parent 8cebb95 commit 350454b

File tree

7 files changed

+61
-3
lines changed

7 files changed

+61
-3
lines changed

src/index.js

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

1012+
function prependDeclaration(path, id, init, kind = "const") {
1013+
path.insertBefore(t.variableDeclaration(kind, [
1014+
t.variableDeclarator(id, init)
1015+
]));
1016+
const constPath = path.getSibling(path.key - 1);
1017+
path.scope.registerBinding(kind, constPath);
1018+
}
1019+
10121020
// TYPE DEFINITIONS
10131021
definePluginType("ForInArrayStatement", {
10141022
visitor: ["idx", "elem", "array", "body"],
@@ -1578,9 +1586,10 @@ export default function (babel) {
15781586
argRef = discriminant;
15791587
} else {
15801588
argRef = alias;
1581-
path.insertBefore(t.variableDeclaration("const", [
1582-
t.variableDeclarator(argRef, discriminant)
1583-
]));
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);
15841593
}
15851594

15861595
const matchBody = transformMatchCases(argRef, path.get("cases"));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
match foo():
2+
| 1: "ok"
3+
4+
match bar():
5+
| 2: "oops"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Cannot re-use `it` binding in the same scope. Try a new shorthand name (eg; `match foo as x:`)."
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match foo():
2+
| 1:
3+
match it:
4+
| 2: it
5+
| with { x }:
6+
match x:
7+
| 2: it
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function _hasProps(obj) { if (obj == null) return false; if (typeof obj !== "object" && typeof obj !== "function") return false; var i = arguments.length; while (--i > 0) { if (!(arguments[i] in obj)) return false; } return true; }
2+
3+
const it = foo();
4+
5+
if (it === 1) {
6+
const it = it;
7+
8+
if (it === 2) {
9+
it;
10+
}
11+
} else if (_hasProps(it, "x")) {
12+
const { x } = it;
13+
const it = x;
14+
15+
if (it === 2) {
16+
it;
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
match foo():
2+
| 1: "ok"
3+
4+
match bar() as x:
5+
| 2: "ok 2"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const it = foo();
2+
3+
if (it === 1) {
4+
"ok";
5+
}
6+
7+
const x = bar();
8+
9+
if (x === 2) {
10+
"ok 2";
11+
}

0 commit comments

Comments
 (0)