Skip to content

Latest commit

 

History

History
205 lines (159 loc) · 3.84 KB

import-destructuring-spacing.md

File metadata and controls

205 lines (159 loc) · 3.84 KB

enforce line breaks in destructured imports (import-destructuring-spacing)

🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.

Rule Details

We want clear rules on how destructured import specifiers are broken up by lines.

Options

This rule has a mixed option:

For example, to enforce multiline behavior (default):

{
    "indent": ["error", "multiline", 2]
}

or use the default:

{
    "indent": ["error"]
}

Or to enforce a specifier limit:

{
    "indent": ["error", 3, 2]
}

Indentation

Both options support a second options argument to determine how many spaces of indentation to use. This is necessary because as of eslint 3, indent will not enforce indentation of import statements.

Multiline

Using multiline enforcement means that you have two options:

  • Use line breaks between no specifiers
  • Use line breaks between all specifiers

👎 Examples of incorrect code for this rule with the "multiline", 2 option:

// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { 
  a, b, 
  c
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { 
    a, b, 
    c
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { a, b, c
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { 
  a, b, c } from 'somewhere';

👍 Examples of correct code for this rule with the "multiline" option:

// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { a, b, c } from 'somewhere';
// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { a, b, c } 
  from 'somewhere';
// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { 
  a, 
  b, 
  c 
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", "multiline", 2]
import { 
  a, 
  b, 
  c 
} 
  from 'somewhere';

Specifier Limit

Using a specifier limit means that for this number or greater specifier, you must use separate lines for all of them. If you have less than this many, you must put them on a single line.

👎 Examples of incorrect code for this rule with the 3, 2 option (specifier limit of 3 with indentation of 2 spaces):

// eslint import-destructuring-spacing: ["error", 3, 2]
import { a, b, c } from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { 
  a, 
  b
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { 
    a, 
    b, 
    c 
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { a, b, c, d } from 'somewhere';

👍 Examples of correct code for this rule with the 3, 2 option (specifier limit of 3 with indentation of 2 spaces):

// eslint import-destructuring-spacing: ["error", 3, 2]
import { a, b } from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { a, b } 
  from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { 
  a, 
  b, 
  c 
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { 
  a, 
  b, 
  c 
} 
  from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { 
  a, 
  b, 
  c,
  d
} from 'somewhere';
// eslint import-destructuring-spacing: ["error", 3, 2]
import { 
  a, 
  b, 
  c,
  d
} 
  from 'somewhere';

When Not To Use It

If you don't want to enforce line breaks in import speecifiers

Resouces