Skip to content

A shareable ESLint configuration for the development of Perfective TypeScript packages

License

Notifications You must be signed in to change notification settings

perfective/eslint-config

Repository files navigation

Perfective ESLint Config

@perfective/eslint-config provides a shareable ESLint configuration that is used for the development of the @perfective packages. These rules are primarily configured for TypeScript projects.

In addition to the core ESLint, @typescript-eslint, and ESLint Stylistic plugin rules, @perfective/eslint-config includes configurations for the ESLint plugins:

To simplify configuring ESLint support in the IDEs and editors, the severity of all fixable rules is a warning. In addition, it allows distinguishing errors that have to be fixed manually from issues that will be fixed automatically.

Setup

Important
@perfective/eslint-config only support ES module syntax. If your project uses CommonJS by default you need to use eslint.config.mjs file instead of eslint.config.js to run it in ESM mode.
  1. Install @perfective/eslint-config as a dev dependency:

    npm install --save-dev @perfective/eslint-config

    Required peer dependencies are installed automatically.

  2. Import perfectiveEslintConfig to eslint.config.js.

    import { perfectiveEslintConfig } from '@perfective/eslint-config';
    
    const eslintConfig = perfectiveEslintConfig();
    
    export default eslintConfig;
  3. Optional Install optional peer dependencies to add tool-specific linting rules.

    npm install --save-dev \
        eslint-plugin-cypress \
        eslint-plugin-jest \
        eslint-plugin-jest-dom \
        eslint-plugin-rxjs-x \
        eslint-plugin-testing-library

    Import configurations to eslint.config.js.

    import { perfectiveEslintConfig } from '@perfective/eslint-config';
    
    // Optional dependencies.
    import { cypressConfig } from '@perfective/eslint-config/cypress';
    import { jestConfig } from '@perfective/eslint-config/jest';
    import { jestDomConfig } from '@perfective/eslint-config/jest-dom';
    import { rxjsConfig } from '@perfective/eslint-config/rxjs';
    import { testingLibraryConfig } from '@perfective/eslint-config/testing-library';
    
    const eslintConfig = perfectiveEslintConfig([
        cypressConfig,
        jestConfig,
        jestDomConfig,
        rxjsConfig,
        testingLibraryConfig,
    ]);
    
    export default eslintConfig;
  4. Optional Customize configuration rules in eslint.config.js

    import { perfectiveEslintConfig, typescriptFiles } from '@perfective/eslint-config'; // (1)
    
    const eslintConfig = perfectiveEslintConfig([
        // ...Optional configurations...
        {
            // These rules are overridden to all files
            rules: {
                '@stylistic/indent': ['warn', 2],
            },
        },
        {
            // These rules are overridden to TypeScript files only
            files: typescriptFiles,
            rules: {
                '@stylistic/indent': ['warn', 4],
            },
        },
    ]);
    
    export default eslintConfig;
    1. See the list of available globs below.

Note
*.d.ts files and dist directories are ignored by the configuration. node_modules and dot-files are ignored by the eslint. If more directories or file types need to be ignored, see the .eslintignore file docs.

Rules configuration functions

Some rules have complex configuration objects or arrays that are not merged but are completely overridden by ESLint. Therefore, it requires copying and pasting a rule config instead of just extending it. Maintaining rules updates is challenging and is simplified by the custom config functions.

These functions and related types are exported from the subpackages and match the rule name in camelCase. If you need an extended configuration, you can use these functions in the eslint.config.js file:

import perfectiveEslintConfig from '@perfective/eslint-config';
import { simpleImportSortImports } from '@perfective/eslint-config/simple-import-sort'; // (1)

const eslintConfig = perfectiveEslintConfig([
    // ...Optional configurations...
    {
        rules: {
            'simple-import-sort/imports': ['warn', simpleImportSortImports([
                '@perfective',
            ])],
        },
    },
]);

export default eslintConfig;
  1. Framework-specific packages, based on @perfective/eslint-config, re-export all the rules. So rules should be required from those packages for correct node_modules resolution.

