diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 5e7840478..faa52a962 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -35,6 +35,7 @@ module.exports = { ], "no-implicit-coercion": "error", "no-inner-declarations": "error", + "no-lonely-if": "error", "no-restricted-syntax": [ "error", // `!foo === bar` and `!foo !== bar` @@ -152,6 +153,7 @@ module.exports = { "unicorn/new-for-builtins": "error", "unicorn/no-array-for-each": "error", "unicorn/no-array-push-push": "error", + "unicorn/no-lonely-if": "error", "unicorn/no-new-array": "error", "unicorn/no-unreadable-iife": "error", "unicorn/no-useless-length-check": "error", diff --git a/src/cli/format.js b/src/cli/format.js index 6eb7728bf..caa43e8e9 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -90,11 +90,9 @@ async function listDifferent(context, input, options, filename) { "No parser and no file path given, couldn't infer a parser." ); } - if (!(await prettier.check(input, options))) { - if (!context.argv.write) { - context.logger.log(filename); - process.exitCode = 1; - } + if (!(await prettier.check(input, options)) && !context.argv.write) { + context.logger.log(filename); + process.exitCode = 1; } } catch (error) { context.logger.error(error.message); diff --git a/src/document/printer.js b/src/document/printer.js index 64773f299..d38ad960f 100644 --- a/src/document/printer.js +++ b/src/document/printer.js @@ -387,6 +387,7 @@ function printDocToString(doc, options) { // representation first to the most expanded. If a // group has these, we need to manually go through // these states and find the first one that fits. + // eslint-disable-next-line no-lonely-if if (doc.expandedStates) { const mostExpanded = doc.expandedStates.at(-1); diff --git a/src/language-css/clean.js b/src/language-css/clean.js index 71c52aaed..8ff868f11 100644 --- a/src/language-css/clean.js +++ b/src/language-css/clean.js @@ -115,13 +115,11 @@ function clean(ast, newObj, parent) { if (ast.type === "selector-attribute") { newObj.attribute = newObj.attribute.trim(); - if (newObj.namespace) { - if (typeof newObj.namespace === "string") { - newObj.namespace = newObj.namespace.trim(); + if (newObj.namespace && typeof newObj.namespace === "string") { + newObj.namespace = newObj.namespace.trim(); - if (newObj.namespace.length === 0) { - newObj.namespace = true; - } + if (newObj.namespace.length === 0) { + newObj.namespace = true; } } diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index 20cd3aa09..5baa659ba 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -160,13 +160,13 @@ function addTypePrefix(node, prefix, skipPrefix) { delete node.parent; for (const key in node) { addTypePrefix(node[key], prefix, skipPrefix); - if (key === "type" && typeof node[key] === "string") { - if ( - !node[key].startsWith(prefix) && - (!skipPrefix || !skipPrefix.test(node[key])) - ) { - node[key] = prefix + node[key]; - } + if ( + key === "type" && + typeof node[key] === "string" && + !node[key].startsWith(prefix) && + (!skipPrefix || !skipPrefix.test(node[key])) + ) { + node[key] = prefix + node[key]; } } } diff --git a/src/language-html/print/children.js b/src/language-html/print/children.js index 70238bdb6..6da03f347 100644 --- a/src/language-html/print/children.js +++ b/src/language-html/print/children.js @@ -170,16 +170,14 @@ function printChildren(path, options, print) { prevParts.push(hardline, hardline); } else if (prevBetweenLine === hardline) { prevParts.push(hardline); + } else if (isTextLikeNode(childNode.prev)) { + leadingParts.push(prevBetweenLine); } else { - if (isTextLikeNode(childNode.prev)) { - leadingParts.push(prevBetweenLine); - } else { - leadingParts.push( - ifBreak("", softline, { - groupId: groupIds[childIndex - 1], - }) - ); - } + leadingParts.push( + ifBreak("", softline, { + groupId: groupIds[childIndex - 1], + }) + ); } } diff --git a/src/language-js/clean.js b/src/language-js/clean.js index 10111d886..a4186cab6 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -28,17 +28,17 @@ function clean(ast, newObj, parent) { } if ( - ast.type === "BigIntLiteral" || - ast.type === "BigIntLiteralTypeAnnotation" + (ast.type === "BigIntLiteral" || + ast.type === "BigIntLiteralTypeAnnotation") && + newObj.value ) { - if (newObj.value) { - newObj.value = newObj.value.toLowerCase(); - } + newObj.value = newObj.value.toLowerCase(); } - if (ast.type === "BigIntLiteral" || ast.type === "Literal") { - if (newObj.bigint) { - newObj.bigint = newObj.bigint.toLowerCase(); - } + if ( + (ast.type === "BigIntLiteral" || ast.type === "Literal") && + newObj.bigint + ) { + newObj.bigint = newObj.bigint.toLowerCase(); } if (ast.type === "DecimalLiteral") { diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index 977918f87..f6b8bff5e 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -511,24 +511,23 @@ function isTestCall(node, parent) { if (isUnitTestSetUp(node)) { return isAngularTestWrapper(node.arguments[0]); } - } else if (node.arguments.length === 2 || node.arguments.length === 3) { - if ( - (node.arguments[0].type === "TemplateLiteral" || - isStringLiteral(node.arguments[0])) && - isTestCallCallee(node.callee) - ) { - // it("name", () => { ... }, 2500) - if (node.arguments[2] && !isNumericLiteral(node.arguments[2])) { - return false; - } - return ( - (node.arguments.length === 2 - ? isFunctionOrArrowExpression(node.arguments[1]) - : isFunctionOrArrowExpressionWithBody(node.arguments[1]) && - getFunctionParameters(node.arguments[1]).length <= 1) || - isAngularTestWrapper(node.arguments[1]) - ); + } else if ( + (node.arguments.length === 2 || node.arguments.length === 3) && + (node.arguments[0].type === "TemplateLiteral" || + isStringLiteral(node.arguments[0])) && + isTestCallCallee(node.callee) + ) { + // it("name", () => { ... }, 2500) + if (node.arguments[2] && !isNumericLiteral(node.arguments[2])) { + return false; } + return ( + (node.arguments.length === 2 + ? isFunctionOrArrowExpression(node.arguments[1]) + : isFunctionOrArrowExpressionWithBody(node.arguments[1]) && + getFunctionParameters(node.arguments[1]).length <= 1) || + isAngularTestWrapper(node.arguments[1]) + ); } return false; }