Blog

ESLint for new projects

October, 2020

I start new projects often and I'm obsessive about doing them the right way. ESLint, Prettier, the works. Every project, even if it's a single page Next.js site, needs to have all the bells and whistles so things are perfectly consistent across all my code ever written.

This collection is still a work in progress while I tinker with things, but I want to create a central repository so I can easily access everything.

Let's get started.

Prettier

I love Prettier. It's opinionated and gives me a fantastic base to get started. I use a precommit hook alongside Prettier to make sure anything missed gets formatted.

Dependencies

Copy
yarn add --dev prettier husky pretty-quick

Precommit hook — package.json

Copy
{
  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  }
}

Config — prettier.config.js

Copy
module.exports = {
  bracketSpacing: true,
  jsxBracketSameLine: true,
  semi: false,
  singleQuote: true,
  trailingComma: 'none'
}

ESLint

I rely on a lot of ESLint plugins to help lint my code. I'm obsessive about standards and consistency. Other than Prettier and standard ESLint / TypeScript rules, here are the things I need.

  • Imports should be grouped and sorted
  • Object (including destructured) keys should be sorted alphabetically
  • Component props should be sorted alphabetically
  • Component props should be sorted alphabetically

By using these rules, I can be certain that code across repos will be consistent. But different types of projects require different rules and configurations. Let's break it down.

Node.js

Dependencies

Copy
yarn add --dev @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-simple-import-sort eslint-plugin-sort-destructure-keys eslint-plugin-sort-keys-fix

Script — package.json

Copy
eslint . --ext js,ts

Configuration — .eslintrc.json

Copy
{
  "extends": [
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/@typescript-eslint"
  ],
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint",
    "sort-keys-fix",
    "sort-destructure-keys",
    "simple-import-sort",
    "prettier"
  ],
  "rules": {
    "prettier/prettier": "error",
    "simple-import-sort/sort": "error",
    "sort-destructure-keys/sort-destructure-keys": "error",
    "sort-keys-fix/sort-keys-fix": "error"
  }
}

Next.js

Dependencies

Copy
yarn add --dev @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-simple-import-sort eslint-plugin-sort-destructure-keys eslint-plugin-sort-keys-fix

Script — package.json

Copy
eslint . --ext js,ts,tsx

Configuration — .eslintrc.json

Copy
{
  "env": {
    "browser": true,
    "node": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended",
    "plugin:react-hooks/recommended",
    "prettier",
    "prettier/react",
    "prettier/@typescript-eslint"
  ],
  "overrides": [
    {
      "files": ["*.js"],
      "rules": {
        "@typescript-eslint/no-var-requires": "off"
      }
    }
  ],
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint",
    "react-hooks",
    "sort-keys-fix",
    "sort-destructure-keys",
    "simple-import-sort",
    "prettier"
  ],
  "rules": {
    "@typescript-eslint/explicit-function-return-type": "off",
    "prettier/prettier": "error",
    "react-hooks/exhaustive-deps": "error",
    "react-hooks/rules-of-hooks": "error",
    "react/jsx-sort-props": "error",
    "simple-import-sort/sort": "error",
    "sort-destructure-keys/sort-destructure-keys": "error",
    "sort-keys-fix/sort-keys-fix": "error"
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

React Native

Dependencies

Copy
yarn add --dev @react-native-community/eslint-config @react-native-community/eslint-plugin @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-native eslint-plugin-simple-import-sort eslint-plugin-sort-destructure-keys eslint-plugin-sort-keys-fix

Script — package.json

Copy
eslint . --ext js,ts,tsx

Configuration — .eslintrc.json

Copy
{
  "extends": [
    "@react-native-community",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react-hooks/recommended",
    "prettier",
    "prettier/react",
    "prettier/@typescript-eslint"
  ],
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint",
    "react-hooks",
    "react-native",
    "react",
    "simple-import-sort",
    "sort-destructure-keys",
    "sort-keys-fix",
    "prettier"
  ],
  "rules": {
    "@typescript-eslint/explicit-member-accessibility": "off",
    "@typescript-eslint/explicit-module-boundary-types": "error",
    "@typescript-eslint/no-use-before-define": [
      "error",
      {
        "variables": false
      }
    ],
    "prettier/prettier": "error",
    "react-hooks/exhaustive-deps": "error",
    "react-hooks/rules-of-hooks": "error",
    "react-native/no-inline-styles": "error",
    "react-native/no-raw-text": "error",
    "react-native/no-unused-styles": "error",
    "react-native/split-platform-components": "error",
    "react/jsx-sort-props": "error",
    "simple-import-sort/sort": "error",
    "sort-destructure-keys/sort-destructure-keys": "error",
    "sort-keys-fix/sort-keys-fix": "error"
  }
}

I'll keep updating this as I discover more useful stuff or remove redundant or incorrect stuff. If you have any suggestions, hit me up on Twitter.