Skip to content

fix: allow accessing snippet in script tag #15789

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 8 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: allow accessing snippet in script tag
  • Loading branch information
paoloricciuti committed Apr 17, 2025
commit c43bfebcd7d7c24e4602c812346ac109eb6eec03
5 changes: 5 additions & 0 deletions .changeset/few-horses-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: allow accessing snippet in script tag
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression } from 'estree' */
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression, Statement } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types.js' */
import { dev } from '../../../../state.js';
Expand All @@ -15,21 +15,24 @@ export function SnippetBlock(node, context) {
body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
}

/** @type {ArrowFunctionExpression | CallExpression} */
let fn = b.arrow([b.id('$$payload'), ...node.parameters], body);
let fn = b.function_declaration(
node.expression,
[b.id('$$payload'), ...node.parameters],
/** @type {BlockStatement} */ (context.visit(node.body))
);

const push_to = node.metadata.can_hoist ? context.state.hoisted : context.state.init;

if (dev) {
fn = b.call('$.prevent_snippet_stringification', fn);
push_to.push(b.stmt(b.call('$.prevent_snippet_stringification', fn.id)));
}

const declaration = b.declaration('const', [b.declarator(node.expression, fn)]);

// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
fn.___snippet = true;

if (node.metadata.can_hoist) {
context.state.hoisted.push(declaration);
push_to.push(fn);
} else {
context.state.init.push(declaration);
push_to.push(fn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function fn(snippet) {
return snippet;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { fn } from "./fn.js";
let variable = $state("var");

fn(test);
</script>

{#snippet test()}
{variable}
{/snippet}
1 change: 1 addition & 0 deletions svelte-renderer-api
Submodule svelte-renderer-api added at 97817a
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy