Skip to content

Commit d0e7273

Browse files
committed
fix: add compile checks for public derived and literal
1 parent d6d467b commit d0e7273

File tree

8 files changed

+59
-9
lines changed

8 files changed

+59
-9
lines changed

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ export function analyze_component(root, source, options) {
605605
has_props_rune: false,
606606
component_slots: new Set(),
607607
expression: null,
608-
private_derived_state: [],
608+
derived_state: [],
609609
function_depth: scope.function_depth,
610610
instance_scope: instance.scope,
611611
reactive_statement: null,
@@ -676,7 +676,7 @@ export function analyze_component(root, source, options) {
676676
reactive_statements: analysis.reactive_statements,
677677
component_slots: new Set(),
678678
expression: null,
679-
private_derived_state: [],
679+
derived_state: [],
680680
function_depth: scope.function_depth
681681
};
682682

packages/svelte/src/compiler/phases/2-analyze/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface AnalysisState {
1919
component_slots: Set<string>;
2020
/** Information about the current expression/directive/block value */
2121
expression: ExpressionMetadata | null;
22-
private_derived_state: string[];
22+
derived_state: string[];
2323
function_depth: number;
2424

2525
// legacy stuff

packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import { get_rune } from '../../scope.js';
88
*/
99
export function ClassBody(node, context) {
1010
/** @type {string[]} */
11-
const private_derived_state = [];
11+
const derived_state = [];
1212

1313
for (const definition of node.body) {
1414
if (
1515
definition.type === 'PropertyDefinition' &&
16-
definition.key.type === 'PrivateIdentifier' &&
16+
(definition.key.type === 'PrivateIdentifier' || definition.key.type === 'Identifier') &&
1717
definition.value?.type === 'CallExpression'
1818
) {
1919
const rune = get_rune(definition.value, context.state.scope);
2020
if (rune === '$derived' || rune === '$derived.by') {
21-
private_derived_state.push(definition.key.name);
21+
derived_state.push(definition.key.name);
2222
}
2323
}
2424
}
2525

26-
context.next({ ...context.state, private_derived_state });
26+
context.next({ ...context.state, derived_state });
2727
}

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ export function validate_assignment(node, argument, state) {
3838
if (
3939
argument.type === 'MemberExpression' &&
4040
argument.object.type === 'ThisExpression' &&
41-
argument.property.type === 'PrivateIdentifier' &&
42-
state.private_derived_state.includes(argument.property.name)
41+
(((argument.property.type === 'PrivateIdentifier' || argument.property.type === 'Identifier') &&
42+
state.derived_state.includes(argument.property.name)) ||
43+
(argument.property.type === 'Literal' &&
44+
argument.property.value &&
45+
typeof argument.property.value === 'string' &&
46+
state.derived_state.includes(argument.property.value)))
4347
) {
4448
e.constant_assignment(node, 'derived state');
4549
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "constant_assignment",
4+
"message": "Cannot assign to derived state",
5+
"start": {
6+
"column": 3,
7+
"line": 6
8+
},
9+
"end": {
10+
"column": 29,
11+
"line": 6
12+
}
13+
}
14+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
class Test{
3+
der = $derived({ test: 0 });
4+
5+
set test(v){
6+
this["der"] = { test: 45 };
7+
}
8+
}
9+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "constant_assignment",
4+
"message": "Cannot assign to derived state",
5+
"start": {
6+
"column": 3,
7+
"line": 6
8+
},
9+
"end": {
10+
"column": 26,
11+
"line": 6
12+
}
13+
}
14+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
class Test{
3+
der = $derived({ test: 0 });
4+
5+
set test(v){
6+
this.der = { test: 45 };
7+
}
8+
}
9+
</script>

0 commit comments

Comments
 (0)