Skip to content

[fix] do not warn if module variables are not the only dependencies in reactive statements #6510

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

Merged
merged 1 commit into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ export default class Component {
const assignees = new Set<string>();
const assignee_nodes = new Set();
const dependencies = new Set<string>();
const module_dependencies = new Set<string>();

let scope = this.instance_scope;
const map = this.instance_scope_map;
Expand Down Expand Up @@ -1211,7 +1212,7 @@ export default class Component {
variable.is_reactive_dependency = true;
if (variable.module) {
should_add_as_dependency = false;
component.warn(node as any, compiler_warnings.module_script_variable_reactive_declaration(name));
module_dependencies.add(name);
}
}
const is_writable_or_mutated =
Expand All @@ -1236,6 +1237,10 @@ export default class Component {
}
});

if (module_dependencies.size > 0 && dependencies.size === 0) {
component.warn(node.body as any, compiler_warnings.module_script_variable_reactive_declaration(Array.from(module_dependencies)));
}

const { expression } = node.body as ExpressionStatement;
const declaration = expression && (expression as AssignmentExpression).left;

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compile/compiler_warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default {
code: 'non-top-level-reactive-declaration',
message: '$: has no effect outside of the top-level'
},
module_script_variable_reactive_declaration: (name: string) => ({
module_script_variable_reactive_declaration: (names: string[]) => ({
code: 'module-script-reactive-declaration',
message: `"${name}" is declared in a module script and will not be reactive`
message: `${names.map(name => `"${name}"`).join(', ')} ${names.length > 1 ? 'are' : 'is'} declared in a module script and will not be reactive`
}),
missing_declaration: (name: string, has_script: boolean) => ({
code: 'missing-declaration',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script context="module">
let PI = 3.14;
</script>
<script>
let r;
$: area = PI * r * r;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
10 changes: 5 additions & 5 deletions test/validator/samples/reactive-module-variable/warnings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
{
"code": "module-script-reactive-declaration",
"message": "\"foo\" is declared in a module script and will not be reactive",
"pos": 65,
"pos": 59,
"start": {
"character": 65,
"column": 10,
"character": 59,
"column": 4,
"line": 5
},
"end": {
"character": 68,
"column": 13,
"character": 69,
"column": 14,
"line": 5
}
}
Expand Down