Skip to content

Commit

Permalink
Update: changed reporting location (ref #12334)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed May 11, 2020
1 parent 3dd6741 commit cf31cdb
Show file tree
Hide file tree
Showing 2 changed files with 393 additions and 8 deletions.
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
*
* 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) {
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 `{`
* @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;
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

0 comments on commit cf31cdb

Please sign in to comment.