-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pattern Matching #22
Open
rattrayalex
wants to merge
7
commits into
master
Choose a base branch
from
match
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pattern Matching #22
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
64bd12b
Pattern Matching WIP
rattrayalex a4f700c
wip
rattrayalex 4c3d2df
wip more tests
rattrayalex 5395193
Replace PlaceholderExpression with it
rattrayalex 8cebb95
Disallow object and array literals, cleanup hasProps
rattrayalex 350454b
Prevent sibling 'it'
rattrayalex 1e720c1
Allow sibling matches to use it
rattrayalex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
match x: | ||
| with []: | ||
"empty" | ||
| with [ a, b ]: | ||
a - b | ||
| with [ a, b = 2 ]: | ||
a + b - 2 | ||
| with [ a, ...b ]: | ||
b.concat(a) | ||
| with [ | ||
[ | ||
b | ||
d = 'e' | ||
] | ||
[ g, , h ] | ||
...j | ||
]: | ||
[b, d, g, ...j].join('') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
assert.equal( | ||
"empty", | ||
match []: | ||
| with []: | ||
"empty" | ||
) | ||
assert.equal( | ||
"empty", | ||
match []: | ||
| with []: | ||
"empty" | ||
) | ||
assert.equal( | ||
undefined, | ||
match []: | ||
| with [a]: | ||
a + 1 | ||
) | ||
assert.equal( | ||
undefined, | ||
match [1]: | ||
| with [a, b]: | ||
a + b | ||
) | ||
assert.equal( | ||
5, | ||
match [1]: | ||
| with [a, b = 4]: | ||
a + b | ||
) | ||
assert.equal( | ||
3, | ||
match [1, 2]: | ||
| with [a, b]: | ||
a + b | ||
) | ||
assert.equal( | ||
4, | ||
match [1, 2, 3]: | ||
| with [a,, b]: | ||
a + b | ||
) | ||
assert.equal( | ||
undefined, | ||
match [1, 2]: | ||
| with [a,, b]: | ||
a + b | ||
) | ||
assert.deepEqual( | ||
[2, 3, 1], | ||
match [1, 2, 3]: | ||
| with [a, ...b]: | ||
b.concat(a) | ||
) | ||
assert.deepEqual( | ||
[1, 4], | ||
match [4]: | ||
| with [a, b = 1, ...c]: | ||
c.concat([b, a]) | ||
) | ||
assert.deepEqual( | ||
[6, 7, 5, 4], | ||
match [4, 5, 6, 7]: | ||
| 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 | ||
] | ||
[ g, , h ] | ||
...j | ||
]: | ||
[b, d, g, h, ...j] | ||
) | ||
assert.deepEqual( | ||
undefined, | ||
match [[1], [4, 5], 7, 8]: | ||
| with [ | ||
[ | ||
b | ||
d = 2 | ||
] | ||
[ g, , h ] | ||
...j | ||
]: | ||
[b, d, g, h, ...j] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function _hasLength(arr, minLength, maxLength) { minLength = minLength || 0; maxLength = maxLength != null ? maxLength : Number.MAX_SAFE_INTEGER; return arr != null && typeof arr !== "function" && arr.length === arr.length | 0 && arr.length >= minLength && arr.length <= maxLength; } | ||
|
||
if (_hasLength(x, 0, 0)) { | ||
const [] = x; | ||
|
||
"empty"; | ||
} else if (_hasLength(x, 2, 2)) { | ||
const [a, b] = x; | ||
|
||
a - b; | ||
} else if (_hasLength(x, 1, 2)) { | ||
const [a, b = 2] = x; | ||
|
||
a + b - 2; | ||
} else if (_hasLength(x, 1)) { | ||
const [a, ...b] = x; | ||
|
||
b.concat(a); | ||
} else if (_hasLength(x, 2) && _hasLength(x[0], 1, 2) && _hasLength(x[1], 3, 3)) { | ||
const [[b, d = 'e'], [g,, h], ...j] = x; | ||
|
||
[b, d, g, ...j].join(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
3 changes: 3 additions & 0 deletions
3
test/fixtures/match/as-identifier-statement-body-reference/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
match x: | ||
| 1: "foo" | ||
| "1": it |
7 changes: 7 additions & 0 deletions
7
test/fixtures/match/as-identifier-statement-body-reference/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/match/as-identifier-statement-not-referenced/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
match x: | ||
| 1: "foo" | ||
| "1": "bar" |
5 changes: 5 additions & 0 deletions
5
test/fixtures/match/as-identifier-statement-not-referenced/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if (x === 1) { | ||
"foo"; | ||
} else if (x === "1") { | ||
"bar"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
test/fixtures/match/as-it-identifier-expression/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
test/fixtures/match/as-it-identifier-statement/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
match foo(): | ||
| 1: | ||
"one" | ||
| 2: | ||
"two" | ||
| else: | ||
"idk" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const it = foo(); | ||
|
||
if (it === 1) { | ||
"one"; | ||
} else if (it === 2) { | ||
"two"; | ||
} else { | ||
"idk"; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I liked this way of doing helpers a lot, so I tried to adopt it. When I applied it to my local build of
lscdiag
I got some very strange code output in the generated helpers. Took me a while to figure out why -- turns out that minifiers mangle these things and thenfn.toString()
brings in the minified code.We could just say we don't care about that, but I think live compilers and minifiers are good things generally, whereas this, while neat, could just be replaced with a plain string passed to babel-template. I recommend the latter approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, yeah, that's why. I kind of don't mind – it's babel helping make sure that I'm writing cross-browser code. But yes, if you see the tests, the output isn't the exact same as what I wrote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't sound quite right... the transform being run here is just the one specified in
.babelrc
for the plugin, which is justenv: { node: 4 }
-- that's definitely not cross browser code.I think having cross-browser code is a good point, but might be better to paste the helper into babel-repl, set the env to Netscape Navigator 1.0, and paste the output into a plain string.
Alternatively, an extra build step could compile the helpers with a different env? But that seems like overtooling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My current implementation of
hasProps
andhasLength
doesn't change due to this factor, which I think is desirable, so I think I'll leave this as-is for now, hope that's okThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I hadn't read that comment very carefully – I did check in the babel repl that with an ie<6 target the output doesn't change