import { parse } from "@babel/parser"; import { traverseFast } from "@babel/types"; const literalValue = (node) => { if (node?.type === "StringLiteral") return node.value; if (node?.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis[0]?.value.cooked ?? undefined; return undefined; }; /** Static string specifier on one import or re-export node, when it has one. */ export const importSpecifier = (node) => { switch (node.type) { case "ImportDeclaration": case "ExportNamedDeclaration": case "ExportAllDeclaration": case "ImportExpression": case "TSImportType": return literalValue(node.source); case "TSImportEqualsDeclaration": return node.moduleReference.type === "TSExternalModuleReference" ? literalValue(node.moduleReference.expression) : undefined; default: return undefined; } }; /** Whether TypeScript erases this module edge under verbatimModuleSyntax. */ const isTypeOnly = (node) => { switch (node.type) { case "TSImportType": return true; case "ImportDeclaration": case "TSImportEqualsDeclaration": return node.importKind === "type"; case "ExportNamedDeclaration": case "ExportAllDeclaration": return node.exportKind === "type"; default: return false; } }; const isImportMetaUrl = (node) => node?.type === "MemberExpression" && node.object.type === "MetaProperty" && node.property.type === "Identifier" && node.property.name === "url"; /** `""` or `new URL("", import.meta.url).href`. */ const moduleUrlLiteral = (node) => { const literal = literalValue(node); if (literal !== undefined) return literal; if ( node?.type !== "MemberExpression" || node.property.type !== "Identifier" || node.property.name !== "href" || node.object.type !== "NewExpression" || node.object.callee.type !== "Identifier" || node.object.callee.name !== "URL" || !isImportMetaUrl(node.object.arguments[1]) ) return undefined; return literalValue(node.object.arguments[0]); }; const readModuleSpecifiers = (source, includeVerifierSources) => { const imports = []; const verifierSources = []; const tree = parse(source, { sourceType: "module", plugins: ["typescript"] }); traverseFast(tree, (node) => { const specifier = importSpecifier(node); if (specifier !== undefined) { imports.push({ specifier, line: node.loc?.start.line ?? 1, typeOnly: isTypeOnly(node), }); return; } if ( !includeVerifierSources || node.type !== "CallExpression" || node.callee.type !== "Identifier" || node.callee.name !== "importVerifierSource" || !isImportMetaUrl(node.arguments[1]) ) return; const sourceSpecifier = moduleUrlLiteral(node.arguments[0]); if (sourceSpecifier !== undefined) verifierSources.push({ specifier: sourceSpecifier, line: node.loc?.start.line ?? 1, }); }); return { imports, verifierSources }; }; /** Static import and re-export specifiers in source order. */ export const readImportSpecifiers = (source) => readModuleSpecifiers(source, false).imports; /** Static imports and literal verifier source loads in separate source-ordered lists. */ export const readImportClosureSpecifiers = (source) => readModuleSpecifiers(source, true);