https://github.com/perfective/eslint-config
A shareable ESLint configuration for the development of Perfective TypeScript packages
https://github.com/perfective/eslint-config
code-quality code-style eslint eslint-config lint typescript
Last synced: 5 months ago
JSON representation
A shareable ESLint configuration for the development of Perfective TypeScript packages
- Host: GitHub
- URL: https://github.com/perfective/eslint-config
- Owner: perfective
- License: mit
- Created: 2021-01-05T08:10:53.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-24T16:14:18.000Z (about 2 years ago)
- Last Synced: 2024-04-25T18:43:06.745Z (about 2 years ago)
- Topics: code-quality, code-style, eslint, eslint-config, lint, typescript
- Language: TypeScript
- Homepage:
- Size: 4.54 MB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.adoc
- Changelog: CHANGELOG.adoc
- License: LICENSE
Awesome Lists containing this project
README
= Perfective ESLint Config
`@perfective/eslint-config` provides
a https://eslint.org/docs/latest/developer-guide/shareable-configs[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 https://eslint.org/docs/latest/rules/[ESLint],
`link:https://typescript-eslint.io/rules/[@typescript-eslint]`,
and https://eslint.style[ESLint Stylistic] plugin rules,
`@perfective/eslint-config` includes configurations for the ESLint plugins:
* `link:https://github.com/freaktechnik/eslint-plugin-array-func[eslint-plugin-array-func]`;
* `link:https://github.com/cypress-io/eslint-plugin-cypress[eslint-plugin-cypress]` _(optional)_;
* `link:https://eslint-community.github.io/eslint-plugin-eslint-comments/[@eslint-community/eslint-plugin-eslint-comments]`;
* `link:https://github.com/import-js/eslint-plugin-import[eslint-plugin-import]`;
* `link:https://github.com/jest-community/eslint-plugin-jest[eslint-plugin-jest]` _(optional)_;
* `link:https://github.com/testing-library/eslint-plugin-jest-dom[eslint-plugin-jest-dom]` _(optional)_;
* `link:https://github.com/gajus/eslint-plugin-jsdoc[eslint-plugin-jsdoc]`;
* `link:https://github.com/eslint-community/eslint-plugin-n[eslint-plugin-n]`;
* `link:https://github.com/TristonJ/eslint-plugin-prefer-arrow[eslint-plugin-prefer-arrow]`;
* `link:https://github.com/eslint-community/eslint-plugin-promise[eslint-plugin-promise]`;
* `link:https://github.com/JasonWeinzierl/eslint-plugin-rxjs-x[eslint-plugin-rxjs-x]` _(optional)_;
* `link:https://github.com/eslint-community/eslint-plugin-security[eslint-plugin-security]`;
* `link:https://github.com/lydell/eslint-plugin-simple-import-sort[eslint-plugin-simple-import-sort]`;
* `link:https://github.com/testing-library/eslint-plugin-testing-library[eslint-plugin-testing-library]` _(optional)_;
* `link:https://github.com/sindresorhus/eslint-plugin-unicorn[eslint-plugin-unicorn]`.
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.
. Install `@perfective/eslint-config` as a dev dependency:
+
[source,bash]
----
npm install --save-dev @perfective/eslint-config
----
+
Required peer dependencies are installed automatically.
+
. Import `perfectiveEslintConfig` to `eslint.config.js`.
+
[source,javascript]
----
import { perfectiveEslintConfig } from '@perfective/eslint-config';
const eslintConfig = perfectiveEslintConfig();
export default eslintConfig;
----
+
. _Optional_ Install optional peer dependencies to add tool-specific linting rules.
+
[source,bash]
----
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`.
+
[source,javascript]
----
import { perfectiveEslintConfig } from '@perfective/eslint-config';
// Optional dependencies.
import { cypressConfig } from '@perfective/eslint-config/cypress';
import { jestConfig, jestTypescriptConfig } 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,
jestTypescriptConfig,
jestDomConfig,
rxjsConfig,
testingLibraryConfig,
]);
export default eslintConfig;
----
+
. _Optional_ Customize configuration rules in `eslint.config.js`
+
[source,javascript]
----
import { perfectiveEslintConfig, typescriptFiles } from '@perfective/eslint-config'; // <.>
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;
----
<.> 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
`link:https://eslint.org/docs/latest/use/configure/ignore#the-eslintignore-file[.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 link:#_packages[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:
[source,javascript]
----
import perfectiveEslintConfig from '@perfective/eslint-config';
import { simpleImportSortImports } from '@perfective/eslint-config/simple-import-sort'; // <.>
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 link:https://jestjs.io/docs/configuration#testmatch-arraystring[default] glob patterns
Jest uses to find test files.
** `jestJavascriptFiles: Glob[]`
— the link:https://jestjs.io/docs/configuration#testmatch-arraystring[default] glob patterns
Jest uses to find JavaScript test files.
** `jestTypescriptFiles: Glob[]`
— the link:https://jestjs.io/docs/configuration#testmatch-arraystring[default] glob patterns
Jest uses to find TypeScript test files.
** `cypressFiles: Glob`
— the link:https://docs.cypress.io/app/references/configuration#e2e[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`
— 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`
— 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.
This config excludes the rules that require `@typescript-eslint` plugin.
+
* `jestTypescriptConfig(files: Glob[] = jestTypescriptFiles): Linter.Config`
— creates a flat config for `eslint-plugin-jest` for a given list of TypeScript file globs.
This config includes only the rules that require `@typescript-eslint` plugin.
=== `@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`
* `typescriptEslintDependentConfig(plugin: ESLint.Plugin, rules: Linter.RulesRecord, files: Glob[] = typescriptFiles): Linter.Config`
— a function to create a flat config for a given `plugin` that requires TypeScript ESLint plugin.
+
* `interface TypescriptEslintNamingConvention`
— configuration options for the
`link:https://typescript-eslint.io/rules/naming-convention/[@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.
** `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
`link:https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prevent-abbreviations.md[unicorn/prevent-abbreviation]` rule.
** `type UnicornPreventAbbreviationReplacements`
— nominal type for the `replacements` option of the `unicorn/prevent-abbreviation` rule.
** `unicornPreventAbbreviations(replacements: UnicornPreventAbbreviationReplacements = {}, options: Partial> = {}): UnicornPreventAbbreviations`
— creates configuration for the `unicorn/prevent-abbreviation` rule with the given replacements and options.