Skip to content

fix: possible name clash in hoisted functions #11237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-parents-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: possible name clash in hoisted functions
22 changes: 16 additions & 6 deletions packages/svelte/src/compiler/phases/3-transform/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,28 @@ export const function_visitor = (node, context) => {
function get_hoistable_params(node, context) {
const scope = context.state.scope;

/** @type {import('estree').Pattern[]} */
/** @type {import('estree').Identifier[]} */
const params = [];
let added_props = false;

/**
* we only want to push if it's not already present to avoid name clashing
* @param {import('estree').Identifier} id
*/
function safe_push(id) {
if (!params.find((param) => param.name === id.name)) {
params.push(id);
}
}

for (const [reference] of scope.references) {
const binding = scope.get(reference);

if (binding !== null && !scope.declarations.has(reference) && binding.initial !== node) {
if (binding.kind === 'store_sub') {
// We need both the subscription for getting the value and the store for updating
params.push(b.id(binding.node.name.slice(1)));
params.push(b.id(binding.node.name));
safe_push(b.id(binding.node.name.slice(1)));
safe_push(b.id(binding.node.name));
} else if (
// If it's a destructured derived binding, then we can extract the derived signal reference and use that.
binding.expression !== null &&
Expand All @@ -477,7 +487,7 @@ function get_hoistable_params(node, context) {
binding.expression.object.callee.name === '$.get' &&
binding.expression.object.arguments[0].type === 'Identifier'
) {
params.push(b.id(binding.expression.object.arguments[0].name));
safe_push(b.id(binding.expression.object.arguments[0].name));
} else if (
// If we are referencing a simple $$props value, then we need to reference the object property instead
(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
Expand All @@ -488,11 +498,11 @@ function get_hoistable_params(node, context) {
// Handle $$props.something use-cases
if (!added_props) {
added_props = true;
params.push(b.id('$$props'));
safe_push(b.id('$$props'));
}
} else {
// create a copy to remove start/end tags which would mess up source maps
params.push(b.id(binding.node.name));
safe_push(b.id(binding.node.name));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
recover: true,
mode: ['client']
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
import { writable } from 'svelte/store';
const store = writable(0);

async function logStore() {
console.log($store)
store.set(100);
}
</script>

<button onclick={logStore}>Click me</button>