Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: fix wrong indentation about catch,finally #7371

Merged
merged 3 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,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 @@ -717,11 +735,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 @@ -2971,6 +2971,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"])
},
]
});