From e118ab02cbc006b1fe73bab8ab8fe221d36136ca Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 18 Apr 2024 15:59:28 +0200 Subject: [PATCH] mark exports as "can only bind to them" --- .../fixtures/bind-this/expected_svelte_5.json | 2 +- .../fixtures/bindings/Legacy.svelte | 6 ++++ .../fixtures/bindings/Runes.svelte | 7 ++++ .../fixtures/bindings/expected_svelte_5.json | 36 +++++++++++++++++++ .../fixtures/bindings/expectedv2.json | 1 + .../fixtures/bindings/input.svelte | 25 +++++++++++++ .../src/svelte2tsx/nodes/ExportedNames.ts | 22 +++++++++--- .../expected/TestRunes.svelte.d.ts | 4 +-- .../expectedv2.ts | 2 +- .../expectedv2.ts | 2 +- .../expectedv2.ts | 2 +- .../expectedv2.ts | 2 +- 12 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Legacy.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Runes.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expected_svelte_5.json create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/input.svelte diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bind-this/expected_svelte_5.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bind-this/expected_svelte_5.json index 02e74721f..09171f654 100644 --- a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bind-this/expected_svelte_5.json +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bind-this/expected_svelte_5.json @@ -36,7 +36,7 @@ }, "severity": 1, "source": "ts", - "message": "Type 'Component' is not assignable to type 'OtherComponent'.\n Types of property '$$prop_def' are incompatible.\n Type 'StripBindable>' is not assignable to type 'StripBindable>'.\n Type 'StripBindable>' is not assignable to type 'StripBindable>'.", + "message": "Type 'Component' is not assignable to type 'OtherComponent'.\n The types of '$$prop_def.prop' are incompatible between these types.\n Type 'boolean' is not assignable to type 'string'.", "code": 2322, "tags": [] }, diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Legacy.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Legacy.svelte new file mode 100644 index 000000000..1bd159644 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Legacy.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Runes.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Runes.svelte new file mode 100644 index 000000000..34b9e88d7 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/Runes.svelte @@ -0,0 +1,7 @@ + diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expected_svelte_5.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expected_svelte_5.json new file mode 100644 index 000000000..8c6a50806 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expected_svelte_5.json @@ -0,0 +1,36 @@ +[ + { + "code": 2322, + "message": "Type 'Binding' is not assignable to type 'string'.", + "range": { + "end": { + "character": 20, + "line": 23 + }, + "start": { + "character": 12, + "line": 23 + } + }, + "severity": 1, + "source": "ts", + "tags": [] + }, + { + "code": 2322, + "message": "Type '() => boolean' is not assignable to type 'Binding<() => boolean>'.", + "range": { + "end": { + "character": 17, + "line": 24 + }, + "start": { + "character": 8, + "line": 24 + } + }, + "severity": 1, + "source": "ts", + "tags": [] + } +] diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expectedv2.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expectedv2.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expectedv2.json @@ -0,0 +1 @@ +[] diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/input.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/input.svelte new file mode 100644 index 000000000..3978c6f17 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/input.svelte @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts index a99808643..a66330012 100644 --- a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts +++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts @@ -610,7 +610,12 @@ export class ExportedNames { ? `__sveltets_2_Bindings<${this.$props.type}, ${this.$props.bindings.map((b) => `"${b}"`).join('|')}>` : this.$props.type) + (others.length - ? ' & { ' + this.createReturnElementsType(others).join(',') + ' }' + ? ' & { ' + + this.createReturnElementsType(others, undefined, [ + 'import("svelte").Binding<', + '>' + ]).join(',') + + ' }' : '') ); } @@ -630,7 +635,7 @@ export class ExportedNames { return ( this.$props.comment.slice(0, idx) + (others.length > 0 - ? `{${this.createReturnElementsType(others, false)}} & ` + ? `{${this.createReturnElementsType(others, false, ['import("svelte").Binding<', '>'])}} & ` : '') + (has_bindings ? '__sveltets_2_Bindings<' : '') + this.$props.comment.slice(idx, end) + @@ -700,16 +705,23 @@ export class ExportedNames { }); } - private createReturnElementsType(names: Array<[string, ExportedName]>, addDoc = true) { + private createReturnElementsType( + names: Array<[string, ExportedName]>, + addDoc = true, + wrapWith?: [string, string] + ) { + const wrap = wrapWith + ? (str: string) => `${wrapWith[0]}${str}${wrapWith[1]}` + : (str: string) => str; return names.map(([key, value]) => { const identifier = `${value.doc && addDoc ? `\n${value.doc}` : ''}${ value.identifierText || key }${value.required ? '' : '?'}`; if (!value.type) { - return `${identifier}: typeof ${key}`; + return `${identifier}: ${wrap(`typeof ${key}`)}`; } - return `${identifier}: ${value.type}`; + return `${identifier}: ${wrap(value.type)}`; }); } diff --git a/packages/svelte2tsx/test/emitDts/samples/typescript-runes.v5/expected/TestRunes.svelte.d.ts b/packages/svelte2tsx/test/emitDts/samples/typescript-runes.v5/expected/TestRunes.svelte.d.ts index 98230cac5..619e8bd8a 100644 --- a/packages/svelte2tsx/test/emitDts/samples/typescript-runes.v5/expected/TestRunes.svelte.d.ts +++ b/packages/svelte2tsx/test/emitDts/samples/typescript-runes.v5/expected/TestRunes.svelte.d.ts @@ -3,7 +3,7 @@ declare const __propDef: { props: { foo: string; bar?: import("svelte").Bindable; - baz?: () => void; + baz?: import("svelte").Binding<() => void>; }; events: { [evt: string]: CustomEvent; @@ -17,7 +17,7 @@ export default class TestRunes extends SvelteComponent; - baz?: () => void; + baz?: import("svelte").Binding<() => void>; }>); get baz(): () => void; } diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune-no-changes/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune-no-changes/expectedv2.ts index a6fd870bf..9f526ad45 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune-no-changes/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune-no-changes/expectedv2.ts @@ -7,7 +7,7 @@ const snapshot = {}; ; async () => {}; -return { props: /** @type {{snapshot?: typeof snapshot} & $$ComponentProps} */({}), slots: {}, events: {} }} +return { props: /** @type {{snapshot?: import("svelte").Binding} & $$ComponentProps} */({}), slots: {}, events: {} }} export default class Page__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['snapshot'], __sveltets_2_with_any_event(render()))) { constructor(options = __sveltets_2_runes_constructor(__sveltets_2_partial(['snapshot'], __sveltets_2_with_any_event(render())))) { super(options); } diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune/expectedv2.ts index b0b79ef83..ee256ab79 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/sveltekit-autotypes-$props-rune/expectedv2.ts @@ -5,7 +5,7 @@ const snapshot/*Ωignore_startΩ*/: import('./$types.js').Snapshot/*Ωignore_endΩ*/ = {}; ; async () => {}; -return { props: /** @type {{snapshot?: typeof snapshot} & $$ComponentProps} */({}), slots: {}, events: {} }} +return { props: /** @type {{snapshot?: import("svelte").Binding} & $$ComponentProps} */({}), slots: {}, events: {} }} export default class Page__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(['snapshot'], __sveltets_2_with_any_event(render()))) { constructor(options = __sveltets_2_runes_constructor(__sveltets_2_partial(['snapshot'], __sveltets_2_with_any_event(render())))) { super(options); } diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune-unchanged/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune-unchanged/expectedv2.ts index 42c9a6030..0b709e340 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune-unchanged/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune-unchanged/expectedv2.ts @@ -5,7 +5,7 @@ let { form, data }:$$ComponentProps = $props(); ; async () => {}; -return { props: {} as any as $$ComponentProps & { snapshot?: any }, slots: {}, events: {} }} +return { props: {} as any as $$ComponentProps & { snapshot?: import("svelte").Binding }, slots: {}, events: {} }} export default class Page__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) { constructor(options = __sveltets_2_runes_constructor(__sveltets_2_with_any_event(render()))) { super(options); } diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune/expectedv2.ts index 3be5dd593..72746c967 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune/expectedv2.ts @@ -5,7 +5,7 @@ let { form, data }: $$ComponentProps = $props(); ; async () => {}; -return { props: {} as any as $$ComponentProps & { snapshot?: typeof snapshot }, slots: {}, events: {} }} +return { props: {} as any as $$ComponentProps & { snapshot?: import("svelte").Binding }, slots: {}, events: {} }} export default class Page__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) { constructor(options = __sveltets_2_runes_constructor(__sveltets_2_with_any_event(render()))) { super(options); }