Skip to content

Instantly share code, notes, and snippets.

@pom421
Last active December 19, 2020 18:03
Show Gist options
  • Save pom421/275c81599169682ea47086f0243d79ed to your computer and use it in GitHub Desktop.
Save pom421/275c81599169682ea47086f0243d79ed to your computer and use it in GitHub Desktop.
Get instant VSCode configured with ESLint, Prettier following AirBnB ESLint's rules

ESLint and Prettier are 2 importants tools in the JS tools belt. How to make them aware of each other? Here is how.

From:

VSCode - ESLint, Prettier & Airbnb Setup

1. Install ESLint & "Prettier - Code formatter" extensions for VSCode

2. Install NPM packages in your project

yarn add -D eslint prettier eslint-plugin-prettier eslint-config-prettier eslint-plugin-node eslint-config-node

Usage of AirBnB's ESLint rules, for example.

npx install-peerdeps --dev eslint-config-airbnb

NB:

  • npx may propose to use yarn. Say yes
  • VSCode at the end, may display a popup noticing that "The ESLint extension will use 'node_modules/eslint' for validation'. Say Allow to activate ESLint in VSCode.

3. Create .prettierrc.json for any prettier rules (semicolons, quotes, etc)

{
    "extends": [
      "plugin:prettier/recommended"
    ],
    "printWidth": 120,
    "parser": "typescript",
    "semi": false,
    "tabWidth": 2,
    "trailingComma":  "all"
  }

I like this sober configuration with no semicolon.

4. Create .eslintrc.json file (You can generate with eslint --init if you install eslint globally)

{
  "extends": ["airbnb", "prettier", "plugin:node/recommended"],
  "plugins": ["prettier"],
  "rules": {
    "prettier/prettier": "error",
    "no-unused-vars": "warn",
    "no-console": "off",
    "func-names": "off",
    "no-process-exit": "off",
    "object-shorthand": "off",
    "class-methods-use-this": "off"
  }
}

5. Add format on save for VS code

Add this minimal configuration file in .vscode/settings.json

{
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    },
    "editor.formatOnSave": true,
    "workbench.colorCustomizations": {
      "statusBar.background": "#63ace5" // Change this color!
    }
  }

PS: colorCustomizations is not necessary but it is handful to add a specific color in the "status bar", to distinguish between multiple instances of VS Code.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment