eslint / typescript-eslint-parser

An ESLint custom parser which leverages TypeScript ESTree to allow for ESLint to lint TypeScript source code.
Other
915 stars 92 forks source link

Parse error on generic arrow function #473

Closed WhiteAbeLincoln closed 5 years ago

WhiteAbeLincoln commented 6 years ago

What version of TypeScript are you using? ^2.8.3

What version of typescript-eslint-parser are you using? ^15.0.0

What code were you trying to parse?

export const somes = <T>(options: ReadonlyArray<Option<T>>): ReadonlyArray<T> =>
  options.filter(isSome).map(o => o.value)

What did you expect to happen? There not to be a parse error

What happened? Eslint reported Parsing error: Identifier expected on Option<T>:

export const somes = <T>(options: ReadonlyArray<Option<T>>): ReadonlyArray<T> =>
                                                ^^^^^^^^^
  options.filter(isSome).map(o => o.value)

Changing this to a function statement instead of a arrow function removes the parse error. These are both valid:

export const somes = function<T>(options: ReadonlyArray<Option<T>>): ReadonlyArray<T> {
  return options.filter(isSome).map(o => o.value)
}
export function somes<T>(options: ReadonlyArray<Option<T>>): ReadonlyArray<T> {
  return options.filter(isSome).map(o => o.value)
}

Additionally, removing the type from the parameter changes the error to JSX element 'T' has no corresponding closing tag on the return type ReadonlyArray<T>:

export const somes = <T>(options: Option<T>[]): ReadonlyArray<T> =>
                                                ^^^^^^^^^^^^^^ 
  options.filter(isSome).map(o => o.value)

This is not a .tsx file, so the parser should probably not be attempting to parse types as JSX tags

gmathieu commented 6 years ago

I noticed the same error with the following:

import { getMiddleware } from 'lib/env'
import { createStore as reduxCreateStore, applyMiddleware, StoreCreator, Reducer } from 'redux'
import thunk from 'redux-thunk'

const storeCreator: StoreCreator = <S>(reducer: Reducer<S>) => {
    return reduxCreateStore(reducer, applyMiddleware(thunk, ...getMiddleware()))
}

export default storeCreator

lib/create-store.ts
  6:4  error  Parsing error: Expression expected
JamesHenry commented 5 years ago

Does your configuration have jsx: true?

If so it is highly likely: https://github.com/eslint/typescript-eslint-parser/issues/517

You can reproduce the same behaviour with the TypeScript parser directly on https://astexplorer.net/

JamesHenry commented 5 years ago

Hopefully this was covered by the resolution to #517, feel free to reopen if not!