Skip to content

Commit 5395193

Browse files
committed
Replace PlaceholderExpression with it
1 parent 4c3d2df commit 5395193

File tree

35 files changed

+279
-81
lines changed

35 files changed

+279
-81
lines changed

src/index.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -950,16 +950,22 @@ export default function (babel) {
950950
return test;
951951
}
952952

953+
function containsAliasReference(path, alias) {
954+
let containsAlias = false;
955+
path.traverse({
956+
ReferencedIdentifier(refPath) {
957+
if (refPath.node.name === alias.name) {
958+
containsAlias = true;
959+
refPath.stop();
960+
}
961+
}
962+
});
963+
return containsAlias;
964+
}
965+
953966
function transformMatchCases(argRef, cases) {
954967
return cases.reduce((rootIf, path) => {
955968

956-
// fill in placeholders
957-
path.get("test").traverse({
958-
PlaceholderExpression(placeholderPath) {
959-
placeholderPath.replaceWith(argRef);
960-
}
961-
});
962-
963969
// add in ===, etc
964970
transformMatchCaseTest(path.get("test"), argRef);
965971

@@ -1243,9 +1249,6 @@ export default function (babel) {
12431249
// only allowed in MatchCase, so don't alias to Expression
12441250
});
12451251

1246-
definePluginType("PlaceholderExpression", {
1247-
aliases: ["Expression"]
1248-
});
12491252

12501253
// traverse as top-level item so as to run before other babel plugins
12511254
// (and avoid traversing any of their output)
@@ -1547,9 +1550,9 @@ export default function (babel) {
15471550
},
15481551

15491552
MatchExpression(path) {
1550-
const { discriminant } = path.node;
1553+
const { discriminant, alias = null } = path.node;
15511554

1552-
const argRef = path.scope.generateUidIdentifier("it");
1555+
const argRef = alias || t.identifier("it");
15531556
const matchBody = transformMatchCases(argRef, path.get("cases"));
15541557

15551558
const iife = t.callExpression(
@@ -1561,12 +1564,13 @@ export default function (babel) {
15611564

15621565
MatchStatement(path) {
15631566
const { discriminant } = path.node;
1567+
const alias = path.node.alias || t.identifier("it");
15641568

15651569
let argRef;
1566-
if (t.isIdentifier(discriminant)) {
1570+
if (t.isIdentifier(discriminant) && !containsAliasReference(path, alias)) {
15671571
argRef = discriminant;
15681572
} else {
1569-
argRef = path.scope.generateUidIdentifier("it");
1573+
argRef = alias;
15701574
path.insertBefore(t.variableDeclaration("const", [
15711575
t.variableDeclarator(argRef, discriminant)
15721576
]));

test/fixtures/match/arr-pattern/actual.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
match x:
2-
| []:
2+
| with []:
33
"empty"
4-
| [ a, b ]:
4+
| with [ a, b ]:
55
a - b
6-
| [ a, b = 2 ]:
6+
| with [ a, b = 2 ]:
77
a + b - 2
8-
| [ a, ...b ]:
8+
| with [ a, ...b ]:
99
b.concat(a)
10-
| [
10+
| with [
1111
[
1212
b
1313
d = 'e'

test/fixtures/match/arr-pattern/exec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
assert.equal(
22
"empty",
33
match []:
4-
| []:
4+
| with []:
55
"empty"
66
)
77
assert.equal(
88
"empty",
99
match []:
10-
| []:
10+
| with []:
1111
"empty"
1212
)
1313
assert.equal(
1414
undefined,
1515
match []:
16-
| [a]:
16+
| with [a]:
1717
a + 1
1818
)
1919
assert.equal(
2020
undefined,
2121
match [1]:
22-
| [a, b]:
22+
| with [a, b]:
2323
a + b
2424
)
2525
assert.equal(
2626
5,
2727
match [1]:
28-
| [a, b = 4]:
28+
| with [a, b = 4]:
2929
a + b
3030
)
3131
assert.equal(
3232
3,
3333
match [1, 2]:
34-
| [a, b]:
34+
| with [a, b]:
3535
a + b
3636
)
3737
assert.equal(
3838
4,
3939
match [1, 2, 3]:
40-
| [a,, b]:
40+
| with [a,, b]:
4141
a + b
4242
)
4343
assert.equal(
4444
undefined,
4545
match [1, 2]:
46-
| [a,, b]:
46+
| with [a,, b]:
4747
a + b
4848
)
4949
assert.deepEqual(
5050
[2, 3, 1],
5151
match [1, 2, 3]:
52-
| [a, ...b]:
52+
| with [a, ...b]:
5353
b.concat(a)
5454
)
5555
assert.deepEqual(
5656
[1, 4],
5757
match [4]:
58-
| [a, b = 1, ...c]:
58+
| with [a, b = 1, ...c]:
5959
c.concat([b, a])
6060
)
6161
assert.deepEqual(
6262
[6, 7, 5, 4],
6363
match [4, 5, 6, 7]:
64-
| [a, b = 1, ...c]:
64+
| with [a, b = 1, ...c]:
6565
c.concat([b, a])
6666
)
6767
assert.deepEqual(
6868
[1, 2, 4, 6, 7, 8],
6969
match [[1], [4, 5, 6], 7, 8]:
70-
| [
70+
| with [
7171
[
7272
b
7373
d = 2
@@ -80,7 +80,7 @@ assert.deepEqual(
8080
assert.deepEqual(
8181
undefined,
8282
match [[1], [4, 5], 7, 8]:
83-
| [
83+
| with [
8484
[
8585
b
8686
d = 2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
z = match foo.bar() as y:
2+
| y < 1: "lt one"
3+
| y + 1 == 1: "eq zero"
4+
| y == 2: "eq two"
5+
| y~f(): "f(x) truthy"
6+
| y.prop: "has prop"
7+
| y.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const z = (y => {
2+
if (y < 1) {
3+
return "lt one";
4+
} else if (y + 1 === 1) {
5+
return "eq zero";
6+
} else if (y === 2) {
7+
return "eq two";
8+
} else if (f(y)) {
9+
return "f(x) truthy";
10+
} else if (y.prop) {
11+
return "has prop";
12+
} else if (y[0]) {
13+
return "has first child";
14+
}
15+
})(foo.bar());
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match foo() as y:
2+
| y < 1: "lt one"
3+
| y + 1 == 1: "eq zero"
4+
| y == 2: "eq two"
5+
| y~f(): "f(y) truthy"
6+
| y.prop: "has prop"
7+
| y.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const y = foo();
2+
3+
if (y < 1) {
4+
"lt one";
5+
} else if (y + 1 === 1) {
6+
"eq zero";
7+
} else if (y === 2) {
8+
"eq two";
9+
} else if (f(y)) {
10+
"f(y) truthy";
11+
} else if (y.prop) {
12+
"has prop";
13+
} else if (y[0]) {
14+
"has first child";
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
z = match x as y:
2+
| y < 1: "lt one"
3+
| y + 1 == 1: "eq zero"
4+
| y == 2: "eq two"
5+
| y~f(): "f(x) truthy"
6+
| y.prop: "has prop"
7+
| y.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const z = (y => {
2+
if (y < 1) {
3+
return "lt one";
4+
} else if (y + 1 === 1) {
5+
return "eq zero";
6+
} else if (y === 2) {
7+
return "eq two";
8+
} else if (f(y)) {
9+
return "f(x) truthy";
10+
} else if (y.prop) {
11+
return "has prop";
12+
} else if (y[0]) {
13+
return "has first child";
14+
}
15+
})(x);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
match x:
2+
| 1: "foo"
3+
| "1": it
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const it = x;
2+
3+
if (it === 1) {
4+
"foo";
5+
} else if (it === "1") {
6+
it;
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
match x:
2+
| 1: "foo"
3+
| "1": "bar"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (x === 1) {
2+
"foo";
3+
} else if (x === "1") {
4+
"bar";
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match x as y:
2+
| y < 1: "lt one"
3+
| y + 1 == 1: "eq zero"
4+
| y == 2: "eq two"
5+
| y~f(): "f(x) truthy"
6+
| y.prop: "has prop"
7+
| y.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const y = x;
2+
3+
if (y < 1) {
4+
"lt one";
5+
} else if (y + 1 === 1) {
6+
"eq zero";
7+
} else if (y === 2) {
8+
"eq two";
9+
} else if (f(y)) {
10+
"f(x) truthy";
11+
} else if (y.prop) {
12+
"has prop";
13+
} else if (y[0]) {
14+
"has first child";
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
z = match foo.bar():
2+
| it < 1: "lt one"
3+
| it + 1 == 1: "eq zero"
4+
| it == 2: "eq two"
5+
| it~f(): "f(x) truthy"
6+
| it.prop: "has prop"
7+
| it.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const z = (it => {
2+
if (it < 1) {
3+
return "lt one";
4+
} else if (it + 1 === 1) {
5+
return "eq zero";
6+
} else if (it === 2) {
7+
return "eq two";
8+
} else if (f(it)) {
9+
return "f(x) truthy";
10+
} else if (it.prop) {
11+
return "has prop";
12+
} else if (it[0]) {
13+
return "has first child";
14+
}
15+
})(foo.bar());
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match foo():
2+
| it < 1: "lt one"
3+
| it + 1 == 1: "eq zero"
4+
| it == 2: "eq two"
5+
| it~f(): "f(x) truthy"
6+
| it.prop: "has prop"
7+
| it.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const it = foo();
2+
3+
if (it < 1) {
4+
"lt one";
5+
} else if (it + 1 === 1) {
6+
"eq zero";
7+
} else if (it === 2) {
8+
"eq two";
9+
} else if (f(it)) {
10+
"f(x) truthy";
11+
} else if (it.prop) {
12+
"has prop";
13+
} else if (it[0]) {
14+
"has first child";
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
z = match x:
2+
| it < 1: "lt one"
3+
| it + 1 == 1: "eq zero"
4+
| it == 2: "eq two"
5+
| it~f(): "f(x) truthy"
6+
| it.prop: "has prop"
7+
| it.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const z = (it => {
2+
if (it < 1) {
3+
return "lt one";
4+
} else if (it + 1 === 1) {
5+
return "eq zero";
6+
} else if (it === 2) {
7+
return "eq two";
8+
} else if (f(it)) {
9+
return "f(x) truthy";
10+
} else if (it.prop) {
11+
return "has prop";
12+
} else if (it[0]) {
13+
return "has first child";
14+
}
15+
})(x);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match x:
2+
| it < 1: "lt one"
3+
| it + 1 == 1: "eq zero"
4+
| it == 2: "eq two"
5+
| it~f(): "f(x) truthy"
6+
| it.prop: "has prop"
7+
| it.0: "has first child"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const it = x;
2+
3+
if (it < 1) {
4+
"lt one";
5+
} else if (it + 1 === 1) {
6+
"eq zero";
7+
} else if (it === 2) {
8+
"eq two";
9+
} else if (f(it)) {
10+
"f(x) truthy";
11+
} else if (it.prop) {
12+
"has prop";
13+
} else if (it[0]) {
14+
"has first child";
15+
}

0 commit comments

Comments
 (0)