sveld 0.10.2 → 0.11.0
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 +5 -1
- package/README.md +36 -5
- package/lib/ComponentParser.d.ts +2 -0
- package/lib/ComponentParser.js +13 -5
- package/lib/writer/writer-ts-definitions.js +12 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -10,7 +10,11 @@ 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.
|
|
13
|
+
## [0.11.0](https://github.com/carbon-design-system/sveld/releases/tag/v0.11.0) - 2021-12-16
|
|
14
|
+
|
|
15
|
+
- support writing `<!-- @component -->` comments in Svelte components to TypeScript definitions
|
|
16
|
+
|
|
17
|
+
## [0.10.2](https://github.com/carbon-design-system/sveld/releases/tag/v0.10.2) - 2021-08-29
|
|
14
18
|
|
|
15
19
|
- tolerate slot spread syntax (`<slot {...props} />`) when parsing Svelte components
|
|
16
20
|
|
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# sveld
|
|
2
2
|
|
|
3
3
|
[![NPM][npm]][npm-url]
|
|
4
|
-
|
|
4
|
+

|
|
5
|
+

|
|
5
6
|
|
|
6
7
|
`sveld` generates TypeScript definitions for Svelte components by statically analyzing their props, events, slots and more. Prop types and signatures can be defined using [JSDoc notation](https://jsdoc.app/). This documentation generator can also emit component documentation in Markdown and JSON output formats.
|
|
7
8
|
|
|
@@ -13,7 +14,7 @@ The purpose of this project is to make third party Svelte component libraries co
|
|
|
13
14
|
- [Component Index](https://github.com/IBM/carbon-components-svelte/blob/master/COMPONENT_INDEX.md): Markdown file documenting component props, slots, and events
|
|
14
15
|
- [Component API](https://github.com/IBM/carbon-components-svelte/blob/master/docs/src/COMPONENT_API.json): Component API metadata in JSON format
|
|
15
16
|
|
|
16
|
-
**Please note** that the generated TypeScript definitions require [Svelte version 3.31](https://github.com/sveltejs/svelte/blob/master/CHANGELOG.md#
|
|
17
|
+
**Please note** that the generated TypeScript definitions require [Svelte version 3.31](https://github.com/sveltejs/svelte/blob/master/CHANGELOG.md#3310) or greater.
|
|
17
18
|
|
|
18
19
|
---
|
|
19
20
|
|
|
@@ -399,13 +400,45 @@ Signature:
|
|
|
399
400
|
Example:
|
|
400
401
|
|
|
401
402
|
```js
|
|
402
|
-
/** @extends {"./Button"} ButtonProps */
|
|
403
|
+
/** @extends {"./Button.svelte"} ButtonProps */
|
|
403
404
|
|
|
404
405
|
export const secondary = true;
|
|
405
406
|
|
|
406
407
|
import Button from "./Button.svelte";
|
|
407
408
|
```
|
|
408
409
|
|
|
410
|
+
### `@component` comments
|
|
411
|
+
|
|
412
|
+
The Svelte Language Server supports component-level comments through the following syntax: `<!-- @component [comment] -->`.
|
|
413
|
+
|
|
414
|
+
`sveld` will copy these over to the exported default component in the TypeScript definition.
|
|
415
|
+
|
|
416
|
+
Example:
|
|
417
|
+
|
|
418
|
+
```svelte
|
|
419
|
+
<!-- @component
|
|
420
|
+
@example
|
|
421
|
+
<Button>
|
|
422
|
+
Text
|
|
423
|
+
</Button>
|
|
424
|
+
-->
|
|
425
|
+
<button>
|
|
426
|
+
<slot />
|
|
427
|
+
</button>
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
Output:
|
|
431
|
+
|
|
432
|
+
```ts
|
|
433
|
+
/**
|
|
434
|
+
* @example
|
|
435
|
+
* <Button>
|
|
436
|
+
* Text
|
|
437
|
+
* </Button>
|
|
438
|
+
*/
|
|
439
|
+
export default class Button extends SvelteComponentTyped<ButtonProps, {}, { default: {} }> {}
|
|
440
|
+
```
|
|
441
|
+
|
|
409
442
|
## Contributing
|
|
410
443
|
|
|
411
444
|
Refer to the [contributing guidelines](CONTRIBUTING.md).
|
|
@@ -416,5 +449,3 @@ Refer to the [contributing guidelines](CONTRIBUTING.md).
|
|
|
416
449
|
|
|
417
450
|
[npm]: https://img.shields.io/npm/v/sveld.svg?color=262626&style=for-the-badge
|
|
418
451
|
[npm-url]: https://npmjs.com/package/sveld
|
|
419
|
-
[build]: https://img.shields.io/travis/com/ibm/sveld?color=24a148&style=for-the-badge
|
|
420
|
-
[build-badge]: https://travis-ci.com/ibm/sveld
|
package/lib/ComponentParser.d.ts
CHANGED
|
@@ -58,6 +58,7 @@ export interface ParsedComponent {
|
|
|
58
58
|
typedefs: TypeDef[];
|
|
59
59
|
rest_props: RestProps;
|
|
60
60
|
extends?: Extends;
|
|
61
|
+
componentComment?: string;
|
|
61
62
|
}
|
|
62
63
|
export default class ComponentParser {
|
|
63
64
|
private options?;
|
|
@@ -65,6 +66,7 @@ export default class ComponentParser {
|
|
|
65
66
|
private compiled?;
|
|
66
67
|
private rest_props?;
|
|
67
68
|
private extends?;
|
|
69
|
+
private componentComment?;
|
|
68
70
|
private readonly reactive_vars;
|
|
69
71
|
private readonly props;
|
|
70
72
|
private readonly slots;
|
package/lib/ComponentParser.js
CHANGED
|
@@ -160,6 +160,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
160
160
|
this.compiled = undefined;
|
|
161
161
|
this.rest_props = undefined;
|
|
162
162
|
this["extends"] = undefined;
|
|
163
|
+
this.componentComment = undefined;
|
|
163
164
|
this.reactive_vars.clear();
|
|
164
165
|
this.props.clear();
|
|
165
166
|
this.slots.clear();
|
|
@@ -182,7 +183,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
182
183
|
var callees = [];
|
|
183
184
|
(0, compiler_1.walk)(this.compiled.ast, {
|
|
184
185
|
enter: function (node, parent, prop) {
|
|
185
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
186
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
186
187
|
if (node.type === "CallExpression") {
|
|
187
188
|
if (node.callee.name === "createEventDispatcher") {
|
|
188
189
|
dispatcher_name = parent === null || parent === void 0 ? void 0 : parent.id.name;
|
|
@@ -201,7 +202,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
201
202
|
}
|
|
202
203
|
}
|
|
203
204
|
if (node.type === "ExportNamedDeclaration" && node.declaration != null) {
|
|
204
|
-
var
|
|
205
|
+
var _l = node.declaration.declarations ? node.declaration.declarations[0] : node.declaration, declaration_type = _l.type, id = _l.id, init = _l.init, body = _l.body;
|
|
205
206
|
var prop_name = id.name;
|
|
206
207
|
var value = undefined;
|
|
207
208
|
var type = undefined;
|
|
@@ -264,8 +265,14 @@ var ComponentParser = /** @class */ (function () {
|
|
|
264
265
|
reactive: _this.reactive_vars.has(prop_name)
|
|
265
266
|
});
|
|
266
267
|
}
|
|
268
|
+
if (node.type === "Comment") {
|
|
269
|
+
var data = (_h = (_g = node === null || node === void 0 ? void 0 : node.data) === null || _g === void 0 ? void 0 : _g.trim()) !== null && _h !== void 0 ? _h : "";
|
|
270
|
+
if (/^@component/.test(data)) {
|
|
271
|
+
_this.componentComment = data.replace(/^@component/, "");
|
|
272
|
+
}
|
|
273
|
+
}
|
|
267
274
|
if (node.type === "Slot") {
|
|
268
|
-
var slot_name = (
|
|
275
|
+
var slot_name = (_j = node.attributes.find(function (attr) { return attr.name === "name"; })) === null || _j === void 0 ? void 0 : _j.value[0].data;
|
|
269
276
|
var slot_props = node.attributes
|
|
270
277
|
.filter(function (attr) { return attr.name !== "name"; })
|
|
271
278
|
.reduce(function (slot_props, _a) {
|
|
@@ -302,7 +309,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
302
309
|
}
|
|
303
310
|
return __assign(__assign({}, slot_props), (_b = {}, _b[name] = slot_prop_value, _b));
|
|
304
311
|
}, {});
|
|
305
|
-
var fallback = (
|
|
312
|
+
var fallback = (_k = node.children) === null || _k === void 0 ? void 0 : _k.map(function (_a) {
|
|
306
313
|
var start = _a.start, end = _a.end;
|
|
307
314
|
return _this.sourceAtPos(start, end);
|
|
308
315
|
}).join("").trim();
|
|
@@ -387,7 +394,8 @@ var ComponentParser = /** @class */ (function () {
|
|
|
387
394
|
events: ComponentParser.mapToArray(this.events),
|
|
388
395
|
typedefs: ComponentParser.mapToArray(this.typedefs),
|
|
389
396
|
rest_props: this.rest_props,
|
|
390
|
-
"extends": this["extends"]
|
|
397
|
+
"extends": this["extends"],
|
|
398
|
+
componentComment: this.componentComment
|
|
391
399
|
};
|
|
392
400
|
};
|
|
393
401
|
return ComponentParser;
|
|
@@ -148,15 +148,25 @@ function genImports(def) {
|
|
|
148
148
|
return "";
|
|
149
149
|
return "import { " + def["extends"].interface + " } from " + def["extends"]["import"] + ";";
|
|
150
150
|
}
|
|
151
|
+
function genComponentComment(def) {
|
|
152
|
+
if (!def.componentComment)
|
|
153
|
+
return "";
|
|
154
|
+
if (!/\n/.test(def.componentComment))
|
|
155
|
+
return "/** " + def.componentComment.trim() + " */";
|
|
156
|
+
return "/*" + def.componentComment
|
|
157
|
+
.split("\n")
|
|
158
|
+
.map(function (line) { return "* " + line; })
|
|
159
|
+
.join("\n") + "\n*/";
|
|
160
|
+
}
|
|
151
161
|
function writeTsDefinition(component) {
|
|
152
|
-
var moduleName = component.moduleName, typedefs = component.typedefs, props = component.props, slots = component.slots, events = component.events, rest_props = component.rest_props, _extends = component["extends"];
|
|
162
|
+
var moduleName = component.moduleName, typedefs = component.typedefs, props = component.props, slots = component.slots, events = component.events, rest_props = component.rest_props, _extends = component["extends"], componentComment = component.componentComment;
|
|
153
163
|
var _a = genPropDef({
|
|
154
164
|
moduleName: moduleName,
|
|
155
165
|
props: props,
|
|
156
166
|
rest_props: rest_props,
|
|
157
167
|
"extends": _extends
|
|
158
168
|
}), props_name = _a.props_name, prop_def = _a.prop_def;
|
|
159
|
-
return "\n /// <reference types=\"svelte\" />\n import { SvelteComponentTyped } from \"svelte\";\n " + genImports({ "extends": _extends }) + "\n " + getTypeDefs({ typedefs: typedefs }) + "\n " + prop_def + "\n\n export default class " + (moduleName === "default" ? "" : moduleName) + " extends SvelteComponentTyped<\n " + props_name + ",\n {" + genEventDef({ events: events }) + "},\n {" + genSlotDef({ slots: slots }) + "}\n > {\n " + genAccessors({ props: props }) + "\n }";
|
|
169
|
+
return "\n /// <reference types=\"svelte\" />\n import { SvelteComponentTyped } from \"svelte\";\n " + genImports({ "extends": _extends }) + "\n " + getTypeDefs({ typedefs: typedefs }) + "\n " + prop_def + "\n " + genComponentComment({ componentComment: componentComment }) + "\n export default class " + (moduleName === "default" ? "" : moduleName) + " extends SvelteComponentTyped<\n " + props_name + ",\n {" + genEventDef({ events: events }) + "},\n {" + genSlotDef({ slots: slots }) + "}\n > {\n " + genAccessors({ props: props }) + "\n }";
|
|
160
170
|
}
|
|
161
171
|
exports.writeTsDefinition = writeTsDefinition;
|
|
162
172
|
function writeTsDefinitions(components, options) {
|