Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: check allman-style blocks correctly in indent rule (fixes #8493
…) (#8499)

* Update: check allman-style blocks correctly in indent rule (fixes #8493)

Previously, there was a bug in the indent rule where allman-style blocks with function bodies would be indented by one too many levels. This commit fixes the issue and updates the BlockStatement indentation handler to use the same logic as other lists of nodes (e.g. arrays).

* Update explanation comment about BlockStatements
  • Loading branch information
not-an-aardvark authored and ilyavolodin committed May 5, 2017
1 parent f6256d4 commit 74ab344
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 43 deletions.
74 changes: 31 additions & 43 deletions lib/rules/indent.js
Expand Up @@ -555,49 +555,6 @@ module.exports = {
return sourceCode.getTokens(node, { includeComments: true });
}

/**
* Check indentation for blocks
* @param {ASTNode} node node to check
* @returns {void}
*/
function addBlockIndent(node) {

let blockIndentLevel;

if (node.parent && isOuterIIFE(node.parent)) {
blockIndentLevel = options.outerIIFEBody;
} else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) {
blockIndentLevel = options.FunctionExpression.body;
} else if (node.parent && node.parent.type === "FunctionDeclaration") {
blockIndentLevel = options.FunctionDeclaration.body;
} else {
blockIndentLevel = 1;
}

/*
* If the block starts on its own line, then match the tokens in the block against the opening curly of the block.
* Otherwise, match the token in the block against the tokens in the block's parent.
*
* For example:
* function foo() {
* {
* // (random block, tokens should get matched against the { that opens the block)
* foo;
* }
*
* if (foo &&
* bar) {
* baz(); // Tokens in the block should get matched against the `if` statement, even though the opening curly is indented.
* }
*/
const tokens = getTokensAndComments(node);
const tokenToMatchAgainst = tokenInfo.isFirstTokenOfLine(tokens[0]) ? tokens[0] : sourceCode.getFirstToken(node.parent);

offsets.matchIndentOf(tokenToMatchAgainst, tokens[0]);
offsets.setDesiredOffsets(tokens, tokens[0], blockIndentLevel);
offsets.matchIndentOf(tokenToMatchAgainst, tokens[tokens.length - 1]);
}

/**
* Check indentation for lists of elements (arrays, objects, function params)
* @param {Token[]} tokens list of tokens
Expand Down Expand Up @@ -652,6 +609,37 @@ module.exports = {
});
}

/**
* Check indentation for blocks
* @param {ASTNode} node node to check
* @returns {void}
*/
function addBlockIndent(node) {

let blockIndentLevel;

if (node.parent && isOuterIIFE(node.parent)) {
blockIndentLevel = options.outerIIFEBody;
} else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) {
blockIndentLevel = options.FunctionExpression.body;
} else if (node.parent && node.parent.type === "FunctionDeclaration") {
blockIndentLevel = options.FunctionDeclaration.body;
} else {
blockIndentLevel = 1;
}

const tokens = getTokensAndComments(node);

/*
* For blocks that aren't lone statements, ensure that the opening curly brace
* is aligned with the parent.
*/
if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) {
offsets.matchIndentOf(sourceCode.getFirstToken(node.parent), tokens[0]);
}
addElementListIndent(tokens, node.body, blockIndentLevel);
}

/**
* Check indent for array block content or object block content
* @param {ASTNode} node node to examine
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -3111,6 +3111,22 @@ ruleTester.run("indent", rule, {
);
`
},
{
code: unIndent`
if (foo)
{
bar();
}
`
},
{
code: unIndent`
function foo(bar)
{
baz();
}
`
},
{
code: unIndent`
() =>
Expand All @@ -3133,6 +3149,23 @@ ruleTester.run("indent", rule, {
)
`,
parserOptions: { ecmaVersion: 6 }
},
{
code: unIndent`
var x = function foop(bar)
{
baz();
}
`
},
{
code: unIndent`
var x = (bar) =>
{
baz();
}
`,
parserOptions: { ecmaVersion: 6 }
}
],

Expand Down

0 comments on commit 74ab344

Please sign in to comment.