Refactor code to comply with ESLint rules

Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
This commit is contained in:
undergroundwires
2022-01-02 18:20:14 +01:00
parent 96265b75de
commit 5b1fbe1e2f
341 changed files with 16126 additions and 15101 deletions

View File

@@ -1,3 +1,5 @@
const { rules: baseStyleRules } = require('eslint-config-airbnb-base/rules/style');
module.exports = {
root: true,
env: {
@@ -22,91 +24,10 @@ module.exports = {
ecmaVersion: 'latest',
},
rules: {
'no-console': 'off', // process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': 'off', // process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'linebreak-style': 'off',
'no-useless-constructor': 'off',
'import/prefer-default-export': 'off',
'class-methods-use-this': 'off',
'no-use-before-define': 'off',
'no-restricted-syntax': 'off',
'global-require': 'off',
'max-len': 'off',
'import/no-unresolved': 'off',
'import/no-webpack-loader-syntax': 'off',
'import/extensions': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'no-plusplus': 'off',
'no-mixed-operators': 'off',
'import/no-extraneous-dependencies': 'off',
'@typescript-eslint/no-empty-function': 'off',
'no-return-assign': 'off',
'no-await-in-loop': 'off',
'no-shadow': 'off',
'vuejs-accessibility/accessible-emoji': 'off',
'no-promise-executor-return': 'off',
'no-new': 'off',
'no-useless-escape': 'off',
'prefer-destructuring': 'off',
'no-param-reassign': 'off',
'no-irregular-whitespace': 'off',
'no-undef': 'off',
'no-underscore-dangle': 'off',
'vuejs-accessibility/form-control-has-label': 'off',
'vuejs-accessibility/click-events-have-key-events': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'camelcase': 'off',
'no-restricted-globals': 'off',
'default-param-last': 'off',
'no-continue': 'off',
'vuejs-accessibility/anchor-has-content': 'off',
'@typescript-eslint/no-extra-semi': 'off',
'no-multi-spaces': 'off',
'indent': 'off',
'comma-dangle': 'off',
'semi': 'off',
'quotes': 'off',
'key-spacing': 'off',
'lines-between-class-members': 'off',
'import/order': 'off',
'space-in-parens': 'off',
'array-bracket-spacing': 'off',
'object-curly-spacing': 'off',
'@typescript-eslint/no-inferrable-types': 'off',
'import/no-duplicates': 'off',
'function-paren-newline': 'off',
'operator-linebreak': 'off',
'no-multiple-empty-lines': 'off',
'object-curly-newline': 'off',
'object-property-newline': 'off',
'arrow-body-style': 'off',
'no-useless-return': 'off',
'prefer-template': 'off',
'func-call-spacing': 'off',
'no-spaced-func': 'off',
'padded-blocks': 'off',
'implicit-arrow-linebreak': 'off',
'function-call-argument-newline': 'off',
'comma-spacing': 'off',
'comma-style': 'off',
'newline-per-chained-call': 'off',
'no-useless-computed-key': 'off',
'no-else-return': 'off',
'quote-props': 'off',
'no-restricted-properties': 'off',
'prefer-exponentiation-operator': 'off',
'semi-spacing': 'off',
'prefer-object-spread': 'off',
'import/newline-after-import': 'off',
'strict': 'off',
'no-trailing-spaces': 'off',
'no-confusing-arrow': 'off',
'eol-last': 'off',
'import/no-useless-path-segments': 'off',
'spaced-comment': 'off',
'@typescript-eslint/no-empty-interface': 'off',
...getOwnRules(),
...getTurnedOffBrokenRules(),
...getOpinionatedRuleOverrides(),
...getTodoRules(),
},
overrides: [
{
@@ -118,5 +39,77 @@ module.exports = {
mocha: true,
},
},
{
files: ['**/tests/**/*.{j,t}s?(x)'],
rules: {
'no-console': 'off',
},
},
],
};
function getOwnRules() {
return {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'linebreak-style': ['error', 'unix'], // Also enforced in .editorconfig
'import/order': [ // Enforce strict import order taking account into aliases
'error',
{
groups: [ // Enforce more strict order than AirBnb
'builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
pathGroups: [ // Fix manually configured paths being incorrectly grouped as "external"
'@/**', // @/..
'@tests/**', // @tests/.. (not matching anything after @** because there can be third parties as well)
'js-yaml-loader!@/**', // E.g. js-yaml-loader!@/..
].map((pattern) => ({ pattern, group: 'internal' })),
},
],
};
}
function getTodoRules() { // Should be worked on separate future commits
return {
'import/no-extraneous-dependencies': 'off',
// Requires webpack configuration change with import '..yaml' files.
'import/no-webpack-loader-syntax': 'off',
'import/extensions': 'off',
'import/no-unresolved': 'off',
// Accessibility improvements:
'vuejs-accessibility/form-control-has-label': 'off',
'vuejs-accessibility/click-events-have-key-events': 'off',
'vuejs-accessibility/anchor-has-content': 'off',
'vuejs-accessibility/accessible-emoji': 'off',
};
}
function getTurnedOffBrokenRules() {
return {
// Broken in TypeScript
'no-useless-constructor': 'off', // Cannot interpret TypeScript constructors
'no-shadow': 'off', // Fails with TypeScript enums
};
}
function getOpinionatedRuleOverrides() {
return {
// https://erkinekici.com/articles/linting-trap#no-use-before-define
'no-use-before-define': 'off',
// https://erkinekici.com/articles/linting-trap#arrow-body-style
'arrow-body-style': 'off',
// https://erkinekici.com/articles/linting-trap#no-plusplus
'no-plusplus': 'off',
// https://erkinekici.com/articles/linting-trap#no-param-reassign
'no-param-reassign': 'off',
// https://erkinekici.com/articles/linting-trap#class-methods-use-this
'class-methods-use-this': 'off',
// https://erkinekici.com/articles/linting-trap#importprefer-default-export
'import/prefer-default-export': 'off',
// https://erkinekici.com/articles/linting-trap#disallowing-for-of
// Original: https://github.com/airbnb/javascript/blob/d8cb404da74c302506f91e5928f30cc75109e74d/packages/eslint-config-airbnb-base/rules/style.js#L333-L351
'no-restricted-syntax': [
baseStyleRules['no-restricted-syntax'][0],
...baseStyleRules['no-restricted-syntax'].slice(1).filter((rule) => rule.selector !== 'ForOfStatement'),
],
};
}