Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit396305a

Browse files
committed
fix: warn when a variable is created an read within the same reaction
1 parent2af7ba2 commit396305a

File tree

9 files changed

+86
-1
lines changed

9 files changed

+86
-1
lines changed

‎.changeset/lovely-ants-own.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+
fix: warn when a variable is created an read within the same reaction

‎documentation/docs/98-reference/.generated/client-warnings.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ Consider the following code:
200200
201201
To fix it, either create callback props to communicate changes, or mark`person` as [`$bindable`]($bindable).
202202
203+
### reading_just_created_state
204+
205+
```
206+
The variable`%name%` is created and read within`%reaction%`.`%reaction%` will not depend on it.
207+
```
208+
203209
### select_multiple_invalid_value
204210
205211
```

‎packages/svelte/messages/client-warnings/warnings.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ Consider the following code:
168168
169169
To fix it, either create callback props to communicate changes, or mark`person` as [`$bindable`]($bindable).
170170
171+
## reading_just_created_state
172+
173+
> The variable`%name%` is created and read within`%reaction%`.`%reaction%` will not depend on it.
174+
171175
## select_multiple_invalid_value
172176
173177
> The`value` property of a`<select multiple>` element should be an array, but it received a non-array value. The selection will be kept as is.

‎packages/svelte/src/internal/client/runtime.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { flush_tasks } from './dom/task.js';
2828
import{internal_set,old_values}from'./reactivity/sources.js';
2929
import{destroy_derived_effects,update_derived}from'./reactivity/deriveds.js';
3030
import*asefrom'./errors.js';
31+
import*aswfrom'./warnings.js';
3132

3233
import{tracing_mode_flag}from'../flags/index.js';
3334
import{tracing_expressions,get_stack}from'./dev/tracing.js';
@@ -39,7 +40,6 @@ import {
3940
set_dev_current_component_function
4041
}from'./context.js';
4142
import{handle_error,invoke_error_boundary}from'./error-handling.js';
42-
import{snapshot}from'../shared/clone.js';
4343

4444
letis_flushing=false;
4545

@@ -742,6 +742,13 @@ export function get(signal) {
742742
new_deps.push(signal);
743743
}
744744
}
745+
}else{
746+
w.reading_just_created_state(
747+
signal.label??'$state',
748+
(active_reaction.f&DERIVED)!==0
749+
?`${/**@type {Derived} */(active_reaction).label??'$derived'}`
750+
:'$effect'
751+
);
745752
}
746753
}elseif(
747754
is_derived&&

‎packages/svelte/src/internal/client/warnings.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ export function ownership_invalid_mutation(name, location, prop, parent) {
158158
}
159159
}
160160

161+
/**
162+
* The variable `%name%` is created and read within `%reaction%`. `%reaction%` will not depend on it.
163+
*@param {string} name
164+
*@param {string} reaction
165+
*/
166+
exportfunctionreading_just_created_state(name,reaction){
167+
if(DEV){
168+
console.warn(`%c[svelte] reading_just_created_state\n%cThe variable \`${name}\` is created and read within \`${reaction}\`. \`${reaction}\` will not depend on it.\nhttps://svelte.dev/e/reading_just_created_state`,bold,normal);
169+
}else{
170+
console.warn(`https://svelte.dev/e/reading_just_created_state`);
171+
}
172+
}
173+
161174
/**
162175
* The `value` property of a `<select multiple>` element should be an array, but it received a non-array value. The selection will be kept as is.
163176
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import{test}from'../../test';
2+
3+
exportdefaulttest({
4+
compileOptions:{
5+
dev:true
6+
},
7+
asynctest({ assert, warnings}){
8+
console.log(warnings);
9+
assert.deepEqual(warnings,[
10+
'The variable `count` is created and read within `derived_count`. `derived_count` will not depend on it.'
11+
]);
12+
}
13+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
functiontest(){
3+
let count=$state(0);
4+
5+
return {
6+
getcount(){
7+
return count;
8+
},
9+
setcount(c){
10+
count= c
11+
}
12+
};
13+
}
14+
15+
let derived_count=$derived(test().count);
16+
</script>
17+
18+
{derived_count}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import{test}from'../../test';
2+
3+
exportdefaulttest({
4+
compileOptions:{
5+
dev:true
6+
},
7+
asynctest({ assert, warnings}){
8+
console.log(warnings);
9+
assert.deepEqual(warnings,[
10+
'The variable `x` is created and read within `$effect`. `$effect` will not depend on it.'
11+
]);
12+
}
13+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
$effect(()=>{
3+
let x=$state(0);
4+
x++;
5+
})
6+
</script>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp