From 96c4455af933636e3450df8386d65fabb7b4db68 Mon Sep 17 00:00:00 2001 From: Jacob Wright <jacwright@gmail.com> Date: Fri, 24 Aug 2018 14:01:51 -0600 Subject: [PATCH] Fixes an error with outros and elseifs This is a fix for when an elseif doesn't have a final else, and the following error was thrown: ``` TypeError: Cannot read property 'o' of undefined ``` See https://svelte.technology/repl?version=2.12.0&gist=c33d308077447f8ba06b79d8ef5ab1e4 --- src/compile/nodes/IfBlock.ts | 7 ++++--- .../if-block-outro-nested-else/Component.html | 1 + .../if-block-outro-nested-else/_config.js | 8 ++++++++ .../samples/if-block-outro-nested-else/main.html | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/runtime/samples/if-block-outro-nested-else/Component.html create mode 100644 test/runtime/samples/if-block-outro-nested-else/_config.js create mode 100644 test/runtime/samples/if-block-outro-nested-else/main.html diff --git a/src/compile/nodes/IfBlock.ts b/src/compile/nodes/IfBlock.ts index 2a5a49af16..eef654b90b 100644 --- a/src/compile/nodes/IfBlock.ts +++ b/src/compile/nodes/IfBlock.ts @@ -139,9 +139,10 @@ export default class IfBlock extends Node { this.buildCompoundWithOutros(block, parentNode, parentNodes, branches, dynamic, vars); if (this.compiler.options.nestedTransitions) { - block.builders.outro.addLine( - `${name}.o(#outrocallback);` - ); + block.builders.outro.addBlock(deindent` + if (${name}) ${name}.o(#outrocallback); + else #outrocallback(); + `); } } else { this.buildCompound(block, parentNode, parentNodes, branches, dynamic, vars); diff --git a/test/runtime/samples/if-block-outro-nested-else/Component.html b/test/runtime/samples/if-block-outro-nested-else/Component.html new file mode 100644 index 0000000000..281c6866c3 --- /dev/null +++ b/test/runtime/samples/if-block-outro-nested-else/Component.html @@ -0,0 +1 @@ +<div></div> \ No newline at end of file diff --git a/test/runtime/samples/if-block-outro-nested-else/_config.js b/test/runtime/samples/if-block-outro-nested-else/_config.js new file mode 100644 index 0000000000..4fe5eaa926 --- /dev/null +++ b/test/runtime/samples/if-block-outro-nested-else/_config.js @@ -0,0 +1,8 @@ +export default { + nestedTransitions: true, + + test ( assert, component, target ) { + // Would cause "TypeError: Cannot read property 'o' of undefined" + component.set({ foo: false }); + } +}; diff --git a/test/runtime/samples/if-block-outro-nested-else/main.html b/test/runtime/samples/if-block-outro-nested-else/main.html new file mode 100644 index 0000000000..02e000b3c3 --- /dev/null +++ b/test/runtime/samples/if-block-outro-nested-else/main.html @@ -0,0 +1,16 @@ +{#if foo} + {#if false} + <Component/> + {:elseif false} + <Component/> + {/if} +{/if} + +<script> + export default { + components: { + Component: './Component.html', + }, + data: () => ({ foo: true }), + } +</script>