Skip to content

Instantly share code, notes, and snippets.

@andrew-aladjev
Created April 8, 2024 10:39
Show Gist options
  • Save andrew-aladjev/ae703072cbe95babb2d743a9194fa3fe to your computer and use it in GitHub Desktop.
Save andrew-aladjev/ae703072cbe95babb2d743a9194fa3fe to your computer and use it in GitHub Desktop.
eslint.config.mjs
import eslint from '@eslint/js';
import simpleImportSort from 'eslint-plugin-simple-import-sort';
import globals from 'globals';
import lodash from 'lodash';
import typescriptEslint from 'typescript-eslint';
// Merged typescript eslint config.
export const eslintConfigTypescript = typescriptEslint.configs.all.reduce(
(result, config) => lodash.merge(result, config),
{},
);
// File wildcards.
export const ESLINT_JS_FILE_WILDCARDS = ['src/**/*.{js,cjs,mjs}', 'src/*.{js,cjs,mjs}', '*.{js,cjs,mjs}'];
export const ESLINT_TS_FILE_WILDCARDS = ['src/**/*.ts', 'src/*.ts', '*.ts'];
export const ESLINT_JS_IGNORES_FILE_WILDCARDS = ['.pnp.*'];
export const ESLINT_TS_IGNORES_FILE_WILDCARDS = [...ESLINT_JS_IGNORES_FILE_WILDCARDS];
// Rules.
export const ESLINT_JS_RULES = {
'capitalized-comments': 'off',
complexity: 'off',
eqeqeq: 'off',
// eslint-disable-next-line no-magic-numbers
indent: ['error', 2],
'max-depth': 'off',
'max-lines': 'off',
'max-lines-per-function': 'off',
'max-statements': 'off',
'multiline-comment-style': 'off',
'no-await-in-loop': 'off',
'no-bitwise': 'off',
'no-console': 'off',
'no-continue': 'off',
'no-duplicate-imports': 'off',
'no-eq-null': 'off',
'no-negated-condition': 'off',
'no-ternary': 'off',
'no-undefined': 'off',
'no-void': 'off',
'one-var': 'off',
'prefer-named-capture-group': 'off',
'require-atomic-updates': 'off',
'simple-import-sort/imports': 'warn',
'simple-import-sort/exports': 'warn',
'sort-imports': 'off',
'sort-keys': 'off',
'sort-vars': 'off',
};
export const ESLINT_TS_RULES = {
...ESLINT_JS_RULES,
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'enumMember',
format: ['UPPER_CASE'],
},
],
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/init-declarations': 'off',
'@typescript-eslint/max-params': 'off',
'@typescript-eslint/no-magic-numbers': [
'error',
{
// eslint-disable-next-line no-magic-numbers
ignore: [0, 1, -1],
},
],
'@typescript-eslint/prefer-readonly-parameter-types': 'off',
'@typescript-eslint/require-array-sort-compare': 'off',
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'off',
};
// Plugins.
export const ESLINT_JS_PLUGINS = {
'simple-import-sort': simpleImportSort,
};
export const ESLINT_TS_PLUGINS = {
...ESLINT_JS_PLUGINS,
'@typescript-eslint': typescriptEslint.plugin,
};
// Other options.
export const ESLINT_GLOBALS = {
...globals.node,
};
export const ESLINT_JS_LANGUAGE_OPTIONS = {
globals: ESLINT_GLOBALS,
ecmaVersion: 'latest',
sourceType: 'module',
};
export const ESLINT_TS_LANGUAGE_OPTIONS = {
...ESLINT_JS_LANGUAGE_OPTIONS,
parser: typescriptEslint.parser,
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: 'tsconfig.json',
},
};
// Utils.
export const convertToRecord = (object) => ({ ...object });
// Configs.
export const JS_CONFIG = lodash.merge({}, convertToRecord(eslint.configs.all), {
files: ESLINT_JS_FILE_WILDCARDS,
ignores: ESLINT_JS_IGNORES_FILE_WILDCARDS,
languageOptions: ESLINT_JS_LANGUAGE_OPTIONS,
plugins: ESLINT_JS_PLUGINS,
rules: ESLINT_JS_RULES,
});
export const TS_CONFIG = lodash.merge({}, convertToRecord(eslint.configs.all), eslintConfigTypescript, {
files: ESLINT_TS_FILE_WILDCARDS,
ignores: ESLINT_TS_IGNORES_FILE_WILDCARDS,
languageOptions: ESLINT_TS_LANGUAGE_OPTIONS,
plugins: ESLINT_TS_PLUGINS,
rules: ESLINT_TS_RULES,
});
export default [JS_CONFIG, TS_CONFIG];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment