Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ts): improve type inference for attr tags #295

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/great-cars-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@marko/language-server": patch
"@marko/type-check": patch
"marko-vscode": patch
---

Improve type inference for repeated attr tags
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@
9 |
```

### Ln 10, Col 4
```marko
8 | </>
9 |
> 10 | <@item>
| ^^^^^ Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<{ x: number; renderBody?: Body<[], void> | undefined; }>'.
Property 'x' is missing in type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' but required in type '{ x: number; renderBody?: Body<[], void> | undefined; }'.
11 | Hello!
12 | </>
13 | </test-tag>
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Hovers
### Ln 6, Col 7
```marko
4 | <@foo bar/>
5 | <@foo|data|>
> 6 | ${data}
| ^ (parameter) data: {
a: string;
b: number;
}
7 | //^?
8 | </@foo>
9 | </child>
```

### Ln 14, Col 7
```marko
12 | <@foo bar/>
13 | <@foo|data|>
> 14 | ${data}
| ^ (parameter) data: {
a: string;
b: number;
}
15 | //^?
16 | </@foo>
17 | </Child>
```

### Ln 23, Col 7
```marko
21 | <@foo bar/>
22 | <@foo|data|>
> 23 | ${data}
| ^ (parameter) data: any
24 | //^?
25 | </@foo>
26 | </>
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @typedef {{
* foo: Marko.AttrTag<{
* bar?: boolean;
* renderBody?: Marko.Body<[{a: string, b: number}]>
* }>
* }} Input
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Child from "<child>";

<child>
<@foo bar/>
<@foo|data|>
${data}
//^?
</@foo>
</child>

<Child>
<@foo bar/>
<@foo|data|>
${data}
//^?
</@foo>
</Child>

// This errors for now, because `typeof (some + expression)` is not allowed
<${true && Child}>
<@foo bar/>
<@foo|data|>
${data}
//^?
</@foo>
</>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"script-lang": "js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Hovers
### Ln 6, Col 7
```marko
4 | <@foo bar/>
5 | <@foo|data|>
> 6 | ${data}
| ^ (parameter) data: {
a: string;
b: number;
}
7 | //^?
8 | </@foo>
9 | </child>
```

### Ln 14, Col 7
```marko
12 | <@foo bar/>
13 | <@foo|data|>
> 14 | ${data}
| ^ (parameter) data: {
a: string;
b: number;
}
15 | //^?
16 | </@foo>
17 | </Child>
```

### Ln 23, Col 7
```marko
21 | <@foo bar/>
22 | <@foo|data|>
> 23 | ${data}
| ^ (parameter) data: any
24 | //^?
25 | </@foo>
26 | </>
```

## Diagnostics
### Ln 22, Col 9
```marko
20 | <${true && Child}>
21 | <@foo bar/>
> 22 | <@foo|data|>
| ^^^^ Parameter 'data' implicitly has an 'any' type.
23 | ${data}
24 | //^?
25 | </@foo>
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Input {
foo: Marko.AttrTag<{
bar?: boolean;
renderBody?: Marko.Body<[{a: string, b: number}]>
}>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Child from "<child>";

<child>
<@foo bar/>
<@foo|data|>
${data}
//^?
</@foo>
</child>

<Child>
<@foo bar/>
<@foo|data|>
${data}
//^?
</@foo>
</Child>

// This errors for now, because `typeof (some + expression)` is not allowed
<${true && Child}>
<@foo bar/>
<@foo|data|>
${data}
//^?
</@foo>
</>
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
## Hovers
### Ln 3, Col 7
```marko
1 | <comments>
2 | <@comment#a>
> 3 | <@comment#b>
| ^ (property) "@comment": Marko.AttrTag<Comment> | undefined
4 | // ^?
5 | <let/b = "b" as const/>
6 | </@comment>
```

### Ln 15, Col 3
```marko
13 |
Expand Down Expand Up @@ -31,16 +42,3 @@
21 | }/>
```

## Diagnostics
### Ln 2, Col 4
```marko
1 | <comments>
> 2 | <@comment#a>
| ^^^^^^^^ Type '{ comment: { id: string; renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }; renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; id: string; } | { renderBody: () => MarkoReturn<...>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<Comment>'.
Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<Comment>'.
Property 'id' is missing in type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' but required in type 'Comment'.
3 | <@comment#b>
4 | // ^?
5 | <let/b = "b" as const/>
```

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</@comment>
<let/a = "a" as const/>
</@comment>
<@comment>
<@comment#c>
<let/c = "c" as const/>
</@comment>
</comments>
Expand Down
28 changes: 23 additions & 5 deletions packages/language-tools/src/extractors/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ constructor(_?: Return) {}
#writeTag(tag: Node.Tag) {
const tagName = tag.nameText;
const renderId = this.#getRenderId(tag);
let nestedTagType: string | undefined;

if (renderId) {
this.#extractor.write(
Expand All @@ -709,6 +710,10 @@ constructor(_?: Return) {}

if (def) {
const importPath = resolveTagImport(this.#filename, def);
const isMarkoFile = importPath?.endsWith(".marko");
if (isMarkoFile) {
nestedTagType = `import("${importPath}").Input`;
}
const renderer = importPath?.endsWith(".marko")
? `renderTemplate(import("${importPath}"))`
: def.html
Expand All @@ -727,6 +732,7 @@ constructor(_?: Return) {}
this.#extractor.write(varShared(renderer));
}
} else if (REG_TAG_NAME_IDENTIFIER.test(tagName)) {
nestedTagType = `Marko.Input<typeof ${this.#read(tag.name)}>`;
this.#extractor
.write(`${varShared("renderDynamicTag")}(\n`)
.copy(tag.name)
Expand All @@ -746,7 +752,7 @@ constructor(_?: Return) {}
this.#extractor.write("()()(");
}

this.#writeTagInputObject(tag);
this.#writeTagInputObject(tag, nestedTagType);

if (renderId) {
this.#extractor.write(`)`);
Expand Down Expand Up @@ -1035,6 +1041,7 @@ constructor(_?: Return) {}
#writeAttrTags(
{ staticAttrTags, dynamicAttrTagParents }: ProcessedBody,
inMerge: boolean,
nestedTagType?: string,
) {
let wasMerge = false;

Expand All @@ -1051,7 +1058,7 @@ constructor(_?: Return) {}
}

if (staticAttrTags) {
this.#writeStaticAttrTags(staticAttrTags, inMerge);
this.#writeStaticAttrTags(staticAttrTags, inMerge, nestedTagType);
if (dynamicAttrTagParents)
this.#extractor.write(`}${SEP_COMMA_NEW_LINE}`);
}
Expand All @@ -1065,6 +1072,7 @@ constructor(_?: Return) {}
#writeStaticAttrTags(
staticAttrTags: Exclude<ProcessedBody["staticAttrTags"], undefined>,
wasMerge: boolean,
nestedTagType?: string,
) {
if (!wasMerge) this.#extractor.write("...{");
this.#extractor.write(
Expand Down Expand Up @@ -1099,7 +1107,13 @@ constructor(_?: Return) {}
this.#extractor.write("]: ");

if (isRepeated) {
this.#extractor.write(`${varShared("repeatedAttrTag")}(\n`);
this.#extractor.write(`${varShared("repeatedAttrTag")}(\n...\n`);
if (nestedTagType && this.#scriptLang === ScriptLang.js) {
this.#extractor.write(
`/** @satisfies {${nestedTagType}["${name}"][]} */\n`,
);
}
this.#extractor.write(`([\n`);
}

for (const childNode of attrTag) {
Expand All @@ -1108,6 +1122,10 @@ constructor(_?: Return) {}
}

if (isRepeated) {
this.#extractor.write("])");
if (nestedTagType && this.#scriptLang === ScriptLang.ts) {
this.#extractor.write(` satisfies ${nestedTagType}["${name}"][]`);
}
this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
}
}
Expand Down Expand Up @@ -1194,7 +1212,7 @@ constructor(_?: Return) {}
}
}

#writeTagInputObject(tag: Node.ParentTag) {
#writeTagInputObject(tag: Node.ParentTag, nestedTagType?: string) {
if (!tag.params) this.#writeComments(tag);

let hasInput = false;
Expand Down Expand Up @@ -1227,7 +1245,7 @@ constructor(_?: Return) {}
let hasRenderBody = false;
if (body) {
hasInput = true;
this.#writeAttrTags(body, false);
this.#writeAttrTags(body, false, nestedTagType);
hasRenderBody = body.renderBody !== undefined;
} else if (tag.close) {
hasRenderBody = true;
Expand Down
Loading