diff --git a/lib/bemhtml/compiler.js b/lib/bemhtml/compiler.js index 310b113a..c77ecb78 100644 --- a/lib/bemhtml/compiler.js +++ b/lib/bemhtml/compiler.js @@ -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') { @@ -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); @@ -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: [{ @@ -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, @@ -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: { diff --git a/lib/bemhtml/i-bem.js b/lib/bemhtml/i-bem.js index 05a04984..689211aa 100644 --- a/lib/bemhtml/i-bem.js +++ b/lib/bemhtml/i-bem.js @@ -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, @@ -283,7 +283,7 @@ match(this._mode === '')( (this.block || this.elem) ? (this.position = (this.position || 0) + 1) : this._listLength--; - apply(this)(); + apply(); }); }), @@ -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); @@ -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; @@ -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) { @@ -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'); @@ -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 @@ -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, diff --git a/lib/bemhtml/runtime.js b/lib/bemhtml/runtime.js index 41dd3f31..0c56c905 100644 --- a/lib/bemhtml/runtime.js +++ b/lib/bemhtml/runtime.js @@ -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, ''); diff --git a/package.json b/package.json index 49a509f6..95be9cb0 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/api-test.js b/test/api-test.js index 65971cd4..dcdb1003 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -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' }, ''); @@ -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' }, '
  • '); @@ -40,7 +40,7 @@ describe('BEM.js compiler', function() { it('should work without ibem', function() { test(function() { block('b1')(function() { - return '
    ' + apply(this)('content') + '
    ' + return '
    ' + apply('content') + '
    ' }); block('b1').content()('ahhaha'); }, { block: 'b1' }, '
    ahhaha
    ', {