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] export flat configs from plugin root and fix flat config crash #3694

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

bradzacher
Copy link
Contributor

@bradzacher bradzacher commented Feb 21, 2024

fixes #3693

This PR exposes the flat configs at the root of the plugin:

const reactPlugin = require('eslint-plugin-react');

module.exports = [
  // ...
  reactPlugin.configs['flat/recommended'],
  // ...
];

The flat/ prefix is an approach a number of plugins are taking to enable supporting both styles at the top-level.


This was the easiest way to set things up so that the plugin reference was consistent across the root and all configs.

I'm open to a different approach to fixing the issue - for example if you want to ensure the config/ exports all work as well - it's just going to be a much larger refactor to make that work because we need to restructure the configs so there isn't a cyclic dependency between the configs and the plugin.

Personally I'm of the opinion that this "everything from the root" approach offers better DevX compared to forcing users to separately import the configs. It's a stylistic thing though - up to you really.

Copy link

codecov bot commented Feb 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.76%. Comparing base (393bfa2) to head (eb0c123).

Current head eb0c123 differs from pull request most recent head a44e025

Please upload reports for the commit a44e025 to get more accurate results.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3694      +/-   ##
==========================================
+ Coverage   94.28%   97.76%   +3.47%     
==========================================
  Files         134      133       -1     
  Lines        9613     9475     -138     
  Branches     3486     3472      -14     
==========================================
+ Hits         9064     9263     +199     
+ Misses        549      212     -337     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ljharb
Copy link
Member

ljharb commented Feb 21, 2024

hmm - i definitely prefer deep imports; barrel files/manifest exports cause all sorts of problems.

how would the refactor work to have flat configs not be at the root?

@bradzacher
Copy link
Contributor Author

how would the refactor work to have flat configs not be at the root?

An example of a plugin that goes your deep import approach is https://github.com/eslint-community/eslint-plugin-eslint-plugin/

They define the flat configs as a function of the legacy configs.

EG

const plugin = require('../index');

module.exports = {
  plugins: { react: plugin },
  rules: plugin.configs.recommended.rules,
};

But their usecase is a bit simpler as it doesn't need to worry about language options etc.

@ljharb
Copy link
Member

ljharb commented Mar 3, 2024

I've rebased this; i'm still hopeful for a way to have the root be the current legacy config, in particular to avoid a breaking change.

@ljharb ljharb marked this pull request as draft March 3, 2024 23:35
@jakec-dev
Copy link

Any update on this? This plugin is unusable with Airbnb config until this is pulled

@ljharb
Copy link
Member

ljharb commented Apr 8, 2024

@jakec-dev the airbnb config doesn't use flat config, so it should be perfectly usable if you use a compatible version of eslint (not 9, yet) and the default config format of the supported eslint versions (eslintrc)

@mdjermanovic
Copy link
Contributor

I've rebased this; i'm still hopeful for a way to have the root be the current legacy config, in particular to avoid a breaking change.

It isn't a problem for either of the two config systems (eslintrc and flat) to include both config formats in configs, with different keys of course. Eslintrc users would keep using the same configs (no breaking changes) and flat config users would be using the new configs (reactPlugin.configs['flat/recommended'] as in the original post).

@mdjermanovic
Copy link
Contributor

On the other hand, if you prefer deep imports, a solution could be to invert 17858be: move rule configs and parserOptions back to index.js and update configs/recommended.js and others to load index.js and translate configs into the flat config format.

@mdjermanovic
Copy link
Contributor

Also relevant: eslint/eslint#18095

@ljharb
Copy link
Member

ljharb commented Jun 12, 2024

I vastly prefer deep imports, for both eslintrc and flat.

If we can use those, and ensure no breakage for existing eslintrc users, then that sounds like a great solution to get this over the line.

@mdjermanovic
Copy link
Contributor

I vastly prefer deep imports, for both eslintrc and flat.

If we can use those, and ensure no breakage for existing eslintrc users, then that sounds like a great solution to get this over the line.

eslintrc config system doesn't provide a way for plugins to export configs other than under the configs key, so there's probably no point in adding new (deep) exports for eslintrc configs.

As for flat configs, this is doable as @bradzacher suggested in #3694 (comment). Technically, it could be implemented as suggested in #3694 (comment).

