From c3d08c943e08ae791cd702bcf8fcd95a20fed38b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 15 Feb 2021 11:29:58 +0800 Subject: [PATCH] Style: Disallow else if in `no-else-return` (#10310) --- .eslintrc.yml | 4 +++- src/document/doc-debug.js | 17 ++++++++++++---- src/document/doc-utils.js | 17 ++++++++++++---- src/language-js/comments.js | 5 ++++- src/language-js/needs-parens.js | 33 +++++++++++++++++++++++-------- src/language-js/utils.js | 5 ++++- src/language-yaml/printer-yaml.js | 4 +++- 7 files changed, 65 insertions(+), 20 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index d9496df3d..2426b5c5c 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -26,7 +26,9 @@ rules: - error - devDependencies: ["tests*/**", "scripts/**"] import/order: error - no-else-return: error + no-else-return: + - error + - allowElseIf: false no-implicit-coercion: error no-inner-declarations: error no-unneeded-ternary: error diff --git a/src/document/doc-debug.js b/src/document/doc-debug.js index 2499d323f..418857463 100644 --- a/src/document/doc-debug.js +++ b/src/document/doc-debug.js @@ -17,7 +17,9 @@ function flattenDoc(doc) { } return { type: "concat", parts: res }; - } else if (doc.type === "if-break") { + } + + if (doc.type === "if-break") { return { ...doc, breakContents: @@ -25,7 +27,9 @@ function flattenDoc(doc) { flatContents: doc.flatContents != null ? flattenDoc(doc.flatContents) : null, }; - } else if (doc.type === "group") { + } + + if (doc.type === "group") { return { ...doc, contents: flattenDoc(doc.contents), @@ -33,11 +37,16 @@ function flattenDoc(doc) { ? doc.expandedStates.map(flattenDoc) : doc.expandedStates, }; - } else if (doc.type === "fill") { + } + + if (doc.type === "fill") { return { type: "fill", parts: doc.parts.map(flattenDoc) }; - } else if (doc.contents) { + } + + if (doc.contents) { return { ...doc, contents: flattenDoc(doc.contents) }; } + return doc; } diff --git a/src/document/doc-utils.js b/src/document/doc-utils.js index 8bd722a8b..649a1cfd5 100644 --- a/src/document/doc-utils.js +++ b/src/document/doc-utils.js @@ -73,14 +73,20 @@ function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) { function mapDoc(doc, cb) { if (Array.isArray(doc)) { return cb(doc.map((part) => mapDoc(part, cb))); - } else if (doc.type === "concat" || doc.type === "fill") { + } + + if (doc.type === "concat" || doc.type === "fill") { const parts = doc.parts.map((part) => mapDoc(part, cb)); return cb({ ...doc, parts }); - } else if (doc.type === "if-break") { + } + + if (doc.type === "if-break") { const breakContents = doc.breakContents && mapDoc(doc.breakContents, cb); const flatContents = doc.flatContents && mapDoc(doc.flatContents, cb); return cb({ ...doc, breakContents, flatContents }); - } else if (doc.contents) { + } + + if (doc.contents) { const contents = mapDoc(doc.contents, cb); return cb({ ...doc, contents }); } @@ -172,9 +178,12 @@ function removeLinesFn(doc) { // of breaking existing assumptions otherwise. if (doc.type === "line" && !doc.hard) { return doc.soft ? "" : " "; - } else if (doc.type === "if-break") { + } + + if (doc.type === "if-break") { return doc.flatContents || ""; } + return doc; } diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 53b7111e3..944672998 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -710,7 +710,9 @@ function handleOnlyComments({ comment, enclosingNode, ast, isLastComment }) { addLeadingComment(ast, comment); } return true; - } else if ( + } + + if ( enclosingNode && enclosingNode.type === "Program" && enclosingNode.body.length === 0 && @@ -724,6 +726,7 @@ function handleOnlyComments({ comment, enclosingNode, ast, isLastComment }) { } return true; } + return false; } diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 0dbd01a74..290fb3658 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -494,32 +494,46 @@ function needsParens(path, options) { if (name === "body" && parent.type === "ArrowFunctionExpression") { return true; - } else if ( + } + + if ( name === "key" && (parent.type === "ClassProperty" || parent.type === "PropertyDefinition") && parent.computed ) { return false; - } else if ( + } + + if ( (name === "init" || name === "update") && parent.type === "ForStatement" ) { return false; - } else if (parent.type === "ExpressionStatement") { + } + + if (parent.type === "ExpressionStatement") { return node.left.type === "ObjectPattern"; - } else if (name === "key" && parent.type === "TSPropertySignature") { + } + + if (name === "key" && parent.type === "TSPropertySignature") { return false; - } else if (parent.type === "AssignmentExpression") { + } + + if (parent.type === "AssignmentExpression") { return false; - } else if ( + } + + if ( parent.type === "SequenceExpression" && grandParent && grandParent.type === "ForStatement" && (grandParent.init === parent || grandParent.update === parent) ) { return false; - } else if ( + } + + if ( name === "value" && parent.type === "Property" && grandParent && @@ -527,9 +541,12 @@ function needsParens(path, options) { grandParent.properties.includes(parent) ) { return false; - } else if (parent.type === "NGChainedExpression") { + } + + if (parent.type === "NGChainedExpression") { return false; } + return true; } case "ConditionalExpression": diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 1498a27a7..073b595ec 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -644,9 +644,12 @@ function isSimpleTemplateLiteral(node) { function getTypeScriptMappedTypeModifier(tokenNode, keyword) { if (tokenNode === "+") { return "+" + keyword; - } else if (tokenNode === "-") { + } + + if (tokenNode === "-") { return "-" + keyword; } + return keyword; } diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 3e2510d67..6b17477a5 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -285,7 +285,9 @@ function printNode(node, parentNode, path, options, print) { printFlowScalarContent(node.type, raw, options), originalQuote, ]; - } else if (raw.includes(doubleQuote)) { + } + + if (raw.includes(doubleQuote)) { return [ singleQuote, printFlowScalarContent(