Okay so I came here to share what I learned as it took me a while to find it. Looking at the picture of the configuration file above, it looks like your doing it by implement individual rules for the support you seek, however, there is a Babel Preset NPM Package that makes it so all ECMAS Stage-3 Proposals are supported, and its a lot less configuration, you just need to know how to do it, which is why I am writing this. First I want to say that I can tell you now that this will not be a short answer. To explain the process required will take some typing, and effort on my part. One reason so many guides fail to successful help people get babel to work in sync with ESLint is because it takes a bit of time to explain the solution in text. Though once it is explained, you will see, it is pretty simple.
HERE WE GO:
First Off you will need to make sure you have ESLint, so go ahead and do the following.
:~$ npm init
:~$ npm i -D eslint
:~$ eslint --init
Hopefully your familiar with the above commands. If you are not then you need to go learn how to configure ESLint first. At this point you should have a fresh eslintrc.json file (or the YAML equivalent) that is rule free and plugin'less. It probably extends Google or Airbnb (I use Google's JS style-guide, but it doesn't matter). You should also have an a fresh set if NPM package files, though we wont use them. Now we will install BABEL. Babel, has really improved. Configuring Babel in the past was difficult IMO, but now they have a preset module and it makes configuring Babel much more simple.
There Are 4x NPM Packages That You Will Need to Download:
All the Babel modules you'll need are published as separate npm packages scoped under @babel (since version 7). This modular design allows for various tools each designed for a specific use case. Here we'll look at @babel/core
-- BabelJS.io
There are four Downloads from NPM that need to be completed to get support for Private Members, and other ECMAScript Stage-3 Proposals:
Babel Itself
Babel Configuration Module (Makes Life Easy)
Babels ESLint Plugin
Babel-Parser for ESLint
To give this answer the best chance at giving you the solution you need, I am going to go through each NPM module separately, and Ill give you a link to the documentation in case something isn't working right. Every Development Environment varies, and this configuration can, and likely will, very on the Development Environment you are using. For example I format with prettier which posed another set of challenges to make it work after I switched the parser that ESLint was using. If you use the resources put forward. There is no reason you shouldn't be able to get Babel working with eslint, and get ESLint configured properly so that Private-Members, and every other Stage-3 ECMAScript Proposal, do not through errors when you lint your documents.
(NPM DL #1)
Install Babel:
In order TO GET BABEL Copy and paste the following command into your Terminal.
:~$ npm i -D @babel/core
Babel/core is self explanatory as the core functionality of Babel resides at the heart of the @babel/core module.
(NPM DL #2)
Install Babel's Preset Module
:~$ npm i -D @babel/preset-env
Without any configuration, this preset will include all plugins to support modern JavaScript. It also includes support for all stage-3 ECMAScript Proposals. The whole issue that caused us to need Babel's parser is that ESLint's default parser and core rules only support the latest final ECMAScript standard syntax and do not support experimental syntax.
(NPM DL #3)
Install Babel Parser For ESLint
:~$ npm
ESLint allows for the use of custom parsers. When using this plugin, your code is parsed by Babel's parser (using the configuration specified in your Babel configuration file) and the resulting AST (For those who don't know AST is an Abstract Syntax Tree) is transformed into an ESTree-compliant structure that ESLint can understand. All location info such as line numbers, columns is also retained so you can track down errors with ease.
Note:
ESLint's core rules do not support experimental syntax and may therefore not work as expected when using @babel/eslint-parser. Please use the companion @babel/eslint-plugin plugin for core rules that you have issues with.
-- Babel-ESLint-Parser README.md Document
(NPM DL #4)
Install Babel's ESLint Plugin:
:~$ npm i -D @babel/eslint-plugin
Companion rules for @babel/eslint-parser. @babel/eslint-parser does a great job at adapting eslint for use with Babel, but it can't change the built-in rules to support experimental features. @babel/eslint-plugin re-implements problematic rules so they do not give false positives or negatives.
-- The @babel/eslint-plugin Repository
The Following is DEPRECATED, and THEREFORE, WRONG!
npm install eslint babel-eslint --save-dev
If you see a solution using the above, or any other NPM packages that don't have "@babel/..." in the NPM Package name, then that solution is deprecated and probably will not work.
Configure the '.babelrc' & '.eslintrc' Files
First I will show you what the .eslintrc file should look like, and explain it through comments.
{
"env": {
// Make sure your environment is configured properly
"es2021": true,
"commonjs": true,
"node": true,
"browser": false
},
"extends": ["google"],
"plugins": [
"prettier",
"@babel" // This is where you inform ESLint that you want to use @babel/eslint-plugin. If you have other plugins use an array.
],
"parser": "@babel/eslint-parser", // He is where you communicate to ESLint that you want to use the @babel/eslint-parser.
"parserOptions": {
"ecmaVersion": 2021, // 12 or 2021 is the most current ECMAS Version
"sourceType": "module", // For use with JS Modules set to module, otherwise it can be set to script
"allowImportExportEverywhere": false, // When set to true this configuration allows import and export declarations to be placed where ever a statement can be made. Obviously your Env must support the Dynamic placement of the import/export statements for it to work.
"ecmaFeatures": {
"globalReturn": false // allow return statements in the global scope when used with sourceType: "script".
}
},
"rules": {
"require-jsdoc": "off",
"arrow-body-style": "off",
"prefer-arrow-callback": "off",
"no-unused-expressions": "warn",
"no-unused-vars": "warn",
// These are all the rules that Babel-Plugin has support for. If the plugin will implement a rule, you should have it do so instead of using ESLint's equal rule.
"@babel/new-cap": "error",
"@babel/no-invalid-this": "error",
"@babel/no-unused-expressions": "error",
"@babel/object-curly-spacing": "error",
"@babel/semi": "error",
/*
I Omitted this rule because @Babel-Plugin offers this rule.
"semi": ["error", "always"], */
}
}
Finally We Can Create a ".babelrc" File now!
I don't know all the mechanics behind everything we are doing in this answer, and I won't pretend to. I tried, about 8 months ago, when doing somthing else with Babel, to get a .babelrc file to work, and I could not. I had to use babelrc.js. For some reason the configuration I am explaining makes it so the file we must use is .babelrc, which makes me very happy.
So with that said, in Your Projects Root Directory create the file .babelrc. After you create the "...ProjectRoot/.babelrc" configuration file, the rest of this answer is very simple, as you can see below.
- In your .babelrc file paste the following:
{
"presets": [
[
"@babel/preset-env",
{
"shippedProposals": true,
"ignoreBrowserslistConfig": true
}
]
]
}
And that is it!
If this answer doesn't get Babel working with eslint, to be able to lint private-class-members, or other ECMAS Stage-3 Proposals, Message me or comment below and I will reply guaranteed.
Thats all folks!