diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index f3a012341d5a..4ca111f7ea23 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1192,6 +1192,7 @@ export default class Component { const assignees = new Set(); const assignee_nodes = new Set(); const dependencies = new Set(); + const module_dependencies = new Set(); let scope = this.instance_scope; const map = this.instance_scope_map; @@ -1228,7 +1229,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 = @@ -1253,6 +1254,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; diff --git a/src/compiler/compile/compiler_warnings.ts b/src/compiler/compile/compiler_warnings.ts index 3b8ba929fa1e..bfcd779fc7ea 100644 --- a/src/compiler/compile/compiler_warnings.ts +++ b/src/compiler/compile/compiler_warnings.ts @@ -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', diff --git a/test/validator/samples/reactive-module-variable-2/input.svelte b/test/validator/samples/reactive-module-variable-2/input.svelte new file mode 100644 index 000000000000..d36d9cf21054 --- /dev/null +++ b/test/validator/samples/reactive-module-variable-2/input.svelte @@ -0,0 +1,7 @@ + + diff --git a/test/validator/samples/reactive-module-variable-2/warnings.json b/test/validator/samples/reactive-module-variable-2/warnings.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/test/validator/samples/reactive-module-variable-2/warnings.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/reactive-module-variable/warnings.json b/test/validator/samples/reactive-module-variable/warnings.json index c3c52cb47963..d08fc1309f02 100644 --- a/test/validator/samples/reactive-module-variable/warnings.json +++ b/test/validator/samples/reactive-module-variable/warnings.json @@ -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 } }