JS: Simplify clean function (#10154)

This commit is contained in:
fisker Cheung authored and GitHub committed 2021-01-27 14:36:02 +02:00
1 parent 9786ce2fb6
commit bba719283d
1 file changed
+22 -45
+22 -45
View File
@@ -18,6 +18,12 @@ const ignoredProperties = new Set([
"tokens",
]);
const removeTemplateElementsValue = (node) => {
for (const templateElement of node.quasis) {
delete templateElement.value;
}
};
function clean(ast, newObj, parent) {
if (ast.type === "Program") {
delete newObj.sourceType;
@@ -94,21 +100,13 @@ function clean(ast, newObj, parent) {
ast.openingElement.name.name === "style" &&
ast.openingElement.attributes.some((attr) => attr.name.name === "jsx")
) {
const templateLiterals = newObj.children
.filter(
(child) =>
child.type === "JSXExpressionContainer" &&
child.expression.type === "TemplateLiteral"
)
.map((container) => container.expression);
const quasis = templateLiterals.reduce(
(quasis, templateLiteral) => [...quasis, ...templateLiteral.quasis],
[]
);
for (const q of quasis) {
delete q.value;
for (const { type, expression } of newObj.children) {
if (
type === "JSXExpressionContainer" &&
expression.type === "TemplateLiteral"
) {
removeTemplateElementsValue(expression);
}
}
}
@@ -119,9 +117,7 @@ function clean(ast, newObj, parent) {
ast.value.type === "JSXExpressionContainer" &&
ast.value.expression.type === "TemplateLiteral"
) {
for (const q of newObj.value.expression.quasis) {
delete q.value;
}
removeTemplateElementsValue(newObj.value.expression);
}
// We change quotes
@@ -147,26 +143,18 @@ function clean(ast, newObj, parent) {
index,
prop,
] of newObj.expression.arguments[0].properties.entries()) {
let templateLiteral = null;
switch (astProps[index].key.name) {
case "styles":
if (prop.value.type === "ArrayExpression") {
templateLiteral = prop.value.elements[0];
removeTemplateElementsValue(prop.value.elements[0]);
}
break;
case "template":
if (prop.value.type === "TemplateLiteral") {
templateLiteral = prop.value;
removeTemplateElementsValue(prop.value);
}
break;
}
if (templateLiteral) {
for (const q of templateLiteral.quasis) {
delete q.value;
}
}
}
}
@@ -183,9 +171,7 @@ function clean(ast, newObj, parent) {
ast.tag.name === "html")) ||
ast.tag.type === "CallExpression")
) {
for (const quasi of newObj.quasi.quasis) {
delete quasi.value;
}
removeTemplateElementsValue(newObj.quasi);
}
if (ast.type === "TemplateLiteral") {
// This checks for a leading comment that is exactly `/* GraphQL */`
@@ -204,21 +190,12 @@ function clean(ast, newObj, parent) {
);
if (
hasLanguageComment ||
(parent.type === "CallExpression" && parent.callee.name === "graphql")
(parent.type === "CallExpression" && parent.callee.name === "graphql") ||
// TODO: check parser
// `flow` and `typescript` don't have `leadingComments`
!ast.leadingComments
) {
for (const quasi of newObj.quasis) {
delete quasi.value;
}
}
// TODO: check parser
// `flow` and `typescript` don't have `leadingComments`
if (!ast.leadingComments) {
for (const quasi of newObj.quasis) {
if (quasi.value) {
delete quasi.value.cooked;
}
}
removeTemplateElementsValue(newObj);
}
}