Skip to content

Instantly share code, notes, and snippets.

@byurhannurula
Last active May 17, 2021 08:14
Show Gist options
  • Save byurhannurula/508e2b20e6d415193c85132bb64f42d1 to your computer and use it in GitHub Desktop.
Save byurhannurula/508e2b20e6d415193c85132bb64f42d1 to your computer and use it in GitHub Desktop.
ESLint/Prettier setup for VS Code

ESLint/Prettier setup for VS Code

Install dependencies

npm install --save-dev babel-eslint eslint eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks prettier

Configure ESLint .eslintrc.js

module.exports = {
  parser: 'babel-eslint',
  extends: [
    'airbnb',
    'plugin:react/recommended',
    'plugin:prettier/recommended',
  ],
  plugins: ['react-hooks'],
  parserOptions: {
    ecmaVersion: 2018,
    ecmaFeatures: {
      jsx: true,
    },
  },
  env: {
    es6: true,
    node: true,
    jest: true,
    browser: true,
  },
  rules: {
    // GENERAL
    radix: 0,
    'no-undef': 0,
    'new-cap': 0,
    'no-alert': 0,
    'no-console': 0,
    'func-names': 0,
    'no-plusplus': 0,
    'comma-dangle': 0,
    'no-unused-vars': 0,
    'no-param-reassign': 0,
    'consistent-return': 0,
    'no-use-before-define': 0,
    'class-methods-use-this': 0,

    // REACT
    'react/prop-types': 0,
    'react/display-name': 0,
    'react/jsx-props-no-spreading': 0,
    'react/jsx-one-expression-per-line': 0,
    'react/prefer-stateless-function': 0,
    'react/destructuring-assignment': 0,
    'react/jsx-filename-extension': [
      1,
      {
        extensions: ['.js', '.jsx'],
      },
    ],

    // JSX-A11Y
    'jsx-a11y/no-noninteractive-element-to-interactive-role': 0,
    'jsx-a11y/click-events-have-key-events': 0,
    'jsx-a11y/href-no-hash': 0,
    'jsx-a11y/anchor-is-valid': [
      'warn',
      {
        aspects: ['invalidHref', 'preferButton'],
      },
    ],

    // IMPORT
    'import/no-unresolved': 0,
    'import/prefer-default-export': 0,
    'import/no-extraneous-dependencies': 0,

    // REACT-HOOKS
    'react-hooks/rules-of-hooks': 'error',
    'react-hooks/exhaustive-deps': 'warn',
  },
};

Configure Prettier - .prettierrc.json

{
  "jsxBracketSameLine": false,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 80,
  "semi": true,
  "tabWidth": 2,
  "useTabs": false
}

Auto Lint/Fix with VS Code

  1. Install the ESLint package for VS Code

  2. Now we need to setup some VS Code settings via Code/File → Preferences → Settings. It's easier to enter these settings from settings.json file, so click the {} icon in the top right corner:

{
  // Turn off "formatOnSave" for JS and JSX, we will set eslint to do this
  "[javascript]": {
    "editor.formatOnSave": false,
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[javascriptreact]": {
    "editor.formatOnSave": false
  },
  // Tell the ESLint plugin to run on save
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment