Налаштування лінтера
Last updated
Last updated
Опціональні налаштування системи для розробника. Можна зробити так, щоб VSCode сам контролював правильність написання коду і в разі помилок - позначав місце помилки та не дозволяв зробити коміт на GitHub поки вони не будудуть виправлені.
Для почаку встановлюємо розширення .
Тепер в корені нашого проєкту створимо файл .eslintrc.js.
Внесемо цей файл наступні налаштування.
module.exports = {
env: {
es6: true,
node: true,
},
globals: {},
parserOptions: {
ecmaVersion: 2020,
},
extends: ['airbnb-base', 'eslint:recommended'],
plugins: [],
rules: {
'class-methods-use-this': 'off',
'array-bracket-newline': 'off',
'array-element-newline': 'off',
'arrow-body-style': 'error',
'block-scoped-var': 'error',
complexity: 'error',
'constructor-super': 'error',
camelcase: 'off',
curly: 'error',
'default-case': 'off',
'no-case-declarations': 'off',
'dot-notation': 'error',
'eol-last': 'error',
eqeqeq: 'error',
'func-names': 'warn',
'guard-for-in': 'off',
'global-require': 'off',
'implicit-arrow-linebreak': 'off',
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
indent: ['warn', 2, { SwitchCase: 1 }],
'linebreak-style': 'off',
'max-classes-per-file': 'off',
'newline-per-chained-call': 'error',
'new-parens': 'error',
'no-alert': 'error',
'no-duplicate-imports': 'error',
'no-nested-ternary': 'error',
'no-mixed-spaces-and-tabs': 'error',
'no-underscore-dangle': 'off',
'no-bitwise': 'error',
'no-caller': 'error',
'no-cond-assign': 'error',
'no-console': 'warn',
'no-else-return': 'error',
'no-empty': 'off',
'no-plusplus': 'off',
'no-empty-functions': 'off',
'consistent-return': 'off',
'no-dynamic-require': 'off',
'no-eval': 'error',
'no-extend-native': 'error',
'no-extra-bind': 'error',
'no-fallthrough': 'error',
'no-floating-decimal': 'error',
'no-implicit-globals': 'error',
'no-implied-eval': 'error',
'no-invalid-this': 'off',
'no-iterator': 'error',
'no-labels': 'error',
'no-lone-blocks': 'error',
'no-loop-func': 'error',
'max-len': [
'error',
{
ignoreRegExpLiterals: true,
ignoreUrls: true,
comments: 130,
code: 130,
},
],
'no-magic-numbers': ['off', { ignoreArrayIndexes: true }],
'no-multi-spaces': 'error',
'no-multi-str': 'error',
'no-multiple-empty-lines': 'error',
'no-new': 'error',
'comma-dangle': 'off',
'no-new-func': 'error',
'no-new-wrappers': 'error',
'no-octal-escape': 'error',
'no-param-reassign': 'warn',
'no-proto': 'error',
'no-return-assign': 'error',
'no-return-await': 'error',
'no-restricted-syntax': 'off',
'no-script-url': 'error',
'no-self-compare': 'error',
'no-sequences': 'error',
'no-throw-literal': 'error',
'no-undef-init': 'error',
'no-unsafe-finally': 'error',
'no-unused-expressions': 'error',
'no-unused-vars': 'warn',
'no-use-before-define': ['error', { functions: false }],
'no-useless-call': 'error',
'no-useless-concat': 'error',
'no-useless-return': 'error',
'no-var': 'error',
'no-void': 'error',
'object-shorthand': 'error',
'object-curly-newline': 'warn',
'one-var': ['error', 'never'],
'padding-line-between-statements': 'error',
'prefer-const': 'error',
'prefer-object-spread': 'warn',
'quote-props': ['error', 'as-needed'],
radix: 'error',
'require-await': 'error',
'sort-imports': [
'error',
{
ignoreCase: true,
ignoreDeclarationSort: true,
ignoreMemberSort: false,
},
],
'space-before-function-paren': [
'error',
{
anonymous: 'never',
asyncArrow: 'always',
named: 'never',
},
],
'use-isnan': 'error',
'wrap-iife': 'error',
yoda: 'error',
},
overrides: [
{
files: ['**/*.test.js'],
env: {
jest: true,
},
},
],
};
Крім того, в package.json в devDependencies вписуємо такі пакети.
"devDependencies": {
"eslint": "^8.34.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"husky": "^4.3.8",
"lint-staged": "^13.1.2",
"nodemon": "^2.0.22"
},
"lint-staged": {
"*.js": [
"eslint"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"uuid": "^9.0.0"
}
Нові залежності потрібно встановити:
npm i
Тепер лінтер буде перевіряти наш код, підсвічувати помилки і не пропустить коміт поки їх не виправлять. Далі можна конфігурувати налаштування під себе.