facebookarchive / flow-remove-types

🚿 Removes Flow type annotations from JavaScript files with speed and simplicity.
MIT License
467 stars 52 forks source link

some code to cause SyntaxError: Unexpected token => when remove types #67

Closed ishowshao closed 5 years ago

ishowshao commented 6 years ago
const normalizeEvent = cached((name: string): {
  name: string,
  once: boolean,
  capture: boolean,
  passive: boolean,
  handler?: Function,
  params?: Array<any>
} => {
})

removed:

const normalizeEvent = cached((name        )   

  => {
})

because:

An arrow function cannot contain a line break between its parameters and its arrow.

var func = ()
           => 1; 
// SyntaxError: expected expression, got '=>'
motiz88 commented 5 years ago

Huh! This is pretty bad. I'll take a stab at fixing this for the v2 release.

motiz88 commented 5 years ago

So one possible fix is to make --pretty mode (which deletes the type syntax and doesn't leave whitespace) mandatory, and bring in source-map-support as part of flow-node to keep stack traces in sync with the source code. It's a bit heavy-handed, but would work.

The alternative is to essentially move the arrow to the start of the stretch of whitespace. I'll see if I can make that work in an efficient way.

motiz88 commented 5 years ago

We've just published v2.99.0 of both flow-remove-types and flow-node, which should resolve this issue. Future releases will be aligned with Flow releases (e.g. v2.99.0 ↔ Flow 0.99.0) and will always use the latest version of the Flow parser for maximum compatibility.

The code has now moved to the main Flow repo, and this separate repo will be archived soon; please use the Flow issue tracker for any further discussion.