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

Syntax error for unnamed argument in Flow function type inside generic type argument of arrow function return type #616

Open
noppa opened this issue May 15, 2021 · 0 comments

Comments

@noppa
Copy link

noppa commented May 15, 2021

The title is a bit of a mouthful, but what I mean is this:

// @flow
const foo = (): Foo<number => number> => {}

fails to parse with error

sucrase src -d dist -t flow
$ node_modules/.bin/sucrase src -d dist -t flow
src/index.js -> dist/index.js
SyntaxError: Error transforming src/index.js: Unexpected token, expected ";" (2:15)
    at unexpected (node_modules/sucrase/dist/parser/traverser/util.js:83:15)
    at semicolon (node_modules/sucrase/dist/parser/traverser/util.js:61:5)
    at parseVarStatement (node_modules/sucrase/dist/parser/traverser/statement.js:454:19)
    at parseStatementContent (node_modules/sucrase/dist/parser/traverser/statement.js:171:7)
    at parseStatement (node_modules/sucrase/dist/parser/traverser/statement.js:109:3)
    at parseBlockBody (node_modules/sucrase/dist/parser/traverser/statement.js:504:5)
    at parseTopLevel (node_modules/sucrase/dist/parser/traverser/statement.js:85:3)
    at parseFile (node_modules/sucrase/dist/parser/traverser/index.js:17:35)
    at parse (node_modules/sucrase/dist/parser/index.js:26:35)
    at getSucraseContext (node_modules/sucrase/dist/index.js:88:30) {
  pos: 23,
  loc: Loc { line: 2, column: 15 }

The error only reproduces when

  • The argument in function type number => number does not have a name, i.e. not (x: number) => number. This syntax is allowed in Flow
  • The function type is inside a generic type, e.g. Foo<number => number> or Promise<number => number>
  • The whole generic type is the return type of an arrow function, it does not repro in type declarations or return types of named function declarations

The following similar code examples parse just fine:

// @flow
type X = Foo<number => number>
  
const foo1 = (): X => {}

const foo2 = (): Foo<(x:number) => number> => {}

const foo3 = (): (number => number) => {}

function foo4(): Foo<number => number> {}

Although without parenthesis around the return type, the third example actually produces invalid output, but that's probably a separate issue.

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

1 participant