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: support ESLint Flat Configs #55

Merged
merged 44 commits into from
Apr 11, 2025
Merged

Conversation

arthurgeron
Copy link
Owner

@arthurgeron arthurgeron commented Dec 15, 2024

feat: support ESLint Flat Configs

Key Changes

  • Added support for ESLint v9 flat configs (eslint.config.js) + compatibility layer between ESLint v8 and v9

Maintained backward compatibility with ESLint v8 traditional configs (.eslintrc.js)

  • Created example projects demonstrating implementation in both configurations
  • Test cases were extracted into isolate modules that are now ran on both ESLint V8 and V9 to verify compatibility and avoid regressions
  • Updated plugin to export in both CommonJS and ESM formats
  • Included detailed migration guide for users upgrading to ESLint v9
  • Fixed Husky pre-commit hooks to properly validate code

Technical Implementation

The implementation uses a compatibility layer to handle differences between ESLint v8 and v9 APIs, particularly around rule context, scope handling, and configuration formats. This allows the same rule logic to work seamlessly in both environments without code duplication.

New Example Projects

Example projects' configurations now include custom rule options demonstrating how to:

  • Ignore specific components with ignoredComponents
  • Exclude props from memo requirements with ignoredPropNames
  • Configure strictness of checks with strict option
  • Customize hook behavior with ignoredHookCallsNames

Documentation Updates

  • Added migration guide for transitioning to ESLint v9
  • Updated README with configuration examples for both formats
  • Included examples demonstrating proper usage
  • Added ESLint guidelines and best practices documentation

Testing

All rule functionality has been tested with both ESLint v8 and v9 configurations to ensure consistent behavior across environments. The test suite includes:

  • Shared test cases for all rules
  • Separate test runners for ESLint v8 and v9
  • Example projects with working configurations

Developer Impact

Possible approaches:

  1. Continue using the plugin with ESLint v8 traditional configs
  2. Migrate to ESLint v9 flat configs when ready
  3. Follow the migration guide now for a smooth transition

Utilize enhanced rule options for more granular control

This version has been tagged as 2.5.0--beta5 for testing before the final release.

@arthurgeron arthurgeron changed the title Ag/feat/eslint-flag-config feat: support ESLint Flat Configs Dec 15, 2024
Repository owner deleted a comment from github-actions bot Dec 15, 2024
@sedlukha
Copy link

Hey @arthurgeron , I really appreciate the work you're doing on this plugin! I'd be happy to help test it early and provide feedback if that would be helpful. Thanks!

- Added `eslint.config.js` for ESLint V9 flat config compatibility.
- Updated `jest.config.js` to disable TypeScript diagnostics.
- Modified `package.json` to reflect beta version and adjust ESLint dependency.
- Enhanced README with ESLint V9 flat config usage instructions.
- Refactored test files to utilize a shared `ruleTester` configuration.
- Introduced `eslint-configs.ts` for flat config rule definitions.
- Updated `eslintConfig.ts` to include metadata and improve rule exports.
- Introduced new test files for ESLint v9 compatibility, including , , and .
- Updated  to include  for environment variable management in tests.
- Updated `package.json` to replace `ts-prune` with `knip` for dead code analysis.
- Added `@types/estree` as a dependency for type definitions.
- Introduced `flat-config.ts` and `traditional-config.ts` for ESLint rule definitions.
- Updated `index.ts` to import from the new config files.
- Moved messages for useMemo rules in `messages.ts`.
- Updated `knip.json` to ignore the new config files.
Repository owner deleted a comment from github-actions bot Mar 13, 2025
Repository owner deleted a comment from github-actions bot Mar 13, 2025
Repository owner deleted a comment from github-actions bot Mar 13, 2025
@arthurgeron
Copy link
Owner Author

Thank you, @sedlukha.
I'd appreciate if you could test 2.5.0--beta1 release and give some feedback.

@sedlukha
Copy link

@arthurgeron when i try to use

Option 2: Using the recommended config

import { flatConfig } from '@arthurgeron/eslint-plugin-react-usememo';
export default [
  // Other configs...
  flatConfig.configs.recommended,
];

