From abbb30ae2343aa46e8c9d10040e9082ad4b32cea Mon Sep 17 00:00:00 2001 From: dpiercey Date: Tue, 8 Oct 2024 13:57:20 -0700 Subject: [PATCH] fix: improve renderBody codegen --- .changeset/swift-pumas-dream.md | 8 +++++ .../custom-tag-args.expected/index.md | 19 +++++++--- .../custom-tag-args/components/test-tag.marko | 2 +- .../script/custom-tag-args/index.marko | 2 +- .../components/my-tag/index.marko | 5 --- .../components/test-tag.marko | 2 +- .../components/test-tag.md | 19 +++++----- .../components/test-tag.marko | 6 ++-- packages/language-tools/marko.internal.d.ts | 35 +++++++------------ .../src/extractors/script/index.ts | 32 ++++++++--------- 10 files changed, 63 insertions(+), 67 deletions(-) create mode 100644 .changeset/swift-pumas-dream.md diff --git a/.changeset/swift-pumas-dream.md b/.changeset/swift-pumas-dream.md new file mode 100644 index 00000000..ba57c584 --- /dev/null +++ b/.changeset/swift-pumas-dream.md @@ -0,0 +1,8 @@ +--- +"@marko/language-server": patch +"@marko/language-tools": patch +"@marko/type-check": patch +"marko-vscode": patch +--- + +Align tag arguments type generation with Marko 6. diff --git a/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/__snapshots__/custom-tag-args.expected/index.md b/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/__snapshots__/custom-tag-args.expected/index.md index 6cd804c7..2a3becde 100644 --- a/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/__snapshots__/custom-tag-args.expected/index.md +++ b/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/__snapshots__/custom-tag-args.expected/index.md @@ -1,11 +1,20 @@ ## Diagnostics -### Ln 3, Col 10 +### Ln 2, Col 11 ```marko - 1 | - 2 | + 1 | +> 2 | + | ^^^^^^^^ Argument of type 'string' is not assignable to parameter of type 'Directives & Input'. + Type 'string' is not assignable to type 'Input'. + 3 | + 4 | +``` + +### Ln 3, Col 21 +```marko + 1 | + 2 | > 3 | - | ^^^^^^^^^^^^^^^^ Type of computed property's value is '[string, number, number]', which is not assignable to type '[string, (number | undefined)?]'. - Source has 3 element(s) but target allows only 2. + | ^^^^ Expected 1 arguments, but got 3. 4 | ``` diff --git a/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/components/test-tag.marko b/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/components/test-tag.marko index 90bc8c18..24046b6a 100644 --- a/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/components/test-tag.marko +++ b/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/components/test-tag.marko @@ -1,4 +1,4 @@ export interface Input { - value: [string, number?] + value: string }
diff --git a/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/index.marko b/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/index.marko index cd1e097f..9030b177 100644 --- a/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/index.marko +++ b/packages/language-server/src/__tests__/fixtures/script/custom-tag-args/index.marko @@ -1,3 +1,3 @@ + - diff --git a/packages/language-server/src/__tests__/fixtures/script/render-body-basic/components/my-tag/index.marko b/packages/language-server/src/__tests__/fixtures/script/render-body-basic/components/my-tag/index.marko index e27336b2..38b5a925 100644 --- a/packages/language-server/src/__tests__/fixtures/script/render-body-basic/components/my-tag/index.marko +++ b/packages/language-server/src/__tests__/fixtures/script/render-body-basic/components/my-tag/index.marko @@ -2,9 +2,4 @@ export interface Input { renderBody: Marko.Body<[string]> } -<${input.renderBody} value=["hi"] /> - -<${input.renderBody}=["hi"] /> - <${input.renderBody}("hi") /> - diff --git a/packages/language-server/src/__tests__/fixtures/script/return-tag-nested/components/test-tag.marko b/packages/language-server/src/__tests__/fixtures/script/return-tag-nested/components/test-tag.marko index 5473a9ee..fc1bc6ae 100644 --- a/packages/language-server/src/__tests__/fixtures/script/return-tag-nested/components/test-tag.marko +++ b/packages/language-server/src/__tests__/fixtures/script/return-tag-nested/components/test-tag.marko @@ -2,4 +2,4 @@ export interface Input { renderBody: Marko.Body<["a"], { value: "a" | "b" }> } -<${input.renderBody}=["a"] /> +<${input.renderBody}("a") /> diff --git a/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/__snapshots__/tag-params-basic.expected/components/test-tag.md b/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/__snapshots__/tag-params-basic.expected/components/test-tag.md index b34e96dd..c50c1060 100644 --- a/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/__snapshots__/tag-params-basic.expected/components/test-tag.md +++ b/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/__snapshots__/tag-params-basic.expected/components/test-tag.md @@ -1,22 +1,21 @@ ## Diagnostics -### Ln 7, Col 28 +### Ln 7, Col 27 ```marko - 5 | <${input.renderBody}=["a", "b"] /> + 5 | <${input.renderBody}("a", "b") /> 6 | -> 7 | <${input.renderBody}=["a", "c"] /> - | ^^^ Type '"c"' is not assignable to type '"b"'. +> 7 | <${input.renderBody}("a", "c") /> + | ^^^ Argument of type '"c"' is not assignable to parameter of type '"b"'. 8 | - 9 | <${input.renderBody}=["a", "b", "c"] /> + 9 | <${input.renderBody}("a", "b", "c") /> 10 | ``` -### Ln 9, Col 21 +### Ln 9, Col 32 ```marko - 7 | <${input.renderBody}=["a", "c"] /> + 7 | <${input.renderBody}("a", "c") /> 8 | -> 9 | <${input.renderBody}=["a", "b", "c"] /> - | ^ Type '["a", "b", string]' is not assignable to type '["a", "b"]'. - Source has 3 element(s) but target allows only 2. +> 9 | <${input.renderBody}("a", "b", "c") /> + | ^^^ Expected 2 arguments, but got 3. 10 | ``` diff --git a/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/components/test-tag.marko b/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/components/test-tag.marko index fa73ae6f..4ac9cd34 100644 --- a/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/components/test-tag.marko +++ b/packages/language-server/src/__tests__/fixtures/script/tag-params-basic/components/test-tag.marko @@ -2,8 +2,8 @@ export interface Input { renderBody: Marko.Body<["a", "b"]> } -<${input.renderBody}=["a", "b"] /> +<${input.renderBody}("a", "b") /> -<${input.renderBody}=["a", "c"] /> +<${input.renderBody}("a", "c") /> -<${input.renderBody}=["a", "b", "c"] /> +<${input.renderBody}("a", "b", "c") /> diff --git a/packages/language-tools/marko.internal.d.ts b/packages/language-tools/marko.internal.d.ts index 20c69c34..86b1cc2a 100644 --- a/packages/language-tools/marko.internal.d.ts +++ b/packages/language-tools/marko.internal.d.ts @@ -306,14 +306,10 @@ declare global { ? NativeTagRenderer : [Name] extends [AnyMarkoBody] ? BodyRenderer - : [Name] extends [{ renderBody?: AnyMarkoBody }] - ? [Name["renderBody"]] extends [AnyMarkoBody] - ? BodyRenderer - : BaseRenderer< - RenderBodyInput< - BodyParameters> - > - > + : [Name] extends [ + { renderBody?: infer Name extends AnyMarkoBody }, + ] + ? BodyRenderer : DefaultRenderer; export type TemplateRenderer