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

Imports rules: add eslint flat config support #2818

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"homepage": "https://github.com/airbnb/javascript",
"devDependencies": {
"globals": "^13.21.0",
"markdownlint": "^0.29.0",
"markdownlint-cli": "^0.35.0"
}
Expand Down
17 changes: 16 additions & 1 deletion packages/eslint-config-airbnb-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 64 additions & 0 deletions packages/eslint-config-airbnb-base/flat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* 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 {
extends: airbnbRules,
...airbnbConfig
} = require('eslint-config-airbnb-base');
const importPlugin = require('eslint-plugin-import');
const globals = require('globals');

const mappedEnvs = {
es6: 'es2015',
OlivierZal marked this conversation as resolved.
Show resolved Hide resolved
};
const handledPlugins = {
OlivierZal marked this conversation as resolved.
Show resolved Hide resolved
import: importPlugin,
};

function convertIntoEslintFlatConfig(config) {
const newConfig = { ...config, languageOptions: {} };

// Handle the `extends` key
OlivierZal marked this conversation as resolved.
Show resolved Hide resolved
if ('env' in newConfig) {
newConfig.languageOptions.globals = Object.keys(newConfig.env)
.filter((key) => newConfig.env[key] === true)
.reduce((acc, key) => {
OlivierZal marked this conversation as resolved.
Show resolved Hide resolved
if (key in globals) {
return { ...acc, ...globals[key] };
}
if (key in mappedEnvs && mappedEnvs[key] in globals) {
return { ...acc, ...globals[mappedEnvs[key]] };
}
return acc;
}, {});
delete newConfig.env;
}

// Handle the `parserOptions` key
if ('parserOptions' in newConfig) {
newConfig.languageOptions.parserOptions = newConfig.parserOptions;
delete newConfig.parserOptions;
}

// Handle the `plugins` key
if ('plugins' in newConfig) {
newConfig.plugins = newConfig.plugins.reduce((acc, plugin) => {
if (plugin in handledPlugins) {
return { ...acc, [plugin]: handledPlugins[plugin] };
}
return acc;
}, {});
}

return newConfig;
}

module.exports = [
...airbnbRules.map((rule) => convertIntoEslintFlatConfig(require(rule))),
convertIntoEslintFlatConfig(airbnbConfig)
];
1 change: 1 addition & 0 deletions packages/eslint-config-airbnb-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config-airbnb-base/rules/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}],
Expand Down
Loading