it returns error:

Oops! Something went wrong! :(

ESLint: 9.22.0


A config object has a "plugins" key defined as an array of strings. It looks something like this:

    {
        "plugins": ["@arthurgeron/react-usememo"]
    }

Flat config requires "plugins" to be an object, like this:

    {
        plugins: {
            @arthurgeron/react-usememo: pluginObject
        }
    }

@sedlukha
Copy link

@arthurgeron this type of config

Option 1: Importing the default config

import { flatConfig } from '@arthurgeron/eslint-plugin-react-usememo';
export default [
  {
    files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
    languageOptions: {
      ecmaVersion: 2020,
      sourceType: 'module',
      ecmaFeatures: {
        jsx: true,
      },
    },
    plugins: {
      '@arthurgeron/react-usememo': flatConfig,
    },
    rules: {
      '@arthurgeron/react-usememo/require-usememo': 'error',
      '@arthurgeron/react-usememo/require-memo': 'error',
      '@arthurgeron/react-usememo/require-usememo-children': 'error',
    },
  },
];

fails with error too:

Oops! Something went wrong! :(

ESLint: 9.22.0

TypeError: Key "languageOptions": Unexpected key "ecmaFeatures" found.

but without ecmaFeatures if fails with next error:

Oops! Something went wrong! :(

ESLint: 9.22.0

TypeError: context.getScope is not a function
Occurred while linting ...pathToFile
Rule: "@arthurgeron/react-usememo/require-usememo-children"

@rakleed
Copy link

rakleed commented Mar 13, 2025

@arthurgeron I recommend checking this migration guide: https://eslint.org/docs/latest/extend/plugin-migration-flat-config, seems like you skip some parts of it

@arthurgeron
Copy link
Owner Author

I'm figuring out a way to test both V8 and V9, but there are many dependencies related. I might have to only support v9 and make this a new major.

- Updated `jest.config.js` to ignore specific test paths using regex for better flexibility.
- Modified `package.json` to streamline the test command for ESLint v9.
- Removed legacy `ruleTester.ts` file and replaced it with a new implementation for ESLint v8 and v9.
- Added comprehensive test cases for the `require-memo`, `require-usecallback`, and `require-
@sedlukha
Copy link

sedlukha commented Mar 31, 2025

@arthurgeron could you please create new beta release? i will test on my projects

@arthurgeron
Copy link
Owner Author

@arthurgeron could you please create new beta release? i will test on my projects

Surely, just keep in mind not all rules are working with ESLint V9, yet.
I've also added a example project with ESLint's Flat Config.

@2.5.0--beta3 has been released.

…nese

- Introduced new markdown files for code style, ESLint, project general, and testing guidelines to enhance project documentation.
- Established best practices for TypeScript, React, error handling, and memoization.
- Documented ESLint configuration options and rules, including compatibility with ESLint v8 and v9.
- Outlined testing approach and structure, ensuring compatibility testing across ESLint versions.
- Added a new test path to Jest configuration to ignore specific test cases.
- Updated README to reflect changes in ESLint parser options for JSX support.
- Modified ESLint rule tester to use the flat config format and adjusted parser settings.
- Cleaned up test cases by removing unnecessary parser options and dummy tests.
- Changed ESLint rules in example configurations from error to warn for better flexibility during development.
@arthurgeron
Copy link
Owner Author

@sedlukha could you please test @2.5.0--beta4?

- Added custom rule options to both v8 and v9 example configurations
- Created example components to demonstrate ignored components and props
- Added validation test cases for each rule option
@arthurgeron arthurgeron marked this pull request as ready for review April 2, 2025 18:50
@arthurgeron
Copy link
Owner Author

Looking for volunteers to test out @2.5.0--beta5 as the final beta before the release.

@arthurgeron arthurgeron requested review from EduVencovsky and removed request for EduVencovsky April 6, 2025 21:48
@arthurgeron arthurgeron merged commit d036dad into main Apr 11, 2025
7 checks passed
@arthurgeron arthurgeron deleted the ag/feat/eslint-flag-config branch April 11, 2025 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support flat config
4 participants