Ali Zahid
ESLint for new projects

ESLint for new projects

A reference store of my ESLint configurations for new projects.
October 14, 2020

I start new projects often, and I'm obsessive about doing them the right way. ESLint, Prettier, the works. Even if it's a single page Next.js site, every project 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 to access everything easily.

Let's get started.

Prettier

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

Before installing Husky, make sure you've created a Git repository first, or installing Git hooks will fail.

Install dependencies

yarn add --dev prettier husky pretty-quick

Prettier config — prettier.config.js

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

Setup Husky

yarn husky init

Precommit hook — .husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn pretty-quick --staged

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

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

Node.js

Dependencies

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

eslint . --ext js,ts

Configuration — .eslintrc.json

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

Next.js

Dependencies

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

eslint . --ext js,ts,tsx

Configuration — .eslintrc.json

{
"env": {
"browser": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:prettier/recommended"
],
"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/exports": "error",
"simple-import-sort/imports": "error",
"sort-destructure-keys/sort-destructure-keys": "error",
"sort-keys-fix/sort-keys-fix": "error"
},
"settings": {
"react": {
"version": "detect"
}
}
}

React Native

Dependencies

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

eslint . --ext js,ts,tsx

Configuration — .eslintrc.json

{
"extends": [
"@react-native-community",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
"plugin:prettier/recommended"
],
"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/exports": "error",
"simple-import-sort/imports": "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.