@wc-toolkit/cem-generator-svelte 0.1.17 → 0.1.19

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.
Files changed (2) hide show
  1. package/dist/index.js +25 -4
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import path from "node:path";
2
+ import ts from "typescript";
2
3
  import { parseCssMetadata, } from "@wc-toolkit/cem-generator";
4
+ import { parseCemClassTags } from "@wc-toolkit/cem-generator-utils";
3
5
  /** Detects Svelte components compiled as custom elements. */
4
6
  export function sveltePlugin() {
5
7
  return {
@@ -38,6 +40,7 @@ export function sveltePlugin() {
38
40
  cssParts: mergeNamed(discoverParts(source), metadata.cssParts),
39
41
  cssProperties: mergeNamed(discoverCssProperties(source), metadata.cssProperties),
40
42
  cssStates: metadata.cssStates,
43
+ customJsDocTags: metadata.customJsDocTags,
41
44
  };
42
45
  return { [fragment.name]: fragment };
43
46
  },
@@ -56,18 +59,23 @@ function getProps(source) {
56
59
  const props = [];
57
60
  const script = source.match(/<script\b[^>]*>([\s\S]*?)<\/script>/)?.[1] ?? "";
58
61
  const interfaces = new Map();
59
- for (const match of script.matchAll(/(?:\/\*\*([\s\S]*?)\*\/\s*)?(\w+)\??\s*:\s*([^;\n,}]+)/g)) {
60
- interfaces.set(match[2], { type: match[3].trim(), description: cleanComment(match[1]) });
62
+ for (const match of script.matchAll(/(?:\/\*\*((?:(?!\*\/)[\s\S])*)\*\/\s*)?(\w+)\??\s*:\s*([^;\n,}]+)/g)) {
63
+ interfaces.set(match[2], {
64
+ type: match[3].trim(),
65
+ description: getDescription(match[1]),
66
+ customJsDocTags: parseCustomJsDocTags(match[1]),
67
+ });
61
68
  }
62
- for (const match of script.matchAll(/(?:\/\*\*([\s\S]*?)\*\/\s*)?export\s+let\s+(\w+)\s*(?::\s*([^=;]+))?\s*(?:=\s*([^;\n]+))?/g)) {
69
+ for (const match of script.matchAll(/(?:\/\*\*((?:(?!\*\/)[\s\S])*)\*\/\s*)?export\s+let\s+(\w+)\s*(?::\s*([^=;]+))?\s*(?:=\s*([^;\n]+))?/g)) {
63
70
  const [, comment, name, type, defaultValue] = match;
64
71
  props.push({
65
72
  name,
66
73
  kind: "field",
67
74
  type: type?.trim() ?? inferType(defaultValue),
68
- description: cleanComment(comment),
75
+ description: getDescription(comment),
69
76
  default: defaultValue?.trim(),
70
77
  attribute: name.toLowerCase(),
78
+ customJsDocTags: parseCustomJsDocTags(comment),
71
79
  });
72
80
  }
73
81
  const runeProps = script.match(/\b(?:let|const)\s*\{([\s\S]*?)\}\s*(?::\s*(\w+))?\s*=\s*\$props\s*\(\s*\)/)?.[1];
@@ -83,6 +91,7 @@ function getProps(source) {
83
91
  description: info?.description,
84
92
  default: match[2]?.trim(),
85
93
  attribute: match[1].toLowerCase(),
94
+ customJsDocTags: info?.customJsDocTags,
86
95
  });
87
96
  }
88
97
  const options = source.match(/<svelte:options\b([\s\S]*?)\/>/)?.[1] ?? "";
@@ -108,8 +117,17 @@ function getComponentMetadata(source, tagName) {
108
117
  cssParts: parseNamedTags(comment, "csspart"),
109
118
  cssProperties: parseCssTags(comment),
110
119
  cssStates: parseNamedTags(comment, "cssState"),
120
+ customJsDocTags: parseCustomJsDocTags(comment),
111
121
  };
112
122
  }
123
+ /** Reuses the shared JSDoc parser by wrapping a Svelte comment body as a JSDoc block. */
124
+ function parseCustomJsDocTags(comment) {
125
+ if (!comment)
126
+ return undefined;
127
+ const sourceFile = ts.createSourceFile("comment.ts", `/**${comment}*/\nclass Doc {}`, ts.ScriptTarget.Latest, true);
128
+ const node = sourceFile.statements.find(ts.isClassDeclaration);
129
+ return node ? parseCemClassTags(node).customJsDocTags : undefined;
130
+ }
113
131
  function discoverSlots(source) {
114
132
  return [...source.matchAll(/<slot(?:\s+name\s*=\s*["']([^"']+)["'])?[^>]*>/g)].map((match) => ({
115
133
  name: match[1] ?? "",
@@ -159,6 +177,9 @@ function parseCssTags(comment) {
159
177
  function getTagDescription(comment, tag) {
160
178
  return comment.match(new RegExp(`@${tag}\\s+([^\\n]*)`))?.[1]?.trim() || undefined;
161
179
  }
180
+ function getDescription(comment) {
181
+ return cleanComment(comment?.replace(/(^|\s)@\w[\s\S]*/, "$1"));
182
+ }
162
183
  function cleanComment(value) {
163
184
  if (!value)
164
185
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wc-toolkit/cem-generator-svelte",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "Svelte detector plugin for generating Custom Elements Manifests.",
5
5
  "keywords": [
6
6
  "custom-elements",
@@ -30,8 +30,8 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "typescript": "6.0.3",
33
- "@wc-toolkit/cem-generator": "0.2.4",
34
- "@wc-toolkit/cem-generator-utils": "0.1.6"
33
+ "@wc-toolkit/cem-generator": "0.2.5",
34
+ "@wc-toolkit/cem-generator-utils": "0.1.7"
35
35
  },
36
36
  "devDependencies": {
37
37
  "svelte": "^5.0.0"