Skip to content

Commit

Permalink
fix: do not duplicated variable names when multiple `svelte:component…
Browse files Browse the repository at this point in the history
…` exist (#121)
  • Loading branch information
baseballyama authored May 9, 2024
1 parent 9dfd5e1 commit 452be37
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/six-pears-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-preprocess-delegate-events': patch
---

fix: do not duplicated variable names when multiple svelte:component exist
2 changes: 1 addition & 1 deletion src/preprocess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const getUniqueVarName = (usedVarNames, name) => {
// Remove chars that can not use for variable name.
const normalized = name.replace(/[^a-zA-Z_$]|^(\d)/g, '_');
let i = 0;
while (usedVarNames.has(`${name}${i}`)) {
while (usedVarNames.has(`${normalized}${i}`)) {
i++;
}
usedVarNames.add(`${normalized}${i}`);
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/element-multi-element-with-this/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let button1;
let button2;
</script>

<button on:* bind:this={button1}>Click Me</button>
<button on:* bind:this={button2}>Click Me</button>
14 changes: 14 additions & 0 deletions test/fixture/element-multi-element-with-this/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import { registerDelegatedEvents } from 'svelte-preprocess-delegate-events/runtime';
import { get_current_component } from 'svelte/internal';
let button1;
let button2;
const component0 = get_current_component();
$: registerDelegatedEvents(button1, component0, (handler) => handler, {});
$: registerDelegatedEvents(button2, component0, (handler) => handler, {});
</script>

<button bind:this={button1}>Click Me</button>
<button bind:this={button2}>Click Me</button>
12 changes: 12 additions & 0 deletions test/fixture/svelte:component/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import Component from './Component.svelte';
import Component2 from './Component2.svelte';
const a = false;
</script>

{#if a}
<svelte:component this={Component} on:* />
{:else}
<svelte:component this={Component2} on:* />
{/if}
22 changes: 22 additions & 0 deletions test/fixture/svelte:component/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts">
import { get_current_component } from 'svelte/internal';
import { boundComponents } from 'svelte-preprocess-delegate-events/runtime';
import { proxyCallbacks } from 'svelte-preprocess-delegate-events/runtime';
import Component from './Component.svelte';
import Component2 from './Component2.svelte';
const a = false;
const svelte_component0 = boundComponents();
const component0 = get_current_component();
$: proxyCallbacks(component0, svelte_component0.bounds, false);
const svelte_component1 = boundComponents();
$: proxyCallbacks(component0, svelte_component1.bounds, false);
</script>

{#if a}
<svelte:component this={Component} bind:this={svelte_component0.bounds} />
{:else}
<svelte:component this={Component2} bind:this={svelte_component1.bounds} />
{/if}

0 comments on commit 452be37

Please sign in to comment.