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

Parse error on unnamed optional param in function type annotation #321

Closed
babel-bot opened this issue Jan 22, 2017 · 2 comments · Fixed by #338
Closed

Parse error on unnamed optional param in function type annotation #321

babel-bot opened this issue Jan 22, 2017 · 2 comments · Fixed by #338

Comments

@babel-bot
Copy link

babel-bot commented Jan 22, 2017

Original issue submitted by @jrajav in babel/babel#5191

Annotating a function type in Flowtype with a default parameter (which looks like ?Type) seems to cause a parsing error in the current version of Babel, ^6.21. This could be a regression of babel/babel#2493.

Input Code

const fn: ( Object, ?Object ) => void = ( o1, o2 ) => o1

https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&code=const%20fn%3A%20(%20Object%2C%20%3FObject%20)%20%3D%3E%20void%20%3D%20(%20o1%2C%20o2%20)%20%3D%3E%20o1

Babel Configuration (.babelrc, package.json, cli command)

NOTE: Fails on REPL too so probably irrelevant, see link above. Included anyway just in case:

{
  "presets": [
    "es2015",
    "react"
  ],
  "plugins": [
    "react-hot-loader/babel",
    "ramda",
    ["babel-root-slash-import", {
      "rootPathSuffix": "src"
    }],
    ["extensible-destructuring", {
      "mode": "optout",
      "impl": "immutable"
    }]
  ],
  "env": {
    "test": {
      "plugins": [
        "istanbul"
      ]
    }
  }
}

Expected Behavior

The code parses successfully.

Current Behavior

A parsing error occurs at the default parameter Flow annotation:

Unexpected token, expected ) (1:20)
> 1 | const fn: ( Object, ?Object ) => void = ( o1, o2 ) => o1
@danharper
Copy link
Member

@jrajav temp workaround is to name the parameter in the annotation, e.g.

const fn: ( Object, o2: ?Object ) => void = ( o1, o2 ) => o1

@danharper danharper changed the title Default parameter Flow annotations cause parsing error Parse error on unnamed optional param in function type annotation Jan 22, 2017
@danez danez added the i: bug label Jan 27, 2017
@danez
Copy link
Member

danez commented Jan 27, 2017

This might be a missing case from #197.
//cc @gabelevi

@danez danez added the Has PR label Jan 31, 2017
danez added a commit that referenced this issue Feb 10, 2017
* Fix #321 by allowing question marks in type params

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

Successfully merging a pull request may close this issue.

3 participants