|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
const astUtils = require("./utils/ast-utils"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
meta: { |
|
type: "suggestion", |
|
|
|
docs: { |
|
description: "Enforce consistent brace style for all control statements", |
|
recommended: false, |
|
url: "https://eslint.org/docs/latest/rules/curly" |
|
}, |
|
|
|
schema: { |
|
anyOf: [ |
|
{ |
|
type: "array", |
|
items: [ |
|
{ |
|
enum: ["all"] |
|
} |
|
], |
|
minItems: 0, |
|
maxItems: 1 |
|
}, |
|
{ |
|
type: "array", |
|
items: [ |
|
{ |
|
enum: ["multi", "multi-line", "multi-or-nest"] |
|
}, |
|
{ |
|
enum: ["consistent"] |
|
} |
|
], |
|
minItems: 0, |
|
maxItems: 2 |
|
} |
|
] |
|
}, |
|
|
|
fixable: "code", |
|
|
|
messages: { |
|
missingCurlyAfter: "Expected { after '{{name}}'.", |
|
missingCurlyAfterCondition: "Expected { after '{{name}}' condition.", |
|
unexpectedCurlyAfter: "Unnecessary { after '{{name}}'.", |
|
unexpectedCurlyAfterCondition: "Unnecessary { after '{{name}}' condition." |
|
} |
|
}, |
|
|
|
create(context) { |
|
|
|
const multiOnly = (context.options[0] === "multi"); |
|
const multiLine = (context.options[0] === "multi-line"); |
|
const multiOrNest = (context.options[0] === "multi-or-nest"); |
|
const consistent = (context.options[1] === "consistent"); |
|
|
|
const sourceCode = context.sourceCode; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isCollapsedOneLiner(node) { |
|
const before = sourceCode.getTokenBefore(node); |
|
const last = sourceCode.getLastToken(node); |
|
const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last; |
|
|
|
return before.loc.start.line === lastExcludingSemicolon.loc.end.line; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isOneLiner(node) { |
|
if (node.type === "EmptyStatement") { |
|
return true; |
|
} |
|
|
|
const first = sourceCode.getFirstToken(node); |
|
const last = sourceCode.getLastToken(node); |
|
const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last; |
|
|
|
return first.loc.start.line === lastExcludingSemicolon.loc.end.line; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isLexicalDeclaration(node) { |
|
if (node.type === "VariableDeclaration") { |
|
return node.kind === "const" || node.kind === "let"; |
|
} |
|
|
|
return node.type === "FunctionDeclaration" || node.type === "ClassDeclaration"; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function isElseKeywordToken(token) { |
|
return token.value === "else" && token.type === "Keyword"; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function isFollowedByElseKeyword(node) { |
|
const nextToken = sourceCode.getTokenAfter(node); |
|
|
|
return Boolean(nextToken) && isElseKeywordToken(nextToken); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function needsSemicolon(closingBracket) { |
|
const tokenBefore = sourceCode.getTokenBefore(closingBracket); |
|
const tokenAfter = sourceCode.getTokenAfter(closingBracket); |
|
const lastBlockNode = sourceCode.getNodeByRangeIndex(tokenBefore.range[0]); |
|
|
|
if (astUtils.isSemicolonToken(tokenBefore)) { |
|
|
|
|
|
return false; |
|
} |
|
|
|
if (!tokenAfter) { |
|
|
|
|
|
return false; |
|
} |
|
|
|
if (lastBlockNode.type === "BlockStatement" && lastBlockNode.parent.type !== "FunctionExpression" && lastBlockNode.parent.type !== "ArrowFunctionExpression") { |
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
} |
|
|
|
if (tokenBefore.loc.end.line === tokenAfter.loc.start.line) { |
|
|
|
|
|
return true; |
|
} |
|
|
|
if (/^[([/`+-]/u.test(tokenAfter.value)) { |
|
|
|
|
|
return true; |
|
} |
|
|
|
if (tokenBefore.type === "Punctuator" && (tokenBefore.value === "++" || tokenBefore.value === "--")) { |
|
|
|
|
|
return true; |
|
} |
|
|
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function hasUnsafeIf(node) { |
|
switch (node.type) { |
|
case "IfStatement": |
|
if (!node.alternate) { |
|
return true; |
|
} |
|
return hasUnsafeIf(node.alternate); |
|
case "ForStatement": |
|
case "ForInStatement": |
|
case "ForOfStatement": |
|
case "LabeledStatement": |
|
case "WithStatement": |
|
case "WhileStatement": |
|
return hasUnsafeIf(node.body); |
|
default: |
|
return false; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function areBracesNecessary(node) { |
|
const statement = node.body[0]; |
|
|
|
return isLexicalDeclaration(statement) || |
|
hasUnsafeIf(statement) && isFollowedByElseKeyword(node); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function prepareCheck(node, body, name, opts) { |
|
const hasBlock = (body.type === "BlockStatement"); |
|
let expected = null; |
|
|
|
if (hasBlock && (body.body.length !== 1 || areBracesNecessary(body))) { |
|
expected = true; |
|
} else if (multiOnly) { |
|
expected = false; |
|
} else if (multiLine) { |
|
if (!isCollapsedOneLiner(body)) { |
|
expected = true; |
|
} |
|
|
|
|
|
|
|
} else if (multiOrNest) { |
|
if (hasBlock) { |
|
const statement = body.body[0]; |
|
const leadingCommentsInBlock = sourceCode.getCommentsBefore(statement); |
|
|
|
expected = !isOneLiner(statement) || leadingCommentsInBlock.length > 0; |
|
} else { |
|
expected = !isOneLiner(body); |
|
} |
|
} else { |
|
|
|
|
|
expected = true; |
|
} |
|
|
|
return { |
|
actual: hasBlock, |
|
expected, |
|
check() { |
|
if (this.expected !== null && this.expected !== this.actual) { |
|
if (this.expected) { |
|
context.report({ |
|
node, |
|
loc: body.loc, |
|
messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter", |
|
data: { |
|
name |
|
}, |
|
fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`) |
|
}); |
|
} else { |
|
context.report({ |
|
node, |
|
loc: body.loc, |
|
messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter", |
|
data: { |
|
name |
|
}, |
|
fix(fixer) { |
|
|
|
|
|
|
|
|
|
|
|
const needsPrecedingSpace = node.type === "DoWhileStatement" && |
|
sourceCode.getTokenBefore(body).range[1] === body.range[0] && |
|
!astUtils.canTokensBeAdjacent("do", sourceCode.getFirstToken(body, { skip: 1 })); |
|
|
|
const openingBracket = sourceCode.getFirstToken(body); |
|
const closingBracket = sourceCode.getLastToken(body); |
|
const lastTokenInBlock = sourceCode.getTokenBefore(closingBracket); |
|
|
|
if (needsSemicolon(closingBracket)) { |
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
} |
|
|
|
const resultingBodyText = sourceCode.getText().slice(openingBracket.range[1], lastTokenInBlock.range[0]) + |
|
sourceCode.getText(lastTokenInBlock) + |
|
sourceCode.getText().slice(lastTokenInBlock.range[1], closingBracket.range[0]); |
|
|
|
return fixer.replaceText(body, (needsPrecedingSpace ? " " : "") + resultingBodyText); |
|
} |
|
}); |
|
} |
|
} |
|
} |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function prepareIfChecks(node) { |
|
const preparedChecks = []; |
|
|
|
for (let currentNode = node; currentNode; currentNode = currentNode.alternate) { |
|
preparedChecks.push(prepareCheck(currentNode, currentNode.consequent, "if", { condition: true })); |
|
if (currentNode.alternate && currentNode.alternate.type !== "IfStatement") { |
|
preparedChecks.push(prepareCheck(currentNode, currentNode.alternate, "else")); |
|
break; |
|
} |
|
} |
|
|
|
if (consistent) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
const expected = preparedChecks.some(preparedCheck => { |
|
if (preparedCheck.expected !== null) { |
|
return preparedCheck.expected; |
|
} |
|
return preparedCheck.actual; |
|
}); |
|
|
|
preparedChecks.forEach(preparedCheck => { |
|
preparedCheck.expected = expected; |
|
}); |
|
} |
|
|
|
return preparedChecks; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
IfStatement(node) { |
|
const parent = node.parent; |
|
const isElseIf = parent.type === "IfStatement" && parent.alternate === node; |
|
|
|
if (!isElseIf) { |
|
|
|
|
|
prepareIfChecks(node).forEach(preparedCheck => { |
|
preparedCheck.check(); |
|
}); |
|
} |
|
|
|
|
|
}, |
|
|
|
WhileStatement(node) { |
|
prepareCheck(node, node.body, "while", { condition: true }).check(); |
|
}, |
|
|
|
DoWhileStatement(node) { |
|
prepareCheck(node, node.body, "do").check(); |
|
}, |
|
|
|
ForStatement(node) { |
|
prepareCheck(node, node.body, "for", { condition: true }).check(); |
|
}, |
|
|
|
ForInStatement(node) { |
|
prepareCheck(node, node.body, "for-in").check(); |
|
}, |
|
|
|
ForOfStatement(node) { |
|
prepareCheck(node, node.body, "for-of").check(); |
|
} |
|
}; |
|
} |
|
}; |
|
|