From 1bc89b5eb6e745f05fd534b45dc62b2d269f05f2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 21 Nov 2023 03:49:36 -0500 Subject: [PATCH] fix: deconflict generated names against globals (#9570) fixes #9559 --- .changeset/green-hounds-play.md | 5 +++++ packages/svelte/src/compiler/phases/scope.js | 4 ++++ .../samples/globals-deconflicted/_config.js | 15 +++++++++++++++ .../samples/globals-deconflicted/main.svelte | 1 + 4 files changed, 25 insertions(+) create mode 100644 .changeset/green-hounds-play.md create mode 100644 packages/svelte/tests/runtime-runes/samples/globals-deconflicted/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/globals-deconflicted/main.svelte diff --git a/.changeset/green-hounds-play.md b/.changeset/green-hounds-play.md new file mode 100644 index 0000000000..6a2922d0d5 --- /dev/null +++ b/.changeset/green-hounds-play.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: deconflict generated names against globals diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index 5c18e78f7c..22e5d3eaec 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -184,6 +184,10 @@ export class Scope { declaration.references.push({ node, path }); } else if (this.#parent) { this.#parent.reference(node, path); + } else { + // no declaration was found, and this is the top level scope, + // which means this is a global + this.root.conflicts.add(node.name); } } } diff --git a/packages/svelte/tests/runtime-runes/samples/globals-deconflicted/_config.js b/packages/svelte/tests/runtime-runes/samples/globals-deconflicted/_config.js new file mode 100644 index 0000000000..3d1ab275b9 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/globals-deconflicted/_config.js @@ -0,0 +1,15 @@ +import { test } from '../../test'; + +export default test({ + html: `

hello

`, + + before_test: () => { + // @ts-expect-error + globalThis.frag = 'hello'; + }, + + after_test: () => { + // @ts-expect-error + delete globalThis.frag; + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/globals-deconflicted/main.svelte b/packages/svelte/tests/runtime-runes/samples/globals-deconflicted/main.svelte new file mode 100644 index 0000000000..25a651859c --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/globals-deconflicted/main.svelte @@ -0,0 +1 @@ +

{frag}