Skip to content
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

fix : bug "$0 is not defined" with svelte:element and a single class with a function call #15396

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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/two-dragons-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix : bug "$0 is not defined" on svelte:element with a function call on class
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ export function SvelteElement(node, context) {
if (
attributes.length === 1 &&
attributes[0].type === 'Attribute' &&
attributes[0].name.toLowerCase() === 'class'
attributes[0].name.toLowerCase() === 'class' &&
attributes[0].value !== true &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong but there should be a is_text_attrobute utility somewhere...can you check if that's the case and use that?

!Array.isArray(attributes[0].value) &&
attributes[0].value.metadata.expression.has_call === false
) {
// special case when there only a class attribute
// special case when there only a class attribute, without call expression
let { value, has_state } = build_attribute_value(
attributes[0].value,
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
html: `
<div></div>
<div class="red svelte-p153w3"></div>
<div></div>
<div class="red svelte-p153w3"></div>
<div class="blue svelte-p153w3"></div>
<div class="blue svelte-p153w3"></div>
`,

async test({ assert, target, component }) {
component.active = true;
flushSync();

assert.htmlEqual(
target.innerHTML,
`
<div></div>
<div class="red svelte-p153w3"></div>
<div class="active"></div>
<div class="red svelte-p153w3 active"></div>
<div class="blue svelte-p153w3"></div>
<div class="blue svelte-p153w3 active"></div>
`
);

component.tag = 'span';
flushSync();

assert.htmlEqual(
target.innerHTML,
`
<span></span>
<span class="red svelte-p153w3"></span>
<span class="active"></span>
<span class="red svelte-p153w3 active"></span>
<span class="blue svelte-p153w3"></span>
<span class="blue svelte-p153w3 active"></span>
`
);

component.active = false;
flushSync();

assert.htmlEqual(
target.innerHTML,
`
<span></span>
<span class="red svelte-p153w3"></span>
<span class=""></span>
<span class="red svelte-p153w3"></span>
<span class="blue svelte-p153w3"></span>
<span class="blue svelte-p153w3"></span>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
let { tag = "div", active = false } = $props();

function cn(classname) {
return classname;
}
</script>

<svelte:element this={tag}></svelte:element>
<svelte:element this={tag} class="red"></svelte:element>
<svelte:element this={tag} class:active={active}></svelte:element>
<svelte:element this={tag} class="red" class:active={active}></svelte:element>
<svelte:element this={tag} class={cn("blue")}></svelte:element>
<svelte:element this={tag} class={cn("blue")} class:active={active}></svelte:element>

<style>
.red {
color: red;
}
</style>