Skip to content

Commit

Permalink
Ensure that existing blockParams and depths are respected on dupe pro…
Browse files Browse the repository at this point in the history
…grams

Fixes #1186
  • Loading branch information
charleso committed Mar 7, 2016
1 parent 27212f5 commit c7be766
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,11 @@ JavaScriptCompiler.prototype = {
child = children[i];
compiler = new this.compiler(); // eslint-disable-line new-cap

let index = this.matchExistingProgram(child);
let existing = this.matchExistingProgram(child);

if (index == null) {
if (existing == null) {
this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
index = this.context.programs.length;
let index = this.context.programs.length;
child.index = index;
child.name = 'program' + index;
this.context.programs[index] = compiler.compile(child, options, this.context, !this.precompile);
Expand All @@ -714,20 +714,22 @@ JavaScriptCompiler.prototype = {

this.useDepths = this.useDepths || compiler.useDepths;
this.useBlockParams = this.useBlockParams || compiler.useBlockParams;
child.useDepths = this.useDepths;
child.useBlockParams = this.useBlockParams;
} else {
child.index = index;
child.name = 'program' + index;
child.index = existing.index;
child.name = 'program' + existing.index;

this.useDepths = this.useDepths || child.useDepths;
this.useBlockParams = this.useBlockParams || child.useBlockParams;
this.useDepths = this.useDepths || existing.useDepths;
this.useBlockParams = this.useBlockParams || existing.useBlockParams;
}
}
},
matchExistingProgram: function(child) {
for (let i = 0, len = this.context.environments.length; i < len; i++) {
let environment = this.context.environments[i];
if (environment && environment.equals(child)) {
return i;
return environment;
}
}
},
Expand Down
9 changes: 9 additions & 0 deletions spec/regressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,13 @@ describe('Regressions', function() {
' 1. IF: John--\n'
+ ' 2. MYIF: John==\n');
});

it('GH-1186: Support block params for existing programs', function() {
var string =
'{{#*inline "test"}}{{> @partial-block }}{{/inline}}'
+ '{{#>test }}{{#each listOne as |item|}}{{ item }}{{/each}}{{/test}}'
+ '{{#>test }}{{#each listTwo as |item|}}{{ item }}{{/each}}{{/test}}';

shouldCompileTo(string, { listOne: ['a'], listTwo: ['b']}, 'ab', '');
});
});

0 comments on commit c7be766

Please sign in to comment.