Skip to content

Commit

Permalink
compiler: cache should work with __$history
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed May 27, 2013
1 parent f03b001 commit 8b76f70
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/bemhtml/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,15 @@ Compiler.prototype.traceChanges = function traceChanges(changes, expr) {
}
};

function check(expr) {
return {
type: 'IfStatement',
test: history,
consequent: { type: 'ExpressionStatement', expression: expr },
alternate: null
};
}

// function() { push_history(); invoke local; }.call(this)
var res = { type: 'Identifier', name: '__$r' };
return {
Expand Down Expand Up @@ -505,21 +514,19 @@ Compiler.prototype.traceChanges = function traceChanges(changes, expr) {
id: res,
init: null
}]
}, {
type: 'ExpressionStatement',
expression: save
}, {
},
check(save),
{
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: res,
right: expr
}
}, {
type: 'ExpressionStatement',
expression: restore
}, {
},
check(restore),
{
type: 'ReturnStatement',
argument: res
}]
Expand Down
44 changes: 44 additions & 0 deletions test/cache-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var bem = require('..');
var assert = require('assert');
var utile = require('utile');

describe('BEM.js cache', function() {
function test(fn, data, expected, options) {
if (!options) options = {};

var body = (options.ibem !== false ? require('./fixtures/i-bem') : '') +
';\n' +
fn.toString().replace(/^function\s*\(\)\s*{|}$/g, '');
var fns = [
bem.compile(body, utile.mixin({}, options, { cache: true }))
];

fns.forEach(function(fn) {
assert.deepEqual(fn.apply.call(data || {}), expected);
});
}

it('should work with locals', function() {
test(function() {
match()(function() {
return local('mode', { x: 1, y: this.x.y, z: this[false] })(function() {
return this.__$history
});
});
}, { __$history: [] }, [
['_mode', 'mode'],
['x',1],
['y','x.y',1]
], { ibem: false });
});

it('should work without history', function() {
test(function() {
match()(function() {
return local('mode', { x: 1 })(function() {
return 'ok';
});
});
}, {}, 'ok', { ibem: false });
});
});

0 comments on commit 8b76f70

Please sign in to comment.