eslint-config-peerigon
Peerigon coding rules as eslint config
Last updated 3 months ago by peeri .
Unlicense · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install eslint-config-peerigon 
SYNC missed versions from official npm registry.

eslint-config-peerigon

Peerigon coding rules as ESLint config.

Dependency Status semantic-release

Quick start

There are presets for the most common setups:

Prettier + TypeScript

npm i eslint eslint-config-peerigon @typescript-eslint/eslint-plugin @typescript-eslint/parser --save-dev
{
    "extends": [
        "peerigon/presets/prettier-typescript.js"
    ]
}

Prettier + TypeScript + React

npm i eslint eslint-config-peerigon @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-react eslint-plugin-jsx-a11y eslint-plugin-react-hooks --save-dev
{
    "extends": [
        "peerigon/presets/prettier-typescript-react.js"
    ]
}

Motivation

Linting and formatting rules are always a balance between

  • ease of reading
  • ease of refactoring
  • ease of writing.

We think that

  • code is read more often than refactored
  • and refactored more often than written from scratch.

Our linting rules have been designed with these assumptions in mind.

Features

Atomic changes

Our formatting rules have been chosen carefully so that a change of a file is as atomic as possible. This makes it easier to review pull requests because there are no meaningless changes anymore.

Example: I want to change a variable from let to const.

// Bad coding style because useless whitespace changes were necessary
-let a   = 1,
+let   a   = 1,
-    bbb = 2,
+      cc  = 3;
-    cc  = 3;
+const bbb = 3;
// Good coding style because only the relevant parts need to be changed
let a = 1;
-let bb = 2;
+const bb = 2;
let ccc = 3;

This is also the reason why we prefer dangling commas for multiline arrays, objects and arguments although they look very unfamiliar on first sight (see discussion).

Consistent formatting

For the purpose of atomic changes, our rules are intentionally strict about formatting which are usually autofixable. You should use an editor configuration where you can apply these autofixes on demand (for instance when saving the file).

We recommend combining these linting rules with Prettier (see below). There's also a recommended configuration for VSCode.

Code smells as a warning

Developers take shortcuts. And that's ok because at the end of the day we have to deliver software within fixed time frames and budgets. Sometimes it's also because we don't know of a better alternative. We call these shortcuts "code smells" and our linting rules will complain about them with a warning.

This means that this code is potentially problematic, but you don't have to fix it right away. You should keep the warning and come back later to refactor it (e.g. during a refactoring sprint). The amount of warnings is also a good indicator for technical debt.

If you think that there is a good reason for deviating from the standard path, disable the warning and put an explanation above that comment why it's ok to disable the rule in that case, like:

// The API returns snakecase properties
// eslint-disable-next-line babel/camelcase
function fetchUsers() {
    // ...
}

Disabling rules

Try to disable as less rules as possible. In most cases it's best to just write

// eslint-disable-next-line [rule-code]

where [rule-code] is the code that is displayed along the error message. Disabling the next line is usually better because it resists Prettier reformatting.

Sometimes it makes sense to disable a rule within a specifc file. In that case you can put the following snippet at the beginning of the file:

/* eslint-disable [rule-code] */

If you don't agree with a rule, please do not just disable the rule. Often there are good reasons and the current setting is the result of years of experience. It's better to create an issue here to start a discussion about the pros and cons of a rule.

Different styles

We acknowledge that there are certain rules where there are no actual pros and cons or where there is no clear winner. You just have to decide for one style and stick with it. We also know that some rules make sense in one project, but don't make sense in another project. That's why we also provide a list of accepted custom styles (see also this discussion) which you can pick.

Naming conventions for properties

Sometimes we're not in full control over the naming conventions in our codebase, for instance if data is coming from a foreign API. While it often is preferable to transform property names into camelCase, it might not be practical. In these situations you can disable the check for properties like this:

const options = require("eslint-config-peerigon/options.js");

module.exports = {
    /* ... */
    rules: {
        // The API uses snake_case as properties
        "babel/camelcase": ["warn", {
            ...options["camelcase"],
            properties: "never"
        }]
    },
};

In TypeScript projects:

const options = require("eslint-config-peerigon/options.js");

