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

Add defineRules function #231

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 50 additions & 48 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
const { defineConfig } = require('.');
const { defineConfig, defineTypeScriptRules, defineEslintRules } = require('.');
const { readGitignoreFiles } = require('eslint-gitignore');

module.exports = defineConfig({
Expand All @@ -26,59 +26,61 @@ module.exports = defineConfig({
},
plugins: ['@typescript-eslint', 'prettier'],
rules: {
curly: ['error'],
'linebreak-style': ['error', 'unix'],
'no-case-declarations': 'warn',
quotes: ['error', 'single', { avoidEscape: true }],
semi: ['error', 'always'],

'@typescript-eslint/array-type': [
'error',
{ default: 'array-simple', readonly: 'generic' },
],
'@typescript-eslint/ban-ts-comment': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'error',
'@typescript-eslint/naming-convention': [
'error',
{
format: ['PascalCase'],
selector: ['class', 'interface', 'typeAlias', 'typeParameter'],
leadingUnderscore: 'forbid',
trailingUnderscore: 'forbid',
},
],
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'error',
'@typescript-eslint/padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: 'block-like', next: '*' },
],
'@typescript-eslint/prefer-nullish-coalescing': 'warn',
'@typescript-eslint/prefer-optional-chain': 'warn',
'@typescript-eslint/prefer-readonly': 'warn',
'@typescript-eslint/restrict-template-expressions': [
'error',
{ allowNumber: true, allowBoolean: true },
],
'@typescript-eslint/typedef': [
'warn',
{ memberVariableDeclaration: true, variableDeclaration: true },
],
...defineEslintRules({
curly: ['error'],
'linebreak-style': ['error', 'unix'],
'no-case-declarations': 'warn',
quotes: ['error', 'single', { avoidEscape: true }],
semi: ['error', 'always'],
}),
...defineTypeScriptRules({
'array-type': ['error', { default: 'array-simple', readonly: 'generic' }],
'ban-ts-comment': 'error',
'consistent-type-imports': 'error',
'explicit-module-boundary-types': 'error',
'naming-convention': [
'error',
{
format: ['PascalCase'],
selector: ['class', 'interface', 'typeAlias', 'typeParameter'],
leadingUnderscore: 'forbid',
trailingUnderscore: 'forbid',
},
],
'no-inferrable-types': 'off',
'no-unsafe-argument': 'error',
'no-unsafe-assignment': 'off',
'no-unsafe-call': 'off',
'no-unsafe-member-access': 'off',
'no-unsafe-return': 'error',
'padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: 'block-like', next: '*' },
],
'prefer-nullish-coalescing': 'warn',
'prefer-optional-chain': 'warn',
'prefer-readonly': 'warn',
'restrict-template-expressions': [
'error',
{ allowNumber: true, allowBoolean: true },
],
typedef: [
'warn',
{ memberVariableDeclaration: true, variableDeclaration: true },
],
}),
},
overrides: [
{
// Rule files are generated by `generate:rules` script
files: ['src/rules/**/*.d.ts'],
rules: {
'@typescript-eslint/array-type': 'off',
'@typescript-eslint/no-duplicate-type-constituents': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-redundant-type-constituents': 'off',
...defineTypeScriptRules({
'array-type': 'off',
'no-duplicate-type-constituents': 'off',
'no-explicit-any': 'off',
'no-redundant-type-constituents': 'off',
}),
},
},
],
Expand Down
42 changes: 42 additions & 0 deletions src/define-rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { EslintRules } from './rules/eslint';
import type { RuleConfig } from './rules/rule-config';
import type { TypeScriptRules } from './rules/typescript-eslint';

type RemovePrefix<
TPrefix extends string,
TString extends string,
> = TString extends `${TPrefix}${infer T}` ? T : never;

export function defineEslintRules(
rules: Partial<{
[TKey in keyof EslintRules]: EslintRules[TKey];
}>,
): Partial<EslintRules> {
return defineRules('eslint', rules);
}

export function defineTypeScriptRules(
rules: Partial<{
[TKey in RemovePrefix<
'@typescript-eslint/',
keyof TypeScriptRules
>]: TypeScriptRules[`@typescript-eslint/${TKey}`];
}>,
): Partial<TypeScriptRules> {
return defineRules('@typescript-eslint', rules);
}

function defineRules<
TOutput,
TRules extends { [x: string]: RuleConfig | undefined },
>(pluginName: string, rules: TRules): TOutput {
if (pluginName === 'eslint') {
return rules as unknown as TOutput;
}

return Object.entries(rules).reduce((prev, [key, value]) => {
// @ts-expect-error: do it
prev[`${pluginName}/${key}`] = value;
return prev;
}, {} as TOutput);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export function defineFlatConfig(config: unknown): unknown {
return config;
}

export { defineEslintRules, defineTypeScriptRules } from './define-rules';

export type * from './config';
export type * from './flat-config';
export type * from './parser-options';
Expand Down