Skip to content

Commit

Permalink
Generate source module path and symbol in web-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kouzukii committed Mar 11, 2024
1 parent bc40fe4 commit ee1210a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
7 changes: 7 additions & 0 deletions packages/jet-brains-integration/README.md
Expand Up @@ -108,6 +108,9 @@ export interface Options {
};
/** Used to create links within the component info bubble */
referencesTemplate?: (name: string, tag?: string) => Reference;
/** Used to specify the path to the given component's source module, defaults to `module.path` from the CEM.
* When `undefined` is returned, no source reference is generated */
sourceModuleTemplate?: (args: {name: string, tag?: string, modulePath: string}) => string | undefined;
/** The property form your CEM component object to display your types */
typesSrc?: string;
/** Automatically adds reference to yor package.json */
Expand Down Expand Up @@ -171,6 +174,10 @@ const options = {
url: `https://example.com/components/${tag}`
},

/** Used to specify the path to the given component's source module, defaults to `module.path` from the CEM.
* When `undefined` is returned, no source reference is generated */
sourceModuleTemplate: ({name, tag, modulePath}) => `src/components/${tag}/${name}.ts`,

/** The property form your CEM component object to display your types */
typesSrc: 'expandedType'
};
Expand Down
3 changes: 3 additions & 0 deletions packages/jet-brains-integration/src/types.d.ts
Expand Up @@ -20,6 +20,9 @@ export interface Options extends BaseOptions {
packageJson?: boolean;
/** Used to create a link within the component info bubble */
referenceTemplate?: (name: string, tag?: string) => Reference;
/** Used to specify the path to the given component's source module, defaults to `module.path` from the CEM.
* When `undefined` is returned, no source reference is generated */
sourceModuleTemplate?: (args: {name: string, tag?: string, modulePath: string}) => string | undefined;
}

export interface Params {
Expand Down
27 changes: 20 additions & 7 deletions packages/jet-brains-integration/src/web-types-generator.ts
Expand Up @@ -2,7 +2,6 @@ import fs from "fs";
import type {
JsProperties,
Options,
Reference,
WebTypeAttribute,
WebTypeCssProperty,
WebTypeElement,
Expand All @@ -13,6 +12,7 @@ import {
getComponents,
type CEM,
Component,
ComponentWithModule,
getComponentDetailsTemplate,
} from "../../../tools/cem-utils";
import type * as schema from "custom-elements-manifest/schema";
Expand All @@ -33,20 +33,33 @@ import { updateConfig } from "../../../tools/configurations";
const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8"));

export function getTagList(
components: Component[],
options: Options,
referenceTemplate?: (name: string, tag?: string) => Reference
components: ComponentWithModule[],
options: Options
): WebTypeElement[] {
return components.map((component: Component) => {
const reference = referenceTemplate
? referenceTemplate(component.name, component.tagName)
return components.map((component: ComponentWithModule) => {
const reference = options.referenceTemplate
? options.referenceTemplate(component.name, component.tagName)
: undefined;

const sourceModule = options.sourceModuleTemplate
? options.sourceModuleTemplate({
name: component.name,
tag: component.tagName,
modulePath: component.module.path,
})
: component.module.path;

return {
name: `${options.prefix}${
component.tagName || toKebabCase(component.name)
}${options.suffix}`,
description: getComponentDetailsTemplate(component, options),
source: sourceModule
? {
symbol: component.name,
module: sourceModule,
}
: undefined,
["doc-url"]: reference?.url || "",
attributes: getComponentAttributes(component, options.typesSrc),
slots: component.slots?.map((slot) => {
Expand Down
19 changes: 10 additions & 9 deletions tools/cem-utils/src/cem-utilities.ts
@@ -1,6 +1,6 @@
import { removeQuoteWrappers } from "../../utilities";
import type * as schema from "custom-elements-manifest";
import type { CEM, Component } from "./types";
import type { CEM, Component, ComponentWithModule } from "./types";

export const EXCLUDED_TYPES = [
"any",
Expand Down Expand Up @@ -51,17 +51,18 @@ export function getComponentDescription(
export function getComponents(
customElementsManifest: CEM,
exclude?: string[]
): Component[] {
): ComponentWithModule[] {
return (
customElementsManifest.modules?.map(
(mod) =>
mod?.declarations?.filter(
(dec) =>
!exclude?.includes(dec.name) &&
((dec as Component).customElement || (dec as Component).tagName)
) || []
mod?.declarations
?.filter(
(dec) =>
!exclude?.includes(dec.name) && (dec.customElement || dec.tagName)
)
.map((dec) => ({ ...dec, module: mod })) || []
) || []
).flat() as Component[];
).flat();
}
/**
* Gets a list of public properties from a CEM component
Expand Down Expand Up @@ -111,7 +112,7 @@ export function getComponentMethods(
member.kind === "method" &&
member.privacy !== "private" &&
member.description?.length &&
!member.name.startsWith("#")
!member.name.startsWith("#")
) as schema.ClassMethod[];
}

Expand Down
2 changes: 1 addition & 1 deletion tools/cem-utils/src/index.ts
@@ -1,3 +1,3 @@
export * from "./cem-utilities.js";
export * from "./description-templates.js";
export type { CEM, Component } from "./types.js";
export type { CEM, Component, ComponentWithModule } from "./types.js";
4 changes: 4 additions & 0 deletions tools/cem-utils/src/types.d.ts
Expand Up @@ -20,6 +20,10 @@ export interface Component extends schema.CustomElementDeclaration {
};
}

export interface ComponentWithModule extends Component {
module: CustomModule;
}

export interface CustomModule extends schema.JavaScriptModule {
/**
* The declarations of a module.
Expand Down

0 comments on commit ee1210a

Please sign in to comment.