module.exports = {
    /* ... */
    rules: {
        // The API uses snake_case as properties
        "@typescript-eslint/naming-convention": [
            "warn",
            options["@typescript-eslint/naming-convention"].ignoreProperties,
            ...options["@typescript-eslint/naming-convention"].defaultRules,
        ],
    },
};

Provided configs

peerigon

Base rules for every project. You should always add these rules.

npm i eslint eslint-config-peerigon --save-dev

These rules assume a modern project with full ES2015 support, including ES modules. For specific environments like Node.js or old JS engines, see below. The base rules do not define an env, so you might want to do that for yourself to enable specific globals.

Add an .eslintrc.json to the project's root folder:

{
    "extends": [
        // Base rules for every project
        "peerigon",
        "prettier" // add this at the end of 'extends' if you're using Prettier
    ],
    // Do not search for further eslint configs in upper directories
    "root": true
}

In your package.json, add a test:lint script and run it as posttest:

{
    "scripts": {
        "test:lint": "eslint --cache ./src ./test",
        "posttest": "npm run test:lint"
    }
}

The base rules use the eslint-plugin-import to resolve imports. Although it's possible to define custom resolvers, it's highly discouraged to deviate from the common Node/webpack resolving algorithm. Other tools like linters and intellisense don't work reliably when you change the resolver.

peerigon/node

Special rules for Node.js >= 8.0.0 environments:

{
    "extends": [
        // Base rules with full ES2015 support
        "peerigon",
        // Rules for node
        "peerigon/node",
        "prettier" // add this if you're using Prettier
    ]
    // Setting env.node = true is not necessary, this is already done by peerigon/node
}

These rules assume that you're using CommonJS modules. In case you're using ECMAScript modules, you should set parserOptions.sourceType: "module". We will change that once a LTS Node.js version has official support for ECMAScript modules.

peerigon/react

Important: Requires eslint-plugin-react, eslint-plugin-jsx-a11y and eslint-plugin-react-hooks as project dependency.

npm i eslint-plugin-react eslint-plugin-jsx-a11y eslint-plugin-react-hooks --save-dev

Rules for React development, including accessibility rules. These rules are also applicable in other JSX environments, like Preact:

{
    "extends": [
        "peerigon",
        "peerigon/react",
        "prettier", // add this and...
        "prettier/react" // ...this if you're using Prettier
    ],
    "root": true
}

We recommend using peerigon/styles/react-jsx-no-literals if you're using i18n in your project. You can use peerigon/styles/react-jsx-no-bind if you're using memo and shouldComponentUpdate a lot.

peerigon/typescript

Important: Requires @typescript-eslint/eslint-plugin and @typescript-eslint/parser as project dependency.

npm i @typescript-eslint/eslint-plugin @typescript-eslint/parser --save-dev

Rules for TypeScript.

⚠️ Attention: These rules require your tsconfig.json. Specify the path in parserOptions.project (see also here for more information). The path should be relative to the folder where eslint is executed.

{
    "extends": [
        "peerigon",
        "peerigon/typescript",
        // Arrow functions are preferred with TypeScript
        // See https://github.com/peerigon/eslint-config-peerigon/issues/23#issuecomment-472614432
        "peerigon/styles/prefer-arrow",
        "prettier", // add this and...
        "prettier/@typescript-eslint" // ...this if you're using Prettier
    ],
    "parserOptions": {
        // Relative to the folder where eslint is executed
        // See https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/parser/README.md#parseroptionsproject
        "project": "./tsconfig.json"
    },
    "root": true,
}

You need to add --ext js,ts,tsx to the test:lint script:

{
    "scripts": {
        "test:lint": "eslint --cache --ext js,jsx,ts,tsx ./src ./test"
    }
}

We recommend using peerigon/styles/prefer-arrow because arrow functions (or function expressions in general) can leverage TypeScript's contextual typing.

Do you see an error that looks like this?

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: ...
The file must be included in at least one of the projects provided

This is a sign that ESLint is trying to lint a file that is not included by your tsconfig.json. You need to adjust either parserOptions.project or include of the referenced tsconfig.json.

peerigon/jsdoc

Important: Requires eslint-plugin-jsdoc as project dependency.

