Skip to content

Commit 4512766

Browse files
mohd-akramjaylinski
authored andcommitted
Fix non-contiguous program indices
1 parent e497a35 commit 4512766

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

lib/handlebars/compiler/javascript-compiler.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,10 @@ JavaScriptCompiler.prototype = {
165165

166166
let { programs, decorators } = this.context;
167167
for (i = 0, l = programs.length; i < l; i++) {
168-
if (programs[i]) {
169-
ret[i] = programs[i];
170-
if (decorators[i]) {
171-
ret[i + '_d'] = decorators[i];
172-
ret.useDecorators = true;
173-
}
168+
ret[i] = programs[i];
169+
if (decorators[i]) {
170+
ret[i + '_d'] = decorators[i];
171+
ret.useDecorators = true;
174172
}
175173
}
176174

@@ -914,8 +912,8 @@ JavaScriptCompiler.prototype = {
914912
let existing = this.matchExistingProgram(child);
915913

916914
if (existing == null) {
917-
this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
918-
let index = this.context.programs.length;
915+
// Placeholder to prevent name conflicts for nested children
916+
let index = this.context.programs.push('') - 1;
919917
child.index = index;
920918
child.name = 'program' + index;
921919
this.context.programs[index] = compiler.compile(
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
define(['handlebars.runtime'], function(Handlebars) {
22
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3-
return templates['known.helpers'] = template({"1":function(container,depth0,helpers,partials,data) {
3+
return templates['known.helpers'] = template({"0":function(container,depth0,helpers,partials,data) {
44
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
55
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
66
return parent[propertyName];
77
}
88
return undefined
99
};
1010
return " <div>Some known helper</div>\n"
11-
+ ((stack1 = lookupProperty(helpers,"anotherHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"anotherHelper","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":22}}})) != null ? stack1 : "");
12-
},"2":function(container,depth0,helpers,partials,data) {
11+
+ ((stack1 = lookupProperty(helpers,"anotherHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"anotherHelper","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":22}}})) != null ? stack1 : "");
12+
},"1":function(container,depth0,helpers,partials,data) {
1313
return " <div>Another known helper</div>\n";
1414
},"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
1515
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
@@ -18,7 +18,7 @@ return parent[propertyName];
1818
}
1919
return undefined
2020
};
21-
return ((stack1 = lookupProperty(helpers,"someHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"someHelper","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":15}}})) != null ? stack1 : "");
21+
return ((stack1 = lookupProperty(helpers,"someHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"someHelper","hash":{},"fn":container.program(0, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":15}}})) != null ? stack1 : "");
2222
},"useData":true});
2323
});
2424

spec/runtime.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ describe('runtime', function() {
9191
it('should expose child template', function() {
9292
var template = Handlebars.compile('{{#foo}}bar{{/foo}}');
9393
// Calling twice to hit the non-compiled case.
94-
equal(template._child(1)(), 'bar');
95-
equal(template._child(1)(), 'bar');
94+
equal(template._child(0)(), 'bar');
95+
equal(template._child(0)(), 'bar');
9696
});
9797
it('should render depthed content', function() {
9898
var template = Handlebars.compile('{{#foo}}{{../bar}}{{/foo}}');
9999
// Calling twice to hit the non-compiled case.
100-
equal(template._child(1, undefined, [], [{ bar: 'baz' }])(), 'baz');
100+
equal(template._child(0, undefined, [], [{ bar: 'baz' }])(), 'baz');
101101
});
102102
});
103103

0 commit comments

Comments
 (0)