Skip to content

Commit

Permalink
Mark accessors as required properties (#35)
Browse files Browse the repository at this point in the history
* fix(writer): mark accessors as required properties

* test: add function declaration to integration test

* chore(writer): clean up genAccessors
  • Loading branch information
metonym authored Jul 10, 2021
1 parent c9af2fd commit 61f827d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 21 deletions.
10 changes: 10 additions & 0 deletions integration/multi-export/COMPONENT_API.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
"isFunctionDeclaration": false,
"constant": false,
"reactive": false
},
{
"name": "print",
"kind": "function",
"type": "() => any",
"value": "() => { console.log(0); }",
"isFunction": true,
"isFunctionDeclaration": true,
"constant": false,
"reactive": false
}
],
"slots": [
Expand Down
9 changes: 5 additions & 4 deletions integration/multi-export/COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

### Props

| Prop name | Kind | Reactive | Type | Default value | Description |
| :-------- | :--------------- | :------- | :------------------- | --------------------- | ----------- |
| type | <code>let</code> | No | <code>string</code> | <code>"button"</code> | -- |
| primary | <code>let</code> | No | <code>boolean</code> | <code>false</code> | -- |
| Prop name | Kind | Reactive | Type | Default value | Description |
| :-------- | :-------------------- | :------- | :--------------------- | -------------------------------------- | ----------- |
| type | <code>let</code> | No | <code>string</code> | <code>"button"</code> | -- |
| primary | <code>let</code> | No | <code>boolean</code> | <code>false</code> | -- |
| print | <code>function</code> | No | <code>() => any</code> | <code>() => { console.log(0); }</code> | -- |

### Slots

Expand Down
3 changes: 3 additions & 0 deletions integration/multi-export/src/Button.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script>
export let type = "button";
export let primary = false;
export function print() {
console.log(0);
}
</script>

<button {...$$restProps} {type} class:primary on:click>
Expand Down
4 changes: 3 additions & 1 deletion integration/multi-export/types/Button.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export default class Button extends SvelteComponentTyped<
ButtonProps,
{ click: WindowEventMap["click"] },
{ default: {} }
> {}
> {
print: () => any;
}
14 changes: 2 additions & 12 deletions src/writer/writer-ts-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,11 @@ function genAccessors(def: Pick<ComponentDocApi, "props">) {
return def.props
.filter((prop) => prop.isFunctionDeclaration)
.map((prop) => {
const prop_comments = [
addCommentLine(prop.description?.replace(/\n/g, "\n* ")),
/* addCommentLine(
prop.value,
`@default ${typeof prop.value === "string" ? prop.value.replace(/\s+/g, " ") : prop.value}`
), */
]
.filter(Boolean)
.join("");

let prop_value = /* prop.constant && !prop.isFunction ? prop.value : */ prop.type;
const prop_comments = [addCommentLine(prop.description?.replace(/\n/g, "\n* "))].filter(Boolean).join("");

return `
${prop_comments.length > 0 ? `/**\n${prop_comments}*/` : EMPTY_STR}
${prop.name}?: ${prop_value};`;
${prop.name}: ${prop.type};`;
})
.join("\n");
}
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/function-declaration/output.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export interface InputProps {
}

export default class Input extends SvelteComponentTyped<InputProps, {}, {}> {
add?: () => any;
add: () => any;

/**
* Multiplies two numbers
*/
multiply?: (a: number, b: number) => number;
multiply: (a: number, b: number) => number;
}
2 changes: 1 addition & 1 deletion tests/snapshots/infer-basic/output.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export interface InputProps {
}

export default class Input extends SvelteComponentTyped<InputProps, {}, { default: {} }> {
fn?: () => any;
fn: () => any;
}
2 changes: 1 addition & 1 deletion tests/snapshots/infer-with-types/output.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export interface InputProps {
}

export default class Input extends SvelteComponentTyped<InputProps, {}, { default: {} }> {
fn?: () => any;
fn: () => any;
}

0 comments on commit 61f827d

Please sign in to comment.