Monkey patch directly

This commit is contained in:
Alex
2023-07-31 03:17:22 -04:00
parent 13f2f75acf
commit a27416f190
5 changed files with 34 additions and 66 deletions

201
src/index.ts Normal file
View File

@ -0,0 +1,201 @@
import Module from 'module';
import type { ESLintConfig } from 'eslint-define-config';
// @ts-expect-error
const { name } = (0, require)('./package.json');
function unique<T>(arr: T[]): T[] {
return [...new Set(arr)];
}
const error = 'error';
const warn = 'warn';
const off = 'off';
export function extendConfig({
plugins,
settings,
rules,
extends: _extends,
...rest
}: ESLintConfig): ESLintConfig {
const hasReact = plugins?.includes('react');
const hasUnicorn = plugins?.includes('unicorn');
const hasReactRefresh = plugins?.includes('react-refresh');
const result: ESLintConfig = {
root: true,
parser: '@typescript-eslint/parser',
plugins: unique(['@typescript-eslint', 'import', ...(plugins ?? [])]),
env: { node: true, browser: true, es2023: true },
reportUnusedDisableDirectives: true,
parserOptions: {
project: ['./tsconfig.json'],
},
extends: unique([
'eslint:recommended',
'prettier',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:import/errors',
'plugin:import/typescript',
...(hasReact
? [
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'plugin:jsx-a11y/recommended',
]
: []),
...(hasUnicorn ? ['plugin:unicorn/recommended'] : []),
...(_extends ?? []),
]),
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.mts', '.cts'],
},
'import/resolver': {
typescript: {
alwaysTryTypes: true,
},
},
react: {
version: 'detect',
},
...settings,
},
rules: {
'no-duplicate-imports': error,
'no-restricted-imports': [
error,
{
paths: [
{
name: 'crypto',
importNames: ['webcrypto'],
message: 'Use global `crypto` instead',
},
],
},
],
'no-restricted-globals': [error, 'event', 'name', 'length'],
'@typescript-eslint/ban-ts-comment': [
error,
{
'ts-expect-error': 'allow-with-description',
'ts-check': false,
'ts-ignore': 'allow-with-description',
'ts-nocheck': 'allow-with-description',
},
],
'@typescript-eslint/consistent-type-imports': [
error,
{ disallowTypeAnnotations: false, fixStyle: 'inline-type-imports' },
],
'@typescript-eslint/ban-types': [error, { extendDefaults: true }],
'@typescript-eslint/no-empty-interface': [error, { allowSingleExtends: true }],
'@typescript-eslint/no-explicit-any': off,
'@typescript-eslint/no-extraneous-class': error,
'@typescript-eslint/no-namespace': off,
'@typescript-eslint/no-unnecessary-type-assertion': error,
'@typescript-eslint/no-unsafe-argument': off,
'@typescript-eslint/no-unsafe-assignment': off,
'@typescript-eslint/no-unsafe-member-access': off,
'@typescript-eslint/no-unsafe-call': off,
'@typescript-eslint/no-unsafe-return': off,
'@typescript-eslint/no-unused-vars': [
error,
{ ignoreRestSiblings: true, varsIgnorePattern: '^_' },
],
'@typescript-eslint/no-use-before-define': off,
'@typescript-eslint/no-var-requires': off,
'@typescript-eslint/unbound-method': off,
'@typescript-eslint/triple-slash-reference': off,
'arrow-body-style': [error, 'as-needed'],
'class-methods-use-this': off,
complexity: [warn, { max: 100 }],
curly: [error, 'multi-line', 'consistent'],
eqeqeq: [error, 'smart'],
'no-async-promise-executor': off,
'no-case-declarations': off,
'no-console': warn,
'no-constant-condition': [error, { checkLoops: false }],
'no-debugger': warn,
'no-empty': [error, { allowEmptyCatch: true }],
'no-inner-declarations': off,
'no-lonely-if': error,
'no-template-curly-in-string': error,
'no-var': error,
'import/export': off,
'import/order': [error, { groups: ['builtin', 'external'] }],
'object-shorthand': [error, 'always', { ignoreConstructors: true }],
'one-var': [error, { var: 'never', let: 'never' }],
'prefer-arrow-callback': error,
'prefer-const': [error, { destructuring: 'all' }],
'prefer-destructuring': [
warn,
{ AssignmentExpression: { array: false, object: false } },
],
'prefer-object-spread': error,
'prefer-rest-params': warn,
'prefer-spread': warn,
'quote-props': [error, 'as-needed'],
'spaced-comment': [error, 'always', { markers: ['/'] }],
'sort-imports': [warn, { ignoreDeclarationSort: true }],
yoda: [error, 'never', { exceptRange: true }],
...(hasReact
? {
'react/display-name': off,
'react/no-children-prop': error,
'react/prop-types': off,
'react/react-in-jsx-scope': off,
'react/no-unknown-property': [error, { ignore: ['css'] }],
}
: {}),
...(hasUnicorn
? {
'unicorn/no-abusive-eslint-disable': off,
'unicorn/no-array-callback-reference': off,
'unicorn/no-array-for-each': warn,
'unicorn/no-array-reduce': off,
'unicorn/no-await-expression-member': off,
'unicorn/no-empty-file': off,
'unicorn/no-negated-condition': warn,
'unicorn/no-new-array': off,
'unicorn/no-null': off,
'unicorn/no-process-exit': off,
'unicorn/no-this-assignment': off,
'unicorn/prefer-module': off,
'unicorn/prefer-string-replace-all': off,
'unicorn/prefer-top-level-await': off,
'unicorn/throw-new-error': off,
'unicorn/no-unnecessary-await': off,
}
: {}),
...(hasReactRefresh
? {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
}
: {}),
...rules,
},
...rest,
};
return result;
}
const _resolveFilename = (Module as any)._resolveFilename;
(Module as any)._resolveFilename = function (module: string, ...args: any[]) {
switch (module) {
case 'eslint-plugin-import':
case 'eslint-plugin-jsx-a11y':
case 'eslint-plugin-local':
case 'eslint-plugin-react':
case 'eslint-plugin-react-hooks':
case 'eslint-plugin-rules':
module = `${name}/${module.slice(14)}`;
}
return _resolveFilename(module, ...args);
};