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

Parsing error for generic function named 'of' #10675

Open
alexwalterbos opened this issue Nov 7, 2019 · 3 comments
Assignees
Labels

Comments

@alexwalterbos
Copy link

@alexwalterbos alexwalterbos commented Nov 7, 2019

Thanks for the great tool guys! I found a parsing bug regarding generic function definitions and specific naming:

Bug Report

Current Behavior
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?

Input Code
Reproduction repository: https://github.com/alexwalterbos/generic-function-of

Expected behavior/code
Parse the generic function named of just like any other generic function.

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

// package.json
{
  "devDependencies": {
    "babel-eslint": "^10.0.3",
    "eslint": "^6.6.0"
  }
}

// .eslintrc.js
module.exports = {
    "parser": "babel-eslint",
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "rules": {
    }
};

Error is triggered simply with these commands in the reproduction repository linked above;

yarn install
yarn eslint .

Environment

  • Babel version(s): v7.7.0 (@babel/parser)
  • Node/npm version: Node v12.13.0, yarn v1.19.1
  • OS: Fedora 29 x64
  • Monorepo: no
  • How you are using Babel: cli

Workaround
Don't name your generic functions of.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Nov 7, 2019

Hey @alexwalterbos! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@rajasekarm

This comment has been minimized.

Copy link
Contributor

@rajasekarm rajasekarm commented Nov 7, 2019

I can pick this.

@rajasekarm

This comment has been minimized.

Copy link
Contributor

@rajasekarm rajasekarm commented Nov 8, 2019

The actual error is in babel-eslint.

In this https://github.com/babel/babel-eslint/blob/master/lib/parse.js#L36, plugin is always set to estree. But in this example, it a flow code.

So first thing, in the project, we've to configure flow as plugin.

    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module",
        "babelOptions": {
            "plugins": ["flow"]
         },
    },

and we've to use this change in the parser inside babel-eslint.

https://github.com/babel/babel-eslint/blob/master/lib/parse.js#L36

I raised a PR here in babel-eslint repo to fix this. babel/babel-eslint#807

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.