npm i eslint-plugin-jsdoc --save-dev

Makes sure that JSDoc annotations are written in a standard-compliant and uniform way.

{
    "extends": [
        "peerigon",
        "peerigon/jsdoc"
    ],
    "root": true
}

peerigon/flowtype

Important: Requires babel-eslint and eslint-plugin-flowtype as project dependency.

npm i babel-eslint eslint-plugin-flowtype --save-dev

Rules for Flowtype.

{
    "extends": [
        "peerigon",
        "peerigon/flowtype",
        "prettier", // add this and...
        "prettier/flowtype" // ...this if you're using Prettier
    ],
    "root": true
}

peerigon/es5

Special rules for older projects:

{
    "extends": [
        // Base rules with full ES2015 support
        "peerigon",
        // Legacy rules for older projects
        "peerigon/es5"
    ],
    "root": true
}

Styles

The following rules enable specific writing styles. Use them as you prefer.

peerigon/styles/prefer-arrow

Important: Requires eslint-plugin-prefer-arrow as project dependency.

npm i eslint-plugin-prefer-arrow --save-dev

Enforces arrow function expressions instead of function declarations (see #23). Regular functions are still allowed as methods in objects or classes.

    "extends": [
        "peerigon",
        "peerigon/styles/prefer-arrow"
    ],

peerigon/styles/no-default-export

Forbids usage of export default. When using default exports, it becomes harder to name classes or functions consistently throughout the codebase since every module can pick its own name for the imported thing. Nicholas C. Zakas, the creator of ESLint, wrote an article with more compelling arguments why he stopped using export default.

    "extends": [
        "peerigon",
        "peerigon/styles/no-default-export"
    ],

Please note: This rule is disabled in .jsx and .tsx files because React components are usually exported via export default. React.lazy even expects the lazy loaded component to be exported as default.

peerigon/styles/no-null

Important: Requires eslint-plugin-no-null as project dependency.

npm i eslint-plugin-no-null --save-dev

Forbids the usage of null. In a codebase it's often better to use a single non-value to represent the absence of a value. With the rise of default parameters and destructuring defaults, JavaScript developed a clear tendency towards undefined. This issue summarizes the arguments (and trade-offs) of null vs. undefined.

    "extends": [
        "peerigon",
        "peerigon/styles/no-null"
    ],

Please note: If you use this rule, you will probably still need a single null value which you can refer to whenever you need to use null because of third-party code:

// eslint-disable-next-line no-null/no-null
export const NULL = null;

peerigon/styles/prefer-interface

Important: Use it in combination with peerigon/typescript.

Prefer interface over type.

    "extends": [
        "peerigon",
        "peerigon/typescript",
        "peerigon/styles/prefer-interface"
    ],

peerigon/styles/react-jsx-no-bind

Important: Use it in combination with peerigon/react.

Depending on the way you write your components, it might be not ok to create functions during render(). Use it if you're using things like React.memo() or shouldComponentUpdate a lot.

    "extends": [
        "peerigon",
        "peerigon/react",
        "peerigon/styles/react-jsx-no-bind"
    ],

peerigon/styles/react-jsx-no-literals

Important: Use it in combination with peerigon/react.

Use this style if you're using i18n. It prevents people from putting raw strings in components.

    "extends": [
        "peerigon",
        "peerigon/react",
        "peerigon/styles/react-jsx-no-literals"
    ],

It disallows this:

const Hello = <div>test</div>;

As an escape hatch, this is still allowed:

const Hello = <div>{'test'}</div>;

peerigon/styles/prefer-array-shorthand

Important: Use it in combination with peerigon/typescript.

Enforces typescript arrays to use the shorthand array-style instead of the generic style.

    "extends": [
        "peerigon",
        "peerigon/typescript",
        "peerigon/styles/prefer-array-shorthand"
    ],

It enforces this:

const foo: string[] = [];

instead of

const foo: Array<string> = [];

Prettier

In order to avoid conflicts between Prettier and our rules, you should always add prettier rules at the end of extends. For example, in a TypeScript + React project you would use the following configuration:

{
    "extends": [
        "peerigon",
        "peerigon/typescript",
        "peerigon/styles/prefer-arrow",
        "peerigon/react",
        // prettier must be at the end
        "prettier",
        "prettier/@typescript-eslint",
        "prettier/react"
    ],
    "root": true,
};

This module already lists eslint-config-prettier as dependency which is why you don't have to install it manually.

VSCode

This is our recommended VSCode configuration using the Prettier extension. Adjust it to the needs of your particular project:

{
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    }
}

License

Unlicense

Sponsors

Current Tags

  • 26.0.0-beta.4                                ...           beta (8 months ago)
  • 28.1.4                                ...           latest (3 months ago)

83 Versions

  • 28.1.4                                ...           3 months ago
  • 28.1.3                                ...           4 months ago
  • 28.1.2                                ...           4 months ago
  • 28.1.1                                ...           4 months ago
  • 28.1.0                                ...           4 months ago
  • 28.0.0                                ...           5 months ago
  • 27.8.0                                ...           5 months ago
  • 27.7.0                                ...           5 months ago
  • 27.6.1                                ...           5 months ago
  • 27.6.0                                ...           7 months ago
  • 27.5.1                                ...           7 months ago
  • 27.5.2                                ...           7 months ago
  • 27.5.0                                ...           8 months ago
  • 27.4.0                                ...           8 months ago
  • 27.3.0                                ...           8 months ago
  • 27.2.0                                ...           8 months ago
  • 27.1.3                                ...           8 months ago
  • 27.1.2                                ...           8 months ago
  • 27.1.1                                ...           8 months ago
  • 27.1.0                                ...           8 months ago
  • 27.0.1                                ...           8 months ago
  • 27.0.0                                ...           8 months ago
  • 26.0.0                                ...           8 months ago
  • 26.0.0-beta.4                                ...           8 months ago
  • 26.0.0-beta.3                                ...           9 months ago
  • 26.0.0-beta.2                                ...           9 months ago
  • 26.0.0-beta.1                                ...           9 months ago
  • 25.3.1                                ...           9 months ago
  • 25.3.0                                ...           9 months ago
  • 25.2.0                                ...           9 months ago
  • 25.1.1                                ...           9 months ago
  • 25.1.0                                ...           9 months ago
  • 25.0.1                                ...           10 months ago
  • 25.0.0                                ...           10 months ago
  • 24.0.0                                ...           a year ago
  • 23.2.0                                ...           a year ago
  • 23.1.0                                ...           a year ago
  • 23.0.0                                ...           a year ago
  • 22.1.0                                ...           a year ago
  • 22.0.0                                ...           a year ago
  • 21.2.0                                ...           a year ago
  • 21.1.0                                ...           a year ago
  • 21.0.0                                ...           a year ago
  • 20.0.1                                ...           a year ago
  • 20.0.0                                ...           a year ago
  • 19.0.0                                ...           a year ago
  • 18.0.0                                ...           a year ago
  • 17.1.0                                ...           2 years ago
  • 17.0.0                                ...           2 years ago
  • 16.0.0                                ...           2 years ago
  • 15.0.2                                ...           2 years ago
  • 15.0.1                                ...           2 years ago
  • 15.0.0                                ...           2 years ago
  • 14.0.0                                ...           2 years ago
  • 13.0.0                                ...           2 years ago
  • 12.0.1                                ...           3 years ago
  • 12.0.0                                ...           3 years ago
  • 11.3.1                                ...           3 years ago
  • 11.3.0                                ...           3 years ago
  • 11.2.0                                ...           3 years ago
  • 11.1.0                                ...           3 years ago
  • 11.0.1                                ...           3 years ago
  • 11.0.0                                ...           3 years ago
  • 10.1.1                                ...           4 years ago
  • 10.1.0                                ...           4 years ago
  • 10.0.0                                ...           4 years ago
  • 9.0.0                                ...           4 years ago
  • 8.0.0                                ...           4 years ago
  • 7.0.0                                ...           4 years ago
  • 6.0.1                                ...           4 years ago
  • 6.0.0                                ...           4 years ago
  • 5.1.0                                ...           4 years ago
  • 5.0.0                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 3.1.1                                ...           5 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
  • 0.0.0                                ...           5 years ago

Copyright 2014 - 2016 © taobao.org |