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

Crash when there's no space after generic type parameters #596

Closed
noppa opened this issue Feb 10, 2021 · 3 comments
Closed

Crash when there's no space after generic type parameters #596

noppa opened this issue Feb 10, 2021 · 3 comments

Comments

@noppa
Copy link

noppa commented Feb 10, 2021

Input

type A<B>= {}

causes a crash with both TypeScript and Flow transforms.

Error for the TS transform:

SyntaxError: Error transforming testdir/test.ts: Unexpectedly reached the end of input. (4:1)
    at unexpected (node_modules/sucrase/dist/parser/traverser/util.js:83:15)
    at nextToken (node_modules/sucrase/dist/parser/tokenizer/index.js:251:24)
    at next (node_modules/sucrase/dist/parser/tokenizer/index.js:151:3)
    at parseIdentifier (node_modules/sucrase/dist/parser/traverser/expression.js:9

Adding a space between the > and = fixes the issue.

@alangpierce
Copy link
Owner

Sorry for the delay! Agreed that this is a bug, though I looked into it a bit and it may end up being a tricky fix. Here's what I've found so far:

The underlying issue is that the tokenizer is recognizing >= as a greater-than-or-equal operator. Generally Sucrase will need to pass more context from the parser (which knows that we're parsing a type declaration) to the tokenizer, though simply doing an isType check in the tokenizer's readToken_lt_gt (like we do to handle >>) seems to break other things.

Babel has the same bug (Babel repl example), though it looks like it doesn't have the same problem with Flow, so there might be a reasonable fix by tracing that code path.

TypeScript fixes it by always parsing the token as > and then sometimes calling reScanGreaterToken to turn it into a >= if it's in the right context. It may be possible to translate the same strategy into Sucrase, though it could end up being a fairly big and fragile change.

Another possible approach is to add a special case to tsParseTypeParameters where we accept tt.relationalOrEqual and then correct the tokens in that case. That approach certainly feels like a hack, but it might still be the best fix here.

@noppa
Copy link
Author

noppa commented Apr 12, 2021

Babel has the same bug (Babel repl example), though it looks like it doesn't have the same problem with Flow

Oh that's very interesting, we use Flow so I didn't realize that Babel could also be having trouble with this. Is this perhaps related to the fact that Flow doesn't have <Foo> foo casting syntax like TS does?

Anyway, this probably isn't a high priority blocker or anything. Writing type A<B>= is probably against most style linting rules anyway. We had grand total of one of these instances in our codebase so I just ended up adding a space there.

@alangpierce
Copy link
Owner

I was taking a look at old bugs and noticed that this one seems to be working now! From some testing, it looks like it was fixed in version 3.24.0, most likely from the tokenizer changes in #717 .

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

No branches or pull requests

2 participants