Skip to content

Commit

Permalink
Replace PlaceholderExpression with it
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex committed Jun 9, 2017
1 parent 4c3d2df commit 5395193
Show file tree
Hide file tree
Showing 35 changed files with 279 additions and 81 deletions.
32 changes: 18 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,16 +950,22 @@ export default function (babel) {
return test;
}

function containsAliasReference(path, alias) {
let containsAlias = false;
path.traverse({
ReferencedIdentifier(refPath) {
if (refPath.node.name === alias.name) {
containsAlias = true;
refPath.stop();
}
}
});
return containsAlias;
}

function transformMatchCases(argRef, cases) {
return cases.reduce((rootIf, path) => {

// fill in placeholders
path.get("test").traverse({
PlaceholderExpression(placeholderPath) {
placeholderPath.replaceWith(argRef);
}
});

// add in ===, etc
transformMatchCaseTest(path.get("test"), argRef);

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

definePluginType("PlaceholderExpression", {
aliases: ["Expression"]
});

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

MatchExpression(path) {
const { discriminant } = path.node;
const { discriminant, alias = null } = path.node;

const argRef = path.scope.generateUidIdentifier("it");
const argRef = alias || t.identifier("it");
const matchBody = transformMatchCases(argRef, path.get("cases"));

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

MatchStatement(path) {
const { discriminant } = path.node;
const alias = path.node.alias || t.identifier("it");

let argRef;
if (t.isIdentifier(discriminant)) {
if (t.isIdentifier(discriminant) && !containsAliasReference(path, alias)) {
argRef = discriminant;
} else {
argRef = path.scope.generateUidIdentifier("it");
argRef = alias;
path.insertBefore(t.variableDeclaration("const", [
t.variableDeclarator(argRef, discriminant)
]));
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/match/arr-pattern/actual.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
match x:
| []:
| with []:
"empty"
| [ a, b ]:
| with [ a, b ]:
a - b
| [ a, b = 2 ]:
| with [ a, b = 2 ]:
a + b - 2
| [ a, ...b ]:
| with [ a, ...b ]:
b.concat(a)
| [
| with [
[
b
d = 'e'
Expand Down
26 changes: 13 additions & 13 deletions test/fixtures/match/arr-pattern/exec.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
assert.equal(
"empty",
match []:
| []:
| with []:
"empty"
)
assert.equal(
"empty",
match []:
| []:
| with []:
"empty"
)
assert.equal(
undefined,
match []:
| [a]:
| with [a]:
a + 1
)
assert.equal(
undefined,
match [1]:
| [a, b]:
| with [a, b]:
a + b
)
assert.equal(
5,
match [1]:
| [a, b = 4]:
| with [a, b = 4]:
a + b
)
assert.equal(
3,
match [1, 2]:
| [a, b]:
| with [a, b]:
a + b
)
assert.equal(
4,
match [1, 2, 3]:
| [a,, b]:
| with [a,, b]:
a + b
)
assert.equal(
undefined,
match [1, 2]:
| [a,, b]:
| with [a,, b]:
a + b
)
assert.deepEqual(
[2, 3, 1],
match [1, 2, 3]:
| [a, ...b]:
| with [a, ...b]:
b.concat(a)
)
assert.deepEqual(
[1, 4],
match [4]:
| [a, b = 1, ...c]:
| with [a, b = 1, ...c]:
c.concat([b, a])
)
assert.deepEqual(
[6, 7, 5, 4],
match [4, 5, 6, 7]:
| [a, b = 1, ...c]:
| with [a, b = 1, ...c]:
c.concat([b, a])
)
assert.deepEqual(
[1, 2, 4, 6, 7, 8],
match [[1], [4, 5, 6], 7, 8]:
| [
| with [
[
b
d = 2
Expand All @@ -80,7 +80,7 @@ assert.deepEqual(
assert.deepEqual(
undefined,
match [[1], [4, 5], 7, 8]:
| [
| with [
[
b
d = 2
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/match/as-call-expression/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
z = match foo.bar() as y:
| y < 1: "lt one"
| y + 1 == 1: "eq zero"
| y == 2: "eq two"
| y~f(): "f(x) truthy"
| y.prop: "has prop"
| y.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-call-expression/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const z = (y => {
if (y < 1) {
return "lt one";
} else if (y + 1 === 1) {
return "eq zero";
} else if (y === 2) {
return "eq two";
} else if (f(y)) {
return "f(x) truthy";
} else if (y.prop) {
return "has prop";
} else if (y[0]) {
return "has first child";
}
})(foo.bar());
7 changes: 7 additions & 0 deletions test/fixtures/match/as-call-statement/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
match foo() as y:
| y < 1: "lt one"
| y + 1 == 1: "eq zero"
| y == 2: "eq two"
| y~f(): "f(y) truthy"
| y.prop: "has prop"
| y.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-call-statement/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const y = foo();

if (y < 1) {
"lt one";
} else if (y + 1 === 1) {
"eq zero";
} else if (y === 2) {
"eq two";
} else if (f(y)) {
"f(y) truthy";
} else if (y.prop) {
"has prop";
} else if (y[0]) {
"has first child";
}
7 changes: 7 additions & 0 deletions test/fixtures/match/as-identifier-expression/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
z = match x as y:
| y < 1: "lt one"
| y + 1 == 1: "eq zero"
| y == 2: "eq two"
| y~f(): "f(x) truthy"
| y.prop: "has prop"
| y.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-identifier-expression/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const z = (y => {
if (y < 1) {
return "lt one";
} else if (y + 1 === 1) {
return "eq zero";
} else if (y === 2) {
return "eq two";
} else if (f(y)) {
return "f(x) truthy";
} else if (y.prop) {
return "has prop";
} else if (y[0]) {
return "has first child";
}
})(x);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
match x:
| 1: "foo"
| "1": it
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const it = x;

if (it === 1) {
"foo";
} else if (it === "1") {
it;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
match x:
| 1: "foo"
| "1": "bar"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (x === 1) {
"foo";
} else if (x === "1") {
"bar";
}
7 changes: 7 additions & 0 deletions test/fixtures/match/as-identifier-statement/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
match x as y:
| y < 1: "lt one"
| y + 1 == 1: "eq zero"
| y == 2: "eq two"
| y~f(): "f(x) truthy"
| y.prop: "has prop"
| y.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-identifier-statement/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const y = x;

if (y < 1) {
"lt one";
} else if (y + 1 === 1) {
"eq zero";
} else if (y === 2) {
"eq two";
} else if (f(y)) {
"f(x) truthy";
} else if (y.prop) {
"has prop";
} else if (y[0]) {
"has first child";
}
7 changes: 7 additions & 0 deletions test/fixtures/match/as-it-call-expression/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
z = match foo.bar():
| it < 1: "lt one"
| it + 1 == 1: "eq zero"
| it == 2: "eq two"
| it~f(): "f(x) truthy"
| it.prop: "has prop"
| it.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-it-call-expression/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const z = (it => {
if (it < 1) {
return "lt one";
} else if (it + 1 === 1) {
return "eq zero";
} else if (it === 2) {
return "eq two";
} else if (f(it)) {
return "f(x) truthy";
} else if (it.prop) {
return "has prop";
} else if (it[0]) {
return "has first child";
}
})(foo.bar());
7 changes: 7 additions & 0 deletions test/fixtures/match/as-it-call-statement/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
match foo():
| it < 1: "lt one"
| it + 1 == 1: "eq zero"
| it == 2: "eq two"
| it~f(): "f(x) truthy"
| it.prop: "has prop"
| it.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-it-call-statement/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const it = foo();

if (it < 1) {
"lt one";
} else if (it + 1 === 1) {
"eq zero";
} else if (it === 2) {
"eq two";
} else if (f(it)) {
"f(x) truthy";
} else if (it.prop) {
"has prop";
} else if (it[0]) {
"has first child";
}
7 changes: 7 additions & 0 deletions test/fixtures/match/as-it-identifier-expression/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
z = match x:
| it < 1: "lt one"
| it + 1 == 1: "eq zero"
| it == 2: "eq two"
| it~f(): "f(x) truthy"
| it.prop: "has prop"
| it.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-it-identifier-expression/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const z = (it => {
if (it < 1) {
return "lt one";
} else if (it + 1 === 1) {
return "eq zero";
} else if (it === 2) {
return "eq two";
} else if (f(it)) {
return "f(x) truthy";
} else if (it.prop) {
return "has prop";
} else if (it[0]) {
return "has first child";
}
})(x);
7 changes: 7 additions & 0 deletions test/fixtures/match/as-it-identifier-statement/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
match x:
| it < 1: "lt one"
| it + 1 == 1: "eq zero"
| it == 2: "eq two"
| it~f(): "f(x) truthy"
| it.prop: "has prop"
| it.0: "has first child"
15 changes: 15 additions & 0 deletions test/fixtures/match/as-it-identifier-statement/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const it = x;

if (it < 1) {
"lt one";
} else if (it + 1 === 1) {
"eq zero";
} else if (it === 2) {
"eq two";
} else if (f(it)) {
"f(x) truthy";
} else if (it.prop) {
"has prop";
} else if (it[0]) {
"has first child";
}
Loading

0 comments on commit 5395193

Please sign in to comment.