sveld 0.17.2 → 0.18.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/CHANGELOG.md +10 -0
- package/README.md +32 -5
- package/lib/ComponentParser.d.ts +2 -0
- package/lib/ComponentParser.js +29 -9
- package/lib/writer/writer-ts-definitions.js +16 -19
- package/package.json +16 -16
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
10
10
|
- add isAccessor field to API
|
|
11
11
|
- update Markdown writer to generate a separate table for accessors -->
|
|
12
12
|
|
|
13
|
+
## [0.18.1](https://github.com/carbon-design-system/sveld/releases/tag/v0.18.1) - 2023-06-04
|
|
14
|
+
|
|
15
|
+
- allow `data-*` attributes for props forwarded to HTML elements for `svelte-check@3.x` compatibility
|
|
16
|
+
|
|
17
|
+
## [0.18.0](https://github.com/carbon-design-system/sveld/releases/tag/v0.18.0) - 2022-10-22
|
|
18
|
+
|
|
19
|
+
- support `@slot` tag descriptions for named slots
|
|
20
|
+
- support `@event` tag descriptions
|
|
21
|
+
- remove `sveltekit:*` attributes from type definitions
|
|
22
|
+
|
|
13
23
|
## [0.17.2](https://github.com/carbon-design-system/sveld/releases/tag/v0.17.2) - 2022-06-13
|
|
14
24
|
|
|
15
25
|
- handle `export {}` in script block
|
package/README.md
CHANGED
|
@@ -354,11 +354,19 @@ export let authors = [];
|
|
|
354
354
|
|
|
355
355
|
Use the `@slot` tag for typing component slots. Note that `@slot` is a non-standard JSDoc tag.
|
|
356
356
|
|
|
357
|
+
Descriptions are optional for named slots. Currently, the default slot cannot have a description.
|
|
358
|
+
|
|
357
359
|
Signature:
|
|
358
360
|
|
|
359
361
|
```js
|
|
360
362
|
/**
|
|
361
|
-
* @slot {Type} [slot
|
|
363
|
+
* @slot {Type} slot-name [slot description]
|
|
364
|
+
*/
|
|
365
|
+
|
|
366
|
+
Omit the `slot-name` to type the default slot.
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* @slot {Type}
|
|
362
370
|
*/
|
|
363
371
|
```
|
|
364
372
|
|
|
@@ -368,7 +376,8 @@ Example:
|
|
|
368
376
|
<script>
|
|
369
377
|
/**
|
|
370
378
|
* @slot {{ prop: number; doubled: number; }}
|
|
371
|
-
* @slot {{
|
|
379
|
+
* @slot {{}} title
|
|
380
|
+
* @slot {{ prop: number }} body - Customize the paragraph text.
|
|
372
381
|
*/
|
|
373
382
|
|
|
374
383
|
export let prop = 0;
|
|
@@ -376,22 +385,25 @@ Example:
|
|
|
376
385
|
|
|
377
386
|
<h1>
|
|
378
387
|
<slot {prop} doubled={prop * 2} />
|
|
388
|
+
<slot name="title" />
|
|
379
389
|
</h1>
|
|
380
390
|
|
|
381
391
|
<p>
|
|
382
|
-
<slot name="
|
|
392
|
+
<slot name="body" {prop} />
|
|
383
393
|
</p>
|
|
384
394
|
```
|
|
385
395
|
|
|
386
396
|
### `@event`
|
|
387
397
|
|
|
388
|
-
Use the `@event` tag
|
|
398
|
+
Use the `@event` tag to type dispatched events. An event name is required and a description optional.
|
|
399
|
+
|
|
400
|
+
Use `null` as the value if no event detail is provided.
|
|
389
401
|
|
|
390
402
|
Signature:
|
|
391
403
|
|
|
392
404
|
```js
|
|
393
405
|
/**
|
|
394
|
-
* @event {EventDetail} eventname
|
|
406
|
+
* @event {EventDetail} eventname [event description]
|
|
395
407
|
*/
|
|
396
408
|
```
|
|
397
409
|
|
|
@@ -400,6 +412,7 @@ Example:
|
|
|
400
412
|
```js
|
|
401
413
|
/**
|
|
402
414
|
* @event {{ key: string }} button:key
|
|
415
|
+
* @event {null} key – Fired when `key` changes.
|
|
403
416
|
*/
|
|
404
417
|
|
|
405
418
|
export let key = "";
|
|
@@ -409,6 +422,20 @@ import { createEventDispatcher } from "svelte";
|
|
|
409
422
|
const dispatch = createEventDispatcher();
|
|
410
423
|
|
|
411
424
|
$: dispatch("button:key", { key });
|
|
425
|
+
$: if (key) dispatch("key");
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
Output:
|
|
429
|
+
|
|
430
|
+
```ts
|
|
431
|
+
export default class Component extends SvelteComponentTyped<
|
|
432
|
+
ComponentProps,
|
|
433
|
+
{
|
|
434
|
+
"button:key": CustomEvent<{ key: string }>;
|
|
435
|
+
/** Fired when `key` changes. */ key: CustomEvent<null>;
|
|
436
|
+
},
|
|
437
|
+
{}
|
|
438
|
+
> {}
|
|
412
439
|
```
|
|
413
440
|
|
|
414
441
|
### `@restProps`
|
package/lib/ComponentParser.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ interface ComponentSlot {
|
|
|
22
22
|
default: boolean;
|
|
23
23
|
fallback?: string;
|
|
24
24
|
slot_props?: string;
|
|
25
|
+
description?: string;
|
|
25
26
|
}
|
|
26
27
|
interface ForwardedEvent {
|
|
27
28
|
type: "forwarded";
|
|
@@ -32,6 +33,7 @@ interface DispatchedEvent {
|
|
|
32
33
|
type: "dispatched";
|
|
33
34
|
name: string;
|
|
34
35
|
detail?: any;
|
|
36
|
+
description?: string;
|
|
35
37
|
}
|
|
36
38
|
declare type ComponentEvent = ForwardedEvent | DispatchedEvent;
|
|
37
39
|
interface TypeDef {
|
package/lib/ComponentParser.js
CHANGED
|
@@ -120,26 +120,31 @@ var ComponentParser = /** @class */ (function () {
|
|
|
120
120
|
return "any";
|
|
121
121
|
return type;
|
|
122
122
|
};
|
|
123
|
-
ComponentParser.prototype.addSlot = function (
|
|
123
|
+
ComponentParser.prototype.addSlot = function (_a) {
|
|
124
|
+
var _b;
|
|
125
|
+
var slot_name = _a.slot_name, slot_props = _a.slot_props, slot_fallback = _a.slot_fallback, slot_description = _a.slot_description;
|
|
124
126
|
var default_slot = slot_name === undefined || slot_name === "";
|
|
125
127
|
var name = default_slot ? DEFAULT_SLOT_NAME : slot_name;
|
|
126
128
|
var fallback = ComponentParser.assignValue(slot_fallback);
|
|
127
129
|
var props = ComponentParser.assignValue(slot_props);
|
|
130
|
+
var description = (_b = slot_description === null || slot_description === void 0 ? void 0 : slot_description.split("-").pop()) === null || _b === void 0 ? void 0 : _b.trim();
|
|
128
131
|
if (this.slots.has(name)) {
|
|
129
132
|
var existing_slot = this.slots.get(name);
|
|
130
|
-
this.slots.set(name, __assign(__assign({}, existing_slot), { fallback: fallback, slot_props: existing_slot.slot_props === undefined ? props : existing_slot.slot_props }));
|
|
133
|
+
this.slots.set(name, __assign(__assign({}, existing_slot), { fallback: fallback, slot_props: existing_slot.slot_props === undefined ? props : existing_slot.slot_props, description: existing_slot.description || description }));
|
|
131
134
|
}
|
|
132
135
|
else {
|
|
133
136
|
this.slots.set(name, {
|
|
134
137
|
name: name,
|
|
135
138
|
"default": default_slot,
|
|
136
139
|
fallback: fallback,
|
|
137
|
-
slot_props: slot_props
|
|
140
|
+
slot_props: slot_props,
|
|
141
|
+
description: description
|
|
138
142
|
});
|
|
139
143
|
}
|
|
140
144
|
};
|
|
141
145
|
ComponentParser.prototype.addDispatchedEvent = function (_a) {
|
|
142
|
-
var
|
|
146
|
+
var _b;
|
|
147
|
+
var name = _a.name, detail = _a.detail, has_argument = _a.has_argument, description = _a.description;
|
|
143
148
|
if (name === undefined)
|
|
144
149
|
return;
|
|
145
150
|
/**
|
|
@@ -148,15 +153,17 @@ var ComponentParser = /** @class */ (function () {
|
|
|
148
153
|
* `@event` is not specified.
|
|
149
154
|
*/
|
|
150
155
|
var default_detail = !has_argument && !detail ? "null" : ComponentParser.assignValue(detail);
|
|
156
|
+
var event_description = (_b = description === null || description === void 0 ? void 0 : description.split("-").pop()) === null || _b === void 0 ? void 0 : _b.trim();
|
|
151
157
|
if (this.events.has(name)) {
|
|
152
158
|
var existing_event = this.events.get(name);
|
|
153
|
-
this.events.set(name, __assign(__assign({}, existing_event), { detail: existing_event.detail === undefined ? default_detail : existing_event.detail }));
|
|
159
|
+
this.events.set(name, __assign(__assign({}, existing_event), { detail: existing_event.detail === undefined ? default_detail : existing_event.detail, description: existing_event.description || event_description }));
|
|
154
160
|
}
|
|
155
161
|
else {
|
|
156
162
|
this.events.set(name, {
|
|
157
163
|
type: "dispatched",
|
|
158
164
|
name: name,
|
|
159
|
-
detail: default_detail
|
|
165
|
+
detail: default_detail,
|
|
166
|
+
description: event_description
|
|
160
167
|
});
|
|
161
168
|
}
|
|
162
169
|
};
|
|
@@ -181,10 +188,19 @@ var ComponentParser = /** @class */ (function () {
|
|
|
181
188
|
};
|
|
182
189
|
break;
|
|
183
190
|
case "slot":
|
|
184
|
-
_this.addSlot(
|
|
191
|
+
_this.addSlot({
|
|
192
|
+
slot_name: name,
|
|
193
|
+
slot_props: type,
|
|
194
|
+
slot_description: !!description ? description : undefined
|
|
195
|
+
});
|
|
185
196
|
break;
|
|
186
197
|
case "event":
|
|
187
|
-
_this.addDispatchedEvent({
|
|
198
|
+
_this.addDispatchedEvent({
|
|
199
|
+
name: name,
|
|
200
|
+
detail: type,
|
|
201
|
+
has_argument: false,
|
|
202
|
+
description: !!description ? description : undefined
|
|
203
|
+
});
|
|
188
204
|
break;
|
|
189
205
|
case "typedef":
|
|
190
206
|
_this.typedefs.set(name, {
|
|
@@ -467,7 +483,11 @@ var ComponentParser = /** @class */ (function () {
|
|
|
467
483
|
var start = _a.start, end = _a.end;
|
|
468
484
|
return _this.sourceAtPos(start, end);
|
|
469
485
|
}).join("").trim();
|
|
470
|
-
_this.addSlot(
|
|
486
|
+
_this.addSlot({
|
|
487
|
+
slot_name: slot_name,
|
|
488
|
+
slot_props: JSON.stringify(slot_props, null, 2),
|
|
489
|
+
slot_fallback: fallback
|
|
490
|
+
});
|
|
471
491
|
}
|
|
472
492
|
if (node.type === "EventHandler" && node.expression == null) {
|
|
473
493
|
if (!_this.events.has(node.name) && parent !== undefined) {
|
|
@@ -110,7 +110,7 @@ function addCommentLine(value, returnValue) {
|
|
|
110
110
|
return "* ".concat(returnValue || value, "\n");
|
|
111
111
|
}
|
|
112
112
|
function genPropDef(def) {
|
|
113
|
-
var _a
|
|
113
|
+
var _a;
|
|
114
114
|
var initial_props = def.props
|
|
115
115
|
.filter(function (prop) { return !prop.isFunctionDeclaration && prop.kind !== "const"; })
|
|
116
116
|
.map(function (prop) {
|
|
@@ -132,23 +132,10 @@ function genPropDef(def) {
|
|
|
132
132
|
var prop_value = prop.constant && !prop.isFunction ? prop.value : prop.type;
|
|
133
133
|
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(prop.name).concat(prop.isRequired ? "" : "?", ": ").concat(prop_value, ";");
|
|
134
134
|
});
|
|
135
|
-
if (((_a = def.rest_props) === null || _a === void 0 ? void 0 : _a.type) === "Element") {
|
|
136
|
-
var elements = (_b = def.rest_props) === null || _b === void 0 ? void 0 : _b.name.split("|").map(function (element) { return element.replace(/\s+/g, ""); });
|
|
137
|
-
if (elements.includes("a")) {
|
|
138
|
-
initial_props.push([
|
|
139
|
-
"\n",
|
|
140
|
-
"\n /**\n * SvelteKit attribute to enable data prefetching\n * if a link is hovered over or touched on mobile.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-prefetch\n * @default false\n */\n \"sveltekit:prefetch\"?: boolean;\n ",
|
|
141
|
-
"\n",
|
|
142
|
-
"\n /**\n * SvelteKit attribute to trigger a full page\n * reload after the link is clicked.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-reload\n * @default false\n */\n \"sveltekit:reload\"?: boolean;\n ",
|
|
143
|
-
"\n",
|
|
144
|
-
"\n /**\n * SvelteKit attribute to prevent scrolling\n * after the link is clicked.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-noscroll\n * @default false\n */\n \"sveltekit:noscroll\"?: boolean;\n ",
|
|
145
|
-
].join("\n"));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
135
|
var props = initial_props.join("\n");
|
|
149
136
|
var props_name = "".concat(def.moduleName, "Props");
|
|
150
137
|
var prop_def = EMPTY_STR;
|
|
151
|
-
if (((
|
|
138
|
+
if (((_a = def.rest_props) === null || _a === void 0 ? void 0 : _a.type) === "Element") {
|
|
152
139
|
var extend_tag_map = def.rest_props.name
|
|
153
140
|
.split("|")
|
|
154
141
|
.map(function (name) {
|
|
@@ -159,7 +146,12 @@ function genPropDef(def) {
|
|
|
159
146
|
return "svelte.JSX.HTMLAttributes<HTMLElementTagNameMap[\"".concat(element, "\"]>");
|
|
160
147
|
})
|
|
161
148
|
.join(",");
|
|
162
|
-
|
|
149
|
+
/**
|
|
150
|
+
* Components that extend HTML elements should allow for `data-*` attributes.
|
|
151
|
+
* @see https://github.com/sveltejs/language-tools/issues/1825
|
|
152
|
+
*/
|
|
153
|
+
var dataAttributes = "[key: `data-${string}`]: any;";
|
|
154
|
+
prop_def = "\n export interface ".concat(props_name, " extends ").concat(def["extends"] !== undefined ? "".concat(def["extends"].interface, ", ") : "").concat(extend_tag_map, " {\n ").concat(props, "\n \n ").concat(dataAttributes, "\n }\n ");
|
|
163
155
|
}
|
|
164
156
|
else {
|
|
165
157
|
prop_def = "\n export interface ".concat(props_name, " ").concat(def["extends"] !== undefined ? "extends ".concat(def["extends"].interface) : "", " {\n ").concat(props, "\n }\n ");
|
|
@@ -174,7 +166,8 @@ function genSlotDef(def) {
|
|
|
174
166
|
.map(function (_a) {
|
|
175
167
|
var name = _a.name, slot_props = _a.slot_props, rest = __rest(_a, ["name", "slot_props"]);
|
|
176
168
|
var key = rest["default"] ? "default" : clampKey(name);
|
|
177
|
-
|
|
169
|
+
var description = rest.description ? "/** " + rest.description + " */\n" : "";
|
|
170
|
+
return "".concat(description).concat(clampKey(key), ": ").concat(formatTsProps(slot_props), ";");
|
|
178
171
|
})
|
|
179
172
|
.join("\n");
|
|
180
173
|
}
|
|
@@ -193,9 +186,13 @@ function genEventDef(def) {
|
|
|
193
186
|
};
|
|
194
187
|
return def.events
|
|
195
188
|
.map(function (event) {
|
|
196
|
-
|
|
189
|
+
var description = "";
|
|
190
|
+
if (event.type === "dispatched" && event.description) {
|
|
191
|
+
description = "/** " + event.description + " */\n";
|
|
192
|
+
}
|
|
193
|
+
return "".concat(description).concat(clampKey(event.name), ": ").concat(event.type === "dispatched" ? createDispatchedEvent(event.detail) : "".concat(mapEvent(event.name), "[\"").concat(event.name, "\"]"), ";\n");
|
|
197
194
|
})
|
|
198
|
-
.join("
|
|
195
|
+
.join("");
|
|
199
196
|
}
|
|
200
197
|
function genAccessors(def) {
|
|
201
198
|
return def.props
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sveld",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Generate TypeScript definitions for your Svelte components.",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"dev": "vite",
|
|
10
10
|
"build": "vite build",
|
|
11
11
|
"preview": "vite preview",
|
|
12
|
-
"test:unit": "vitest",
|
|
12
|
+
"test:unit": "vitest -r tests",
|
|
13
13
|
"test:snapshot": "node tests/test-snapshot",
|
|
14
14
|
"test:integration": "node tests/test-integration",
|
|
15
15
|
"format": "prettier --write \"{src,tests}/**/*.{js,ts,svelte,md}\"",
|
|
@@ -17,27 +17,27 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@rollup/plugin-node-resolve": "^13.2.1",
|
|
20
|
-
"acorn": "^8.
|
|
20
|
+
"acorn": "^8.8.0",
|
|
21
21
|
"comment-parser": "^1.3.1",
|
|
22
|
-
"fast-glob": "^3.2.
|
|
22
|
+
"fast-glob": "^3.2.12",
|
|
23
23
|
"prettier": "^2.6.2",
|
|
24
24
|
"rollup": "^2.70.2",
|
|
25
25
|
"rollup-plugin-svelte": "^7.1.0",
|
|
26
|
-
"svelte": "^3.
|
|
26
|
+
"svelte": "^3.52.0",
|
|
27
27
|
"svelte-preprocess": "^4.10.6",
|
|
28
|
-
"typescript": "^4.
|
|
28
|
+
"typescript": "^4.8.4"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@sveltejs/vite-plugin-svelte": "1.0.
|
|
32
|
-
"@types/node": "^
|
|
33
|
-
"@types/prettier": "^2.
|
|
34
|
-
"carbon-components-svelte": "^0.
|
|
35
|
-
"carbon-preprocess-svelte": "^0.9.
|
|
36
|
-
"codemirror": "
|
|
37
|
-
"prettier-plugin-svelte": "^2.
|
|
38
|
-
"svelte-highlight": "^6.
|
|
39
|
-
"vite": "^
|
|
40
|
-
"vitest": "^0.
|
|
31
|
+
"@sveltejs/vite-plugin-svelte": "1.0.9",
|
|
32
|
+
"@types/node": "^18.11.0",
|
|
33
|
+
"@types/prettier": "^2.7.1",
|
|
34
|
+
"carbon-components-svelte": "^0.70.12",
|
|
35
|
+
"carbon-preprocess-svelte": "^0.9.1",
|
|
36
|
+
"codemirror": "5.65.5",
|
|
37
|
+
"prettier-plugin-svelte": "^2.8.0",
|
|
38
|
+
"svelte-highlight": "^6.2.1",
|
|
39
|
+
"vite": "^3.1.8",
|
|
40
|
+
"vitest": "^0.24.3"
|
|
41
41
|
},
|
|
42
42
|
"bin": {
|
|
43
43
|
"sveld": "./cli.js"
|