Skip to content

fix: falsy attachments on components #16021

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
May 28, 2025
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/fuzzy-taxis-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: falsy attachments on components
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,18 @@ export function build_component(node, component_name, context, anchor = context.
}
}
} else if (attribute.type === 'AttachTag') {
const evaluated = context.state.scope.evaluate(attribute.expression);

let expression = /** @type {Expression} */ (context.visit(attribute.expression));

if (attribute.metadata.expression.has_state) {
expression = b.arrow([b.id('$$node')], b.call(expression, b.id('$$node')));
expression = b.arrow(
[b.id('$$node')],
b.call(
evaluated.is_function ? expression : b.logical('||', expression, b.id('$.noop')),
b.id('$$node')
)
);
}

push_prop(b.prop('get', b.call('$.attachment'), expression, true));
Expand Down
21 changes: 20 additions & 1 deletion packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const UNKNOWN = Symbol('unknown');
/** Includes `BigInt` */
export const NUMBER = Symbol('number');
export const STRING = Symbol('string');
export const FUNCTION = Symbol('string');

/** @type {Record<string, [type: NUMBER | STRING | UNKNOWN, fn?: Function]>} */
const globals = {
Expand Down Expand Up @@ -200,6 +201,13 @@ class Evaluation {
*/
is_number = true;

/**
* True if the value is known to be a function
* @readonly
* @type {boolean}
*/
is_function = true;

/**
* @readonly
* @type {any}
Expand All @@ -209,7 +217,7 @@ class Evaluation {
/**
*
* @param {Scope} scope
* @param {Expression} expression
* @param {Expression | FunctionDeclaration} expression
* @param {Set<any>} values
*/
constructor(scope, expression, values) {
Expand Down Expand Up @@ -500,6 +508,13 @@ class Evaluation {
break;
}

case 'ArrowFunctionExpression':
case 'FunctionExpression':
case 'FunctionDeclaration': {
this.values.add(FUNCTION);
break;
}

default: {
this.values.add(UNKNOWN);
}
Expand All @@ -516,6 +531,10 @@ class Evaluation {
this.is_number = false;
}

if (value !== FUNCTION) {
this.is_function = false;
}

if (value == null || value === UNKNOWN) {
this.is_defined = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let props = $props();
</script>

<div {...props}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
test() {}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import Child from './Child.svelte';

function attachment(){
console.log("up");
}

let enabled = $state(false);
</script>

<button onclick={() => enabled = !enabled}></button>

<Child {@attach enabled && attachment} />