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
80 changes: 78 additions & 2 deletions lib/rules/curly.js
Expand Up @@ -10,6 +10,12 @@

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helper
//------------------------------------------------------------------------------

const blockStartLoc = new Set(["for", "for-of", "do", "for-in"]);

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -309,6 +315,72 @@ module.exports = {
hasUnsafeIf(statement) && isFollowedByElseKeyword(node);
}

/**
* Missing curly brace reporting location 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)
* ^
*
* 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)}
* ^
*
* Get the location of missing or non missing curly brace.
* @param {ASTNode} node the node to check.
* @param {string} name name to check.
* @param {boolean} isMissing is curly brace missing.
* @returns {Object} Position object.
*/
function getReportLoc(node, name, isMissing) {

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

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

if (name === "if") {
return isMissing ? astUtils.getNextLocation(sourceCode, node.test.loc.end) : node.consequent.loc.start;
}

if (name === "while") {
return isMissing ? astUtils.getNextLocation(sourceCode, node.test.loc.end) : node.body.loc.start;
}

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 +431,23 @@ module.exports = {
check() {
if (this.expected !== null && this.expected !== this.actual) {
if (this.expected) {
const loc = getReportLoc(node, name, true);

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 = getReportLoc(node, name, false);

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