From 2f867beb2c81e159e5fa535fd5b0b578742b6501 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Dec 2018 22:35:04 -0500 Subject: [PATCH] inject spread props --- src/compile/Component.ts | 36 ++++++++++++++++--- .../samples/spread-own-props/main.html | 2 +- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/compile/Component.ts b/src/compile/Component.ts index f57fe8a534..a264a7853d 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -130,10 +130,6 @@ export default class Component { this.module_script = ast.js.find(script => get_context(script) === 'module'); this.instance_script = ast.js.find(script => get_context(script) === 'default'); - this.walk_module_js(); - this.walk_instance_js(); - this.name = this.getUniqueName(name); - this.meta = process_meta(this, this.ast.html.children); this.namespace = namespaces[this.meta.namespace] || this.meta.namespace; @@ -151,6 +147,10 @@ export default class Component { : options.customElement : this.name; + this.walk_module_js(); + this.walk_instance_js(); + this.name = this.getUniqueName(name); + this.fragment = new Fragment(this, ast.html); if (!options.customElement) this.stylesheet.reify(); @@ -558,7 +558,8 @@ export default class Component { } rewrite_props() { - const { instance_scope, instance_scope_map: map } = this; + const component = this; + const { code, instance_scope, instance_scope_map: map, meta } = this; let scope = instance_scope; // TODO we will probably end up wanting to use this elsewhere @@ -583,11 +584,36 @@ export default class Component { if (node.type === 'VariableDeclaration') { if (node.kind === 'var' || scope === instance_scope) { + let has_meta_props = false; let has_exports = false; let has_only_exports = true; node.declarations.forEach(declarator => { extractNames(declarator.id).forEach(name => { + if (name === meta.props_object) { + if (exported.has(name)) { + component.error(declarator, { + code: 'exported-meta-props', + message: `Cannot export props binding` + }); + } + + if (declarator.id.type !== 'Identifier') { + component.error(declarator, { + code: 'todo', + message: `props binding in destructured declaration is not yet supported` + }); + } + + if (declarator.id.end === declarator.end) { + code.appendLeft(declarator.end, ' = $$props'); + } else { + code.overwrite(declarator.id.end, declarator.end, ' = $$props'); + } + + has_meta_props = true; + } + if (exported.has(name)) { has_exports = true; } else { diff --git a/test/runtime/samples/spread-own-props/main.html b/test/runtime/samples/spread-own-props/main.html index a41baf94dd..5f0a0089b5 100644 --- a/test/runtime/samples/spread-own-props/main.html +++ b/test/runtime/samples/spread-own-props/main.html @@ -3,7 +3,7 @@