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: changed curly reporting location (refs #12334) #13282

Closed
wants to merge 8 commits into from
100 changes: 98 additions & 2 deletions lib/rules/curly.js
Expand Up @@ -309,6 +309,98 @@ module.exports = {
hasUnsafeIf(statement) && isFollowedByElseKeyword(node);
}

/**
* Examples:
*
* The following location should be reported
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
*
* if (foo) bar()
* ^
*
* while(foo) bar();
* ^
*
* for(;;) bar();
* ^
*
* for (var foo of bar) console.log(foo)
* ^
*
* for (var foo in bar) console.log(foo)
* ^
*
* Get the location of missing parent location where it should have been placed.
* @param {ASTNode} node the node to check.
* @param {string} name name to check.
* @returns {Object} Position object.
*/
function getMissingLoc(node, name) {
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
switch (name) {
case "else":
return getElseKeyword(node).loc.start;
case "if":
return astUtils.getNextLocation(sourceCode, node.test.loc.end);
case "while":
return astUtils.getNextLocation(sourceCode, node.test.loc.end);
case "for":
return node.body.loc.start;
case "for-of":
return node.body.loc.start;
case "do":
return node.body.loc.start;
case "for-in":
return node.body.loc.start;
default:
return node.loc;
}
}

/**
* 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)}
* ^
*
* Get the location of unexpected parens. mostly the starting parens `{`
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
* @param {ASTNode} node the node to check.
* @param {string} name name to check.
* @returns {Object} Position object.
*/
function getUnexpectedParenLoc(node, name) {
switch (name) {
case "else":
return getElseKeyword(node).loc.start;
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
case "if":
return node.consequent.loc.start;
case "while":
return node.body.loc.start;
case "for":
return node.body.loc.start;
case "for-of":
return node.body.loc.start;
case "do":
return node.body.loc.start;
case "for-in":
return node.body.loc.start;
default:
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 +451,23 @@ module.exports = {
check() {
if (this.expected !== null && this.expected !== this.actual) {
if (this.expected) {
const loc = getMissingLoc(node, name);

context.report({
node,
loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
loc,
messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter",
data: {
name
},
fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`)
});
} else {
const loc = getUnexpectedParenLoc(node, name);

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