DRY up the code

This commit is contained in:
Michael Shilman 2022-04-26 00:02:33 +08:00
parent 4601876295
commit 28d18329bc

View File

@ -211,22 +211,20 @@ export class CsfFile {
ExportDefaultDeclaration: {
enter({ node, parent }) {
let metaNode: t.ObjectExpression;
if (t.isObjectExpression(node.declaration)) {
const decl =
t.isIdentifier(node.declaration) && t.isProgram(parent)
? findVarInitialization(node.declaration.name, parent)
: node.declaration;
if (t.isObjectExpression(decl)) {
// export default { ... };
metaNode = node.declaration;
metaNode = decl;
} else if (
// export default { ... } as Meta<...>
t.isTSAsExpression(node.declaration) &&
t.isObjectExpression(node.declaration.expression)
t.isTSAsExpression(decl) &&
t.isObjectExpression(decl.expression)
) {
metaNode = node.declaration.expression;
} else if (t.isIdentifier(node.declaration) && t.isProgram(parent)) {
const init = findVarInitialization(node.declaration.name, parent);
if (t.isObjectExpression(init)) {
metaNode = init;
} else if (t.isTSAsExpression(init) && t.isObjectExpression(init.expression)) {
metaNode = init.expression;
}
metaNode = decl.expression;
}
if (!self._meta && metaNode && t.isProgram(parent)) {