@ljharb
Copy link
Member

ljharb commented Jun 12, 2024

Sounds great, let's do that :-)

If you drop a link to a sha or branch here, i can pull it into this PR

@mdjermanovic
Copy link
Contributor

Here's the branch:

https://github.com/mdjermanovic/eslint-plugin-react/tree/fix-flat-configs

I'm not sure why languageOptions in configs were set non-enumerable, but changing that would probably be a breaking change.

@ljharb
Copy link
Member

ljharb commented Jun 15, 2024

It was set that way because eslint threw when it was enumerable - i forget whether it threw with eslintrc, or with flat config, but making it nonenumerable hid it from one and allowed the other to read it.

@ljharb
Copy link
Member

ljharb commented Jun 16, 2024

@mdjermanovic hm, those two commits are on top of master, not this PR, and there's a conflict. I've rebased this PR; can you stack your commits on top of it, and then I can pull them in?

@mdjermanovic
Copy link
Contributor

@ljharb to clarify, do you want eslint-plugin-react to provide two ways to use its flat configs?

1st:

const reactRecommended = require('eslint-plugin-react/configs/recommended');

module.exports = [
  reactRecommended
];

2nd:

const reactPlugin = require('eslint-plugin-react');

module.exports = [
  reactPlugin.configs['flat/recommended']
];

@ljharb
Copy link
Member

ljharb commented Jun 16, 2024

I’m comfortable with that, as long as it doesn’t introduce cycles - but it’d also be sufficient to only have the deep import way (but it should say “flat” somewhere in there)

@mdjermanovic
Copy link
Contributor

Here's a branch on top of this PR. It keeps flat configs in plugin root (those added in this PR), fixes existing flat configs intended for deep imports, and adds tests for both:

https://github.com/mdjermanovic/eslint-plugin-react/tree/fix-deep-flat-configs

Comment on lines +91 to +107
plugin.configs['flat/recommended'] = {
plugins: { react: plugin },
rules: plugin.configs.recommended.rules,
languageOptions: { parserOptions: plugin.configs.recommended.parserOptions },
};

plugin.configs['flat/all'] = {
plugins: { react: plugin },
rules: plugin.configs.all.rules,
languageOptions: { parserOptions: plugin.configs.all.parserOptions },
};

plugin.configs['flat/jsx-runtime'] = {
plugins: { react: plugin },
rules: plugin.configs['jsx-runtime'].rules,
languageOptions: { parserOptions: plugin.configs['jsx-runtime'].parserOptions },
};
Copy link
Member

Choose a reason for hiding this comment

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

this might be more ergonomic if it's a "flat" object with 3 properties, "recommended", "all", "jsx-runtime"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Unlike eslintrc, there are no restrictions on how flat configs can be exported from plugins. However, the convention ESLint recommends is to still export configs as properties of configs (eslint/eslint#18095).

Copy link
Member

Choose a reason for hiding this comment

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

true - if we're following that tho we'd avoid "flat" entirely too.

For destructuring, being able to do { configs: { flat: { all } } } seems much more convenient than requiring brackets and quotes, to me. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me at the moment.

In some future major version of this plugin, it would be good to switch to:

{ configs: { 'legacy/recommended' /* eslintrc */, 'recommended' /* flat */, ... } }

Copy link
Member

Choose a reason for hiding this comment

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

certainly, if indeed the ecosystem prefers flat config then there will come a time when it doesn't make sense to support anything prior to that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's a commit that updates this: mdjermanovic@bbdaaa7

Comment on lines +8 to +10
if (!semver.satisfies(eslintPkg.version, '>= 8.57.0')) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

flat config wasn't added in 8.57; is there a reason this isn't targeting the first version of 8 that has flat config?

Copy link
Contributor

Choose a reason for hiding this comment

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

No particular reason other than that 8.57 is the latest 8.x and thus is the one that's used in CI testing.

I've just tested this with v8.23.0 (the first version when flat config support is announced) and it works well, so this can be updated to '>= 8.23.0'. However, I probably don't have write access to update this PR's branch.

Copy link
Member

Choose a reason for hiding this comment

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

cool, i can do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's also a commit that updates this: mdjermanovic@2f28188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: flat config crash when using manual plugin def and recommended config
4 participants