Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring babel plugins for @babel/eslint-parser in .eslintrc

I've been trying for a while now to get @babel/plugin-proposal-class-properties plugin to work nicely with @babel/eslint-parser and eslint without success.

This is my .eslintrc.js:

...
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "ecmaVersion": 11,
    "requireConfigFile": false,
  },
  "plugins": [
    "@babel",
  ],
...

And these are the related installed packages:

+-- @babel/[email protected]
+-- @babel/[email protected]
+-- @babel/[email protected]
+-- @babel/[email protected]
+-- [email protected]

Under this configuration, ESLint errors with this message:

Parsing error: \eg\example.js: Support for the experimental syntax 'classPrivateProperties' isn't currently enabled (xx:yy): (Fatal)

But if I add @babel/plugin-proposal-class-properties to plugins in .eslintrc.js like this:

  "plugins": [
    "@babel",
    "@babel/plugin-proposal-class-properties",
  ],

I get this error:

Error while running ESLint: Failed to load plugin '@babel/plugin-proposal-class-properties' declared in '.eslintrc.js': Cannot find module '@babel/eslint-plugin-plugin-proposal-class-properties'.

It seems like this isn't the correct way to declare plugins for @babel/eslint-parser in .eslintrc.js. Still, I suspect it is possible due to this quote here:

@babel/eslint-parser also supports applying Babel configuration through your ESLint configuration.

So my question is:

Is it actually possible to declare babel plugins in .eslintrc? If so how exactly?

like image 278
cyqsimon Avatar asked Aug 19 '20 08:08

cyqsimon


People also ask

Do I need babel-eslint parser?

Note: You only need to use @babel/eslint-parser if you are using Babel to transform your code. If this is not the case, please use the relevant parser for your chosen flavor of ECMAScript (note that the default parser supports all non-experimental syntax as well as JSX).

What does babel-eslint parser do?

babel-eslint is a parser that allows ESLint to run on source code that is transformed by Babel. Note: You only need to use babel-eslint if you are using Babel to transform your code.

What parser does ESLint use?

By default, ESLint uses Espree as its parser. You can optionally specify that a different parser should be used in your configuration file so long as the parser meets the following requirements: It must be a Node module loadable from the config file where the parser is used.


Video Answer


1 Answers

It's actually simpler than I thought...

So it turns out that since @babel/plugin-proposal-class-properties is a babel plugin, it needs to be declared in the plugins property of babel's configuration. According to the documentation of @babel/eslint-parser, those can be passed in with babelOptions property.

Therefore it's really as simple as this:

...
  "parserOptions": {
    ...
    "babelOptions": {
      "plugins": [
        "@babel/plugin-proposal-class-properties",
        ...
      ],
    },
  },
  "plugins": [
    "@babel",
  ],
...
like image 143
cyqsimon Avatar answered Sep 30 '22 22:09

cyqsimon