Skip to content

Commit

Permalink
compiler: remove explicit context
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed May 10, 2013
1 parent 60b4543 commit 798cc6f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 73 deletions.
31 changes: 12 additions & 19 deletions lib/bemhtml/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,11 @@ Compiler.prototype.replaceBody = function replaceBody(body) {

return estraverse.replace(body, {
leave: function(node) {
if (node.type !== 'CallExpression' ||
node.callee.type !== 'CallExpression') return;
if (node.type !== 'CallExpression') return;

var callee = node.callee;

// apply(ctx)(locals)
if (callee.callee.type === 'Identifier') {
var name = callee.callee.name;
// apply(locals)
if (node.callee.type === 'Identifier') {
var name = node.callee.name;
if (name !== 'apply' &&
name !== 'applyNext' &&
name !== 'applyCtx') {
Expand All @@ -303,10 +300,10 @@ Compiler.prototype.replaceBody = function replaceBody(body) {

return name === 'applyCtx' ? self.replaceApplyCtx(node) :
self.replaceApply(node);
// local(ctx)(locals)(body)
} else if (callee.callee.type === 'CallExpression' &&
callee.callee.callee.type === 'Identifier') {
var name = callee.callee.callee.name;
// local(locals)(body)
} else if (node.callee.type === 'CallExpression' &&
node.callee.callee.type === 'Identifier') {
var name = node.callee.callee.name;
if (name !== 'local') return;

return self.replaceLocal(node);
Expand All @@ -328,15 +325,11 @@ Compiler.prototype.getModeObj = function getModeObj(mode) {
};

Compiler.prototype.replaceApplyCtx = function replaceApplyCtx(ast) {
// (applyCtx(this))(newCtx) => (apply(this))({ _mode: '', ctx: newCtx })
// applyCtx(newCtx) => apply({ _mode: '', ctx: newCtx })
assert.equal(ast.arguments.length, 1, 'applyCtx() must have one argument');
return {
type: 'CallExpression',
callee: {
type: 'CallExpression',
callee: { type: 'Identifier', name: 'apply' },
arguments: ast.callee.arguments
},
callee: { type: 'Identifier', name: 'apply' },
arguments: [{
type: 'ObjectExpression',
properties: [{
Expand All @@ -355,7 +348,7 @@ Compiler.prototype.replaceApplyCtx = function replaceApplyCtx(ast) {
};

Compiler.prototype.replaceApply = function replaceApply(ast) {
// (apply(this))(mode, {})
// apply(mode, {})
return {
type: 'CallExpression',
callee: ast.callee,
Expand All @@ -367,7 +360,7 @@ Compiler.prototype.replaceApply = function replaceApply(ast) {
};

Compiler.prototype.replaceLocal = function replaceLocal(ast) {
// (local(this))(mode, {})(body)
// (local(mode, {}))(body)
return {
type: 'CallExpression',
callee: {
Expand Down
26 changes: 13 additions & 13 deletions lib/bemhtml/i-bem.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ match(this._mode === '')(

this.ctx || (this.ctx = {});

local(this)('default', {
local('default', {
_links: this.ctx.links || this._links,
block: vBlock || (vElem ? block : undefined),
_currBlock: vBlock || vElem ? undefined : block,
Expand All @@ -283,7 +283,7 @@ match(this._mode === '')(
(this.block || this.elem) ?
(this.position = (this.position || 0) + 1) :
this._listLength--;
apply(this)();
apply();
});
}),

Expand All @@ -305,7 +305,7 @@ match(this._mode === '')(

while(i < l) {
var newCtx = v[i++];
apply(this)({ ctx: newCtx === null ? '' : newCtx });
apply({ ctx: newCtx === null ? '' : newCtx });
}

prevNotNewList || (this.position = prevPos);
Expand All @@ -330,24 +330,24 @@ def()(function() {
buf = this._buf,
tag;

tag = apply(this)('tag');
tag = apply('tag');
typeof tag != 'undefined' || (tag = v.tag);
typeof tag != 'undefined' || (tag = 'div');

if(tag) {
var jsParams, js;
if(this.block && v.js !== false) {
js = apply(this)('js');
js = apply('js');
js = js? this._.extend(v.js, js === true? {} : js) : v.js === true? {} : v.js;
js && ((jsParams = {})[BEM_.INTERNAL.buildClass(this.block, v.elem)] = js);
}

buf.push('<', tag);

var isBEM = apply(this)('bem');
var isBEM = apply('bem');
typeof isBEM != 'undefined' || (isBEM = typeof v.bem != 'undefined' ? v.bem : v.block || v.elem);

var cls = apply(this)('cls');
var cls = apply('cls');
cls || (cls = v.cls);

var addJSInitClass = v.block && jsParams;
Expand All @@ -357,7 +357,7 @@ def()(function() {

BEM_.INTERNAL.buildClasses(this.block, v.elem, v.elemMods || v.mods, buf);

var mix = apply(this)('mix');
var mix = apply('mix');
v.mix && (mix = mix? mix.concat(v.mix) : v.mix);

if(mix) {
Expand Down Expand Up @@ -393,7 +393,7 @@ def()(function() {
// Process nested mixes
if (hasItem && !visited[visitedKey(block, elem)]) {
visited[visitedKey(block, elem)] = true;
var nestedMix = apply(this)({
var nestedMix = apply({
block: block,
elem: elem
}, 'mix');
Expand Down Expand Up @@ -425,14 +425,14 @@ def()(function() {
}

if(jsParams) {
var jsAttr = apply(this)('jsAttr');
var jsAttr = apply('jsAttr');
buf.push(
' ', jsAttr || 'onclick', '="return ',
this._.attrEscape(JSON.stringify(jsParams)),
'"');
}

var attrs = apply(this)('attrs');
var attrs = apply('attrs');
attrs = this._.extend(attrs, v.attrs); // NOTE: возможно стоит делать массив, чтобы потом быстрее сериализовывать
if(attrs) {
var name; // TODO: разобраться с OmetaJS и YUI Compressor
Expand All @@ -448,10 +448,10 @@ def()(function() {
} else {
tag && buf.push('>');

var content = apply(this)('content');
var content = apply('content');
if(content || content === 0) {
var isBEM = this.block || this.elem;
apply(this)('', {
apply('', {
_notNewList: false,
position: isBEM ? 1 : this.position,
_listLength: isBEM ? 1 : this._listLength,
Expand Down
66 changes: 29 additions & 37 deletions lib/bemhtml/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,53 +93,45 @@ module.exports = function() {

// Apply by mode, local by mode and applyCtx
apply = function(apply) {
return function(ctx) {
return function() {
var args = Array.prototype.map.call(arguments, function(arg) {
if (typeof arg === 'string') {
return { _mode: arg };
} else {
return arg;
}
});
return apply(ctx).apply(null, args);
};
return function bemApply() {
var args = Array.prototype.map.call(arguments, function(arg) {
if (typeof arg === 'string') {
return { _mode: arg };
} else {
return arg;
}
});
return apply.apply(null, args);
};
}(apply);

applyNext = function(applyNext) {
return function(ctx) {
return function() {
var args = Array.prototype.map.call(arguments, function(arg) {
if (typeof arg === 'string') {
return { _mode: arg };
} else {
return arg;
}
});
return applyNext(ctx).apply(null, args);
};
return function bemApplyNext() {
var args = Array.prototype.map.call(arguments, function(arg) {
if (typeof arg === 'string') {
return { _mode: arg };
} else {
return arg;
}
});
return applyNext.apply(null, args);
};
}(applyNext);

local = function(local) {
return function(ctx) {
return function() {
var args = Array.prototype.map.call(arguments, function(arg) {
if (typeof arg === 'string') {
return { _mode: arg };
} else {
return arg;
}
});
return local(ctx).apply(null, args);
};
return function bemLocal() {
var args = Array.prototype.map.call(arguments, function(arg) {
if (typeof arg === 'string') {
return { _mode: arg };
} else {
return arg;
}
});
return local.apply(null, args);
};
}(local);

function applyCtx(ctx) {
return function(context) {
return applyNext(ctx)({ _mode: '', ctx: context });
};
function applyCtx(context) {
return applyNext({ _mode: '', ctx: context });
};
}.toString().replace(/^function\s*\(\)\s*{|}$/g, '');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"estraverse": "~1.1.1",
"esprima": "~1.0.2",
"ometajs": "~3.2.2",
"uglify-js": "~2.2.5",
"uglify-js": "~2.3.2",
"xjst": "~0.6.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('BEM.js compiler', function() {
elem('e2').match(function() {
return this.ctx.hooray();
})(function() {
return apply(this)('mode', { 'a': 1 });
return apply('mode', { 'a': 1 });
})
);
}, { block: 'b1', elem: 'e1' }, '<a class="b1__e1"></a>');
Expand All @@ -31,7 +31,7 @@ describe('BEM.js compiler', function() {
it('should understand applyCtx', function() {
test(function() {
block('b1').content()(function() {
return applyCtx(this)({ block: 'b2' });
return applyCtx({ block: 'b2' });
});
block('b2').tag()('li');
}, { block: 'b1' }, '<div class="b1"><li class="b2"></li></div>');
Expand All @@ -40,7 +40,7 @@ describe('BEM.js compiler', function() {
it('should work without ibem', function() {
test(function() {
block('b1')(function() {
return '<div class=b1>' + apply(this)('content') + '</div>'
return '<div class=b1>' + apply('content') + '</div>'
});
block('b1').content()('ahhaha');
}, { block: 'b1' }, '<div class=b1>ahhaha</div>', {
Expand Down

0 comments on commit 798cc6f

Please sign in to comment.