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 1 commit
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
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
45 changes: 44 additions & 1 deletion tests/lib/rules/indent.js
Expand Up @@ -2971,6 +2971,49 @@ ruleTester.run("indent", rule, {
"}",
options: [2, {FunctionExpression: {parameters: 3}}],
errors: expectedErrors([3, 8, 10, "Identifier"])
}
},
{
code: `
{
try {
}
catch (err) {
}
finally {
}
}
Copy link
Member

@gyandeeps gyandeeps Oct 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we should stick to string concat here because this makes it hard to read in this particular scenario. (or maybe its just me)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

`,
output: `
{
try {
}
catch (err) {
}
finally {
}
}
`,
errors: expectedErrors([
[5, 4, 0, "Keyword"],
[7, 4, 0, "Keyword"]
])
},
{
code: `
{
do {
}
while (true)
}
`,
output: `
{
do {
}
while (true)
}
`,
errors: expectedErrors([5, 4, 0, "Keyword"])
},
]
});