Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ofk committed Aug 29, 2024
1 parent 9f65407 commit 8a4e9d6
Show file tree
Hide file tree
Showing 12 changed files with 387 additions and 370 deletions.
52 changes: 47 additions & 5 deletions airbnb.cjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
const { FlatCompat } = require('@eslint/eslintrc');
// eslint-disable-next-line import/extensions
const jsxRuntime = require('eslint-plugin-react/configs/jsx-runtime.js');
const tseslint = require('typescript-eslint');

const { isInstalled } = require('./isInstalled.cjs');

const compat = new FlatCompat();

// https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb

const legacyExtends = isInstalled('react')
? ['eslint-config-airbnb', 'eslint-config-airbnb/hooks']
: ['eslint-config-airbnb/base'];

module.exports = {
compat: () => compat.extends(...legacyExtends),
compat: (compat) => compat.extends(...legacyExtends),
legacy: {
extends: legacyExtends,
},
recommended: tseslint.config(
{
rules: {
// Prohibit default export.
// see. https://basarat.gitbook.io/typescript/main-1/defaultisbad
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-default-export.md
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/prefer-default-export.md
'import/no-default-export': 'error',
'import/prefer-default-export': 'off',

// Use console.{warn,error} intentionally. So suppress the warning.
// https://eslint.org/docs/rules/no-console
'no-console': [
'warn',
{
allow: ['warn', 'error', 'assert'],
},
],

...(isInstalled('react')
? {
// Fix airbnb default rule.
// https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/label-has-associated-control.md
'jsx-a11y/label-has-associated-control': [
'error',
{
assert: 'either', // changed
controlComponents: [],
depth: 25,
labelAttributes: [],
labelComponents: [],
},
],
// Disable this rule if it conflicts with some rules.
// https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/function-component-definition.md
'react/function-component-definition': 'off',
}
: {}),
},
},
...(isInstalled('react') && isInstalled('vite') ? [jsxRuntime] : []),
...(isInstalled('react') && isInstalled('next') ? [jsxRuntime] : []),
),
};
143 changes: 0 additions & 143 deletions best-practices.cjs

This file was deleted.

20 changes: 0 additions & 20 deletions comments.cjs

This file was deleted.

21 changes: 0 additions & 21 deletions config.cjs

This file was deleted.

52 changes: 42 additions & 10 deletions configs.cjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
const { FlatCompat } = require('@eslint/eslintrc');
const globals = require('globals');
const tseslint = require('typescript-eslint');

const config = require('./config.cjs');
const airbnbDefine = require('./airbnb.cjs');
const eslintCommentsDefine = require('./eslint-comments.cjs');
const perfectionistDefine = require('./perfectionist.cjs');
const prettierDefine = require('./prettier.cjs');
const reactRefreshDefine = require('./react-refresh.cjs');
const typescriptDefine = require('./typescript.cjs');

module.exports = {
browser: [
{
languageOptions: {
ecmaVersion: 'latest',
globals: globals.browser,
},
const compat = new FlatCompat();
const airbnb = [...airbnbDefine.compat(compat), ...airbnbDefine.recommended];
const typescript = [...typescriptDefine.compat(compat), ...typescriptDefine.recommended];
const reactRefresh = [...reactRefreshDefine.flat, ...reactRefreshDefine.recommended];
const eslintComments = [...eslintCommentsDefine.flat, ...eslintCommentsDefine.recommended];
const perfectionist = [...perfectionistDefine.flat, ...perfectionistDefine.recommended];
const prettier = [...prettierDefine.flat];

const all = [
...airbnb,
...typescript,
...reactRefresh,
...eslintComments,
...perfectionist,
...prettier,
];
const browser = tseslint.config(
{
languageOptions: {
ecmaVersion: 'latest',
globals: globals.browser,
},
...config,
],
},
...all,
);

module.exports = {
airbnb,
all,
browser,
eslintComments,
perfectionist,
prettier,
reactRefresh,
typescript,
};
19 changes: 19 additions & 0 deletions eslint-comments.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const comments = require('@eslint-community/eslint-plugin-eslint-comments/configs');
const tseslint = require('typescript-eslint');

// https://eslint-community.github.io/eslint-plugin-eslint-comments/

module.exports = {
flat: [comments.recommended],
legacy: {
extends: ['plugin:@eslint-community/eslint-comments/recommended'],
},
recommended: tseslint.config({
rules: {
// https://eslint-community.github.io/eslint-plugin-eslint-comments/rules/disable-enable-pair.html
'@eslint-community/eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
// https://eslint-community.github.io/eslint-plugin-eslint-comments/rules/no-unused-disable.html
'@eslint-community/eslint-comments/no-unused-disable': 'error',
},
}),
};
48 changes: 35 additions & 13 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
const airbnb = require('./airbnb.cjs');
const bestPractices = require('./best-practices.cjs');
const comments = require('./comments.cjs');
const eslintComments = require('./eslint-comments.cjs');
const perfectionist = require('./perfectionist.cjs');
const prettier = require('./prettier.cjs');
const reactRefresh = require('./react-refresh.cjs');
const sorting = require('./sorting.cjs');
const typescript = require('./typescript.cjs');

function mergeConfig(...configs) {
return configs.reduce(
(acc, config) => ({
(
acc,
{
env,
extends: configExtends,
files,
globals,
overrides,
parser,
parserOptions,
plugins,
rules,
},
) => ({
...acc,
...config,
extends: [...acc.extends, ...(Array.isArray(config.extends) ? config.extends : [])],
overrides: [...acc.overrides, ...(Array.isArray(config.overrides) ? config.overrides : [])],
plugins: [...acc.plugins, ...(Array.isArray(config.plugins) ? config.plugins : [])],
rules: { ...acc.rules, ...(config.rules ?? {}) },
env: env ?? acc.env,
extends: [...acc.extends, ...(Array.isArray(configExtends) ? configExtends : [])],
globals: { ...acc.globals, ...(globals ?? {}) },
overrides: [
...acc.overrides,
...(Array.isArray(overrides) ? overrides : []),
...(rules && files ? [{ files, rules }] : []),
],
parser: parser ?? acc.parser,
parserOptions: parserOptions ?? acc.parserOptions,
plugins: [...acc.plugins, ...(Array.isArray(plugins) ? plugins : [])],
rules: { ...acc.rules, ...(rules && !files ? rules : {}) },
}),
{
extends: [],
Expand All @@ -32,11 +51,14 @@ module.exports = mergeConfig(
},
},
airbnb.legacy,
...airbnb.recommended,
typescript.legacy,
...typescript.recommended,
reactRefresh.legacy,
comments.legacy,
sorting.legacy,
...bestPractices.default,
...bestPractices.typescript,
...reactRefresh.recommended,
eslintComments.legacy,
...eslintComments.recommended,
perfectionist.legacy,
...perfectionist.recommended,
prettier.legacy,
);
Loading

0 comments on commit 8a4e9d6

Please sign in to comment.