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: change reporting location for curly rule (refs #12334) #14766

Merged
merged 6 commits into from Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
88 changes: 77 additions & 11 deletions lib/rules/curly.js
Expand Up @@ -131,15 +131,6 @@ module.exports = {
return token.value === "else" && token.type === "Keyword";
}

/**
* Gets the `else` keyword token of a given `IfStatement` node.
* @param {ASTNode} node A `IfStatement` node to get.
* @returns {Token} The `else` keyword token.
*/
function getElseKeyword(node) {
return node.alternate && sourceCode.getFirstTokenBetween(node.consequent, node.alternate, isElseKeywordToken);
}

/**
* Determines whether the given node has an `else` keyword token as the first token after.
* @param {ASTNode} node The node to check.
Expand Down Expand Up @@ -309,6 +300,77 @@ module.exports = {
hasUnsafeIf(statement) && isFollowedByElseKeyword(node);
}

/**
* Missing curly brace reporting location examples:
*
* The following location should be reported
*
* if (foo) bar()
* ^^^^^
*
* while(foo) bar();
* ^^^^^^
*
* do foo() while(bar)
* ^^^^^
*
* for(;;) bar();
* ^^^^^^
*
* for (var foo of bar) console.log(foo)
* ^^^^^^^^^^^^^^^^
*
* for (var foo in bar) console.log(foo)
* ^^^^^^^^^^^^^^^
*
* if (foo) {bar()} else bar(1);
* ^^^^^^^
*
* Unexpected curly brace reporting location examples:
*
* The following location should be reported
*
* if (true) foo(); else { baz(); }
* ^^^^^^^^^^
*
* for (;;) { foo(); }
* ^^^^^^^^^^
*
* while (bar) { foo(); }
* ^^^^^^^^^^
*
* do {foo();} while(bar);
* ^^^^^^^^
*
* for (var foo of bar) {console.log(foo)}
* ^^^^^^^^^^^^^^^^^^
*
* if (foo) bar() else { bar(1) }
* ^^^^^^^^^^
*
* Get the location of missing or non missing curly brace.
* @param {ASTNode} node the node to check.
* @param {string} name name to check.
* @returns {Object} Position object.
*/
function getReportingLoc(node, name) {
const blockStartLoc = new Set(["do", "for", "for-of", "for-in", "while"]);

if (name === "else") {
return node.alternate.loc;
}

if (blockStartLoc.has(name)) {
return node.body.loc;
}

if (name === "if") {
return node.consequent.loc;
}

return node.loc;
}

/**
* Prepares to check the body of a node to see if it's a block statement.
* @param {ASTNode} node The node to report if there's a problem.
Expand Down Expand Up @@ -359,19 +421,23 @@ module.exports = {
check() {
if (this.expected !== null && this.expected !== this.actual) {
if (this.expected) {
const loc = getReportingLoc(node, name);

context.report({
node,
loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
loc,
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter",
data: {
name
},
fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`)
});
} else {
const loc = getReportingLoc(node, name);

context.report({
node,
loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
loc,
messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter",
data: {
name
Expand Down