Skip to content

Commit

Permalink
Implemented a aimple debug macro to throw custom debug messages.
Browse files Browse the repository at this point in the history
Usage in a comment:

//@debugMessage('message', [Error|Warning|Info])

very basic manipalution of the estree but should do it for now.
  • Loading branch information
Kanaye committed Sep 11, 2016
1 parent 43c6ccd commit c0115f1
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions build/tasks/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,38 @@ module.exports = function (grunt) {
if (func) {
// FunctionExpression.BlockStatement.body(Array)
var funcBody = func.body.body;
esprima.parse('var __DEBUG_METHOD = ' + toValueString(data) + '; blocks.debug && blocks.debug.checkArgs(__DEBUG_METHOD, Array.prototype.slice.call(arguments), {});').body.reverse().forEach(function (chunk) {
funcBody.unshift(chunk);
funcBody.unshift.apply(funcBody, esprima.parse('var __DEBUG_METHOD = ' + toValueString(data) + '; blocks.debug && blocks.debug.checkArgs(__DEBUG_METHOD, Array.prototype.slice.call(arguments), {});').body);
}
}
});

var estraverse = require('estraverse');
var regexDebugMessage = /@debugMessage\((:?"|')(.*?)\1,?\s*(Error|Warning|Info)?\)/mi;
function insertMessageIfExsists(raw ,node, parent) {
var message = regexDebugMessage.exec(raw);
if (message) {
var debugChunks = esprima.parse('blocks.debug.throwMessage(\'' + message[2] + '\', __DEBUG_METHOD || null ' + (message[3] ? ',\'' + message[3] + '\'' : '') + ');' ).body;
debugChunks.unshift(parent.body.indexOf(node));
debugChunks.unshift(0);
parent.body.splice.apply(parent.body, debugChunks);
}
}
estraverse.traverse(parsed.parseTree(), {
enter: function (node, parent) {
if (node.trailingComments) {
node.trailingComments.forEach(function (comment) {
insertMessageIfExsists(comment.value, node, parent);
});
}

if (node.leadingComments) {
node.leadingComments.forEach(function (comment) {
insertMessageIfExsists(comment.value, node, parent);
});
}
}
});

var targetCode = escodegen.generate(parsed.parseTree(), {
format: {
indent: {
Expand All @@ -79,6 +105,7 @@ module.exports = function (grunt) {
comment: true
});


targetCode = insertSourceCode(targetCode, grunt.file.read('lib/blocks/jsdebug.js'));
targetCode = insertSourceCode(targetCode, 'blocks.debug.queries = ' + toValueString(queries));
// enable blocks.debug after the framework initialized completly (jsdebug uses some functions that aren't initialized when they are needed the first time)
Expand Down

0 comments on commit c0115f1

Please sign in to comment.