@wc-toolkit/cem-generator-fast 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +174 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wc-toolkit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @wc-toolkit/cem-generator-fast
|
|
2
|
+
|
|
3
|
+
FAST detector plugin for `@microsoft/fast-element` components.
|
|
4
|
+
|
|
5
|
+
## Detects
|
|
6
|
+
|
|
7
|
+
- classes extending `FASTElement` or `FastElement`
|
|
8
|
+
- `@customElement("tag-name")` and `@customElement({ name: "tag-name" })`
|
|
9
|
+
- `@attr` fields and custom attribute names from `@attr({ attribute: "..." })`
|
|
10
|
+
- standard members, JSDoc metadata, and types through the core generator
|
|
11
|
+
- literal `$emit("event-name", detail)` calls as `CustomEvent` entries
|
|
12
|
+
|
|
13
|
+
FAST lifecycle and infrastructure members such as `connectedCallback`,
|
|
14
|
+
`disconnectedCallback`, `attributeChangedCallback`, `$fastController`, and
|
|
15
|
+
`$emit` are excluded from the public member list.
|
|
16
|
+
|
|
17
|
+
`mode: "boolean"` is accepted by FAST but requires no special CEM metadata; the field type remains the source of truth.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { generateCem } from "@wc-toolkit/cem-generator";
|
|
23
|
+
import { fastPlugin } from "@wc-toolkit/cem-generator-fast";
|
|
24
|
+
|
|
25
|
+
const manifest = generateCem({
|
|
26
|
+
plugins: [fastPlugin()],
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Run the included demo with `pnpm demo:fast`. It writes the generated manifest to `demo/custom-elements.json`.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { detectClassMembers, detectClassEvents, mergeClassEvents, discoverFrameworkApis, } from "@wc-toolkit/cem-generator";
|
|
3
|
+
import { getJSDocInfo, getNodeTypeText, getParsedTypeText, parseCemClassTags, resolveParsedTypeFromText, } from "@wc-toolkit/cem-generator-utils";
|
|
4
|
+
export function fastPlugin() {
|
|
5
|
+
return {
|
|
6
|
+
name: "fast",
|
|
7
|
+
shouldAnalyze(sourceText) {
|
|
8
|
+
return /from\s+["']@microsoft\/fast-(?:element|foundation)["']/.test(sourceText) ||
|
|
9
|
+
/extends\s+(?:FASTElement|FastElement)/.test(sourceText) ||
|
|
10
|
+
/@(?:customElement|attr)\b/.test(sourceText);
|
|
11
|
+
},
|
|
12
|
+
onFile(context) {
|
|
13
|
+
const fragment = {};
|
|
14
|
+
ts.forEachChild(context.sourceFile, function visit(node) {
|
|
15
|
+
if (ts.isClassDeclaration(node) && node.name && extendsFastElement(node, context.checker)) {
|
|
16
|
+
const className = node.name.text;
|
|
17
|
+
const classDoc = parseCemClassTags(node);
|
|
18
|
+
const tagName = getCustomElementTagName(node);
|
|
19
|
+
const members = filterFastMembers(detectClassMembers(node, context));
|
|
20
|
+
const attributes = getAttrMetadata(node, context, members);
|
|
21
|
+
const discovered = discoverFrameworkApis(node, context.sourceFile);
|
|
22
|
+
const classFragment = {
|
|
23
|
+
name: className,
|
|
24
|
+
exportName: getExportName(node),
|
|
25
|
+
module: context.filePath,
|
|
26
|
+
description: getJSDocInfo(node).description || undefined,
|
|
27
|
+
summary: classDoc.summary,
|
|
28
|
+
deprecated: classDoc.deprecated,
|
|
29
|
+
tagName: classDoc.tagName ?? tagName,
|
|
30
|
+
superclass: { name: "FASTElement", module: "@microsoft/fast-element" },
|
|
31
|
+
members,
|
|
32
|
+
attributes: mergeByName(attributes, classDoc.attributes),
|
|
33
|
+
slots: mergeByName(discovered.slots, classDoc.slots),
|
|
34
|
+
events: mergeClassEvents(mergeFastEvents(mergeClassEvents(discovered.events, detectClassEvents(node, context)), node, context), classDoc.events?.map((event) => ({
|
|
35
|
+
...event,
|
|
36
|
+
parsedType: resolveParsedTypeFromText(event.type, context.sourceFile, context.checker),
|
|
37
|
+
}))),
|
|
38
|
+
cssParts: mergeByName(discovered.cssParts, classDoc.cssParts),
|
|
39
|
+
cssProperties: mergeByName(discovered.cssProperties, classDoc.cssProperties),
|
|
40
|
+
cssStates: mergeByName(discovered.cssStates, classDoc.cssStates),
|
|
41
|
+
omitInherited: classDoc.omitInherited,
|
|
42
|
+
customJsDocTags: classDoc.customJsDocTags,
|
|
43
|
+
};
|
|
44
|
+
for (const attr of classFragment.attributes ?? []) {
|
|
45
|
+
attr.parsedType ??= resolveParsedTypeFromText(attr.type, context.sourceFile, context.checker);
|
|
46
|
+
}
|
|
47
|
+
fragment[className] = classFragment;
|
|
48
|
+
}
|
|
49
|
+
ts.forEachChild(node, visit);
|
|
50
|
+
});
|
|
51
|
+
return fragment;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function extendsFastElement(node, checker) {
|
|
56
|
+
const heritage = node.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
|
|
57
|
+
const seen = new Set();
|
|
58
|
+
function visit(expression) {
|
|
59
|
+
if (seen.has(expression))
|
|
60
|
+
return false;
|
|
61
|
+
seen.add(expression);
|
|
62
|
+
if (ts.isIdentifier(expression) && (expression.text === "FASTElement" || expression.text === "FastElement"))
|
|
63
|
+
return true;
|
|
64
|
+
if (!ts.isIdentifier(expression))
|
|
65
|
+
return false;
|
|
66
|
+
const symbol = checker.getSymbolAtLocation(expression);
|
|
67
|
+
const resolved = symbol && symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
68
|
+
const declaration = resolved?.declarations?.find(ts.isClassDeclaration);
|
|
69
|
+
const base = declaration?.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
|
|
70
|
+
return !!base?.types[0] && visit(base.types[0].expression);
|
|
71
|
+
}
|
|
72
|
+
return !!heritage?.types.some((type) => visit(type.expression));
|
|
73
|
+
}
|
|
74
|
+
function getAttrMetadata(node, context, members) {
|
|
75
|
+
const byName = new Map((members ?? []).map((member) => [member.name, member]));
|
|
76
|
+
const attributes = [];
|
|
77
|
+
for (const member of node.members) {
|
|
78
|
+
if (!ts.isPropertyDeclaration(member))
|
|
79
|
+
continue;
|
|
80
|
+
const attrDecorator = (ts.getDecorators?.(member) ?? []).find((decorator) => getDecoratorName(decorator) === "attr");
|
|
81
|
+
if (!attrDecorator)
|
|
82
|
+
continue;
|
|
83
|
+
const name = member.name.getText();
|
|
84
|
+
const field = byName.get(name);
|
|
85
|
+
if (!field || field.internal)
|
|
86
|
+
continue;
|
|
87
|
+
const options = getAttrOptions(attrDecorator);
|
|
88
|
+
const attributeName = options.attribute ?? name;
|
|
89
|
+
attributes.push({
|
|
90
|
+
name: attributeName,
|
|
91
|
+
type: getNodeTypeText(member, context.checker),
|
|
92
|
+
parsedType: getParsedTypeText(member, context.checker),
|
|
93
|
+
description: field.description,
|
|
94
|
+
summary: field.summary,
|
|
95
|
+
deprecated: field.deprecated,
|
|
96
|
+
default: field.default,
|
|
97
|
+
fieldName: name,
|
|
98
|
+
});
|
|
99
|
+
field.attribute = attributeName;
|
|
100
|
+
}
|
|
101
|
+
return attributes.length ? attributes : undefined;
|
|
102
|
+
}
|
|
103
|
+
function getCustomElementTagName(node) {
|
|
104
|
+
const decorator = (ts.getDecorators?.(node) ?? []).find((item) => getDecoratorName(item) === "customElement");
|
|
105
|
+
if (!decorator || !ts.isCallExpression(decorator.expression))
|
|
106
|
+
return undefined;
|
|
107
|
+
const argument = decorator.expression.arguments[0];
|
|
108
|
+
if (argument && ts.isStringLiteralLike(argument))
|
|
109
|
+
return argument.text;
|
|
110
|
+
if (!argument || !ts.isObjectLiteralExpression(argument))
|
|
111
|
+
return undefined;
|
|
112
|
+
const name = argument.properties.find((property) => ts.isPropertyAssignment(property) && property.name.getText() === "name")?.initializer;
|
|
113
|
+
return name && ts.isStringLiteralLike(name) ? name.text : undefined;
|
|
114
|
+
}
|
|
115
|
+
function getDecoratorName(decorator) {
|
|
116
|
+
const expression = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
|
|
117
|
+
return ts.isIdentifier(expression) ? expression.text : undefined;
|
|
118
|
+
}
|
|
119
|
+
function getAttrOptions(decorator) {
|
|
120
|
+
if (!ts.isCallExpression(decorator.expression))
|
|
121
|
+
return {};
|
|
122
|
+
const argument = decorator.expression.arguments[0];
|
|
123
|
+
if (!argument || !ts.isObjectLiteralExpression(argument))
|
|
124
|
+
return {};
|
|
125
|
+
const attribute = argument.properties.find((property) => ts.isPropertyAssignment(property) && property.name.getText() === "attribute")?.initializer;
|
|
126
|
+
return attribute && ts.isStringLiteralLike(attribute) ? { attribute: attribute.text } : {};
|
|
127
|
+
}
|
|
128
|
+
function filterFastMembers(members) {
|
|
129
|
+
const frameworkMembers = new Set([
|
|
130
|
+
"$fastController",
|
|
131
|
+
"$emit",
|
|
132
|
+
"connectedCallback",
|
|
133
|
+
"disconnectedCallback",
|
|
134
|
+
"attributeChangedCallback",
|
|
135
|
+
]);
|
|
136
|
+
const filtered = members?.filter((member) => !frameworkMembers.has(member.name));
|
|
137
|
+
return filtered?.length ? filtered : undefined;
|
|
138
|
+
}
|
|
139
|
+
function mergeFastEvents(events, node, context) {
|
|
140
|
+
const byName = new Map((events ?? []).map((event) => [event.name, event]));
|
|
141
|
+
function visit(current) {
|
|
142
|
+
if (ts.isClassDeclaration(current))
|
|
143
|
+
return;
|
|
144
|
+
if (ts.isCallExpression(current) &&
|
|
145
|
+
ts.isPropertyAccessExpression(current.expression) &&
|
|
146
|
+
current.expression.name.text === "$emit") {
|
|
147
|
+
const name = current.arguments[0];
|
|
148
|
+
if (name && ts.isStringLiteralLike(name)) {
|
|
149
|
+
const detail = current.arguments[1];
|
|
150
|
+
byName.set(name.text, {
|
|
151
|
+
name: name.text,
|
|
152
|
+
type: "CustomEvent",
|
|
153
|
+
detail: detail ? getNodeTypeText(detail, context.checker) : undefined,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
ts.forEachChild(current, visit);
|
|
158
|
+
}
|
|
159
|
+
ts.forEachChild(node, visit);
|
|
160
|
+
return byName.size ? [...byName.values()] : undefined;
|
|
161
|
+
}
|
|
162
|
+
function getExportName(node) {
|
|
163
|
+
const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined;
|
|
164
|
+
if (!modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword))
|
|
165
|
+
return undefined;
|
|
166
|
+
return modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword) ? "default" : node.name?.text;
|
|
167
|
+
}
|
|
168
|
+
function mergeByName(...sources) {
|
|
169
|
+
const merged = new Map();
|
|
170
|
+
for (const source of sources)
|
|
171
|
+
for (const item of source ?? [])
|
|
172
|
+
merged.set(item.name, { ...merged.get(item.name), ...item });
|
|
173
|
+
return merged.size ? [...merged.values()] : undefined;
|
|
174
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wc-toolkit/cem-generator-fast",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "FAST detector plugin for generating Custom Elements Manifests.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/wc-toolkit/cem-generator.git",
|
|
9
|
+
"directory": "packages/plugins/fast"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/wc-toolkit/cem-generator/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://wc-toolkit.github.io/cem-generator/plugins/fast/",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"custom-elements",
|
|
17
|
+
"custom-elements-manifest",
|
|
18
|
+
"web-components",
|
|
19
|
+
"fast"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"typescript": "^5.9.0",
|
|
33
|
+
"@wc-toolkit/cem-generator": "0.1.1",
|
|
34
|
+
"@wc-toolkit/cem-generator-utils": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@microsoft/fast-element": "^3.0.2"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node --test tests/*.test.mjs",
|
|
41
|
+
"demo": "tsx demo/run.ts"
|
|
42
|
+
}
|
|
43
|
+
}
|