Skip to content

Commit

Permalink
Allow sibling matches to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex committed Jun 10, 2017
1 parent 350454b commit 1e720c1
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 23 deletions.
28 changes: 16 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ export default function (babel) {
}, null);
}

function prependDeclaration(path, id, init, kind = "const") {
function insertDeclarationBefore(path, id, init, kind = "const") {
path.insertBefore(t.variableDeclaration(kind, [
t.variableDeclarator(id, init)
]));
Expand Down Expand Up @@ -1581,19 +1581,23 @@ export default function (babel) {
const { discriminant } = path.node;
const alias = path.node.alias || t.identifier("it");

let argRef;
if (t.isIdentifier(discriminant) && !containsAliasReference(path, alias)) {
argRef = discriminant;
} else {
argRef = alias;
if (path.scope.hasOwnBinding(argRef.name)) {
throw path.buildCodeFrameError("Cannot re-use `it` binding in the same scope. Try a new shorthand name (eg; `match foo as x:`).");
}
prependDeclaration(path, argRef, discriminant);
}

const argRef = t.isIdentifier(discriminant) && !containsAliasReference(path, alias)
? discriminant
: alias;
const matchBody = transformMatchCases(argRef, path.get("cases"));

path.replaceWith(matchBody);

// insert eg; `const it = foo()` above the body,
// possibly wrapping both in an anonymous block.
if (argRef !== discriminant) {
if (path.scope.hasBinding(alias.name)) {
// wrap in anonymous block
path.replaceWith(t.blockStatement([path.node]));
path = path.get("body.0");
}
insertDeclarationBefore(path, argRef, discriminant);
}
},

});
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/match/illegal-sibling-no-as/options.json

This file was deleted.

9 changes: 9 additions & 0 deletions test/fixtures/match/implicit-return/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
f() ->
match foo():
| 1:
"ok"
| 2:
bar()
match baz():
| 3:
qux()
16 changes: 16 additions & 0 deletions test/fixtures/match/implicit-return/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function f() {
const it = foo();

if (it === 1) {
return "ok";
} else if (it === 2) {
bar();
{
const it = baz();

if (it === 3) {
return qux();
}
}
}
}
20 changes: 12 additions & 8 deletions test/fixtures/match/nested-it/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ function _hasProps(obj) { if (obj == null) return false; if (typeof obj !== "obj
const it = foo();

if (it === 1) {
const it = it;
{
const it = it;

if (it === 2) {
it;
}
if (it === 2) {
it;
}
}
} else if (_hasProps(it, "x")) {
const { x } = it;
const it = x;
{
const it = x;

if (it === 2) {
it;
}
if (it === 2) {
it;
}
}
}
6 changes: 6 additions & 0 deletions test/fixtures/match/shadow-it/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
it = "good"
f() ->
match "bad":
| true: true

it
8 changes: 8 additions & 0 deletions test/fixtures/match/shadow-it/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it = "good"
f() ->
match "bad":
| true: true

it

assert.equal("good", f())
12 changes: 12 additions & 0 deletions test/fixtures/match/shadow-it/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const it = "good";

function f() {
{
const it = "bad";

if (it === true) {
true;
}
}
return it;
}
6 changes: 6 additions & 0 deletions test/fixtures/match/sibling-no-as-implicit-returns/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
f() ->
match foo():
| 1: "ok"

match bar():
| 2: "oops"
15 changes: 15 additions & 0 deletions test/fixtures/match/sibling-no-as-implicit-returns/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function f() {
const it = foo();

if (it === 1) {
"ok";
}

{
const it = bar();

if (it === 2) {
return "oops";
}
}
}
File renamed without changes.
13 changes: 13 additions & 0 deletions test/fixtures/match/sibling-no-as/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const it = foo();

if (it === 1) {
"ok";
}

{
const it = bar();

if (it === 2) {
"oops";
}
}

0 comments on commit 1e720c1

Please sign in to comment.