2 * @fileoverview Rule to flag statements without curly braces
3 * @author Nicholas C. Zakas
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils
= require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
22 description
: "enforce consistent brace style for all control statements",
23 category
: "Best Practices",
25 url
: "https://eslint.org/docs/rules/curly"
44 enum: ["multi", "multi-line", "multi-or-nest"]
59 missingCurlyAfter
: "Expected { after '{{name}}'.",
60 missingCurlyAfterCondition
: "Expected { after '{{name}}' condition.",
61 unexpectedCurlyAfter
: "Unnecessary { after '{{name}}'.",
62 unexpectedCurlyAfterCondition
: "Unnecessary { after '{{name}}' condition."
68 const multiOnly
= (context
.options
[0] === "multi");
69 const multiLine
= (context
.options
[0] === "multi-line");
70 const multiOrNest
= (context
.options
[0] === "multi-or-nest");
71 const consistent
= (context
.options
[1] === "consistent");
73 const sourceCode
= context
.getSourceCode();
75 //--------------------------------------------------------------------------
77 //--------------------------------------------------------------------------
80 * Determines if a given node is a one-liner that's on the same line as it's preceding code.
81 * @param {ASTNode} node The node to check.
82 * @returns {boolean} True if the node is a one-liner that's on the same line as it's preceding code.
85 function isCollapsedOneLiner(node
) {
86 const before
= sourceCode
.getTokenBefore(node
);
87 const last
= sourceCode
.getLastToken(node
);
88 const lastExcludingSemicolon
= astUtils
.isSemicolonToken(last
) ? sourceCode
.getTokenBefore(last
) : last
;
90 return before
.loc
.start
.line
=== lastExcludingSemicolon
.loc
.end
.line
;
94 * Determines if a given node is a one-liner.
95 * @param {ASTNode} node The node to check.
96 * @returns {boolean} True if the node is a one-liner.
99 function isOneLiner(node
) {
100 if (node
.type
=== "EmptyStatement") {
104 const first
= sourceCode
.getFirstToken(node
);
105 const last
= sourceCode
.getLastToken(node
);
106 const lastExcludingSemicolon
= astUtils
.isSemicolonToken(last
) ? sourceCode
.getTokenBefore(last
) : last
;
108 return first
.loc
.start
.line
=== lastExcludingSemicolon
.loc
.end
.line
;
112 * Determines if the given node is a lexical declaration (let, const, function, or class)
113 * @param {ASTNode} node The node to check
114 * @returns {boolean} True if the node is a lexical declaration
117 function isLexicalDeclaration(node
) {
118 if (node
.type
=== "VariableDeclaration") {
119 return node
.kind
=== "const" || node
.kind
=== "let";
122 return node
.type
=== "FunctionDeclaration" || node
.type
=== "ClassDeclaration";
126 * Checks if the given token is an `else` token or not.
127 * @param {Token} token The token to check.
128 * @returns {boolean} `true` if the token is an `else` token.
130 function isElseKeywordToken(token
) {
131 return token
.value
=== "else" && token
.type
=== "Keyword";
135 * Gets the `else` keyword token of a given `IfStatement` node.
136 * @param {ASTNode} node A `IfStatement` node to get.
137 * @returns {Token} The `else` keyword token.
139 function getElseKeyword(node
) {
140 return node
.alternate
&& sourceCode
.getFirstTokenBetween(node
.consequent
, node
.alternate
, isElseKeywordToken
);
144 * Determines whether the given node has an `else` keyword token as the first token after.
145 * @param {ASTNode} node The node to check.
146 * @returns {boolean} `true` if the node is followed by an `else` keyword token.
148 function isFollowedByElseKeyword(node
) {
149 const nextToken
= sourceCode
.getTokenAfter(node
);
151 return Boolean(nextToken
) && isElseKeywordToken(nextToken
);
155 * Determines if a semicolon needs to be inserted after removing a set of curly brackets, in order to avoid a SyntaxError.
156 * @param {Token} closingBracket The } token
157 * @returns {boolean} `true` if a semicolon needs to be inserted after the last statement in the block.
159 function needsSemicolon(closingBracket
) {
160 const tokenBefore
= sourceCode
.getTokenBefore(closingBracket
);
161 const tokenAfter
= sourceCode
.getTokenAfter(closingBracket
);
162 const lastBlockNode
= sourceCode
.getNodeByRangeIndex(tokenBefore
.range
[0]);
164 if (astUtils
.isSemicolonToken(tokenBefore
)) {
166 // If the last statement already has a semicolon, don't add another one.
172 // If there are no statements after this block, there is no need to add a semicolon.
176 if (lastBlockNode
.type
=== "BlockStatement" && lastBlockNode
.parent
.type
!== "FunctionExpression" && lastBlockNode
.parent
.type
!== "ArrowFunctionExpression") {
179 * If the last node surrounded by curly brackets is a BlockStatement (other than a FunctionExpression or an ArrowFunctionExpression),
180 * don't insert a semicolon. Otherwise, the semicolon would be parsed as a separate statement, which would cause
181 * a SyntaxError if it was followed by `else`.
186 if (tokenBefore
.loc
.end
.line
=== tokenAfter
.loc
.start
.line
) {
188 // If the next token is on the same line, insert a semicolon.
192 if (/^[([/`+-]/u.test(tokenAfter.value)) {
194 // If the next token starts with a character that would disrupt ASI, insert a semicolon.
198 if (tokenBefore.type === "Punctuator" && (tokenBefore.value === "++" || tokenBefore.value === "--")) {
200 // If the last token is ++ or --, insert a semicolon to avoid disrupting ASI.
204 // Otherwise, do not insert a semicolon.
209 * Determines whether the code represented by the given node contains an `if` statement
210 * that would become associated with an `else` keyword directly appended to that code.
212 * Examples where it returns `true`:
233 * Examples where it returns `false`:
255 * @param {ASTNode} node Node representing the code to check.
256 * @returns {boolean} `true` if an `if` statement within the code would become associated with an `else` appended to that code.
258 function hasUnsafeIf(node) {
261 if (!node.alternate) {
264 return hasUnsafeIf(node.alternate);
266 case "ForInStatement":
267 case "ForOfStatement":
268 case "LabeledStatement":
269 case "WithStatement":
270 case "WhileStatement":
271 return hasUnsafeIf(node.body);
278 * Determines whether the existing curly braces around the single statement are necessary to preserve the semantics of the code.
279 * The braces, which make the given block body, are necessary in either of the following situations:
281 * 1. The statement is a lexical declaration.
282 * 2. Without the braces, an `if` within the statement would become associated with an `else` after the closing brace:
301 * @param {ASTNode} node `BlockStatement
` body with exactly one statement directly inside. The statement can have its own nested statements.
302 * @returns {boolean} `true` if the braces are necessary - removing them (replacing the given `BlockStatement
` body with its single statement content)
303 * would change the semantics of the code or produce a syntax error.
305 function areBracesNecessary(node) {
306 const statement = node.body[0];
308 return isLexicalDeclaration(statement) ||
309 hasUnsafeIf(statement) && isFollowedByElseKeyword(node);
313 * Prepares to check the body of a node to see if it's a block statement.
314 * @param {ASTNode} node The node to report if there's a problem.
315 * @param {ASTNode} body The body node to check for blocks.
316 * @param {string} name The name to report if there's a problem.
317 * @param {{ condition: boolean }} opts Options to pass to the report functions
318 * @returns {Object} a prepared check object, with "actual", "expected", "check" properties.
319 * "actual" will be `true` or `false` whether the body is already a block statement.
320 * "expected" will be `true` or `false` if the body should be a block statement or not, or
321 * `null` if it doesn't matter, depending on the rule options. It can be modified to change
322 * the final behavior of "check".
323 * "check" will be a function reporting appropriate problems depending on the other
326 function prepareCheck(node, body, name, opts) {
327 const hasBlock = (body.type === "BlockStatement");
330 if (hasBlock && (body.body.length !== 1 || areBracesNecessary(body))) {
332 } else if (multiOnly) {
334 } else if (multiLine) {
335 if (!isCollapsedOneLiner(body)) {
339 // otherwise, the body is allowed to have braces or not to have braces
341 } else if (multiOrNest) {
343 const statement = body.body[0];
344 const leadingCommentsInBlock = sourceCode.getCommentsBefore(statement);
346 expected = !isOneLiner(statement) || leadingCommentsInBlock.length > 0;
348 expected = !isOneLiner(body);
360 if (this.expected !== null && this.expected !== this.actual) {
364 loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
365 messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter",
369 fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}
}`)
374 loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
375 messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter",
382 * `do while` expressions sometimes need a space to be inserted after `do`.
383 * e.g. `do{foo()} while (bar
)` should be corrected to `do foo() while (bar
)`
385 const needsPrecedingSpace = node.type === "DoWhileStatement" &&
386 sourceCode.getTokenBefore(body).range[1] === body.range[0] &&
387 !astUtils.canTokensBeAdjacent("do", sourceCode.getFirstToken(body, { skip: 1 }));
389 const openingBracket = sourceCode.getFirstToken(body);
390 const closingBracket = sourceCode.getLastToken(body);
391 const lastTokenInBlock = sourceCode.getTokenBefore(closingBracket);
393 if (needsSemicolon(closingBracket)) {
396 * If removing braces would cause a SyntaxError due to multiple statements on the same line (or
397 * change the semantics of the code due to ASI), don't perform a fix.
402 const resultingBodyText = sourceCode.getText().slice(openingBracket.range[1], lastTokenInBlock.range[0]) +
403 sourceCode.getText(lastTokenInBlock) +
404 sourceCode.getText().slice(lastTokenInBlock.range[1], closingBracket.range[0]);
406 return fixer.replaceText(body, (needsPrecedingSpace ? " " : "") + resultingBodyText);
416 * Prepares to check the bodies of a "if", "else if" and "else" chain.
417 * @param {ASTNode} node The first IfStatement node of the chain.
418 * @returns {Object[]} prepared checks for each body of the chain. See `prepareCheck
` for more
421 function prepareIfChecks(node) {
422 const preparedChecks = [];
424 for (let currentNode = node; currentNode; currentNode = currentNode.alternate) {
425 preparedChecks.push(prepareCheck(currentNode, currentNode.consequent, "if", { condition: true }));
426 if (currentNode.alternate && currentNode.alternate.type !== "IfStatement") {
427 preparedChecks.push(prepareCheck(currentNode, currentNode.alternate, "else"));
435 * If any node should have or already have braces, make sure they
437 * If all nodes shouldn't have braces, make sure they don't.
439 const expected = preparedChecks.some(preparedCheck => {
440 if (preparedCheck.expected !== null) {
441 return preparedCheck.expected;
443 return preparedCheck.actual;
446 preparedChecks.forEach(preparedCheck => {
447 preparedCheck.expected = expected;
451 return preparedChecks;
454 //--------------------------------------------------------------------------
456 //--------------------------------------------------------------------------
460 const parent = node.parent;
461 const isElseIf = parent.type === "IfStatement" && parent.alternate === node;
465 // This is a top `if`, check the whole `if-else-if` chain
466 prepareIfChecks(node).forEach(preparedCheck => {
467 preparedCheck.check();
471 // Skip `else if`, it's already checked (when the top `if` was visited)
474 WhileStatement(node) {
475 prepareCheck(node, node.body, "while", { condition: true }).check();
478 DoWhileStatement(node) {
479 prepareCheck(node, node.body, "do").check();
483 prepareCheck(node, node.body, "for", { condition: true }).check();
486 ForInStatement(node) {
487 prepareCheck(node, node.body, "for-in").check();
490 ForOfStatement(node) {
491 prepareCheck(node, node.body, "for-of").check();