eslint-config-love

repository·master·Indexed 21 days ago

https://github.com/mightyiam/eslint-config-love

A strict, TypeScript-focused ESLint shareable configuration designed for safety and convention. It prioritizes type safety and standard practices while avoiding formatting and framework-specific rules. Built for ESLint's Flat Config format, it requires typescript-eslint and utilizes projectService for type-aware linting.

Tokens
1.7K
Snippets
9
Records
12
Agent score
73%

What's inside eslint-config-love

  1. Important: TypeScript Parser Configuration

    master
    The configuration exported by eslint-config-love automatically sets languageOptions.parserOptions.project = true. This is required for type-aware linting. Ensure your environment supports this setting as described in the @typescript-eslint documentation.
  2. Disable rules in eslint-config-love

    master

    Due to the strict nature of this configuration, you may need to disable certain rules. It is recommended to:

    1. Minimize the scope of disabled rules.
    2. Prefer using eslint-disable-* comments for ad-hoc cases.
    3. Use configuration overrides to disable rules for specific subsets of files.
  3. Configure eslint-config-love in ECMAScript Modules

    master

    To use eslint-config-love in an ESM-based ESLint configuration, import the love object and spread it into your configuration array. You should specify the files pattern to target your JavaScript and TypeScript files.

    import love from 'eslint-config-love'
    
    export default [
      {
        ...love,
        files: ['**/*.js', '**/*.ts'],
      },
    ]
  4. Configure eslint-config-love in CommonJS

    master

    Because eslint-config-love is an ESM package, you must use dynamic import() within an async function to load it in a CommonJS configuration file.

    module.exports = (async function config() {
      const { default: love } = await import('eslint-config-love')
    
      return [
        {
          ...love,
          files: ['**/*.js', '**/*.ts'],
        },
      ]
    })()
  5. Understand the structure of exported ESLint configurations

    master

    The eslint-config-love package exports a structured set of objects designed for use in ESLint Flat Config. Instead of a single monolithic object, it provides granular access to rules and plugins:

    • rules: A flattened object containing all rules from all included plugins. Rules from non-eslint plugins are automatically prefixed with the plugin name (e.g., pluginName/ruleName).
    • rulesPerPlugin: A mapping where each key is a plugin name and the value is an object containing only the rules associated with that specific plugin.
    • plugins: A mapping of plugin names to their respective plugin objects (excluding the built-in eslint plugin).

    This structure allows you to either use the complete rule set immediately or selectively enable specific plugins and their corresponding rules.

  6. Use eslint-config-love in your ESLint configuration

    master

    To use this configuration, import the project object from the package's entry point (typically ./lib/index.js if you are consuming it locally or via the package name) and include it in your eslint.config.js array.

    To ensure TypeScript rules work correctly with the project's type-aware linting, you must provide languageOptions.parserOptions.projectService configuration. This allows the parser to resolve your TypeScript project settings.

    import project from './lib/index.js'
    
    export default [
      project,
      {
        files: ['**/*.cjs', '**/*.js', '**/*.ts'],
        languageOptions: {
          parserOptions: {
            projectService: {
              allowDefaultProject: ['eslint.config.js', 'commitlint.config.js'],
              defaultProject: './tsconfig.json',
            },
          },
        },
      },
      {
        ignores: ['lib/'],
      },
    ]
  7. Use eslint-config-love as an ESLint Flat Config

    master

    To use this configuration, import the default export in your eslint.config.js file. This configuration is designed for ESLint's Flat Config format and includes pre-configured plugins, rules, and TypeScript parser settings. It requires typescript-eslint to be available as it uses projectService: true for type-aware linting.

    import loveConfig from 'eslint-config-love';
    
    export default [
      ...loveConfig,
      // your other configs
    ];
  8. Configure TypeScript project service for type-aware linting

    master

    When using eslint-config-love, you must configure the projectService within parserOptions to enable type-aware linting for JavaScript and TypeScript files.

    Key options:

    • allowDefaultProject: An array of filenames that are permitted to use the defaultProject even if they aren't explicitly included in your tsconfig.json.
    • defaultProject: The path to your primary TypeScript configuration file (e.g., ./tsconfig.json).
    {
      files: ['**/*.cjs', '**/*.js', '**/*.ts'],
      languageOptions: {
        parserOptions: {
          projectService: {
            allowDefaultProject: ['eslint.config.js', 'commitlint.config.js'],
            defaultProject: './tsconfig.json',
          },
        },
      },
    }
  9. Access rules and plugins via exported constants

    master

    When consuming this configuration in your ESLint Flat Config file, you can import the following constants to build your configuration object:

    import { rules, plugins, rulesPerPlugin } from 'eslint-config-love'
    
    export default [
      {
        plugins: {
          // Use the 'plugins' object to register required plugins
          ...plugins
        },
        rules: {
          // Use the 'rules' object for the full rule set
          ...rules
        }
      },
      {
        // Or use 'rulesPerPlugin' to target specific plugin rules
        rules: {
          ...rulesPerPlugin['@typescript-eslint']
        }
      }
    ]
    import { rules, plugins, rulesPerPlugin } from 'eslint-config-love'
    
    export default [
      {
        plugins: {
          ...plugins
        },
        rules: {
          ...rules
        }
      }
    ]
  10. Import typescript-eslint plugin and parser

    master

    The typescript-eslint module exports the standard plugin and parser required for TypeScript linting within the Flat Config system.

    import { plugin, parser } from 'typescript-eslint';
    
    // Usage in a flat config object:
    // export default [
    //   { 
    //     plugins: { '@typescript-eslint': plugin },
    //     languageOptions: { parser: parser }
    //   }
    // ];