forked from babel/babylon
-
Notifications
You must be signed in to change notification settings - Fork 5
Object comprehensions (lightscript/lightscript#6) #7
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
Merged
rattrayalex
merged 1 commit into
lightscript:lightscript
from
wcjohnson:feature/comprehensions
Mar 26, 2017
Merged
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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
1 change: 1 addition & 0 deletions
1
test/fixtures/lightscript/comprehension/for-as-obj-key/actual.js
This file contains hidden or 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 @@ | ||
{for: 4} |
121 changes: 121 additions & 0 deletions
121
test/fixtures/lightscript/comprehension/for-as-obj-key/expected.json
This file contains hidden or 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,121 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"expression": { | ||
"type": "ObjectExpression", | ||
"start": 0, | ||
"end": 8, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 8 | ||
} | ||
}, | ||
"properties": [ | ||
{ | ||
"type": "ObjectProperty", | ||
"start": 1, | ||
"end": 7, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 1 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 7 | ||
} | ||
}, | ||
"method": false, | ||
"shorthand": false, | ||
"computed": false, | ||
"key": { | ||
"type": "Identifier", | ||
"start": 1, | ||
"end": 4, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 1 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 4 | ||
}, | ||
"identifierName": "for" | ||
}, | ||
"name": "for" | ||
}, | ||
"value": { | ||
"type": "NumericLiteral", | ||
"start": 6, | ||
"end": 7, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 6 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 7 | ||
} | ||
}, | ||
"extra": { | ||
"rawValue": 4, | ||
"raw": "4" | ||
}, | ||
"value": 4 | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/lightscript/comprehension/malformed-as-pattern/actual.js
This file contains hidden or 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 @@ | ||
const { for elem x in arr: x, x } = {} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/lightscript/comprehension/malformed-as-pattern/options.json
This file contains hidden or 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 @@ | ||
{ | ||
"throws": "Binding invalid left-hand side in variable declaration (1:6)" | ||
} |
This file contains hidden or 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 @@ | ||
{for idx i in Array(10): (i,i)} |
221 changes: 221 additions & 0 deletions
221
test/fixtures/lightscript/comprehension/object-basic/expected.json
This file contains hidden or 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,221 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 31, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 31 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 31, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 31 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 31, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 31 | ||
} | ||
}, | ||
"expression": { | ||
"type": "ObjectComprehension", | ||
"start": 0, | ||
"end": 31, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 31 | ||
} | ||
}, | ||
"properties": [], | ||
"loop": { | ||
"type": "ForInArrayStatement", | ||
"start": 1, | ||
"end": 30, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 1 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 30 | ||
} | ||
}, | ||
"idx": { | ||
"type": "Identifier", | ||
"start": 9, | ||
"end": 10, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 9 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 10 | ||
}, | ||
"identifierName": "i" | ||
}, | ||
"name": "i" | ||
}, | ||
"body": { | ||
"type": "ExpressionStatement", | ||
"start": 25, | ||
"end": 30, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 25 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 30 | ||
} | ||
}, | ||
"expression": { | ||
"type": "SequenceExpression", | ||
"start": 26, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 26 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 29 | ||
} | ||
}, | ||
"expressions": [ | ||
{ | ||
"type": "Identifier", | ||
"start": 26, | ||
"end": 27, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 26 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 27 | ||
}, | ||
"identifierName": "i" | ||
}, | ||
"name": "i" | ||
}, | ||
{ | ||
"type": "Identifier", | ||
"start": 28, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 28 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 29 | ||
}, | ||
"identifierName": "i" | ||
}, | ||
"name": "i" | ||
} | ||
], | ||
"extra": { | ||
"parenthesized": true, | ||
"parenStart": 25 | ||
} | ||
} | ||
}, | ||
"array": { | ||
"type": "CallExpression", | ||
"start": 14, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 14 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 23 | ||
} | ||
}, | ||
"callee": { | ||
"type": "Identifier", | ||
"start": 14, | ||
"end": 19, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 14 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 19 | ||
}, | ||
"identifierName": "Array" | ||
}, | ||
"name": "Array" | ||
}, | ||
"arguments": [ | ||
{ | ||
"type": "NumericLiteral", | ||
"start": 20, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 20 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
} | ||
}, | ||
"extra": { | ||
"rawValue": 10, | ||
"raw": "10" | ||
}, | ||
"value": 10 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
{for idx i in Array(10) { (i, i) }} |
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.
ah, that does complicate things...
At least we dont have to deal with
myAnnoyingDestructuredObj = { for }
.I'd almost rather just add a restriction to the language saying that if you want an object where the first key is
for
, you need to wrap it in quotes:{ "for": "reasons" }
.It'd be less confusing as a reader in that case anyway. Thoughts?
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.
Generally speaking I am really against weird edge cases like this that programmers have to remember. I think we can fairly frictionlessly integrate with previously-legal JS at the cost of a lookahead in the parser, which is not that big of a deal. My opinion is we should let people write { for: 4} if they really want to.
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 don't have hard numbers on lookahead perf, but I'm pretty sure it's just about the slowest thing that we can do in the parser 😉 compiler perf can really make a difference to UX.
hmm...
Uh oh!
There was an error while loading. Please reload this page.
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've been looking at the lookahead code and I think we can actually do this in a performant way. Since our lookahead here isn't actually relying on the whole parser state, nor do we call any functions during the lookahead, we may be able to do a "cheap" lookahead where we ignore the parser state altogether.
That should eliminate any object copying and shouldn't be much more expensive than calling
substr
or whatever the tokenizer does.This is the sort of thing I am less likely to worry about than getting the semantics right, but hey, everyone wants their code to compile fast.
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'm pretty nervous about doing something like that... My impression of the way the parser is set up makes me think that could really open a can of worms... would much rather just take the perf hit