Skip to content

Commit

Permalink
Merge pull request #1199 from sveltejs/gh-1195
Browse files Browse the repository at this point in the history
Fix select_block_type scoping issue
  • Loading branch information
Rich-Harris authored Mar 5, 2018
2 parents 8aaf92a + a2873df commit da165be
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/generators/nodes/IfBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,12 @@ function compound(
const current_block_type = block.getUniqueName(`current_block_type`);
const current_block_type_and = hasElse ? '' : `${current_block_type} && `;

generator.blocks.push(deindent`
block.builders.init.addBlock(deindent`
function ${select_block_type}(state) {
${branches
.map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block};`)
.join('\n')}
}
`);

block.builders.init.addBlock(deindent`
var ${current_block_type} = ${select_block_type}(state);
var ${name} = ${current_block_type_and}${current_block_type}(#component, state);
`);
Expand Down
9 changes: 4 additions & 5 deletions test/js/samples/if-block-no-update/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ var proto = {
function create_main_fragment(component, state) {
var if_block_anchor;

function select_block_type(state) {
if (state.foo) return create_if_block;
return create_if_block_1;
}
var current_block_type = select_block_type(state);
var if_block = current_block_type(component, state);

Expand Down Expand Up @@ -268,11 +272,6 @@ function create_if_block_1(component, state) {
};
}

function select_block_type(state) {
if (state.foo) return create_if_block;
return create_if_block_1;
}

function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);
Expand Down
9 changes: 4 additions & 5 deletions test/js/samples/if-block-no-update/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { assign, createComment, createElement, detachNode, init, insertNode, noo
function create_main_fragment(component, state) {
var if_block_anchor;

function select_block_type(state) {
if (state.foo) return create_if_block;
return create_if_block_1;
}
var current_block_type = select_block_type(state);
var if_block = current_block_type(component, state);

Expand Down Expand Up @@ -83,11 +87,6 @@ function create_if_block_1(component, state) {
};
}

function select_block_type(state) {
if (state.foo) return create_if_block;
return create_if_block_1;
}

function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);
Expand Down
9 changes: 9 additions & 0 deletions test/runtime/samples/if-block-else-in-each/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
data: {
array: [true, false],
},
html: `
<div>foo</div>
<div>bar</div>
`,
};
7 changes: 7 additions & 0 deletions test/runtime/samples/if-block-else-in-each/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#each array as item}}
{{#if item}}
<div>foo</div>
{{else}}
<div>bar</div>
{{/if}}
{{/each}}

0 comments on commit da165be

Please sign in to comment.