Skip to content

Commit 92d84aa

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

File tree

35 files changed

+279
-71
lines changed

35 files changed

+279
-71
lines changed

src/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,19 @@ 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

@@ -1547,9 +1560,9 @@ export default function (babel) {
15471560
},
15481561

15491562
MatchExpression(path) {
1550-
const { discriminant } = path.node;
1563+
const { discriminant, alias = null } = path.node;
15511564

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

15551568
const iife = t.callExpression(
@@ -1561,12 +1574,13 @@ export default function (babel) {
15611574

15621575
MatchStatement(path) {
15631576
const { discriminant } = path.node;
1577+
const alias = path.node.alias || t.identifier("it");
15641578

15651579
let argRef;
1566-
if (t.isIdentifier(discriminant)) {
1580+
if (t.isIdentifier(discriminant) && !containsAliasReference(path, alias)) {
15671581
argRef = discriminant;
15681582
} else {
1569-
argRef = path.scope.generateUidIdentifier("it");
1583+
argRef = alias;
15701584
path.insertBefore(t.variableDeclaration("const", [
15711585
t.variableDeclarator(argRef, discriminant)
15721586
]));

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+
}

test/fixtures/match/basic-placeholder/actual.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/fixtures/match/basic-placeholder/expected.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)