--- tags: Frontends --- Eslint === ## Step 1: Install ```bash= yarn add eslint --dev yarn add eslint-plugin-react eslint-plugin-css-modules eslint-config-airbnb eslint-plugin-import eslint-plugin-react-hooks eslint-plugin-jsx-a11y @typescript-eslint/eslint-plugin @typescript-eslint/parser -D -- init eslint configration ./node_modules/.bin/eslint --init ``` Choose the option 1. To check syntax, find problems, and enforce code style 2. JavaScript modules (import/export) 3. None of these (react/ vue) 4. Yes for typescript. 5. Browser (if for frontend) - (node for backend). 6. Use a popular style guide: `airbnb` 7. JavaScript -- format for eslint config 8. Would you like to install them now with npm? › `No` -> if you use `yarn` ## Step 2: Config Eslint - Install Eslint extension - Open setting (Json) of VS code: add configs `settings.json` ```json= "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.validate": ["javascript"], "editor.formatOnSave": true, ``` - Open `package.json`: ```json= "scripts": { "lint": "eslint \"./src/**/*.{ts,tsx}\"", "lintcss": "stylelint \"./src/**/*.{scss,css}\"", }, ``` ## Step 3: Add rules: Ref: https://eslint.org/docs/rules/ 1. if you don't want to produce any error messages: `off` | `warn` | `error` `"no-console": "off",` 2. Double quotes: `"quotes": ["error", "double"],` 3. Indent: `"indent": ["error", 4],` Sample: `eslintrc.js` ```javascript= module.exports = { env: { browser: true, es6: true, }, parser: '@typescript-eslint/parser', parserOptions: { ecmaFeatures: { jsx: true, }, // project: './tsconfig.json', ecmaVersion: 2018, sourceType: 'module', }, extends: [ 'plugin:react/recommended', 'plugin:@typescript-eslint/recommended', 'airbnb', 'plugin:import/errors', 'plugin:import/warnings', 'plugin:css-modules/recommended', ], globals: { Atomics: 'readonly', SharedArrayBuffer: 'readonly', }, plugins: [ 'react', 'react-hooks', '@typescript-eslint', 'css-modules', ], rules: { 'max-len': ['error', { code: 120 }], 'no-console': 'off', indent: ['error', 4, { SwitchCase: 1 }], 'no-plusplus': ['warn', { allowForLoopAfterthoughts: true }], 'class-methods-use-this': 'off', 'react/jsx-indent': ['error', 4], 'react/jsx-indent-props': ['error', 4], 'react/jsx-filename-extension': ['error', { extensions: ['.ts', '.tsx'] }], 'react/state-in-constructor': ['warn', 'never'], 'react/destructuring-assignment': ['warn', 'always', { ignoreClassFields: true }], 'import/prefer-default-export': 'off', 'import/default': 'off', 'import/order': ['warn', { alphabetize: { order: 'asc' } }], 'import/extensions': [ 'error', 'ignorePackages', { js: 'never', jsx: 'never', ts: 'never', tsx: 'never', mjs: 'never', }, ], 'react-hooks/rules-of-hooks': 'error', 'react-hooks/exhaustive-deps': 'warn', 'react/prop-types': 'off', 'react/jsx-props-no-spreading': 'off', 'spaced-comment': ['error', 'always', { markers: ['/'], }], 'no-unused-expressions': 'off', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/explicit-module-boundary-types': ['error', { allowArgumentsExplicitlyTypedAsAny: true }], '@typescript-eslint/no-non-null-assertion': 'off', '@typescript-eslint/no-use-before-define': ['error'], '@typescript-eslint/no-unused-vars': 'error', '@typescript-eslint/no-shadow': 'error', 'no-continue': 'off', 'react/require-default-props': 'off', 'no-use-before-define': 'off', 'no-undef': 'off', 'no-unused-vars': 'off', 'no-shadow': 'off', 'react/jsx-uses-react': 'off', 'react/react-in-jsx-scope': 'off', }, settings: { 'import/resolver': { node: { extensions: ['.js', '.jsx', '.ts', '.tsx'], }, }, }, overrides: [ { files: [ '**/*.test.{ts,tsx}', ], env: { jest: true, }, rules: { 'import/no-extraneous-dependencies': 'off', }, }, ], }; ```