Skip to content

Commit

Permalink
Coverage bundles with source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Aug 18, 2016
1 parent f68ecd8 commit 371e94b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
28 changes: 28 additions & 0 deletions lib/bemxjst/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
45 changes: 21 additions & 24 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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;
};
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 <[email protected]>",
"maintainers": [
Expand Down
8 changes: 7 additions & 1 deletion test/bemtree-test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -15,7 +15,8 @@ module.exports = function(engine) {
options = fn;
fn = function() {};
}
return bemxjst[engine].compile(fn, options || {});

return bemxjst[engine]._compile(fn, options || {});
}

/**
Expand Down

0 comments on commit 371e94b

Please sign in to comment.