eslint-plugin-oxlint

repository·main·Indexed 16 days ago

https://github.com/oxc-project/eslint-plugin-oxlint

A plugin designed to disable ESLint rules that are already covered by the high-performance oxlint linter to avoid duplicate warnings. It supports ESLint Flat Config (>= 9.0) and Legacy Config (< 9.0), providing utilities like buildFromOxlintConfig and buildFromOxlintConfigFile to dynamically synchronize ESLint settings with .oxlintrc.json files. It also includes helpers to handle rule overrides for Vue, Svelte, and Astro files.

Tokens
3.5K
Snippets
15
Records
19
Agent score
65%

What's inside eslint-plugin-oxlint

  1. Configure eslint-plugin-oxlint with Legacy Config (ESLint < 9.0)

    main

    For older ESLint versions using .eslintrc.js or similar, use the extends property to include the plugin:oxlint/recommended preset.

    // .eslintrc.js
    module.exports = {
      ... // other config
      extends: [
        ... // other presets
        "plugin:oxlint/recommended",
      ],
    }
  2. Run oxlint before eslint

    main

    To maximize performance, run oxlint before eslint. This allows oxlint to handle rules that it supports, effectively turning them off in ESLint to avoid redundant work. Add a combined lint script to your package.json.

    {
      "scripts": {
        "lint": "oxlint && eslint"
      }
    }
  3. Configure eslint-plugin-oxlint with Flat Config (ESLint >= 9.0)

    main

    The plugin is optimized for ESLint's Flat Config. You can import the plugin and spread its recommended configuration at the end of your configuration array. It is recommended that oxlint configs are the last ones in the array to ensure they correctly override/disable rules.

    // eslint.config.js
    import oxlint from 'eslint-plugin-oxlint';
    export default [
      ...// other plugins
      ...oxlint.configs['flat/recommended'], // oxlint should be the last one
    ];
  4. Generate Flat Config from .oxlintrc.json or an Oxlint config object

    main

    In Flat Config (ESLint >= 9.0), you can dynamically build your ESLint configuration based on your existing oxlint settings. This ensures that any rule enabled in oxlint is automatically disabled in ESLint.

    Use buildFromOxlintConfigFile to point to a .oxlintrc.json file, or buildFromOxlintConfig to pass a configuration object directly.

    // eslint.config.js
    import oxlint from 'eslint-plugin-oxlint';
    
    // Option 1: From a file
    export default [
      ..., // other plugins
      ...oxlint.buildFromOxlintConfigFile('./.oxlintrc.json'),
    ];
    
    // Option 2: From a config object
    export default [
      ..., // other plugins
      ...oxlint.buildFromOxlintConfig({
        categories: {
          correctness: 'warn'
        },
        rules: {
          eqeqeq: 'warn'
        }
      }),
    ];
  5. Configure oxlint via OxlintConfig

    main

    The OxlintConfig type defines the structure of an .oxlintrc.json (or similar) configuration file used by oxlint. It supports standard configuration keys like extends, plugins, rules, and ignorePatterns, as well as oxlint-specific categories and options.

    Key configuration properties:

    • extends: An array of paths to other oxlint configuration files or resolved configuration objects.
    • plugins: An array of plugin names.
    • categories: A record of categories to enable/disable.
    • rules: A record of rule identifiers and their settings.
    • ignorePatterns: An array of glob patterns to ignore.
    • options: Configuration for the oxlint engine, such as typeAware or typeCheck support.
    // Example of an OxlintConfig object
    const config: OxlintConfig = {
      extends: ['./base-config.json'],
      plugins: ['my-plugin'],
      rules: {
        'my-rule': 'error'
      },
      options: {
        typeAware: true
      }
    };
  6. Reference all available eslint-plugin-oxlint configs

    main

    The plugin provides several configuration presets to disable ESLint rules based on categories, specific plugins, or all rules.

    Note: buildFromOxlintConfigFile and buildFromOxlintConfig are only supported in Flat Config (ESLint >= 9.0).

    configs: {
      // recommended only contains the `correctness` category
      recommended: { plugins: [Array], rules: [Object] },
      'flat/recommended': { rules: [Object] },
    
      // all rules available
      all: { plugins: [Array], rules: [Object] },
      'flat/all': { rules: [Object] },
    
      // turn eslint rules off by plugin
      'flat/eslint': { rules: [Object] },
      'flat/import': { rules: [Object] },
      'flat/jest': { rules: [Object] },
      'flat/jsdoc': { rules: [Object] },
      'flat/jsx-a11y': { rules: [Object] },
      'flat/nextjs': { rules: [Object] },
      'flat/react': { rules: [Object] },
      'flat/react-perf': { rules: [Object] },
      'flat/tree-shaking': { rules: [Object] },
      'flat/typescript': { rules: [Object] },
      'flat/unicorn': { rules: [Object] },
    
      // turn eslint rules off by oxlint category
      'flat/pedantic': { rules: [Object] },
      'flat/style': { rules: [Object] },
      'flat/correctness': { rules: [Object] },
      'flat/restriction': { rules: [Object] },
      'flat/suspicious': { rules: [Object] }
    }
  7. Build ESLint configurations from an Oxlint config file with buildFromOxlintConfigFile

    main

    Use buildFromOxlintConfigFile to transform an existing .oxlintrc.json file directly into an array of EslintPluginOxlintConfig objects.

    If the file cannot be found or parsed, the function returns an empty array [] and emits an error to console.error. This prevents applying incorrect deactivation rules if the configuration state is unknown.

    import { buildFromOxlintConfigFile } from 'eslint-plugin-oxlint';
    
    // Path to your .oxlintrc.json file
    const configs = buildFromOxlintConfigFile('.oxlintrc.json', { typeAware: true });
  8. Handle rule overrides for Vue, Svelte, and Astro in Legacy ESLint config

    main

    If you are using the legacy ESLint configuration format, you can use overrideDisabledRulesForVueAndSvelteFiles to automatically move rules that are incompatible with Vue, Svelte, or Astro files into an overrides block. This prevents these rules from being applied to those specific file types.

    This function identifies rules in your top-level rules object that are part of the rulesDisabledForVueAstroAndSvelteFiles set, removes them from the main configuration, and adds them to an override block that excludes *.vue, *.svelte, and *.astro files.

    import { overrideDisabledRulesForVueAndSvelteFiles } from 'eslint-plugin-oxlint';
    
    const config = {
      rules: {
        'some-rule': 'error',
        'rule-disabled-for-vue': 'error',
      },
    };
    
    const fixedConfig = overrideDisabledRulesForVueAndSvelteFiles(config);
  9. Split disabled rules across multiple Flat Config objects

    main

    If you have a collection of Flat Config objects (e.g., a configuration map), use splitDisabledRulesForVueAstroAndSvelteFilesDeep to process all of them at once. It iterates through the provided object and applies the splitting logic to each configuration entry, returning a new object where each value is a SplittedFlatConfig tuple.

    import { splitDisabledRulesForVueAstroAndSvelteFilesDeep } from 'eslint-plugin-oxlint';
    
    const configs = {
      base: { rules: { 'rule-disabled-for-vue': 'error' } },
      app: { rules: { 'other-rule': 'error' } },
    };
    
    const processedConfigs = splitDisabledRulesForVueAstroAndSvelteFilesDeep(configs);
    // processedConfigs.base will be [baseConfig, exceptionConfig]
    // processedConfigs.app will be [appConfig]
  10. Handle rule overrides for Vue, Svelte, and Astro in Flat ESLint config

    main

    For projects using the ESLint Flat Config format, use splitDisabledRulesForVueAstroAndSvelteFiles to split your configuration into two parts: your original config (with incompatible rules removed) and a new configuration object that explicitly disables those rules for Vue, Svelte, and Astro files using ignores.

    This function returns a SplittedFlatConfig, which is a tuple of either [C] (if no rules needed splitting) or [C, FlatConfig] (if rules were moved to a new config object named oxlint/vue-svelte-astro-exceptions).

    import { splitDisabledRulesForVueAstroAndSvelteFiles } from 'eslint-plugin-oxlint';
    
    const config = {
      name: 'my-config',
      rules: {
        'some-rule': 'error',
        'rule-disabled-for-vue': 'error',
      },
    };
    
    const [originalConfig, exceptionConfig] = splitDisabledRulesForVueAstroAndSvelteFiles(config);
  11. Build ESLint configurations from an Oxlint config object with buildFromOxlintConfig

    main

    Use buildFromOxlintConfig to transform an OxlintConfig object (the structure used in .oxlintrc.json) into an array of EslintPluginOxlintConfig objects. This is useful for automatically deactivating ESLint rules that are already handled by oxlint, ensuring no duplicate linting occurs.

    If config.options.typeAware is set to true and no explicit typeAware option is provided in the options argument, typeAware will be enabled by default.

    Returns an array of ESLint configuration objects, which may include a configuration for ignorePatterns if they are defined in the Oxlint config.

    import { buildFromOxlintConfig } from 'eslint-plugin-oxlint';
    
    const oxlintConfig = {
      rules: {
        'no-unused-vars': 'error'
      },
      options: {
        typeAware: true
      }
    };
    
    const eslintConfigs = buildFromOxlintConfig(oxlintConfig, { typeAware: true });