Enable unicorn/prefer-switch (#11935)

This commit is contained in:
fisker Cheung authored and GitHub committed 2021-12-07 14:13:12 +08:00
1 parent 7bce1d02c7
commit e2d07b35ec
4 files changed
+50 -36

No files matched your search

+1
View File
@@ -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: [
+13 -9
View File
@@ -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);
+22 -17
View File
@@ -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, -"-->".length);
} else if (node.type === "text") {
node.value = node.sourceSpan.toString();
break;
case "comment":
node.value = node.sourceSpan
.toString()
.slice("<!--".length, -"-->".length);
break;
case "text":
node.value = node.sourceSpan.toString();
break;
// No default
}
};
+14 -10
View File
@@ -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;
}