Skip to content

Instantly share code, notes, and snippets.

@oropesa
Created December 13, 2024 19:30
Show Gist options
  • Save oropesa/c8132cf02990a07b7559c5dc01db2cb2 to your computer and use it in GitHub Desktop.
Save oropesa/c8132cf02990a07b7559c5dc01db2cb2 to your computer and use it in GitHub Desktop.
Flat eslint.config.js (package.type="commonjs")
const {
DEFAULT_IGNORES,
setEslintLanguageOptionsBrowser,
setEslintPluginJest,
setEslintPluginJestDom,
setEslintPluginPrettier,
setEslintPluginTypescriptEslint,
setEslintPluginUnicorn,
} = require('./eslint.config.utils.js');
const allowList = ['obj', 'tmp', 'args', 'props', 'OTimerGetTimesArgs'];
module.exports = [
{ ignores: DEFAULT_IGNORES },
setEslintLanguageOptionsBrowser(),
setEslintPluginUnicorn({ allowList }),
setEslintPluginJest(),
setEslintPluginJestDom(),
setEslintPluginPrettier(),
...setEslintPluginTypescriptEslint(),
// NOTE: Allow main js-files to be as common-js (not module-js)
{
files: ['*.js'],
rules: {
'no-undef': 'off',
'@typescript-eslint/no-require-imports': 'off',
},
},
];
const pluginJs = require('@eslint/js');
const globals = require('globals');
const tseslint = require('typescript-eslint');
const eslintConfigPrettier = require('eslint-config-prettier');
const eslintPluginPrettier = require('eslint-plugin-prettier/recommended');
const eslintPluginJest = require('eslint-plugin-jest');
const eslintPluginJestDom = require('eslint-plugin-jest-dom');
//
const DEFAULT_IGNORES = ['coverage/*', 'dist/*', 'tmp.js', '**/*.test.js', '**/*.cjs'];
//
function setEslintLanguageOptionsBrowser() {
return { languageOptions: { globals: globals.browser } };
}
//
function setUnicornAllowList(list = []) {
return {
allowList: list.reduce((list, key) => {
list[key] = true;
return list;
}, {}),
};
}
function setEslintPluginUnicorn({ rules, allowList } = {}) {
return {
...eslintPluginUnicorn.configs['flat/recommended'],
rules: {
...eslintPluginUnicorn.configs['flat/recommended'].rules,
'unicorn/switch-case-braces': ['error', 'avoid'],
'unicorn/prefer-string-replace-all': 'off',
'unicorn/no-nested-ternary': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/prefer-at': 'off',
'unicorn/no-null': 'off',
'unicorn/filename-case': ['error', { cases: { kebabCase: true, pascalCase: true } }],
'unicorn/prevent-abbreviations': ['error', setUnicornAllowList(allowList)],
...rules,
},
};
}
//
function setEslintPluginJest({ rules } = {}) {
return {
...eslintPluginJest.configs['flat/recommended'],
rules: {
...eslintPluginJest.configs['flat/recommended'].rules,
...rules,
},
};
}
function setEslintPluginJestDom({ rules } = {}) {
return {
...eslintPluginJestDom.configs['flat/recommended'],
rules: {
...eslintPluginJestDom.configs['flat/recommended'].rules,
...rules,
},
};
}
//
function setEslintPluginPrettier({ rules } = {}) {
return {
...eslintPluginPrettier,
rules: {
...eslintConfigPrettier.rules,
...rules,
},
};
}
//
function setEslintPluginTypescriptEslint({ rules } = {}) {
return [
{ ...pluginJs.configs.recommended },
...tseslint.configs.recommended,
{
rules: {
'max-params': ['error', 4],
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
'@typescript-eslint/no-var-requires': 'off',
...rules,
},
},
];
}
//
module.exports = {
DEFAULT_IGNORES,
setEslintLanguageOptionsBrowser,
setEslintPluginUnicorn,
setEslintPluginJest,
setEslintPluginJestDom,
setEslintPluginPrettier,
setEslintPluginTypescripEslint,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment