Skip to content

Commit

Permalink
fix: macro parameter with default value overshadowed by unrelated macro
Browse files Browse the repository at this point in the history
close #774
  • Loading branch information
shepherdwind authored and 翰文 committed Jul 12, 2016
1 parent 221fec8 commit 3d6411c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,12 @@ var Compiler = Object.extend({
var name = node.value;
var v;

if((v = frame.lookup(name))) {
// if current scope has name in frame.store, than mean there is
// an formal parameter variable name as `node.value`, so if this
// parameter name can lookup from scope context, than parameter will
// replace by scope value.
// see https://github.com/mozilla/nunjucks/issues/774
if(!frame.has(name) && (v = frame.lookup(name))) {
this.emit(v);
}
else {
Expand Down Expand Up @@ -855,6 +860,7 @@ var Compiler = Object.extend({
'kwargs["' + name + '"] : ');
this._compileExpression(pair.value, frame);
this.emitLine(');');
frame.addKey(name);
}, this);
}

Expand Down
10 changes: 10 additions & 0 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ var Frame = Obj.extend({
this.variables = {};
this.parent = parent;
this.topLevel = false;
this.store = {};
// if this is true, writes (set) should never propagate upwards past
// this frame to its parent (though reads may).
this.isolateWrites = isolateWrites;
},

// add key to store, so we can use is some later
addKey: function(key) {
this.store[key] = true;
},

has: function(key) {
return this.store[key] === true;
},

set: function(name, val, resolveUp) {
// Allow variables with dots by automatically creating the
// nested structure
Expand Down
18 changes: 18 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1570,5 +1570,23 @@

finish(done);
});

it('should get right value when macro parameter conflict with global macro name', function(done) {
render(
'{# macro1 and macro2 definition #}' +
'{% macro macro1() %}' +
'{% endmacro %}' +
'' +
'{% macro macro2(macro1="default") %}' +
'{{macro1}}' +
'{% endmacro %}' +
'' +
'{# calling macro2 #}' +
'{{macro2("this should be outputted") }}', {}, {}, function(err, res) {
expect(res.trim()).to.eql('this should be outputted');
});

finish(done);
});
});
})();

0 comments on commit 3d6411c

Please sign in to comment.