From 86c2e847b2936b544b1b140256685b9a53bd127e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 17 Jul 2018 14:11:07 -0400 Subject: [PATCH 1/2] null out refs to dynamic components - fixes #1596 --- src/compile/nodes/Component.ts | 2 ++ .../dynamic-component-nulled-out/Foo.html | 1 + .../dynamic-component-nulled-out/_config.js | 21 +++++++++++++++++++ .../dynamic-component-nulled-out/main.html | 11 ++++++++++ 4 files changed, 35 insertions(+) create mode 100644 test/runtime/samples/dynamic-component-nulled-out/Foo.html create mode 100644 test/runtime/samples/dynamic-component-nulled-out/_config.js create mode 100644 test/runtime/samples/dynamic-component-nulled-out/main.html diff --git a/src/compile/nodes/Component.ts b/src/compile/nodes/Component.ts index 0f7cd57cb8..a659f5f5f3 100644 --- a/src/compile/nodes/Component.ts +++ b/src/compile/nodes/Component.ts @@ -429,6 +429,8 @@ export default class Component extends Node { `)} ${this.ref && `#component.refs.${this.ref} = ${name};`} + } else { + ${name} = null; } ${this.ref && deindent` diff --git a/test/runtime/samples/dynamic-component-nulled-out/Foo.html b/test/runtime/samples/dynamic-component-nulled-out/Foo.html new file mode 100644 index 0000000000..c62ee08e19 --- /dev/null +++ b/test/runtime/samples/dynamic-component-nulled-out/Foo.html @@ -0,0 +1 @@ +

Foo

\ No newline at end of file diff --git a/test/runtime/samples/dynamic-component-nulled-out/_config.js b/test/runtime/samples/dynamic-component-nulled-out/_config.js new file mode 100644 index 0000000000..17e51546ab --- /dev/null +++ b/test/runtime/samples/dynamic-component-nulled-out/_config.js @@ -0,0 +1,21 @@ +export default { + html: ` +

Foo

+ `, + + nestedTransitions: true, + + test(assert, component, target) { + const state = component.get(); + + component.set({ Foo: null }); + + assert.htmlEqual(target.innerHTML, ``); + + component.set({ Foo: state.Foo }); + + assert.htmlEqual(target.innerHTML, ` +

Foo

+ `); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/dynamic-component-nulled-out/main.html b/test/runtime/samples/dynamic-component-nulled-out/main.html new file mode 100644 index 0000000000..6a1a348557 --- /dev/null +++ b/test/runtime/samples/dynamic-component-nulled-out/main.html @@ -0,0 +1,11 @@ + + + \ No newline at end of file From 661bbe2c2461f03fbcb7fafe7bba8e32daa002ec Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 17 Jul 2018 14:21:00 -0400 Subject: [PATCH 2/2] dont generate nonsensical code --- src/compile/nodes/Component.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/compile/nodes/Component.ts b/src/compile/nodes/Component.ts index a659f5f5f3..f65d8be4f6 100644 --- a/src/compile/nodes/Component.ts +++ b/src/compile/nodes/Component.ts @@ -431,12 +431,11 @@ export default class Component extends Node { ${this.ref && `#component.refs.${this.ref} = ${name};`} } else { ${name} = null; - } - - ${this.ref && deindent` - else if (#component.refs.${this.ref} === ${name}) { + ${this.ref && deindent` + if (#component.refs.${this.ref} === ${name}) { #component.refs.${this.ref} = null; }`} + } } `);