Skip to content
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

Add support for @@iterator #7058

Merged
merged 8 commits into from
Jan 12, 2018
Merged

Add support for @@iterator #7058

merged 8 commits into from
Jan 12, 2018

Conversation

rajasekarm
Copy link
Member

Q                       A
Fixed Issues? Fixes #7053
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR
Any Dependency Changes?
License MIT

@babel-bot
Copy link
Collaborator

babel-bot commented Dec 19, 2017

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6584/

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There is also @@asyncIterator: https://github.com/facebook/flow/blob/4eb62a5597d59174531d44e532c65daa25d8331b/src/parser/lexer.ml#L805-L807
  2. Should the @@iterator identifier be allowed everywhere? e.g. let @@iterator = 2;

@@ -1231,6 +1231,9 @@ export default class Tokenizer extends LocationParser {
const ch = this.fullCharCodeAtPos();
if (isIdentifierChar(ch)) {
this.state.pos += ch <= 0xffff ? 1 : 2;
} else if (this.state.isIterator) {
this.state.pos += 2;
delete this.state.isIterator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use this.state.isIterator here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this.state.isIterator = false lol

@hzoo
Copy link
Member

hzoo commented Dec 21, 2017

@nicolo-ribaudo it's a flow syntax not related to JS, it would error with let @@iterator = 1? Also funny since the link to flow says TODO switch with Symbol.iterator?

@nicolo-ribaudo
Copy link
Member

Also funny since the link to flow says TODO switch with Symbol.iterator?

We should ask to the flow team if they plan to remove @@iterator in the near future.

@hzoo
Copy link
Member

hzoo commented Dec 21, 2017

Should ping the flow team on all the flow prs anyway 😛 @babel/flow

@rajasekarm
Copy link
Member Author

Also we should not allow any other identifier iterator and asyncIterator. I'll update the PR

@rajasekarm
Copy link
Member Author

rajasekarm commented Dec 22, 2017

@nicolo-ribaudo

Should the @@iterator identifier be allowed everywhere? e.g. let @@iterator = 2;

Flow parse the above syntax, but in V8 throws an error.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Dec 22, 2017

Flow parse the above syntax

I checked and flow paeses that syntax sometimes (e.g. it accepts let @@iterator = number but not type @@iterator = number).
I think it's ok for us to allow it only in class/object/interface property types.

@nicolo-ribaudo
Copy link
Member

I have thought about this a little, and I think that we should allow @@iterator only in class/object/interface.
The flow parser is more lenient and accepts things like

function @@iterator() {}
@@iterator();

but I think that we shouldn't parse it since it will generate invalid JavaScript output.

So, instead of throwing if @@iterator is after var/let/type we should throw if it is not inside an object/interface/class property name.

@existentialism
Copy link
Member

@nicolo-ribaudo agreed

@existentialism existentialism added pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jan 3, 2018
@loganfsmyth
Copy link
Member

@danez Would you like to look this over since you're familiar with the parser? It seems fine to me, but I'm also not an expert on it.

Copy link
Member

@danez danez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me, but there are some remaining issues and untested things.

  1. Flow-strip-types probably needs to remove the @@iterator and @@asyncIterator, as the resulting code is otherwise invalid?

  2. Does babel-generator know how to reprint the examples which are added in the babylon tests? Can we add a simple test for this? (We should really have shared testcases for babylon and generator at some point, as everything that is parsable should be printable I guess)

@nicolo-ribaudo
Copy link
Member

Flow-strip-types probably needs to remove the @@iterator and @@asyncIterator, as the resulting code is otherwise invalid?

Currently Flow only removes them from types.

@xtuc
Copy link
Member

xtuc commented Jan 9, 2018

Looks good to me. As mentioned by @danez, babel-generator will probably need to be updated in order to emit this syntax and could you please add a test for it?

It could probably also impact TypeScript? @hzoo could we ping them? I don't think that could break anything but maybe require a transformations (I'm not a TS expert).

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actual/expected -> input/output in tests 😛

@rajasekarm
Copy link
Member Author

@nicolo-ribaudo updated the test cases.

@rajasekarm
Copy link
Member Author

@danez I added test cases, to make sure iterator is removed from types in Flow-strip-types. 36a0731, Do you want to add test case for babel-generator, since it is already removed by Flow-strip-types

@nicolo-ribaudo
Copy link
Member

Yes, since someone might use Babel for a codemod and thus not remove Flow types.

@rajasekarm
Copy link
Member Author

@nicolo-ribaudo @danez added test case for generator and Flow-strip-types plugin

@@ -0,0 +1,15 @@
declare class A {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the output for this file is missing?

Copy link
Member

@existentialism existentialism Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@existentialism Thanks!!

@existentialism existentialism merged commit 2d05487 into babel:master Jan 12, 2018
@existentialism
Copy link
Member

@rajzshkr thanks!

@rajasekarm rajasekarm deleted the bug-7053 branch January 12, 2018 15:33
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[babylon] Add support for @@iterator
8 participants