Skip to content

Commit

Permalink
Update: Add ImportDeclaration option to indent rule (#8955)
Browse files Browse the repository at this point in the history
* Update: Add option to adjust indent on import declarations, with tests

* Docs: Add docs for ImportDeclaration option

* Chore: Add tests and examples for ImportDeclaration option as a number
  • Loading branch information
aviddiviner authored and kaicataldo committed Aug 5, 2017
1 parent de75f9b commit 62911e4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
42 changes: 42 additions & 0 deletions docs/rules/indent.md
Expand Up @@ -82,6 +82,7 @@ This rule has an object option:
* `arguments` (default: 1) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all arguments of the expression must be aligned with the first argument. This can also be set to `"off"` to disable checking for CallExpression arguments.
* `"ArrayExpression"` (default: 1) enforces indentation level for elements in arrays. It can also be set to the string `"first"`, indicating that all the elements in the array should be aligned with the first element. This can also be set to `"off"` to disable checking for array elements.
* `"ObjectExpression"` (default: 1) enforces indentation level for properties in objects. It can be set to the string `"first"`, indicating that all properties in the object should be aligned with the first property. This can also be set to `"off"` to disable checking for object properties.
* `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members.
* `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions.

Level of indentation denotes the multiple of the indent specified. Example:
Expand Down Expand Up @@ -519,6 +520,47 @@ var foo = { bar: 1,
baz: 2 };
```

### ImportDeclaration

Examples of **correct** code for this rule with the `4, { "ImportDeclaration": 1 }` option (the default):

```js
/*eslint indent: ["error", 4, { ImportDeclaration: 1 }]*/

import { foo,
bar,
baz,
} from 'qux';

import {
foo,
bar,
baz,
} from 'qux';
```

Examples of **incorrect** code for this rule with the `4, { ImportDeclaration: "first" }` option:

```js
/*eslint indent: ["error", 4, { ImportDeclaration: "first" }]*/

import { foo,
bar,
baz,
} from 'qux';
```

Examples of **correct** code for this rule with the `4, { ImportDeclaration: "first" }` option:

```js
/*eslint indent: ["error", 4, { ImportDeclaration: "first" }]*/

import { foo,
bar,
baz,
} from 'qux';
```

### flatTernaryExpressions

Examples of **incorrect** code for this rule with the default `4, { "flatTernaryExpressions": false }` option:
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/indent.js
Expand Up @@ -578,6 +578,7 @@ module.exports = {
},
ArrayExpression: ELEMENT_LIST_SCHEMA,
ObjectExpression: ELEMENT_LIST_SCHEMA,
ImportDeclaration: ELEMENT_LIST_SCHEMA,
flatTernaryExpressions: {
type: "boolean"
}
Expand Down Expand Up @@ -616,6 +617,7 @@ module.exports = {
MemberExpression: 1,
ArrayExpression: 1,
ObjectExpression: 1,
ImportDeclaration: 1,
flatTernaryExpressions: false
};

Expand Down Expand Up @@ -1166,7 +1168,7 @@ module.exports = {
const openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken);
const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);

addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, 1);
addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration);
}

const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from");
Expand Down
74 changes: 74 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -3482,6 +3482,46 @@ ruleTester.run("indent", rule, {
code: "import 'foo'",
parserOptions: { sourceType: "module" }
},
{
code: unIndent`
import { foo,
bar,
baz,
} from 'qux';
`,
options: [4, { ImportDeclaration: 1 }],
parserOptions: { sourceType: "module" }
},
{
code: unIndent`
import {
foo,
bar,
baz,
} from 'qux';
`,
options: [4, { ImportDeclaration: 1 }],
parserOptions: { sourceType: "module" }
},
{
code: unIndent`
import { apple as a,
banana as b } from 'fruits';
import { cat } from 'animals';
`,
options: [4, { ImportDeclaration: "first" }],
parserOptions: { sourceType: "module" }
},
{
code: unIndent`
import { declaration,
can,
be,
turned } from 'off';
`,
options: [4, { ImportDeclaration: "off" }],
parserOptions: { sourceType: "module" }
},

// https://github.com/eslint/eslint/issues/8455
{
Expand Down Expand Up @@ -7434,6 +7474,40 @@ ruleTester.run("indent", rule, {
parserOptions: { sourceType: "module" },
errors: expectedErrors([[2, 4, 0, "Identifier"], [3, 4, 2, "Identifier"]])
},
{
code: unIndent`
import { foo,
bar,
baz,
} from 'qux';
`,
output: unIndent`
import { foo,
bar,
baz,
} from 'qux';
`,
options: [4, { ImportDeclaration: "first" }],
parserOptions: { sourceType: "module" },
errors: expectedErrors([[3, 9, 10, "Identifier"]])
},
{
code: unIndent`
import { foo,
bar,
baz,
} from 'qux';
`,
output: unIndent`
import { foo,
bar,
baz,
} from 'qux';
`,
options: [2, { ImportDeclaration: 2 }],
parserOptions: { sourceType: "module" },
errors: expectedErrors([[3, 4, 5, "Identifier"]])
},
{
code: unIndent`
export {
Expand Down

0 comments on commit 62911e4

Please sign in to comment.