Skip to content

Commit ee1210a

Browse files
committed
Generate source module path and symbol in web-types
1 parent bc40fe4 commit ee1210a

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

packages/jet-brains-integration/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ export interface Options {
108108
};
109109
/** Used to create links within the component info bubble */
110110
referencesTemplate?: (name: string, tag?: string) => Reference;
111+
/** Used to specify the path to the given component's source module, defaults to `module.path` from the CEM.
112+
* When `undefined` is returned, no source reference is generated */
113+
sourceModuleTemplate?: (args: {name: string, tag?: string, modulePath: string}) => string | undefined;
111114
/** The property form your CEM component object to display your types */
112115
typesSrc?: string;
113116
/** Automatically adds reference to yor package.json */
@@ -171,6 +174,10 @@ const options = {
171174
url: `https://example.com/components/${tag}`
172175
},
173176

177+
/** Used to specify the path to the given component's source module, defaults to `module.path` from the CEM.
178+
* When `undefined` is returned, no source reference is generated */
179+
sourceModuleTemplate: ({name, tag, modulePath}) => `src/components/${tag}/${name}.ts`,
180+
174181
/** The property form your CEM component object to display your types */
175182
typesSrc: 'expandedType'
176183
};

packages/jet-brains-integration/src/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export interface Options extends BaseOptions {
2020
packageJson?: boolean;
2121
/** Used to create a link within the component info bubble */
2222
referenceTemplate?: (name: string, tag?: string) => Reference;
23+
/** Used to specify the path to the given component's source module, defaults to `module.path` from the CEM.
24+
* When `undefined` is returned, no source reference is generated */
25+
sourceModuleTemplate?: (args: {name: string, tag?: string, modulePath: string}) => string | undefined;
2326
}
2427

2528
export interface Params {

packages/jet-brains-integration/src/web-types-generator.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from "fs";
22
import type {
33
JsProperties,
44
Options,
5-
Reference,
65
WebTypeAttribute,
76
WebTypeCssProperty,
87
WebTypeElement,
@@ -13,6 +12,7 @@ import {
1312
getComponents,
1413
type CEM,
1514
Component,
15+
ComponentWithModule,
1616
getComponentDetailsTemplate,
1717
} from "../../../tools/cem-utils";
1818
import type * as schema from "custom-elements-manifest/schema";
@@ -33,20 +33,33 @@ import { updateConfig } from "../../../tools/configurations";
3333
const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8"));
3434

3535
export function getTagList(
36-
components: Component[],
37-
options: Options,
38-
referenceTemplate?: (name: string, tag?: string) => Reference
36+
components: ComponentWithModule[],
37+
options: Options
3938
): WebTypeElement[] {
40-
return components.map((component: Component) => {
41-
const reference = referenceTemplate
42-
? referenceTemplate(component.name, component.tagName)
39+
return components.map((component: ComponentWithModule) => {
40+
const reference = options.referenceTemplate
41+
? options.referenceTemplate(component.name, component.tagName)
4342
: undefined;
4443

44+
const sourceModule = options.sourceModuleTemplate
45+
? options.sourceModuleTemplate({
46+
name: component.name,
47+
tag: component.tagName,
48+
modulePath: component.module.path,
49+
})
50+
: component.module.path;
51+
4552
return {
4653
name: `${options.prefix}${
4754
component.tagName || toKebabCase(component.name)
4855
}${options.suffix}`,
4956
description: getComponentDetailsTemplate(component, options),
57+
source: sourceModule
58+
? {
59+
symbol: component.name,
60+
module: sourceModule,
61+
}
62+
: undefined,
5063
["doc-url"]: reference?.url || "",
5164
attributes: getComponentAttributes(component, options.typesSrc),
5265
slots: component.slots?.map((slot) => {

tools/cem-utils/src/cem-utilities.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { removeQuoteWrappers } from "../../utilities";
22
import type * as schema from "custom-elements-manifest";
3-
import type { CEM, Component } from "./types";
3+
import type { CEM, Component, ComponentWithModule } from "./types";
44

55
export const EXCLUDED_TYPES = [
66
"any",
@@ -51,17 +51,18 @@ export function getComponentDescription(
5151
export function getComponents(
5252
customElementsManifest: CEM,
5353
exclude?: string[]
54-
): Component[] {
54+
): ComponentWithModule[] {
5555
return (
5656
customElementsManifest.modules?.map(
5757
(mod) =>
58-
mod?.declarations?.filter(
59-
(dec) =>
60-
!exclude?.includes(dec.name) &&
61-
((dec as Component).customElement || (dec as Component).tagName)
62-
) || []
58+
mod?.declarations
59+
?.filter(
60+
(dec) =>
61+
!exclude?.includes(dec.name) && (dec.customElement || dec.tagName)
62+
)
63+
.map((dec) => ({ ...dec, module: mod })) || []
6364
) || []
64-
).flat() as Component[];
65+
).flat();
6566
}
6667
/**
6768
* Gets a list of public properties from a CEM component
@@ -111,7 +112,7 @@ export function getComponentMethods(
111112
member.kind === "method" &&
112113
member.privacy !== "private" &&
113114
member.description?.length &&
114-
!member.name.startsWith("#")
115+
!member.name.startsWith("#")
115116
) as schema.ClassMethod[];
116117
}
117118

tools/cem-utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./cem-utilities.js";
22
export * from "./description-templates.js";
3-
export type { CEM, Component } from "./types.js";
3+
export type { CEM, Component, ComponentWithModule } from "./types.js";

tools/cem-utils/src/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export interface Component extends schema.CustomElementDeclaration {
2020
};
2121
}
2222

23+
export interface ComponentWithModule extends Component {
24+
module: CustomModule;
25+
}
26+
2327
export interface CustomModule extends schema.JavaScriptModule {
2428
/**
2529
* The declarations of a module.

0 commit comments

Comments
 (0)