diff --git a/.eslintrc.js b/.eslintrc.js index 73aea2d35..4e8403735 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -137,6 +137,7 @@ module.exports = { "unicorn/prefer-spread": "error", "unicorn/prefer-string-slice": "error", "unicorn/prefer-string-starts-ends-with": "error", + "unicorn/prefer-switch": "error", "unicorn/prefer-type-error": "error", }, overrides: [ diff --git a/src/common/end-of-line.js b/src/common/end-of-line.js index 0f4142576..994443c59 100644 --- a/src/common/end-of-line.js +++ b/src/common/end-of-line.js @@ -22,15 +22,19 @@ function convertEndOfLineToChars(value) { function countEndOfLineChars(text, eol) { let regex; - /* istanbul ignore else */ - if (eol === "\n") { - regex = /\n/g; - } else if (eol === "\r") { - regex = /\r/g; - } else if (eol === "\r\n") { - regex = /\r\n/g; - } else { - throw new Error(`Unexpected "eol" ${JSON.stringify(eol)}.`); + switch (eol) { + case "\n": + regex = /\n/g; + break; + case "\r": + regex = /\r/g; + break; + case "\r\n": + regex = /\r\n/g; + break; + default: + /* istanbul ignore next */ + throw new Error(`Unexpected "eol" ${JSON.stringify(eol)}.`); } const endOfLines = text.match(regex); diff --git a/src/language-html/parser-html.js b/src/language-html/parser-html.js index a7494dc13..54bf9c2e3 100644 --- a/src/language-html/parser-html.js +++ b/src/language-html/parser-html.js @@ -187,25 +187,30 @@ function ngHtmlParser( * @param {AstNode} node */ const restoreNameAndValue = (node) => { - if (node.type === "element") { - restoreName(node); - for (const attr of node.attrs) { - restoreName(attr); - if (!attr.valueSpan) { - attr.value = null; - } else { - attr.value = attr.valueSpan.toString(); - if (/["']/.test(attr.value[0])) { - attr.value = attr.value.slice(1, -1); + switch (node.type) { + case "element": + restoreName(node); + for (const attr of node.attrs) { + restoreName(attr); + if (!attr.valueSpan) { + attr.value = null; + } else { + attr.value = attr.valueSpan.toString(); + if (/["']/.test(attr.value[0])) { + attr.value = attr.value.slice(1, -1); + } } } - } - } else if (node.type === "comment") { - node.value = node.sourceSpan - .toString() - .slice("".length); - } else if (node.type === "text") { - node.value = node.sourceSpan.toString(); + break; + case "comment": + node.value = node.sourceSpan + .toString() + .slice("".length); + break; + case "text": + node.value = node.sourceSpan.toString(); + break; + // No default } }; diff --git a/src/language-js/parse/postprocess/index.js b/src/language-js/parse/postprocess/index.js index cdf537a9d..bf01da5eb 100644 --- a/src/language-js/parse/postprocess/index.js +++ b/src/language-js/parse/postprocess/index.js @@ -131,16 +131,20 @@ function postprocess(ast, options) { // since `ChainExpression` is the standard `estree` AST for `optional chaining` // https://github.com/estree/estree/blob/master/es2020.md function transformChainExpression(node) { - if (node.type === "CallExpression") { - node.type = "OptionalCallExpression"; - node.callee = transformChainExpression(node.callee); - } else if (node.type === "MemberExpression") { - node.type = "OptionalMemberExpression"; - node.object = transformChainExpression(node.object); - } - // typescript - else if (node.type === "TSNonNullExpression") { - node.expression = transformChainExpression(node.expression); + switch (node.type) { + case "CallExpression": + node.type = "OptionalCallExpression"; + node.callee = transformChainExpression(node.callee); + break; + case "MemberExpression": + node.type = "OptionalMemberExpression"; + node.object = transformChainExpression(node.object); + break; + // typescript + case "TSNonNullExpression": + node.expression = transformChainExpression(node.expression); + break; + // No default } return node; }