mirror of
https://github.com/zoriya/v10.git
synced 2026-08-07 22:56:34 +00:00
feat(site): extract api reference from components (#464)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import * as ts from 'typescript';
|
||||
import type { HtmlExtraction } from './types.js';
|
||||
|
||||
/** Extract tagName from a Lit element file. */
|
||||
export function extractHtml(filePath: string, program: ts.Program, componentName: string): HtmlExtraction | null {
|
||||
const sourceFile = program.getSourceFile(filePath);
|
||||
if (!sourceFile) return null;
|
||||
|
||||
let tagName = '';
|
||||
|
||||
function visit(node: ts.Node) {
|
||||
if (ts.isClassDeclaration(node) && node.name?.text === `${componentName}Element`) {
|
||||
for (const member of node.members) {
|
||||
if (
|
||||
ts.isPropertyDeclaration(member) &&
|
||||
member.name &&
|
||||
ts.isIdentifier(member.name) &&
|
||||
member.name.text === 'tagName' &&
|
||||
member.modifiers?.some((m) => m.kind === ts.SyntaxKind.StaticKeyword) &&
|
||||
member.initializer &&
|
||||
ts.isStringLiteral(member.initializer)
|
||||
) {
|
||||
tagName = member.initializer.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
|
||||
visit(sourceFile);
|
||||
|
||||
return tagName ? { tagName } : null;
|
||||
}
|
||||
Reference in New Issue
Block a user