babel / babel-eslint

:tokyo_tower: A wrapper for Babel's parser used for ESLint (renamed to @babel/eslint-parser)

Home Page:https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing error for generic function named 'of'

alexwalterbos opened this issue · comments

Hi guys, thanks for this great tool :) I seem to have found a small issue with generic functions and the name 'of'. Hoping this is the right repository to report this to - if not, please let me know where to put this.

Bug description

Babel-eslint parses a generic function just fine:

function someGenericFunction<R>(args: R): Array<R> {
  return [args];
}

It fails to parse that same function when it is named of, however:

function of<R>(args: R): Array<R> {
  return [args];
}
/path/to/index.js
  5:12  error  Parsing error: Unexpected token, expected "("

  3 | }
  4 | 
> 5 | function of<R>(args: R): Array<R> {
    |            ^
  6 | 	return [args];
  7 | }
  8 | 

✖ 1 problem (1 error, 0 warnings)

error Command failed with exit code 1.

Flow accepts the function just fine and shows 'No errors!'. It confused me for longer than I'd like to admit, hopefully fixing this will save others some time.
It would seem that of is treated as a keyword - maybe because of the for...of syntax?

Related info

  • I'm using these dependencies:
    "babel-eslint": "^10.0.3",
    "eslint": "^6.6.0"
  • I've also tried this with "parser": "eslint" which obviously fails due to the typing syntax, unrelated to this bug.
  • The same error is produced with eslint-plugin-flowtype added.

Reproduce it

Reproduction repository: https://github.com/alexwalterbos/generic-function-of
Built with yarn - clone the repo, run yarn install and yarn eslint . in the repo to produce the error.

Workaround

Don't name your generic functions of.

Cheers!

Thanks for the report. Since this is a parser error, can you file this in the https://github.com/babel/babel repository? Thanks!