Skip to content

Commit

Permalink
Update: indent from tokens in import statements (fixes #8438) (#8466)
Browse files Browse the repository at this point in the history
* Update: indent `from` tokens in import statements (fixes #8438)

* Handle exports consistently with imports

* Clarify comment about `export from`
  • Loading branch information
not-an-aardvark committed May 5, 2017
1 parent 0a9a90f commit cf940c6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,18 @@ module.exports = {

ExportNamedDeclaration(node) {
if (node.declaration === null) {
addElementListIndent(getTokensAndComments(node).slice(1), node.specifiers, 1);
const tokensInNode = getTokensAndComments(node);
const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
const closingCurlyIndex = lodash.sortedIndexBy(tokensInNode, closingCurly, token => token.range[0]);

// Indent the specifiers in `export {foo, bar, baz}`
addElementListIndent(tokensInNode.slice(1, closingCurlyIndex + 1), node.specifiers, 1);

if (node.source) {

// Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'`
offsets.setDesiredOffsets(tokensInNode.slice(closingCurlyIndex + 1), sourceCode.getFirstToken(node), 1);
}
}
},

Expand Down Expand Up @@ -896,6 +907,14 @@ module.exports = {

addElementListIndent(specifierTokens, node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), 1);
}

const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from");

if (fromToken) {
const tokensToOffset = sourceCode.getTokensBetween(fromToken, sourceCode.getLastToken(node), 1);

offsets.setDesiredOffsets(tokensToOffset, sourceCode.getFirstToken(node), 1);
}
},

LogicalExpression: addBinaryOrLogicalExpressionIndent,
Expand Down
81 changes: 81 additions & 0 deletions tests/lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,16 @@ ruleTester.run("indent", rule, {
`,
parserOptions: { sourceType: "module" }
},
{
code: unIndent`
export {
foo,
bar,
baz
} from 'qux';
`,
parserOptions: { sourceType: "module" }
},
{
code: unIndent`
var a = 1,
Expand Down Expand Up @@ -3045,6 +3055,17 @@ ruleTester.run("indent", rule, {
{
code: "x => {}",
parserOptions: { ecmaVersion: 6 }
},
{
code: unIndent`
import {foo}
from 'bar';
`,
parserOptions: { sourceType: "module" }
},
{
code: "import 'foo'",
parserOptions: { sourceType: "module" }
}
],

Expand Down Expand Up @@ -5577,6 +5598,42 @@ ruleTester.run("indent", rule, {
parserOptions: { sourceType: "module" },
errors: expectedErrors([[2, 4, 0, "Identifier"], [3, 4, 2, "Identifier"]])
},
{
code: unIndent`
export {
foo,
bar,
baz
};
`,
output: unIndent`
export {
foo,
bar,
baz
};
`,
parserOptions: { sourceType: "module" },
errors: expectedErrors([[2, 4, 0, "Identifier"], [3, 4, 2, "Identifier"]])
},
{
code: unIndent`
export {
foo,
bar,
baz
} from 'qux';
`,
output: unIndent`
export {
foo,
bar,
baz
} from 'qux';
`,
parserOptions: { sourceType: "module" },
errors: expectedErrors([[2, 4, 0, "Identifier"], [3, 4, 2, "Identifier"]])
},
{

// https://github.com/eslint/eslint/issues/7233
Expand Down Expand Up @@ -6235,6 +6292,30 @@ ruleTester.run("indent", rule, {
; [1, 2, 3].map(baz)
`,
errors: expectedErrors([3, 0, 4, "Punctuator"])
},
{
code: unIndent`
import {foo}
from 'bar';
`,
output: unIndent`
import {foo}
from 'bar';
`,
parserOptions: { sourceType: "module" },
errors: expectedErrors([2, 4, 0, "Identifier"])
},
{
code: unIndent`
export {foo}
from 'bar';
`,
output: unIndent`
export {foo}
from 'bar';
`,
parserOptions: { sourceType: "module" },
errors: expectedErrors([2, 4, 0, "Identifier"])
}
]
});

0 comments on commit cf940c6

Please sign in to comment.