Skip to content

Commit 097ce8f

Browse files
committed
feat: fix accessors and support migration of accessors
1 parent b665425 commit 097ce8f

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

.changeset/large-rules-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: fix accessors and support migration of accessors

packages/svelte/src/compiler/migrate/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { get_rune } from '../phases/scope.js';
1212
import { reset, reset_warning_filter } from '../state.js';
1313
import { extract_identifiers } from '../utils/ast.js';
1414
import { migrate_svelte_ignore } from '../utils/extract_svelte_ignore.js';
15-
import { determine_slot } from '../utils/slot.js';
1615
import { validate_component_options } from '../validate-options.js';
1716

1817
const regex_style_tags = /(<style[^>]+>)([\S\s]*?)(<\/style>)/g;
@@ -54,6 +53,8 @@ export function migrate(source) {
5453
const analysis = analyze_component(parsed, source, combined_options);
5554
const indent = guess_indent(source);
5655

56+
str.replaceAll(/(<svelte:options\s.*?\s?)accessors\s?/g, (_, $1) => $1);
57+
5758
for (const content of style_contents) {
5859
str.overwrite(content[0], content[0] + style_placeholder.length, content[1]);
5960
}
@@ -265,6 +266,13 @@ export function migrate(source) {
265266
);
266267
}
267268

269+
if (state.props.length > 0 && state.analysis.accessors) {
270+
str.appendRight(
271+
insertion_point,
272+
`\n${indent}export {\n${indent}\t${state.props.map((prop) => `${prop.local},`).join(`\n${indent}\t`)}\n${indent}}\n`
273+
);
274+
}
275+
268276
if (!parsed.instance && need_script) {
269277
str.appendRight(insertion_point, '\n</script>\n\n');
270278
}

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ export function client_component(analysis, options) {
276276
}
277277
}
278278

279+
if (binding?.kind === 'prop' || binding?.kind === 'bindable_prop') {
280+
return [
281+
getter,
282+
b.set(alias ?? name, [b.stmt(b.call(name, b.call('$.proxy', b.id('$$value'))))])
283+
];
284+
}
285+
279286
if (binding?.kind === 'state' || binding?.kind === 'raw_state') {
280287
const value = binding.kind === 'state' ? b.call('$.proxy', b.id('$$value')) : b.id('$$value');
281288
return [getter, b.set(alias ?? name, [b.stmt(b.call('$.set', b.id(name), value))])];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
type $$Props = {
3+
test: string;
4+
}
5+
6+
export let count = 0;
7+
export let stuff;
8+
</script>
9+
10+
<button>
11+
<slot name="cool" />
12+
</button>
13+
14+
<svelte:options accessors immutable/>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script lang="ts">
2+
3+
4+
interface Props {
5+
test: string;
6+
count?: number;
7+
stuff: any;
8+
cool?: import('svelte').Snippet;
9+
}
10+
11+
let {
12+
test,
13+
count = 0,
14+
stuff,
15+
cool
16+
}: Props = $props();
17+
18+
export {
19+
test,
20+
count,
21+
stuff,
22+
cool,
23+
}
24+
</script>
25+
26+
<button>
27+
{@render cool?.()}
28+
</button>
29+
30+
<svelte:options immutable/>

0 commit comments

Comments
 (0)