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

feat: add support for Flat Config #993

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

michaelfaith
Copy link
Contributor

@michaelfaith michaelfaith commented Jun 18, 2024

This change adds support for ESLint's new Flat config system. It maintains backwards compatibility with eslintrc
style configs as well.

To achieve this, we're now dynamically creating four configs: two are the original recommended and strict, and the other two are the new flat/recommended and flat/strict. The two flat ones are setup with the new config format, while the original two have the same options as before.

Usage

Legacy

{
  "extends": ["plugin:jsx-a11y/recommended"]
}

Flat

import globals from 'globals';
import js from '@eslint/js';
import jsxA11y from 'eslint-plugin-jsx-a11y';

export default [
  js.configs.recommended,
  jsxA11y.flatConfigs.recommended,
  {
    files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      globals: globals.browser,
    },
    ignores: ['dist', 'eslint.config.js'],
    rules: {
      'no-unused-vars': 'warn',
      'jsx-a11y/anchor-ambiguous-text': 'warn',
      'jsx-a11y/anchor-is-valid': 'warn',
    },
  },
];

And just to be clear, this does not close the v9 support Issue, as there may be additional work needed on the rules themselves to enable compatibility with v9. This change is solely focused on providing support for the new Flat Config.

This change adds support for ESLint's new Flat config system.
It maintains backwards compatibility with eslintrc style configs as well.

To achieve this, we're now dynamically creating four configs: two are the original `recommended` and `strict`, and the other two are the new `flat/recommended` and `flat/strict`.
The two `flat` ones are setup with the new config format, while the original two have the same options as before.

 Usage
 Legacy
```json
{
  "extends": ["plugin:jsx-a11y/recommended"]
}
```

 Flat
```js
import globals from 'globals';
import js from '@eslint/js';
import jsxA11y from 'eslint-plugin-jsx-a11y';

export default [
  js.configs.recommended,
  jsxA11y.flatConfigs.recommended,
  {
    files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      globals: globals.browser,
    },
    ignores: ['dist', 'eslint.config.js'],
    rules: {
      'no-unused-vars': 'warn',
      'jsx-a11y/anchor-ambiguous-text': 'warn',
      'jsx-a11y/anchor-is-valid': 'warn',
    },
  },
];
```
Copy link

socket-security bot commented Jun 18, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@eslint/js@9.5.0 None 0 14.2 kB eslintbot
npm/cross-env@7.0.3 environment Transitive: filesystem, shell +6 81.2 kB kentcdodds
npm/globals@15.6.0 None 0 170 kB sindresorhus
npm/react-dom@18.3.1 environment +3 4.63 MB react-bot
npm/react@18.3.1 environment +2 339 kB react-bot

View full report↗︎

Copy link

codecov bot commented Jun 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.48%. Comparing base (0d5321a) to head (bb7d65b).
Report is 6 commits behind head on main.

Current head bb7d65b differs from pull request most recent head 6b5f096

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #993      +/-   ##
==========================================
- Coverage   99.02%   98.48%   -0.54%     
==========================================
  Files         105      107       +2     
  Lines        1636     1650      +14     
  Branches      579      581       +2     
==========================================
+ Hits         1620     1625       +5     
- Misses         16       25       +9     

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

@michaelfaith
Copy link
Contributor Author

Looks like the rule doc generator may need new icons for the two new flat configs. Not sure if you want to use the same check and lock as the legacy configs. Since the rulesets are the same, it does seem a bit redundant to have both in the table, but I'm not sure how you'd want to represent that.

@michaelfaith
Copy link
Contributor Author

michaelfaith commented Jun 18, 2024

I went ahead and set the two flat/ configs to ignore on the doc generator, since they're mirrors of recommended and strict. Though it still looks like it's having issues with some formatting (maybe because I'm on Windows and line endings generated on my machine differ?). I reran the generate from my Mac and it resolved the formatting issue.

.eslintignore Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated
const jsxA11y = require('eslint-plugin-jsx-a11y');

export default [
jsxA11y.configs['flat/recommended'],
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 nicer to use as a flat object, with a recommended property?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that. I was going back and forth with that, and landed on this approach because I saw a few others doing it. But there's no real strong need to have them added to the legacy configs object. I'll make that update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think about flatConfigs rather than flat so that it parallels the configs prop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

// Create configs to plugin object
const configs = {
  recommended: createConfig(recommendedRules),
  strict: createConfig(strictRules),
};

const flatConfigs = {
  recommended: createConfig(recommendedRules, 'recommended'),
  strict: createConfig(strictRules, 'strict'),
};

module.exports = { ...jsxA11y, configs, flatConfigs };

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and updated with the above. Let me know what you think about that.

examples/flat-cjs/.gitignore Outdated Show resolved Hide resolved
Comment on lines 12 to 25
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@eslint/js": "^9.5.0",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@vitejs/plugin-react": "^4.2.1",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint-plugin-jsx-a11y": "file:../..",
"globals": "^15.6.0",
"vite": "^5.2.0"
Copy link
Member

Choose a reason for hiding this comment

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

do we really need all these deps in the repo to showcase eslint config code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I was aiming to have it be as "real world" of an example as possible. I just generated a new React app using vite and left everything as is, except for the lint config.

Copy link
Member

Choose a reason for hiding this comment

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

we don't need a working app to demonstrate linting, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed vite, its config, and associated build/test commands (along with the boilerplate README and .gitignore). I did leave react, react-dom and their associated types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went ahead and removed the @types/* too.

package.json Outdated Show resolved Hide resolved
@michaelfaith michaelfaith force-pushed the feat/flag-config-support branch 2 times, most recently from b08c258 to bb7d65b Compare June 18, 2024 21:31
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

LGTM - i'll make some tweaks so we can run the examples tests in CI, but otherwise this seems like it'll work.

@ljharb ljharb merged commit 6b5f096 into jsx-eslint:main Jun 19, 2024
99 of 100 checks passed
@ljharb ljharb mentioned this pull request Jun 19, 2024
@ljharb
Copy link
Member

ljharb commented Jun 20, 2024

v6.9.0 has been released.

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

Successfully merging this pull request may close these issues.

2 participants