diff --git a/lib/bemxjst/utils.js b/lib/bemxjst/utils.js index 8e7ce5b7..48c067e9 100644 --- a/lib/bemxjst/utils.js +++ b/lib/bemxjst/utils.js @@ -78,3 +78,31 @@ exports.identify = function identify(obj, onlyGet) { obj[uniqExpando] = u; return u; }; + +exports.fnToString = function fnToString(code) { + // It is fine to compile without templates at first + if (!code) + return ''; + + if (typeof code === 'function') { + // Examples: + // function () { … } + // function name() { … } + // function (a, b) { … } + // function name(a, b) { … } + var regularFunction = /^function\s*[^{]+{|}$/g; + + // Examples: + // () => { … } + // (a, b) => { … } + // _ => { … } + var arrowFunction = /^(_|\(\w|[^=>]+\))\s=>\s{|}$/g; + + code = code.toString(); + code = code.replace( + code.indexOf('function') === 0 ? regularFunction : arrowFunction, + ''); + } + + return code; +}; diff --git a/lib/compiler.js b/lib/compiler.js index 6e50cbe4..8b2f4e16 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,5 +1,6 @@ var vm = require('vm'); var BEMXJSTError = require('./bemxjst/error').BEMXJSTError; +var fnToString = require('./bemxjst/utils').fnToString; function Compiler(runtime) { this.runtime = runtime; @@ -10,30 +11,7 @@ exports.Compiler = Compiler; Compiler.prototype.generate = function generate(code, options) { if (!options) options = {}; - // It is fine to compile without templates at first - if (!code) - code = ''; - - // Yeah, let people pass functions to us! - if (typeof code === 'function') { - // Examples: - // function () { … } - // function name() { … } - // function (a, b) { … } - // function name(a, b) { … } - var regularFunction = /^function\s*[^{]+{|}$/g; - - // Examples: - // () => { … } - // (a, b) => { … } - // _ => { … } - var arrowFunction = /^(_|\(\w|[^=>]+\))\s=>\s{|}$/g; - - code = code.toString(); - code = code.replace( - code.indexOf('function') === 0 ? regularFunction : arrowFunction, - ''); - } + code = fnToString(code); var exportName = options.exportName || 'BEMHTML'; var engine = options.engine || 'BEMHTML'; @@ -97,3 +75,22 @@ Compiler.prototype.compile = function compile(code, options) { return exports; }; + +Compiler.prototype._compile = function _compile(code, options) { + if (!options) options = {}; + + // It is fine to compile without templates at first + if (!code) + code = function() {}; + + var engineName = options.engine || 'BEMHTML'; + + var Engine = require('./' + engineName.toLowerCase()); + var api = new Engine(options); + + var template = {}; + api.compile(code); + api.exportApply(template); + + return template; +}; diff --git a/package.json b/package.json index 51309fc0..da8713dc 100644 --- a/package.json +++ b/package.json @@ -20,14 +20,14 @@ "scripts": { "prepublish": "npm run make", "preversion": "bash scripts/update-authors.sh && git add AUTHORS && git commit -m \"update AUTHORS\" || true", - "make-bemhtml": "browserify --standalone bemhtml lib/bemhtml/index.js -o lib/bemhtml/bundle.js", - "make-bemtree": "browserify --standalone bemtree lib/bemtree/index.js -o lib/bemtree/bundle.js", - "make": "npm run make-bemhtml && npm run make-bemtree", + "make": "npm run make:bemhtml && npm run make:bemtree", + "make:bemhtml": "browserify --standalone bemhtml lib/bemhtml/index.js -o lib/bemhtml/bundle.js --debug", + "make:bemtree": "browserify --standalone bemtree lib/bemtree/index.js -o lib/bemtree/bundle.js --debug", "clean": "rm -f lib/bem{html,tree}/bundle.js", - "lint": "jscs `ls lib/*.js lib/**/*.js test/*.js | grep -v bundle` && jshint `ls lib/*.js lib/**/*.js test/*.js | grep -v bundle`", - "mocha-test": "mocha --reporter=spec test/*-test.js", - "test": "npm run make && npm run lint && npm run coverage", - "coverage": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -u bdd -R spec --recursive test/*-test.js" + "test": "npm run make && npm run test:lint && npm run test:coverage", + "test:lint": "jscs `ls lib/*.js lib/**/*.js test/*.js | grep -v bundle` && jshint `ls lib/*.js lib/**/*.js test/*.js | grep -v bundle`", + "test:mocha": "mocha --reporter=spec test/*-test.js", + "test:coverage": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -u bdd -R spec test/*-test.js" }, "repository": { "type": "git", @@ -46,11 +46,11 @@ }, "devDependencies": { "browserify": "^13.0.1", + "istanbul": "0.4.4", + "chai": "3.5.0", "jscs": "^3.0.3", "jshint": "^2.7.0", - "mocha": "2.5.3", - "chai": "3.5.0", - "istanbul": "0.4.x" + "mocha": "2.5.3" }, "author": "Fedor Indutny ", "maintainers": [ diff --git a/test/bemtree-test.js b/test/bemtree-test.js index 5cdfbb9d..33beafc3 100644 --- a/test/bemtree-test.js +++ b/test/bemtree-test.js @@ -1,7 +1,13 @@ var assert = require('assert'); var fixtures = require('./fixtures')('bemtree'); var bemxjst = require('../index').bemtree; -var test = fixtures.test; +var fixturesTest = fixtures.test; + +var test = function(fn, data, expected) { + return fixturesTest(fn, data, expected, { + engine: 'BEMTREE' + }); +}; describe('BEMTREE engine tests', function() { describe('applyNext()', function() { diff --git a/test/fixtures.js b/test/fixtures.js index 154ab48e..800cd5fe 100644 --- a/test/fixtures.js +++ b/test/fixtures.js @@ -6,7 +6,7 @@ require('chai').should(); module.exports = function(engine) { function fail(fn, regexp) { assert.throws(function() { - bemxjst[engine].compile(fn); + bemxjst[engine]._compile(fn); }, regexp); } @@ -15,7 +15,8 @@ module.exports = function(engine) { options = fn; fn = function() {}; } - return bemxjst[engine].compile(fn, options || {}); + + return bemxjst[engine]._compile(fn, options || {}); } /**