Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Fails to parse Flow-annotated polymorphic methods which name is a keyword #48

Closed
danez opened this issue Jun 22, 2016 · 9 comments
Closed

Comments

@danez
Copy link
Member

danez commented Jun 22, 2016

I started using Flow recently, and was delighted to see that Babel (which I was already using) supported stripping Flow types by default already! I just found a funny issue though:

sebmck: It's being parsed as a JSX tag for some reason.
sebmck: Figured it out exprAllowed is being set to true on delete tokens so the < is being interpreted as the start of a JSX starting tag expression.
thejameskyle: @sebmck is this an easy one to tackle?

class Foo {
  delete<T>(item: T): T {
    return item;
  }
}

copied from https://phabricator.babeljs.io/T1737

@danez
Copy link
Member Author

danez commented Jun 22, 2016

I tried to solve this some months ago, but couldn't figure out how to fix. This not only happens for delete but also for some other keywords, can't remember which. Maybe void.

@danez danez closed this as completed Jun 22, 2016
@danez danez reopened this Jun 22, 2016
@danez
Copy link
Member Author

danez commented Jun 23, 2016

declare class A {
   while<T>(): void; // this parses
   do<T>(): void; // this does not
}

The input code should parse, however it errors at the '<' after the 'do'.

The above fails to parse because the 'do' keyword is marked as {beforeExpr: true} in tokenizer/types.js.
Then tokenizer interprets the '<' after the do as the start of a jsx tag, rather than as a relational '<'.

The 'while' variant works because it is not marked beforeExpr.

The above will repro with any method whose name is a keyword marked with beforeExpr true, which are:
case, default, do, else, return, throw, new, extends, yield, in, instanceof, typeof, void, delete.

https://phabricator.babeljs.io/T7284

@vkurchatkin
Copy link
Contributor

The same for get/set

@vkurchatkin
Copy link
Contributor

Fixed the get/set part: #55

@vkurchatkin
Copy link
Contributor

@danez it seems that your examples work on master

@hzoo hzoo added the i: bug label Jun 24, 2016
@danez
Copy link
Member Author

danez commented Jun 26, 2016

No both are not working yet.

@vkurchatkin
Copy link
Contributor

Hmm, they work for me. Do you have jsx plugin enabled?

@danez
Copy link
Member Author

danez commented Jun 26, 2016

yes

declare class A {
   while<T>(): void; // this parses
   do<T>(): void; // this does not
}
{
  "sourceType": "module",
  "plugins": ["jsx", "flow", "asyncFunctions"]
}
Unexpected token (3:2)

@danharper
Copy link
Member

I've got a WIP of a fix for this. I think there's still some edge cases to cover to match Flow though (or, they might be bugs in Flow..) I'll probably have a PR ready this weekend.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants