Skip to content

Commit

Permalink
warn if component is expected to be reactive (sveltejs#4409)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and jesseskinner committed Feb 27, 2020
1 parent 4a02e42 commit e546e02
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Fix indirect bindings involving elements with spreads ([#3680](https://github.com/sveltejs/svelte/issues/3680))
* Warn when using `<Foo/>` and `Foo` is dynamic ([#4331](https://github.com/sveltejs/svelte/issues/4331))
* Fix unneeded updating of keyed each blocks ([#4373](https://github.com/sveltejs/svelte/issues/4373))

## 3.18.2
Expand Down
17 changes: 17 additions & 0 deletions src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,28 @@ export default class InlineComponentWrapper extends Wrapper {
block.add_outro();
}

warn_if_reactive() {
const { name } = this.node;
const variable = this.renderer.component.var_lookup.get(name);
if (!variable) {
return;
}

if (variable.reassigned || variable.export_name || variable.is_reactive_dependency) {
this.renderer.component.warn(this.node, {
code: 'reactive-component',
message: `<${name}/> will not be reactive if ${name} changes. Use <svelte:component this={${name}}/> if you want this reactivity.`,
});
}
}

render(
block: Block,
parent_node: Identifier,
parent_nodes: Identifier
) {
this.warn_if_reactive();

const { renderer } = this;
const { component } = renderer;

Expand Down
4 changes: 3 additions & 1 deletion test/validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("validate", () => {
const input = fs.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, "utf-8").replace(/\s+$/, "");
const expected_warnings = tryToLoadJson(`${__dirname}/samples/${dir}/warnings.json`) || [];
const expected_errors = tryToLoadJson(`${__dirname}/samples/${dir}/errors.json`);
const options = tryToLoadJson(`${__dirname}/samples/${dir}/options.json`);

let error;

Expand All @@ -28,7 +29,8 @@ describe("validate", () => {
dev: config.dev,
legacy: config.legacy,
generate: false,
customElement: config.customElement
customElement: config.customElement,
...options,
});

assert.deepEqual(warnings.map(w => ({
Expand Down
18 changes: 18 additions & 0 deletions test/validator/samples/component-dynamic/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
import A from './A.svelte';
import B from './B.svelte';
let Let = A;
function update() {
Let = B;
}
export let ExportLet = B;
$: Reactive = random() ? A : B;
</script>

<Let />
<ExportLet />
<Reactive />
<svelte:component this={Let} />
3 changes: 3 additions & 0 deletions test/validator/samples/component-dynamic/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generate": true
}
47 changes: 47 additions & 0 deletions test/validator/samples/component-dynamic/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"code": "reactive-component",
"message": "<Let/> will not be reactive if Let changes. Use <svelte:component this={Let}/> if you want this reactivity.",
"pos": 190,
"end": {
"character": 197,
"column": 7,
"line": 15
},
"start": {
"character": 190,
"column": 0,
"line": 15
}
},
{
"message": "<ExportLet/> will not be reactive if ExportLet changes. Use <svelte:component this={ExportLet}/> if you want this reactivity.",
"code": "reactive-component",
"pos": 198,
"end": {
"character": 211,
"column": 13,
"line": 16
},
"start": {
"character": 198,
"column": 0,
"line": 16
}
},
{
"message": "<Reactive/> will not be reactive if Reactive changes. Use <svelte:component this={Reactive}/> if you want this reactivity.",
"code": "reactive-component",
"pos": 212,
"end": {
"character": 224,
"column": 12,
"line": 17
},
"start": {
"character": 212,
"column": 0,
"line": 17
}
}
]

0 comments on commit e546e02

Please sign in to comment.