diff --git a/src/Stats.ts b/src/Stats.ts index 4c93e6937e..92b3f25f2a 100644 --- a/src/Stats.ts +++ b/src/Stats.ts @@ -99,7 +99,7 @@ export default class Stats { return { timings, warnings: this.warnings, - vars: component.vars.map(variable => ({ + vars: component.vars.filter(variable => variable.kind !== 'global').map(variable => ({ name: variable.name, kind: variable.kind, import_name: variable.import_name || null, diff --git a/src/compile/Component.ts b/src/compile/Component.ts index 7d3c61dcd1..49cb51d691 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -603,10 +603,18 @@ export default class Component { globals.forEach(name => { if (this.module_scope && this.module_scope.declarations.has(name)) return; - this.add_var({ - name, - kind: 'global' - }); + if (name[0] === '$') { + this.add_var({ + name, + kind: 'injected', + mutated: true + }); + } else { + this.add_var({ + name, + kind: 'global' + }); + } }); this.extract_imports(script.content, false); @@ -682,9 +690,6 @@ export default class Component { if (name[0] === '$' && !scope.has(name)) { component.warn_if_undefined(object, null); - - // cheeky hack - component.add_reference(name); } } }, diff --git a/src/compile/nodes/shared/Expression.ts b/src/compile/nodes/shared/Expression.ts index eb5f7a6e7c..9fc6bf1707 100644 --- a/src/compile/nodes/shared/Expression.ts +++ b/src/compile/nodes/shared/Expression.ts @@ -195,7 +195,7 @@ export default class Expression { if (this.template_scope.is_let(name)) return true; const variable = this.component.var_lookup.get(name); - return variable.mutated; + return variable && variable.mutated; }); } @@ -479,7 +479,7 @@ function isContextual(component: Component, scope: TemplateScope, name: string) const variable = component.var_lookup.get(name); // hoistables, module declarations, and imports are non-contextual - if (variable.hoistable) return false; + if (!variable || variable.hoistable) return false; // assume contextual return true; diff --git a/test/stats/samples/store-referenced/_config.js b/test/stats/samples/store-referenced/_config.js new file mode 100644 index 0000000000..828d227ab8 --- /dev/null +++ b/test/stats/samples/store-referenced/_config.js @@ -0,0 +1,26 @@ +export default { + test(assert, stats) { + assert.deepEqual(stats.vars, [ + { + name: 'foo', + kind: 'let', + import_name: null, + export_name: null, + source: null, + module: false, + mutated: false, + referenced: false + }, + { + name: '$foo', + kind: 'injected', + import_name: null, + export_name: null, + source: null, + module: false, + mutated: true, + referenced: true + } + ]); + } +}; diff --git a/test/stats/samples/store-referenced/input.html b/test/stats/samples/store-referenced/input.html new file mode 100644 index 0000000000..0222372f4d --- /dev/null +++ b/test/stats/samples/store-referenced/input.html @@ -0,0 +1,5 @@ + + +{$foo} \ No newline at end of file diff --git a/test/stats/samples/store-unreferenced/_config.js b/test/stats/samples/store-unreferenced/_config.js new file mode 100644 index 0000000000..52280c13d2 --- /dev/null +++ b/test/stats/samples/store-unreferenced/_config.js @@ -0,0 +1,26 @@ +export default { + test(assert, stats) { + assert.deepEqual(stats.vars, [ + { + name: 'foo', + kind: 'let', + import_name: null, + export_name: null, + source: null, + module: false, + mutated: false, + referenced: false + }, + { + name: '$foo', + kind: 'injected', + import_name: null, + export_name: null, + source: null, + module: false, + mutated: true, + referenced: false + } + ]); + } +}; diff --git a/test/stats/samples/store-unreferenced/input.html b/test/stats/samples/store-unreferenced/input.html new file mode 100644 index 0000000000..3ad525047d --- /dev/null +++ b/test/stats/samples/store-unreferenced/input.html @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/test/stats/samples/undeclared/_config.js b/test/stats/samples/undeclared/_config.js new file mode 100644 index 0000000000..71c255d54b --- /dev/null +++ b/test/stats/samples/undeclared/_config.js @@ -0,0 +1,5 @@ +export default { + test(assert, stats) { + assert.deepEqual(stats.vars, []); + }, +}; diff --git a/test/stats/samples/undeclared/input.html b/test/stats/samples/undeclared/input.html new file mode 100644 index 0000000000..0178466fe3 --- /dev/null +++ b/test/stats/samples/undeclared/input.html @@ -0,0 +1,3 @@ + + +{foo} \ No newline at end of file