From c561084ff7447fd23a4935c196106cd9e7cfd305 Mon Sep 17 00:00:00 2001 From: Olivier Zalmanski Date: Mon, 4 Sep 2023 00:11:06 +0200 Subject: [PATCH] Support eslint flat config --- packages/eslint-config-airbnb-base/README.md | 17 +++- packages/eslint-config-airbnb-base/flat.js | 89 +++++++++++++++++++ .../eslint-config-airbnb-base/package.json | 2 + .../rules/imports.js | 3 +- 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 packages/eslint-config-airbnb-base/flat.js diff --git a/packages/eslint-config-airbnb-base/README.md b/packages/eslint-config-airbnb-base/README.md index 6ddc34dac1..834ca99264 100644 --- a/packages/eslint-config-airbnb-base/README.md +++ b/packages/eslint-config-airbnb-base/README.md @@ -60,7 +60,22 @@ Our default export contains all of our ESLint rules, including ECMAScript 6+. It npm install --save-dev eslint-config-airbnb-base eslint@^#.#.# eslint-plugin-import@^#.#.# ``` -2. Add `"extends": "airbnb-base"` to your .eslintrc. +2. Use it in your eslint config. + + If you use the new flat format, add the following to your eslint.config.js: + + ```javascript + const airbnbBase = require('eslint-config-airbnb-base/flat') + + module.exports = [ + ...airbnbBase, + { + // any other config + } + ] + ``` + + If you still use the regular format: add `"extends": "airbnb-base"` to your .eslintrc. ### eslint-config-airbnb-base/legacy diff --git a/packages/eslint-config-airbnb-base/flat.js b/packages/eslint-config-airbnb-base/flat.js new file mode 100644 index 0000000000..d18b40a01f --- /dev/null +++ b/packages/eslint-config-airbnb-base/flat.js @@ -0,0 +1,89 @@ +/** + * Changes to support the flat config format: + * - The `extends` key has been deprecated. + * - The `parserOptions` key has been moved under the new `languageOptions` key. + * - The `env` key has been deprecated and moved to `languageOptions.globals`. + * - The `plugins` key is no longer a list but an object. + */ + +const importPlugin = require('eslint-plugin-import/config/flat'); +const envs = require('globals'); +const { + extends: airbnbRules, + ...airbnbConfig +} = require('.'); + +const envMapping = { + builtin: 'builtin', + es5: 'es5', + es6: 'es2015', + es2016: 'es2015', + es2017: 'es2017', + es2018: 'es2017', + es2019: 'es2017', + es2020: 'es2020', + es2021: 'es2021', + es2022: 'es2021', + es2023: 'es2021', + es2024: 'es2021', + browser: 'browser', + worker: 'worker', + node: 'node', + nodeBuiltin: 'nodeBuiltin', + commonjs: 'commonjs', + amd: 'amd', + mocha: 'mocha', + jasmine: 'jasmine', + jest: 'jest', + qunit: 'qunit', + phantomjs: 'phantomjs', + couch: 'couch', + rhino: 'rhino', + nashorn: 'nashorn', + wsh: 'wsh', + jquery: 'jquery', + yui: 'yui', + shelljs: 'shelljs', + prototypejs: 'prototypejs', + meteor: 'meteor', + mongo: 'mongo', + applescript: 'applescript', + serviceworker: 'serviceworker', + atomtest: 'atomtest', + embertest: 'embertest', + protractor: 'protractor', + 'shared-node-browser': 'shared-node-browser', + webextensions: 'webextensions', + greasemonkey: 'greasemonkey', + devtools: 'devtools', +}; + +function convertIntoEslintFlatConfig(config) { + const { + env, // convert to explicit `globals` list + parserOptions, // move into `languageOptions` + plugins, // remove the `plugins` key as it will be spread directly during export + ...oldConfig + } = config; + return { + ...oldConfig, + languageOptions: { + ...('env' in config && { + globals: Object.fromEntries( + Object.keys(env) + .filter((key) => env[key] === true && key in envMapping && envMapping[key] in envs) + .flatMap((key) => Object.entries(envs[envMapping[key]])) + ), + ...('parserOptions' in config && { + parserOptions, + }), + }), + }, + }; +} + +module.exports = [ + ...airbnbRules.map((rule) => convertIntoEslintFlatConfig(require(rule))), + convertIntoEslintFlatConfig(airbnbConfig), + ...importPlugin, +]; diff --git a/packages/eslint-config-airbnb-base/package.json b/packages/eslint-config-airbnb-base/package.json index 39c0fdf944..d78e68ac73 100644 --- a/packages/eslint-config-airbnb-base/package.json +++ b/packages/eslint-config-airbnb-base/package.json @@ -5,6 +5,7 @@ "main": "index.js", "exports": { ".": "./index.js", + "./flat": "./flat.js", "./legacy": "./legacy.js", "./whitespace": "./whitespace.js", "./rules/best-practices": "./rules/best-practices.js", @@ -88,6 +89,7 @@ }, "dependencies": { "confusing-browser-globals": "^1.0.11", + "globals": "^13.21.0", "object.entries": "^1.1.6" } } diff --git a/packages/eslint-config-airbnb-base/rules/imports.js b/packages/eslint-config-airbnb-base/rules/imports.js index d36e4908fa..7a5cb39cca 100644 --- a/packages/eslint-config-airbnb-base/rules/imports.js +++ b/packages/eslint-config-airbnb-base/rules/imports.js @@ -91,7 +91,8 @@ module.exports = { '**/protractor.conf.js', // protractor config '**/protractor.conf.*.js', // protractor config '**/karma.conf.js', // karma config - '**/.eslintrc.js' // eslint config + '**/.eslintrc.js', // eslint config + '**/eslint.config.js', // eslint flat config ], optionalDependencies: false, }],