Skip to content

Commit

Permalink
Improved error msg about no block sub predicate (print out template)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Apr 18, 2016
1 parent 676860a commit ffd5310
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"elem", "local", "wrap", "extend", "oninit", "replace" ],
"strict" : false, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : 3, // {int} Max depth of nested blocks (within functions)
"maxdepth" : 5, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line
Expand Down Expand Up @@ -88,6 +88,7 @@
// Custom Globals
"globals" : {
"module": true,
"test": true
"test": true,
"BEMXJSTError": true
}
}
14 changes: 14 additions & 0 deletions lib/bemxjst/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function BEMXJSTError(msg, func) {
this.name = 'BEMXJSTError';
this.message = msg;

if (Error.captureStackTrace)
Error.captureStackTrace(this, func || this.constructor);
else
this.stack = (new Error()).stack;
}

BEMXJSTError.prototype = Object.create(Error.prototype);
BEMXJSTError.prototype.constructor = BEMXJSTError;

exports.BEMXJSTError = BEMXJSTError;
39 changes: 36 additions & 3 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,42 @@ BEMXJST.prototype.groupEntities = function groupEntities(tree) {
j--;
}

// TODO(indutny): print out the template itself
if (block === null)
throw new Error('block("...") not found in one of the templates');
if (block === null) {
var msg = 'block(…) subpredicate is not found.\n' +
' See template with subpredicates:\n * ';

for (var j = 0; j < template.predicates.length; j++) {
var pred = template.predicates[j];

if (j !== 0)
msg += '\n * ';

if (pred.key === '_mode') {
msg += pred.value + '()';
} else {
if (Array.isArray(pred.key)) {
msg += pred.key[0].replace('mods', 'mod')
.replace('elemMods', 'elemMod') +
'(\'' + pred.key[1] + '\', \'' + pred.value + '\')';
} else if (!pred.value || !pred.key) {
msg += 'match(…)';
} else {
msg += pred.key + '(\'' + pred.value + '\')';
}
}
}

msg += '\n And template body: \n (' +
(typeof template.body === 'function' ?
template.body :
JSON.stringify(template.body)) + ')';

if (typeof BEMXJSTError === 'undefined') {
BEMXJSTError = require('./error').BEMXJSTError;
}

throw new BEMXJSTError(msg);
}

var key = this.classBuilder.build(block, elem);

Expand Down
18 changes: 16 additions & 2 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var vm = require('vm');
var BEMXJSTError = require('./bemxjst/error').BEMXJSTError;

function Compiler(runtime) {
this.runtime = runtime;
Expand Down Expand Up @@ -51,6 +52,19 @@ Compiler.prototype.generate = function generate(code, options) {
return source;
};

var _compile = function _compile(fn, exports) {
try {
fn(exports, console);
} catch (e) {
if (e instanceof BEMXJSTError)
throw new BEMXJSTError(e.message);
else
throw e;
}

return exports;
};

Compiler.prototype.compile = function compile(code, options) {
if (!options) options = {};

Expand All @@ -60,9 +74,9 @@ Compiler.prototype.compile = function compile(code, options) {

var fn = options.context === 'this' ?
vm.runInThisContext(out) :
vm.runInNewContext(out);
vm.runInNewContext(out, { Error: Error, BEMXJSTError: BEMXJSTError });

fn(exports, console);
_compile(fn, exports);

return exports;
};
12 changes: 12 additions & 0 deletions test/templates-syntax-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var fixtures = require('./fixtures')('bemhtml');
var test = fixtures.test;
var assert = require('assert');
var bemxjst = require('../').bemhtml;
var BEMXJSTError = require('../lib/bemxjst/error').BEMXJSTError;

describe('Templates syntax', function() {
it('should support block without mode()', function() {
Expand Down Expand Up @@ -30,4 +33,13 @@ describe('Templates syntax', function() {
{ block: 'b' },
'<b></b>');
});

it('should throw error when no block subpredicate', function() {
assert.throws(function() {
bemxjst.compile(function() {
// No block() subpredicate
elem('e').tag()('b');
});
}, BEMXJSTError);
});
});

0 comments on commit ffd5310

Please sign in to comment.