Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): include jsdoc comments from classes #3799

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 27 additions & 7 deletions src/compiler/types/generate-component-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dashToPascalCase, sortBy } from '@utils';
import { dashToPascalCase, getTextDocs, sortBy } from '@utils';

import type * as d from '../../declarations';
import { generateEventTypes } from './generate-event-types';
Expand All @@ -21,6 +21,10 @@ export const generateComponentTypes = (
const tagNameAsPascal = dashToPascalCase(tagName);
const htmlElementName = `HTML${tagNameAsPascal}Element`;

const componentDocs = getTextDocs(cmp.docs);
const componentComment = indentDocComment(componentDocs, 8);
const jsxComment = indentDocComment(componentDocs, 4);

const propAttributes = generatePropTypes(cmp, typeImportData);
const methodAttributes = generateMethodTypes(cmp, typeImportData);
const eventAttributes = generateEventTypes(cmp, typeImportData, tagNameAsPascal);
Expand All @@ -33,21 +37,32 @@ export const generateComponentTypes = (
const isDep = cmp.isCollectionDependency;
const jsxAttributes = attributesToMultiLineString([...propAttributes, ...eventAttributes], true, areTypesInternal);

const component = [
componentComment,
` interface ${tagNameAsPascal} {`,
`${componentAttributes} }`,
].filter(Boolean);

const jsx = [jsxComment, ` interface ${tagNameAsPascal} {`, `${jsxAttributes} }`].filter(Boolean);

const element = [
componentComment,
` interface ${htmlElementName} extends Components.${tagNameAsPascal}, HTMLStencilElement {`,
` }`,
componentComment,
` var ${htmlElementName}: {`,
` prototype: ${htmlElementName};`,
` new (): ${htmlElementName};`,
` };`,
];
].filter(Boolean);

return {
isDep,
tagName,
tagNameAsPascal,
htmlElementName,
component: ` interface ${tagNameAsPascal} {\n${componentAttributes} }`,
jsx: ` interface ${tagNameAsPascal} {\n${jsxAttributes} }`,
component: component.join(`\n`),
jsx: jsx.join(`\n`),
element: element.join(`\n`),
};
};
Expand All @@ -62,9 +77,7 @@ const attributesToMultiLineString = (attributes: d.TypeInfo, jsxAttributes: bool
})
.reduce((fullList, type) => {
if (type.jsdoc) {
fullList.push(` /**`);
fullList.push(...type.jsdoc.split('\n').map((line) => ' * ' + line));
fullList.push(` */`);
fullList.push(indentDocComment(type.jsdoc, 16));
}
const optional = jsxAttributes ? !type.required : type.optional;
fullList.push(` "${type.name}"${optional ? '?' : ''}: ${type.type};`);
Expand All @@ -74,3 +87,10 @@ const attributesToMultiLineString = (attributes: d.TypeInfo, jsxAttributes: bool

return attributesStr !== '' ? `${attributesStr}\n` : '';
};

const indentDocComment = (text: string, indent: number): string => {
if (!text) return '';

const spaces = ' '.repeat(indent);
return [`${spaces}/**`, ...text.split('\n').map((line) => `${spaces} * ${line}`), `${spaces} */`].join('\n');
};
14 changes: 13 additions & 1 deletion test/karma/test-app/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ export namespace Components {
}
interface ShadowDomBasicRoot {
}
/**
* @virtualProp {string} colormode - The mode determines which platform styles to use.
*/
interface ShadowDomMode {
/**
* The mode determines which platform styles to use.
Expand Down Expand Up @@ -868,8 +871,14 @@ declare global {
prototype: HTMLShadowDomBasicRootElement;
new (): HTMLShadowDomBasicRootElement;
};
/**
* @virtualProp {string} colormode - The mode determines which platform styles to use.
*/
interface HTMLShadowDomModeElement extends Components.ShadowDomMode, HTMLStencilElement {
}
/**
* @virtualProp {string} colormode - The mode determines which platform styles to use.
*/
var HTMLShadowDomModeElement: {
prototype: HTMLShadowDomModeElement;
new (): HTMLShadowDomModeElement;
Expand Down Expand Up @@ -1490,7 +1499,10 @@ declare namespace LocalJSX {
}
interface ShadowDomBasicRoot {
}
interface ShadowDomMode {
/**
* @virtualProp {string} colormode - The mode determines which platform styles to use.
*/
interface ShadowDomMode {
/**
* The mode determines which platform styles to use.
*/
Expand Down