@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
68 lines (66 loc) • 2.29 kB
JavaScript
import { parseAst } from './chunk-EMQ6Z6RZ.js';
import { __name } from './chunk-43IZMM3W.js';
import * as t from '@babel/types';
import { isString } from '@stryke/type-checks/is-string';
function findExport(ast, key) {
const type = key === "default" ? "ExportDefaultDeclaration" : "ExportNamedDeclaration";
for (const node of ast.program.body) {
if (node.type === type) {
if (key === "default") {
return node.declaration;
}
if (node.declaration && "declarations" in node.declaration) {
const declaration = node.declaration.declarations[0];
if (declaration && "name" in declaration.id && declaration.id.name === key) {
return declaration.init;
}
}
}
}
}
__name(findExport, "findExport");
function listExports(codeOrAst) {
const ast = isString(codeOrAst) ? parseAst(codeOrAst) : codeOrAst;
return ast.program.body.flatMap((i) => {
if (i.type === "ExportDefaultDeclaration") {
return [
"default"
];
}
if (i.type === "ExportNamedDeclaration" && i.declaration && "declarations" in i.declaration) {
return i.declaration.declarations.map((d) => "name" in d.id ? d.id.name : "");
}
return [];
}).filter(Boolean);
}
__name(listExports, "listExports");
function listImports(ast) {
return ast.program.body.flatMap((i) => {
if (i.type === "ImportDeclaration") {
return i.specifiers.map((s) => {
if (s.type === "ImportDefaultSpecifier") {
return "default";
}
if (s.type === "ImportSpecifier" && "imported" in s) {
return s.imported.type === "Identifier" ? s.imported.name : s.imported.value;
}
return "";
});
}
return [];
}).filter(Boolean);
}
__name(listImports, "listImports");
function isImportCall(calleePath) {
return t.isImport(calleePath.node.callee);
}
__name(isImportCall, "isImportCall");
function getImport(specifier, name, named) {
return t.importDeclaration([
t.importSpecifier(t.identifier(name), t.stringLiteral(named || name))
], t.stringLiteral(specifier));
}
__name(getImport, "getImport");
export { findExport, getImport, isImportCall, listExports, listImports };
//# sourceMappingURL=chunk-HQNYXPKX.js.map
//# sourceMappingURL=chunk-HQNYXPKX.js.map