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

fix(eslint): flat config support #51

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
45 changes: 40 additions & 5 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,54 @@ yarn add -D eslint @green-code-initiative/ecocode-eslint-plugin
npm install -D eslint @green-code-initiative/ecocode-eslint-plugin
```

### Enable whole plugin
### Enable plugin with recommended configuration

Add `@ecocode` recommended configuration to `extends` section of your `.eslintrc`:
#### ESLint Flat Configuration (`eslint.config.js`)

```jsonc
Add `@ecocode` **"flat/recommended"** configuration to your `eslint.config.js`:

```js
import ecocode from '@ecocode/eslint-plugin'

export default [
/* other eslint configurations */
ecocode.configs['flat/recommended'],
]
```

#### ESLint deprecated legacy configuration (`.eslintrc`)

For legacy ESLint versions using the deprecated `.eslintrc` file, add the `@ecocode` **"recommended"** configuration to the `extends` array :

```json
{
"extends": ["plugin:@ecocode/recommended"]
}
```

### Enable only some rules
### Enable specific rules

#### ESLint Flat configuration (`eslint.config.js`)

Add the `ecocode` plugin configuration to your `eslint.config.js` and select the rules to activate:

```js
import ecocode from '@ecocode/eslint-plugin'

export default [
/* other eslint configurations */
{
plugins: { "@ecocode": ecocode },
rules: {
"@ecocode/no-multiple-access-dom-element": "error"
}
}
]
```

#### ESLint deprecated legacy configuration (`.eslintrc`)

Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules configuration:
If your project uses a legacy ESLint version, it may use as well the now deprecated `.eslintrc` file. In such case, Add `@ecocode` to the `plugins` array, potentially followed by rules specific configurations:

```jsonc
{
Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/avoid-brightness-override.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Should avoid to override brightness (`@ecocode/avoid-brightness-override`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/avoid-css-animations.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Avoid usage of CSS animations (`@ecocode/avoid-css-animations`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Avoid using high accuracy geolocation in web applications (`@ecocode/avoid-high-accuracy-geolocation`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/limit-db-query-results.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Should limit the number of returns for a SQL query (`@ecocode/limit-db-query-results`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/no-empty-image-src-attribute.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow usage of image with empty source attribute (`@ecocode/no-empty-image-src-attribute`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/no-import-all-from-library.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Should not import all from library (`@ecocode/no-import-all-from-library`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/no-multiple-access-dom-element.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow multiple access of same DOM element (`@ecocode/no-multiple-access-dom-element`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/no-multiple-style-changes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow multiple style changes at once (`@ecocode/no-multiple-style-changes`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/no-torch.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Should not programmatically enable torch mode (`@ecocode/no-torch`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->
## Why is this an issue?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prefer API collections with pagination (`@ecocode/prefer-collections-with-pagination`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/prefer-shorthand-css-notations.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Encourage usage of shorthand CSS notations (`@ecocode/prefer-shorthand-css-notations`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/docs/rules/provide-print-css.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Enforce providing a print stylesheet (`@ecocode/provide-print-css`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `flat/recommended`, ✅ `recommended`.

<!-- end auto-generated rule header -->

Expand Down
42 changes: 28 additions & 14 deletions eslint-plugin/lib/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,35 @@
*/
"use strict";

const rules = require("./rule-list");
const rulesList = require("./rule-list");

const ruleModules = rules.reduce((map, rule) => {
map[rule.ruleName] = rule.ruleModule;
return map;
}, {});
const allRules = {};
const recommendedRules = {};

const ruleConfigs = rules.reduce((map, rule) => {
const recommended = rule.ruleModule.meta.docs.recommended;
map[`@ecocode/${rule.ruleName}`] =
recommended === false ? "off" : recommended;
return map;
}, {});
for (let { ruleName, ruleModule } of rulesList) {
Copy link
Author

@AMorgaut AMorgaut Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous implementation did 2 loops

The updated code does it only once

allRules[ruleName] = ruleModule;
const { recommended } = ruleModule.meta.docs;
const ruleConfiguration = recommended === false ? "off" : recommended;
recommendedRules[`@ecocode/${ruleName}`] = ruleConfiguration;
}

module.exports = {
rules: ruleModules,
configs: { recommended: { plugins: ["@ecocode"], rules: ruleConfigs } },
const plugin = {
meta: {
name: "@ecocode/eslint-plugin",
version: "1.6.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of version being hard-coded here.
This makes it necessary to modify the version bump workflow to change this value dynamically.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree not fan neither

The ESLint configuration says:

you should add a meta key that contains at least a name key, and ideally, a version key

So version is not mandatory and looking a other plugins, it looks like they don't include it, so we could drop it.

I'll just do some check because it also says:

Without this meta information, your plugin will not be usable with the --cache and --print-config command line options.

Even if the version is not mandatory, I would like to make sure about its impact regarding the cache feature

},
rules: allRules,
};

plugin.configs = {
recommended: {
Copy link
Author

@AMorgaut AMorgaut Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legacy configuration is maintained as-is

no expected regressions

plugins: ["@ecocode"],
rules: recommendedRules,
},
["flat/recommended"]: {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new flat configuration follows the plugin naming convention

plugins: { "@ecocode": plugin },
rules: recommendedRules,
},
};

module.exports = plugin;
Loading