Skip to content
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

fix: show filename information in legacy_recursive_reactive_block #13764

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The `render` function passed to `createRawSnippet` should return HTML for a sing
### legacy_recursive_reactive_block

```
Detected a migrated `$:` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
```

### lifecycle_double_unmount
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/messages/client-warnings/warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The easiest way to log a value as it changes over time is to use the [`$inspect`

## legacy_recursive_reactive_block

> Detected a migrated `$:` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
> Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.

## lifecycle_double_unmount

Expand Down
7 changes: 4 additions & 3 deletions packages/svelte/src/internal/client/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ export function invalid_raw_snippet_render() {
}

/**
* Detected a migrated `$:` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
* Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
* @param {string} filename
*/
export function legacy_recursive_reactive_block() {
export function legacy_recursive_reactive_block(filename) {
if (DEV) {
console.warn(`%c[svelte] legacy_recursive_reactive_block\n%cDetected a migrated \`$:\` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an \`$effect\`.`, bold, normal);
console.warn(`%c[svelte] legacy_recursive_reactive_block\n%cDetected a migrated \`$:\` reactive block in \`${filename}\` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an \`$effect\`.`, bold, normal);
} else {
// TODO print a link to the documentation
console.warn("legacy_recursive_reactive_block");
Expand Down
10 changes: 9 additions & 1 deletion packages/svelte/src/legacy/legacy-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { hydrate, mount, unmount } from '../internal/client/render.js';
import {
active_effect,
component_context,
dev_current_component_function,
flush_sync,
get,
set_signal_status
} from '../internal/client/runtime.js';
import { lifecycle_outside_component } from '../internal/shared/errors.js';
import { define_property, is_array } from '../internal/shared/utils.js';
import * as w from '../internal/client/warnings.js';
import { DEV } from 'esm-env';
import { FILENAME } from '../constants.js';

/**
* Takes the same options as a Svelte 4 component and the component function and returns a Svelte 4 compatible component.
Expand Down Expand Up @@ -182,7 +185,12 @@ export function run(fn) {
var effect = /** @type {import('#client').Effect} */ (active_effect);
// If the effect is immediately made dirty again, mark it as maybe dirty to emulate legacy behaviour
if ((effect.f & DIRTY) !== 0) {
w.legacy_recursive_reactive_block();
let filename = "a file (we can't know which one)";
if (DEV) {
// @ts-ignore
filename = dev_current_component_function?.[FILENAME] ?? filename;
}
w.legacy_recursive_reactive_block(filename);
set_signal_status(effect, MAYBE_DIRTY);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
async test({ warnings, assert }) {
assert.deepEqual(warnings, [
'Detected a migrated `$:` reactive block in `main.svelte` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { run } from 'svelte/legacy';

let count = $state(0);

run(() => {
count = count+1;
});
</script>

{count}