Skip to content

Commit

Permalink
Fixed number changing to string after add filter or set from variable
Browse files Browse the repository at this point in the history
  • Loading branch information
imbcmdth committed Feb 18, 2012
1 parent fe422d3 commit 81d6990
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ exports.compile = function compile(indent, parentBlock) {
parent,
filepath,
blockname,
varOutput;
varOutput,
wrappedInMethod;

indent = indent || '';

Expand Down Expand Up @@ -377,14 +378,16 @@ exports.compile = function compile(indent, parentBlock) {
args = (token.args && token.args.length) ? token.args : '';

code += 'if (typeof _context !== "undefined" && typeof _context' + key + ' === "function") {\n';
code += ' _output += ' + helpers.wrapMethod('', { name: name, args: args }, '_context') + ';\n';
wrappedInMethod = helpers.wrapMethod('', { name: name, args: args }, '_context');
code += ' _output = (typeof _output === "undefined") ? ' + wrappedInMethod + ': _output + ' + wrappedInMethod + ';\n';
if (helpers.isValidName(name)) {
code += '} else if (typeof ' + name + ' === "function") {\n';
code += ' _output += ' + helpers.wrapMethod('', { name: name, args: args }) + ';\n';
wrappedInMethod = helpers.wrapMethod('', { name: name, args: args });
code += ' _output = (typeof _output === "undefined") ? ' + wrappedInMethod + ': _output + ' + wrappedInMethod + ';\n';
}
code += '} else {\n';
code += helpers.setVar('__' + name, token);
code += ' _output += __' + name + ';\n';
code += ' _output = (typeof _output === "undefined") ? __' + name + ': _output + __' + name + ';\n';
code += '}\n';
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tags/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (indent, parentBlock, parser) {
value = parser.parseVariable(value);
return ' ' + varname + ' = ' +
'(function () {\n' +
' var _output = "";\n' +
' var _output;\n' +
parser.compile.apply({ tokens: [value] }, [indent, parentBlock]) + '\n' +
' return _output;\n' +
'})();\n';
Expand Down
4 changes: 2 additions & 2 deletions lib/tags/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ exports.set = testCase({
'set number is really a number [gh-53]': function (test) {
test.strictEqual(swig.compile('{% set foo = 1 %}{{ foo|add(1) }}')(), '2', 'literal number is a number');
test.strictEqual(swig.compile('{% set foo = "1" %}{{ foo|add(1) }}')(), '11', 'string number is not a number');
test.strictEqual(swig.compile('{% set bar = 1 %}{% set foo = bar %}{{ foo|add(1) }}')(), '2', 'number variable remains a number after set');
test.strictEqual(swig.compile('{% set foo = 1 %}{% set foo = foo|add(1) %}{{ foo|add(1) }}')(), '3', 'number variable remains a number after filter');
test.strictEqual(swig.compile('{% set bar = 1 %} {% set foo = bar %}{{ foo|add(1) }}')(), ' 2', 'number remains a number after set from variable');
test.strictEqual(swig.compile('{% set foo = 1 %} {% set foo = foo|add(1) %}{% set foo = foo|add(1) %}{{ foo|add(1) }}')(), ' 4', 'number remains a number after set from filter');
test.done();
}
});

0 comments on commit 81d6990

Please sign in to comment.