From 7fe8a44bd837b14809aaa447b0aa0604b5e93325 Mon Sep 17 00:00:00 2001 From: Lyu Jason Date: Wed, 16 Feb 2022 23:08:31 +0800 Subject: [PATCH 1/3] (fix) regression with boolean props type inference Revert changes with it. Misread the condition. --- .../fixtures/boolean-literal-props/Component.svelte | 3 +++ .../fixtures/boolean-literal-props/expected.json | 1 + .../fixtures/boolean-literal-props/expectedv2.json | 1 + .../fixtures/boolean-literal-props/input.svelte | 5 +++++ .../svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts | 10 +++++++++- .../svelte2tsx/samples/ts-export-boolean/expected.tsx | 6 +++--- 6 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte new file mode 100644 index 000000000..2b3dd7309 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/Component.svelte @@ -0,0 +1,3 @@ + diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte new file mode 100644 index 000000000..af715571d --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/input.svelte @@ -0,0 +1,5 @@ + + + diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts index 8c2e7ee7a..7614f2658 100644 --- a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts +++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts @@ -111,7 +111,15 @@ export class ExportedNames { const jsDocType = ts.getJSDocType(declaration); const type = tsType || jsDocType; - if (!ts.isIdentifier(identifier) || !type) { + if ( + !ts.isIdentifier(identifier) || + (!type && + // Edge case: TS infers `export let bla = false` to type `false`. + // prevent that by adding the any-wrap in this case, too. + ![ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.TrueKeyword].includes( + declaration.initializer?.kind + )) + ) { return; } const name = identifier.getText(); diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx index 1dcc20599..7d1bf98ea 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expected.tsx @@ -1,11 +1,11 @@ /// <>;function render() { - let bla = false; - let blubb = true; + let bla = false/*Ωignore_startΩ*/;bla = __sveltets_1_any(bla);/*Ωignore_endΩ*/; + let blubb = true/*Ωignore_startΩ*/;blubb = __sveltets_1_any(blubb);/*Ωignore_endΩ*/; let bla1: boolean = false/*Ωignore_startΩ*/;bla1 = __sveltets_1_any(bla1);/*Ωignore_endΩ*/; let blubb1: boolean = true/*Ωignore_startΩ*/;blubb1 = __sveltets_1_any(blubb1);/*Ωignore_endΩ*/; - let a1 = true;let a2 = false;let b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/; + let a1 = true/*Ωignore_startΩ*/;a1 = __sveltets_1_any(a1);/*Ωignore_endΩ*/;let a2 = false/*Ωignore_startΩ*/;a2 = __sveltets_1_any(a2);/*Ωignore_endΩ*/;let b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/; ; () => (<>); return { props: {bla: bla , blubb: blubb , bla1: bla1 , blubb1: blubb1 , a1: a1 , a2: a2 , b1: b1 , b2: b2} as {bla?: typeof bla, blubb?: typeof blubb, bla1?: boolean, blubb1?: boolean, a1?: typeof a1, a2?: typeof a2, b1?: boolean, b2?: boolean}, slots: {}, getters: {}, events: {} }} From d6247cf6d094102008153dd12655a0fa5fe56f53 Mon Sep 17 00:00:00 2001 From: Lyu Jason Date: Wed, 16 Feb 2022 23:09:15 +0800 Subject: [PATCH 2/3] lint --- .../diagnostics/fixtures/boolean-literal-props/expected.json | 2 +- .../diagnostics/fixtures/boolean-literal-props/expectedv2.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json index 0637a088a..fe51488c7 100644 --- a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expected.json @@ -1 +1 @@ -[] \ No newline at end of file +[] diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json index 0637a088a..fe51488c7 100644 --- a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/boolean-literal-props/expectedv2.json @@ -1 +1 @@ -[] \ No newline at end of file +[] From 479a708b518a95cdaae7bd31dedd7893f9807b37 Mon Sep 17 00:00:00 2001 From: Lyu Jason Date: Wed, 16 Feb 2022 23:16:43 +0800 Subject: [PATCH 3/3] v2 expected --- .../test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts index 69a266350..e2cfdca2b 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/ts-export-boolean/expectedv2.ts @@ -1,11 +1,11 @@ /// ;function render() { - let bla = false; - let blubb = true; + let bla = false/*Ωignore_startΩ*/;bla = __sveltets_1_any(bla);/*Ωignore_endΩ*/; + let blubb = true/*Ωignore_startΩ*/;blubb = __sveltets_1_any(blubb);/*Ωignore_endΩ*/; let bla1: boolean = false/*Ωignore_startΩ*/;bla1 = __sveltets_1_any(bla1);/*Ωignore_endΩ*/; let blubb1: boolean = true/*Ωignore_startΩ*/;blubb1 = __sveltets_1_any(blubb1);/*Ωignore_endΩ*/; - let a1 = true;let a2 = false;let b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/; + let a1 = true/*Ωignore_startΩ*/;a1 = __sveltets_1_any(a1);/*Ωignore_endΩ*/;let a2 = false/*Ωignore_startΩ*/;a2 = __sveltets_1_any(a2);/*Ωignore_endΩ*/;let b1: boolean = true/*Ωignore_startΩ*/;b1 = __sveltets_1_any(b1);/*Ωignore_endΩ*/;let b2: boolean = false/*Ωignore_startΩ*/;b2 = __sveltets_1_any(b2);/*Ωignore_endΩ*/; ; async () => {}; return { props: {bla: bla , blubb: blubb , bla1: bla1 , blubb1: blubb1 , a1: a1 , a2: a2 , b1: b1 , b2: b2} as {bla?: typeof bla, blubb?: typeof blubb, bla1?: boolean, blubb1?: boolean, a1?: typeof a1, a2?: typeof a2, b1?: boolean, b2?: boolean}, slots: {}, getters: {}, events: {} }}