DEV Community

Nazmul Islam Nahid
Nazmul Islam Nahid

Posted on • Edited on

Eslint and Prettier

Install dependencies

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

Initialize eslint

eslint --init

=> Select this options for react
- To check syntax, find problems, and enforce code style
- JavaScript modules (import/export)
- React
- Does your project use TypeScript? » No
- Browser
- Use a popular style guide
- Airbnb: https://github.com/airbnb/javascript
- JSON
- Would you like to install them now? » Yes
- yarn
Enter fullscreen mode Exit fullscreen mode

Add configuration in .eslintrc

{
  "env": {
    "browser": true,
    "es2021": true,
    "node": true,
    "es6": true,
    "jest": true
  },

  "extends": [
    "airbnb",
    "airbnb/hooks",
    "eslint:recommended",
    "prettier",
    "plugin:jsx-a11y/recommended",
    "plugin:react/recommended",
    "airbnb",
    "prettier"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["react", "prettier", "react-hooks"],

  "rules": {
    "react/react-in-jsx-scope": 0,
    "react-hooks/rules-of-hooks": "error",
    "react/button-has-type": 0,
    "consistent-return": 1,
    "no-unused-vars": 1,
    "import/prefer-default-export": 0,
    "no-param-reassign": 0,
    "no-console": 0,
    "react/state-in-constructor": 0,
    "indent": 0,
    "linebreak-style": 0,
    "react/prop-types": 0,
    "jsx-a11y/click-events-have-key-events": 0,
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [".js", ".jsx"]
      }
    ],
    "prettier/prettier": [
      "error",
      {
        "trailingComma": "es5",
        "singleQuote": true,
        "printWidth": 100,
        "tabWidth": 4,
        "semi": true,
        "endOfLine": "auto"
      }
    ]
  }
}

Enter fullscreen mode Exit fullscreen mode

Workspace settings.json

{
  // Theme
  "workbench.colorTheme": "Default Dark+",

  // config related to code formatting
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "[javascript]": {
    "editor.formatOnSave": false,
    "editor.defaultFormatter": null
  },
  "[javascriptreact]": {
    "editor.formatOnSave": false,
    "editor.defaultFormatter": null
  },
  "javascript.validate.enable": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.fixAll.tslint": true,
    "source.organizeImports": true
  },
  "eslint.alwaysShowStatus": true,
  // emmet
  "emmet.triggerExpansionOnTab": true,
  "emmet.includeLanguages": {
    "javascript": "javascriptreact"
  }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)