Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/api-inherited-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@layerstack/docs': minor
---

feat: Component APIs list the props another component's Props contribute (ex. `Omit<Without<CircleProps, ...>>`), each with `inheritedFrom`, resolving imports and the `$lib` alias; `llms` API tables name them after a component's own
7 changes: 7 additions & 0 deletions packages/docs/src/lib/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface PropertyInfo {
default?: string;
tags?: Record<string, string>;
properties?: PropertyInfo[];
/**
* The component it's inherited from, for a prop another component's Props contribute (ex.
* `Circle` for the `z` that `Points` passes on to each circle)
*/
inheritedFrom?: string;
}

export interface ExtendedType {
Expand All @@ -20,6 +25,8 @@ export interface ExtendedType {
elementType?: string;
/** Whether this is a known library type that should be documented separately */
isLibraryType?: boolean;
/** Another component's Props, whose props are listed (with `inheritedFrom`) in `properties` */
inherited?: boolean;
}

export interface ComponentAPI {
Expand Down
20 changes: 17 additions & 3 deletions packages/docs/src/lib/llms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,31 @@ function escapeMarkdown(text: string): string {
export function generateApiTable(api: ComponentAPI): string {
if (!api.properties || api.properties.length === 0) return '';

const rows = api.properties.map((prop) => {
// Its own props in the table; those other components' Props contribute, named after it
const own = api.properties.filter((prop) => !prop.inheritedFrom);
const inherited = new Map<string, string[]>();
for (const prop of api.properties) {
if (!prop.inheritedFrom) continue;
inherited.set(prop.inheritedFrom, [...(inherited.get(prop.inheritedFrom) ?? []), prop.name]);
}
const inheritedLines = [...inherited].map(
([from, names]) => `Also takes \`${from}\`'s ${names.map((name) => `\`${name}\``).join(', ')}.`
);

const rows = own.map((prop) => {
const name = prop.required ? `**${prop.name}** (required)` : prop.name;
const type = `\`${escapeMarkdown(prop.type)}\``;
const defaultVal = prop.default ? `\`${escapeMarkdown(prop.default)}\`` : '-';
const description = prop.description ? escapeMarkdown(prop.description) : '-';
return `| ${name} | ${type} | ${defaultVal} | ${description} |`;
});

return `| Property | Type | Default | Description |
const table = rows.length
? `| Property | Type | Default | Description |
|----------|------|---------|-------------|
${rows.join('\n')}`;
${rows.join('\n')}`
: '';
return [table, ...inheritedLines].filter(Boolean).join('\n\n');
}

/** Resolve the raw source for an example, by component (optional) + name. */
Expand Down
97 changes: 94 additions & 3 deletions packages/docs/src/lib/node/component-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,60 @@ function extractPropertiesFromType(typeNode, checker, sourceFile) {
}
return properties;
}
/** Type utilities a component's Props may wrap another component's Props in */
const WRAPPERS = new Set(['Omit', 'Without', 'Pick', 'Partial', 'Required', 'Readonly']);
/**
* The component Props type an intersection member refers to, through any utility types wrapping
* it (ex. `Omit<Without<CircleProps, PointsPropsWithoutHTML>, 'ref'>` is `CircleProps`), or
* `undefined` when it isn't another component's Props
*/
function wrappedPropsName(typeNode) {
if (!ts.isTypeReferenceNode(typeNode)) return undefined;
const name = typeNode.typeName.getText();
if (WRAPPERS.has(name)) {
const [first] = typeNode.typeArguments ?? [];
return first ? wrappedPropsName(first) : undefined;
}
return /Props$/.test(name) ? name : undefined;
}
/** The type alias or interface a property is declared in (ex. `CirclePropsWithoutHTML`) */
function declaringTypeName(declaration) {
for (let node = declaration.parent; node; node = node.parent) {
if (ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node)) return node.name.text;
}
return undefined;
}
/**
* The properties another component's Props contribute through an intersection member (ex. the
* `Circle` props `Points` passes on to each circle), resolved by the type checker across files.
* Only the component's own props: not DOM/SVG attributes (from `svelte/elements` or the DOM
* typings), nor those `known` already lists. Each is marked with the component it's from.
*/
function extractInheritedProperties(typeNode, checker, known) {
const propsName = wrappedPropsName(typeNode);
if (!propsName) return [];
const from = propsName.replace(/Props$/, '');
const type = checker.getTypeFromTypeNode(typeNode);
const properties = [];
for (const symbol of checker.getPropertiesOfType(type)) {
const name = symbol.getName();
if (known.has(name)) continue;
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
if (!declaration || !ts.isPropertySignature(declaration)) continue;
const file = declaration.getSourceFile().fileName;
if (file.includes('/node_modules/') || /\/lib\.[\w.]*\.d\.ts$/.test(file)) continue;
// Its own props, not attributes a Props type took from `svelte/elements`
if (!/Props/.test(declaringTypeName(declaration) ?? '')) continue;
properties.push({
name,
type: getTypeString(declaration.type, checker),
required: !declaration.questionToken,
...extractJsDoc(declaration),
inheritedFrom: from,
});
}
return properties;
}
/**
* Extract element type from generic types like SVGAttributes<SVGRectElement>
*/
Expand All @@ -190,8 +244,11 @@ function parseExtendedTypes(typeNode, sourceFile) {
}
for (const type of typeNode.types) {
const typeText = type.getText(sourceFile);
// Skip the base PropsWithoutHTML type (but not Without<> that contains PropsWithoutHTML)
if (!typeText.startsWith('Without<') && typeText.includes('PropsWithoutHTML')) {
// Skip the base PropsWithoutHTML type itself (not a wrapper naming it, ex. `Without<..., XPropsWithoutHTML>`)
if (
ts.isTypeReferenceNode(type) &&
type.typeName.getText(sourceFile).endsWith('PropsWithoutHTML')
) {
continue;
}
// Check if this is a Without<> wrapper (used to exclude props)
Expand All @@ -217,6 +274,17 @@ function parseExtendedTypes(typeNode, sourceFile) {
});
continue;
}
// Another component's Props, wrapped (ex. `Omit<Without<CircleProps, ...>, 'ref'>`)
const wrapped = wrappedPropsName(type);
if (wrapped && WRAPPERS.has(type.typeName.getText(sourceFile))) {
extendedTypes.push({
name: wrapped,
fullType: typeText,
isLibraryType: false,
inherited: true,
});
continue;
}
// Check if this is a reference to another type (like CommonEvents)
if (ts.isTypeReferenceNode(type)) {
const typeName = type.typeName.getText(sourceFile);
Expand Down Expand Up @@ -293,6 +361,15 @@ function resolveSharedReExports(filePath, moduleScript) {
}
return combined;
}
/** `$lib` mapped to the SvelteKit library a file is in (its nearest `src/lib`), if any */
function libPaths(filePath) {
for (let dir = path.dirname(filePath); dir !== path.dirname(dir); dir = path.dirname(dir)) {
if (path.basename(dir) === 'lib' && path.basename(path.dirname(dir)) === 'src') {
return { paths: { $lib: [dir], '$lib/*': [`${dir}/*`] } };
}
}
return {};
}
/**
* Extract component API from a Svelte file
*/
Expand Down Expand Up @@ -367,6 +444,13 @@ ${moduleScript}
const program = ts.createProgram([tempPath], {
target: ts.ScriptTarget.ES2020,
module: ts.ModuleKind.ESNext,
// Following its imports (ex. `../Circle/Circle.shared.svelte.js` to its `.ts`), so another
// component's Props it's composed of resolve
moduleResolution: ts.ModuleResolutionKind.Bundler,
noEmit: true,
skipLibCheck: true,
// SvelteKit's `$lib` alias (ex. `Without` from `$lib/utils/types.js`), else what uses it is `any`
...libPaths(filePath),
});
const sourceFile = program.getSourceFile(tempPath);
if (!sourceFile) {
Expand Down Expand Up @@ -397,9 +481,16 @@ ${moduleScript}
extendedTypes.push(extType);
}
}
// Props another component's Props contribute (ex. `Points`' `Circle` props)
if (ts.isIntersectionTypeNode(node.type)) {
for (const member of node.type.types) {
const known = new Set(properties.map((p) => p.name));
properties.push(...extractInheritedProperties(member, checker, known));
}
}
// Extract properties from non-library extended types (like CommonEvents)
for (const extType of extendedTypes) {
if (!extType.isLibraryType) {
if (!extType.isLibraryType && !extType.inherited) {
// Find this type definition and extract its properties
ts.forEachChild(sourceFile, (typeNode) => {
if (ts.isTypeAliasDeclaration(typeNode) && typeNode.name.text === extType.name) {
Expand Down
Loading