Skip to content

fix: support default parameter in snippet block #2385

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

Closed
Closed
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
7 changes: 5 additions & 2 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/SnippetBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function handleSnippet(
];

if (snippetBlock.parameters?.length) {
const start = snippetBlock.parameters[0].start;
const end = snippetBlock.parameters.at(-1).end;
const first = snippetBlock.parameters[0];
const last = snippetBlock.parameters.at(-1);

const start = first.type === 'AssignmentPattern' ? first.left.start : first.start;
const end = last.type === 'AssignmentPattern' ? last.right.end : last.end;
transforms.push([start, end]);
}

Expand Down
12 changes: 8 additions & 4 deletions packages/svelte2tsx/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,16 @@ export function test_samples(dir: string, transform: TransformSampleFn, js: 'js'
try {
assert.strictEqual(actual, expected, TestError.WrongExpected);
} catch (e) {
// html2jsx tests don't have the default export
const expectDefaultExportPosition = expected.lastIndexOf(
'\n\nexport default class'
);
if (expectDefaultExportPosition === -1) {
throw e;
}
// retry with the last part (the returned default export) stripped because it's always differing between old and new,
// and if that fails then we're going to rethrow the original error
const expectedModified = expected.substring(
0,
expected.lastIndexOf('\n\nexport default class')
);
const expectedModified = expected.substring(0, expectDefaultExportPosition);
const actualModified = actual.substring(0, actual.lastIndexOf('\nconst '));
try {
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
{ const $$_value = await (foo);{ const bar = $$_value; bar;}}
};return __sveltets_2_any(0)};

var default_parameter/*Ωignore_startΩ*/: import('svelte').Snippet<[any]>/*Ωignore_endΩ*/ = (a = '') => {async () => {
a;
};return __sveltets_2_any(0)};

;__sveltets_2_ensureSnippet(foo(1));
;__sveltets_2_ensureSnippet(bar());
;__sveltets_2_ensureSnippet(await_inside());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
{#await foo then bar}{bar}{/await}
{/snippet}

{#snippet default_parameter(a = '')}
{a}
{/snippet}

{@render foo(1)}
{@render bar()}
{@render await_inside()}
Expand Down
Loading