Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Latest commit

 

History

History
54 lines (34 loc) · 2 KB

File metadata and controls

54 lines (34 loc) · 2 KB

Sort import statements and require calls (sort-imports)

This rule enforces (and autofixes) that import statements and require calls — henceforth referred to as just "imports" — appear in the order described in our guidelines.

Rule Details

Imports appear in the following order:

  1. "External" imports (ie. NodeJS built-in modules and dependencies specified in "package.json"; these do not start with "." or "..").
  2. "Internal" imports (ie. local to the projects, starting with "." or "..");

Within each group, we sort lexicographically (and case-sensitively) by the module source (ie. the thing on the right-hand side).

Between groups, we expect a blank line, but that is enforced separately, by the group-imports rule.

Likewise, we expect imports to appear at the top of the file, before other statements, but that too is enforced separately, by the imports-first rule.

See the links in the Further Reading section below for the rationale behind this pattern.

A note about side-effect-only imports

NOTE: Imports like those in the following example are made for side-effects only and cannot be reordered because we don't know statically what may depend on those side-effects occurring in a particular order.

// A side-effect-only import:
imports 'thing';

// A side-effect-only require:
require('other');

Any time this rule encounters such an import, it considers it a boundary and will not reorder any imports across that boundary.

Examples

Examples of incorrect code for this rule:

import {g, z} from 'one';
import x from './x';
import {a} from 'other';

Examples of correct code for this rule:

import {g, z} from 'one';
import {a} from 'other';
import x from './x';

Further Reading