mirror of https://github.com/sveltejs/svelte
feat: turn `reactive_declaration_non_reactive_property` into a runtime warning (#14192)
* turn `reactive_declaration_non_reactive_property` into a runtime warning * ignore warning * Update packages/svelte/src/internal/client/reactivity/effects.js Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> * Update packages/svelte/src/internal/client/runtime.js Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> * fix * test * changeset * Update .changeset/witty-turtles-bake.md Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> * add some details * check * regenerate --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>pull/14529/head
parent
87863da6ff
commit
a5de086f95
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
chore: turn reactive_declaration_non_reactive_property into a runtime warning
|
@ -0,0 +1,25 @@
|
||||
import { DEV } from 'esm-env';
|
||||
import { FILENAME } from '../../../constants.js';
|
||||
import { dev_current_component_function } from '../runtime.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} [line]
|
||||
* @param {number} [column]
|
||||
*/
|
||||
export function get_location(line, column) {
|
||||
if (!DEV || line === undefined) return undefined;
|
||||
|
||||
var filename = dev_current_component_function?.[FILENAME];
|
||||
var location = filename && `${filename}:${line}:${column}`;
|
||||
|
||||
return sanitize_location(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent devtools trying to make `location` a clickable link by inserting a zero-width space
|
||||
* @param {string | undefined} location
|
||||
*/
|
||||
export function sanitize_location(location) {
|
||||
return location?.replace(/\//g, '/\u200b');
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
compileOptions: {
|
||||
dev: true
|
||||
},
|
||||
|
||||
html: '<p>42</p><p>42</p><button>update</button><button>reset</button>',
|
||||
|
||||
test({ assert, target }) {
|
||||
const [update, reset] = target.querySelectorAll('button');
|
||||
flushSync(() => update.click());
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
'<p>42</p><p>42</p><button>update</button><button>reset</button>'
|
||||
);
|
||||
|
||||
flushSync(() => reset.click());
|
||||
},
|
||||
|
||||
warnings: [
|
||||
'A `$:` statement (main.svelte:4:1) read reactive state that was not visible to the compiler. Updates to this state will not cause the statement to re-run. The behaviour of this code will change if you migrate it to runes mode'
|
||||
]
|
||||
});
|
@ -0,0 +1,11 @@
|
||||
export const obj = $state({
|
||||
prop: 42
|
||||
});
|
||||
|
||||
export function update() {
|
||||
obj.prop += 1;
|
||||
}
|
||||
|
||||
export function reset() {
|
||||
obj.prop = 42;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
<script>
|
||||
import { obj, update, reset } from './data.svelte.js';
|
||||
|
||||
$: a = obj.prop;
|
||||
|
||||
// svelte-ignore reactive_declaration_non_reactive_property
|
||||
$: b = obj.prop;
|
||||
</script>
|
||||
|
||||
<p>{a}</p>
|
||||
<p>{b}</p>
|
||||
<button on:click={update}>update</button>
|
||||
<button on:click={reset}>reset</button>
|
@ -1,3 +0,0 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({});
|
@ -1,8 +0,0 @@
|
||||
<script>
|
||||
import { obj } from './data.js';
|
||||
|
||||
$: prop = obj.prop;
|
||||
obj.foo = 'a different prop';
|
||||
</script>
|
||||
|
||||
<p>{prop}</p>
|
@ -1,14 +0,0 @@
|
||||
[
|
||||
{
|
||||
"code": "reactive_declaration_non_reactive_property",
|
||||
"message": "Properties of objects and arrays are not reactive unless in runes mode. Changes to this property will not cause the reactive statement to update",
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 19
|
||||
}
|
||||
}
|
||||
]
|
Loading…
Reference in new issue