From c87a37094777727c88f6e0d57b18db0837e8e280 Mon Sep 17 00:00:00 2001 From: paoloricciuti Date: Mon, 9 Dec 2024 00:52:07 +0100 Subject: [PATCH 1/2] fix: allow exports with source from script module even if no bind is present --- .changeset/four-carrots-burn.md | 5 +++++ .../src/compiler/phases/2-analyze/index.js | 2 +- .../_config.js | 5 +++++ .../main.svelte | 3 +++ packages/svelte/tests/compiler-errors/test.ts | 22 +++++++++++++------ 5 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 .changeset/four-carrots-burn.md create mode 100644 packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js create mode 100644 packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte diff --git a/.changeset/four-carrots-burn.md b/.changeset/four-carrots-burn.md new file mode 100644 index 000000000000..39cefcc4b76e --- /dev/null +++ b/.changeset/four-carrots-burn.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: allow exports with source from script module even if no bind is present diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 8f1efd7f635f..9e29813ee336 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -698,7 +698,7 @@ export function analyze_component(root, source, options) { } for (const node of analysis.module.ast.body) { - if (node.type === 'ExportNamedDeclaration' && node.specifiers !== null) { + if (node.type === 'ExportNamedDeclaration' && node.specifiers !== null && node.source == null) { for (const specifier of node.specifiers) { if (specifier.local.type !== 'Identifier') continue; diff --git a/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js b/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js new file mode 100644 index 000000000000..2212f3b5cbcf --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + error: false +}); diff --git a/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte b/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte new file mode 100644 index 000000000000..445aebcf4fbb --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte @@ -0,0 +1,3 @@ + diff --git a/packages/svelte/tests/compiler-errors/test.ts b/packages/svelte/tests/compiler-errors/test.ts index ea696890b4f8..3b42519b5a00 100644 --- a/packages/svelte/tests/compiler-errors/test.ts +++ b/packages/svelte/tests/compiler-errors/test.ts @@ -5,11 +5,13 @@ import { suite, type BaseTest } from '../suite'; import { read_file } from '../helpers.js'; interface CompilerErrorTest extends BaseTest { - error: { - code: string; - message: string; - position?: [number, number]; - }; + error: + | { + code: string; + message: string; + position?: [number, number]; + } + | false; } const { test, run } = suite((config, cwd) => { @@ -25,6 +27,9 @@ const { test, run } = suite((config, cwd) => { generate: 'client' }); } catch (e) { + if (config.error === false) { + assert.fail('Unexpected error'); + } const error = e as CompileError; caught_error = true; @@ -37,7 +42,7 @@ const { test, run } = suite((config, cwd) => { } } - if (!caught_error) { + if (config.error !== false && !caught_error) { assert.fail('Expected an error'); } } @@ -50,6 +55,9 @@ const { test, run } = suite((config, cwd) => { generate: 'client' }); } catch (e) { + if (config.error === false) { + assert.fail('Unexpected error'); + } const error = e as CompileError; caught_error = true; @@ -62,7 +70,7 @@ const { test, run } = suite((config, cwd) => { } } - if (!caught_error) { + if (config.error !== false && !caught_error) { assert.fail('Expected an error'); } } From 63ebfc248fd528645b9a8e7f74ccb6c61d1620cb Mon Sep 17 00:00:00 2001 From: paoloricciuti Date: Mon, 9 Dec 2024 15:47:53 +0100 Subject: [PATCH 2/2] chore: move test to validator --- .../_config.js | 5 ----- .../main.svelte | 3 --- packages/svelte/tests/compiler-errors/test.ts | 22 ++++++------------- .../errors.json | 1 + .../input.svelte | 3 +++ 5 files changed, 11 insertions(+), 23 deletions(-) delete mode 100644 packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js delete mode 100644 packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte create mode 100644 packages/svelte/tests/validator/samples/export-not-defined-module-with-source/errors.json create mode 100644 packages/svelte/tests/validator/samples/export-not-defined-module-with-source/input.svelte diff --git a/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js b/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js deleted file mode 100644 index 2212f3b5cbcf..000000000000 --- a/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/_config.js +++ /dev/null @@ -1,5 +0,0 @@ -import { test } from '../../test'; - -export default test({ - error: false -}); diff --git a/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte b/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte deleted file mode 100644 index 445aebcf4fbb..000000000000 --- a/packages/svelte/tests/compiler-errors/samples/export-not-defined-module-with-source/main.svelte +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/packages/svelte/tests/compiler-errors/test.ts b/packages/svelte/tests/compiler-errors/test.ts index 3b42519b5a00..ea696890b4f8 100644 --- a/packages/svelte/tests/compiler-errors/test.ts +++ b/packages/svelte/tests/compiler-errors/test.ts @@ -5,13 +5,11 @@ import { suite, type BaseTest } from '../suite'; import { read_file } from '../helpers.js'; interface CompilerErrorTest extends BaseTest { - error: - | { - code: string; - message: string; - position?: [number, number]; - } - | false; + error: { + code: string; + message: string; + position?: [number, number]; + }; } const { test, run } = suite((config, cwd) => { @@ -27,9 +25,6 @@ const { test, run } = suite((config, cwd) => { generate: 'client' }); } catch (e) { - if (config.error === false) { - assert.fail('Unexpected error'); - } const error = e as CompileError; caught_error = true; @@ -42,7 +37,7 @@ const { test, run } = suite((config, cwd) => { } } - if (config.error !== false && !caught_error) { + if (!caught_error) { assert.fail('Expected an error'); } } @@ -55,9 +50,6 @@ const { test, run } = suite((config, cwd) => { generate: 'client' }); } catch (e) { - if (config.error === false) { - assert.fail('Unexpected error'); - } const error = e as CompileError; caught_error = true; @@ -70,7 +62,7 @@ const { test, run } = suite((config, cwd) => { } } - if (config.error !== false && !caught_error) { + if (!caught_error) { assert.fail('Expected an error'); } } diff --git a/packages/svelte/tests/validator/samples/export-not-defined-module-with-source/errors.json b/packages/svelte/tests/validator/samples/export-not-defined-module-with-source/errors.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/packages/svelte/tests/validator/samples/export-not-defined-module-with-source/errors.json @@ -0,0 +1 @@ +[] diff --git a/packages/svelte/tests/validator/samples/export-not-defined-module-with-source/input.svelte b/packages/svelte/tests/validator/samples/export-not-defined-module-with-source/input.svelte new file mode 100644 index 000000000000..df50ebc1fa66 --- /dev/null +++ b/packages/svelte/tests/validator/samples/export-not-defined-module-with-source/input.svelte @@ -0,0 +1,3 @@ +