Packages [#packages]

The following is the list of available packages and their exports.

@perfective/eslint-config

  • perfectiveEslintConfig — a function that creates an array of flat configs.

  • type Glob = string a nominal type for glob patterns.

    • javascriptFiles: Glob[] — a list of glob patterns for JS and JSX files.

    • jsxFiles: Glob — a glob pattern for JSX files.

    • typescriptFiles: Glob[] — a list of glob patterns for TypeScript source files.

    • tsxFiles: Glob — a glob pattern for TSX files.

    • typescriptDeclarationFiles: Glob — a glob pattern for TypeScript declaration files.

    • configurationFiles: Glob — a glob pattern for JavaScript tools configuration files.

    • jestFiles: Glob[] — the default glob patterns Jest uses to find test files.

    • cypressFiles: Glob — the default glob pattern Cypress uses to load test files.

  • ESLint language options:

    • languageOptions(): Linter.LanguageOptions — creates ESLint languageOptions object for all files.

    • javascriptLanguageOptions(): Linter.LanguageOptions — creates ESLint languageOptions object for JavaScript files.

    • typescriptLanguageOptions(): Linter.LanguageOptions — creates ESLint languageOptions object for TypeScript files.

  • type LinterConfig = Linter.Config | (() ⇒ Linter.Config) — an ESLint flat config or a function that returns one.

    • linterConfig(config: LinterConfig): Linter.Config — a function to instantiate ESLint flat config.

  • Optional plugins:

    • hasEslintPlugin — returns true if a given ESLint plugin exists.

    • optionalRule(rule: string, config: unknown): Record<string, unknown> — returns an object with rule as a key and its config as a value, if a given rule belongs to an installed ESLint plugin.

@perfective/eslint-config/cypress

  • cypressConfig(files: Glob[] = [cypressFiles]): Linter.Config — creates a flat config for eslint-plugin-cypress for a given list of files globs. Overrides some rules for perfectiveEslintConfig for compatibility with Cypress.

@perfective/eslint-config/import

  • interface ImportNoExtraneousDependencies — configuration options for the import/no-extraneous-dependencies rule.

    • importNoExtraneousDependencies(overrides: Partial<ImportNoExtraneousDependencies> = {}): ImportNoExtraneousDependencies — returns configuration options for the import/no-extraneous-dependencies rule.

@perfective/eslint-config/jest

  • jestConfig(files: Glob[] = jestFiles): Linter.Config — creates a flat config for eslint-plugin-jest for a given list of files globs.

@perfective/eslint-config/jest-dom

  • jestDomConfig(files: Glob[] = jestFiles): Linter.Config — creates a flat config for eslint-plugin-jest-dom for a given list of files globs.

@perfective/eslint-config/rxjs

  • rxjsConfig(files: Glob[] = typescriptFiles): Linter.Config — creates a flat config for eslint-plugin-rxjs-x for a given list of files globs.

@perfective/eslint-config/simple-import-sort

  • interface SimpleImportSortImports — configuration options for the simple-import-sort/imports rule.

    • simpleImportSortImports(internal: string[] = []): SimpleImportSortImports — creates configuration for the simple-import-sort/imports ESLint rule. Allows to splice internal scope packages imports between the global and relative imports.

@perfective/eslint-config/testing-library

  • testingLibraryConfig(files: Glob[] = jestFiles): Linter.Config — creates a flat config for eslint-plugin-testing-library for a given list of files globs.

@perfective/eslint-config/typescript-eslint

  • interface TypescriptEslintNamingConvention — configuration options for the @typescript-eslint/naming-convention rule.

    • type TypescriptEslintNamingConventionSelector — values for the @typescript-eslint/naming-convention rule selector option.

      • type TypescriptEslintNamingConventionIndividualSelector — values for individual selectors for the @typescript-eslint/naming-convention rule selector option.

      • type TypescriptEslintNamingConventionGroupSelector — values for grouped of individual selectors for the @typescript-eslint/naming-convention rule selector option.

    • type TypescriptEslintNamingConventionFormat — values for the @typescript-eslint/naming-convention rule format option.

    • type TypescriptEslintNamingConventionUnderscore — values for the @typescript-eslint/naming-convention rule leadingUnderscore and trailingUnderscore options.

    • function typescriptEslintNamingConvention(extensions: TypescriptEslintNamingConvention[] = []): TypescriptEslintNamingConvention[] — creates configuration with the given extensions for the @typescript-eslint/naming-convention rule.

@perfective/eslint-config/unicorn

  • interface UnicornPreventAbbreviations — configuration options for the unicorn/prevent-abbreviation rule.

    • type UnicornPreventAbbreviationReplacements — nominal type for the replacements option of the unicorn/prevent-abbreviation rule.

    • function unicornPreventAbbreviations( replacements: UnicornPreventAbbreviationReplacements = {}, options: Partial<Pick<UnicornPreventAbbreviations, 'checkProperties'>> = {}): UnicornPreventAbbreviations — creates configuration for the unicorn/prevent-abbreviation rule with the given replacements and options.

About

A shareable ESLint configuration for the development of Perfective TypeScript packages

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •