Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: macro parameter with default value overshadowed by unrelated macro #791

Merged
merged 2 commits into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ var Compiler = Object.extend({
this._compileAsyncLoop(node, frame, true);
},

_compileMacro: function(node, frame) {
_compileMacro: function(node) {
var args = [];
var kwargs = null;
var funcId = 'macro_' + this.tmpid();
Expand Down Expand Up @@ -824,7 +824,7 @@ var Compiler = Object.extend({
// arguments so support setting positional args with keywords
// args and passing keyword args as positional args
// (essentially default values). See runtime.js.
frame = frame.push();
var frame = new Frame();
this.emitLines(
'var ' + funcId + ' = runtime.makeMacro(',
'[' + argNames.join(', ') + '], ',
Expand Down Expand Up @@ -865,7 +865,6 @@ var Compiler = Object.extend({
this.compile(node.body, frame);
});

frame = frame.pop();
this.emitLine('frame = callerFrame;');
this.emitLine('return new runtime.SafeString(' + bufferId + ');');
this.emitLine('});');
Expand Down
36 changes: 36 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1570,5 +1570,41 @@

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);
});

it('should get right value when macro include macro', function(done) {
render(
'{# macro1 and macro2 definition #}' +
'{% macro macro1() %} foo' +
'{% endmacro %}' +
'' +
'{% macro macro2(text="default") %}' +
'{{macro1()}}' +
'{% endmacro %}' +
'' +
'{# calling macro2 #}' +
'{{macro2("this should be outputted") }}', {}, {}, function(err, res) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit misleading, this text in fact should not be output, in this case

Copy link
Contributor Author

@shepherdwind shepherdwind Jul 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test output asset to equal foo. my last post code is

{# macro1 and macro2 definition #}
{% macro macro1() %}
{% endmacro %}

{% macro macro2(text="default") %}
{{macro1}}
{% endmacro %}

{# calling macro2 #}
{{macro2("this should be outputted") }}

in this case, output will be the macro1 source code. But this output have no problem, because {{macro1}} should output this function toString. so I delete that post, which is mistake.

After I change it to {{macro1()}}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my comment here was just that using the text "this should be outputted" in your test, when that text actually shouldn't be output, is confusing :-) the test and the assert are both correct.

expect(res.trim()).to.eql('foo');
});

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