Skip to content

Commit

Permalink
Update: fix wrong indentation about catch,finally (#7371)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Oct 28, 2016
1 parent 77e3a34 commit 1d5146f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ module.exports = {
checkNodeIndent(node.alternate, neededIndent);
}
}

if (node.type === "TryStatement" && node.handler) {
const catchToken = sourceCode.getFirstToken(node.handler);

checkNodeIndent(catchToken, neededIndent);
}

if (node.type === "TryStatement" && node.finalizer) {
const finallyToken = sourceCode.getTokenBefore(node.finalizer);

checkNodeIndent(finallyToken, neededIndent);
}

if (node.type === "DoWhileStatement") {
const whileToken = sourceCode.getTokenAfter(node.body);

checkNodeIndent(whileToken, neededIndent);
}
}

/**
Expand Down Expand Up @@ -760,11 +778,13 @@ module.exports = {
* not from the beginning of the block.
*/
const statementsWithProperties = [
"IfStatement", "WhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "DoWhileStatement", "ClassDeclaration"
"IfStatement", "WhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "DoWhileStatement", "ClassDeclaration", "TryStatement"
];

if (node.parent && statementsWithProperties.indexOf(node.parent.type) !== -1 && isNodeBodyBlock(node)) {
indent = getNodeIndent(node.parent).goodChar;
} else if (node.parent && node.parent.type === "CatchClause") {
indent = getNodeIndent(node.parent.parent).goodChar;
} else {
indent = getNodeIndent(node).goodChar;
}
Expand Down
41 changes: 40 additions & 1 deletion tests/lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,45 @@ ruleTester.run("indent", rule, {
options: [2, {FunctionExpression: {parameters: 3}}],
errors: expectedErrors([3, 8, 10, "Identifier"])
},
{
code:
"{\n" +
" try {\n" +
" }\n" +
"catch (err) {\n" +
" }\n" +
"finally {\n" +
" }\n" +
"}",
output:
"{\n" +
" try {\n" +
" }\n" +
" catch (err) {\n" +
" }\n" +
" finally {\n" +
" }\n" +
"}",
errors: expectedErrors([
[4, 4, 0, "Keyword"],
[6, 4, 0, "Keyword"]
])
},
{
code:
"{\n" +
" do {\n" +
" }\n" +
"while (true)\n" +
"}",
output:
"{\n" +
" do {\n" +
" }\n" +
" while (true)\n" +
"}",
errors: expectedErrors([4, 4, 0, "Keyword"])
},
{
code:
"function foo() {\n" +
Expand Down Expand Up @@ -3295,6 +3334,6 @@ ruleTester.run("indent", rule, {
"}",
options: [2],
errors: expectedErrors([[2, "2 spaces", "3", "ReturnStatement"]])
}
},
]
});

0 comments on commit 1d5146f

Please sign in to comment.