Skip to content

Commit

Permalink
Merge pull request #149 from Kanaye/master
Browse files Browse the repository at this point in the history
Fixed / Implemented debug messages for "normal" methods.
  • Loading branch information
astoilkov authored Aug 21, 2016
2 parents 4373827 + c4300f4 commit 29b4ac2
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 132 deletions.
43 changes: 22 additions & 21 deletions build/tasks/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ module.exports = function (grunt) {
});
}

if (data.memberof && data.memberof.indexOf('blocks.queries') == 0) {
if (data.memberof && data.memberof.indexOf('blocks.queries') === 0) {
queries[data.name] = data;
return;
}

if (!node) {
if (!node || data.params.length === 0) {
return;
}

var func;
if (node.expression && node.expression.right && node.expression.right.type == 'FunctionExpression') {
func = node.expression.right;
Expand All @@ -60,42 +61,42 @@ module.exports = function (grunt) {
if (func) {
// FunctionExpression.BlockStatement.body(Array)
var funcBody = func.body.body;

esprima.parse('blocks.debug.checkArgs && blocks.debug.checkArgs(' + toValueString(data) + ', Array.prototype.slice.call(arguments))').body.forEach(function (chunk) {
esprima.parse('blocks.debug && blocks.debug.checkArgs(' + toValueString(data) + ', Array.prototype.slice.call(arguments), {})').body.forEach(function (chunk) {
funcBody.unshift(chunk);
});
}
}
});
//var code = escodegen.generate(parsed.parseTree(), {
// format: {
// indent: {
// style: ' ',
// base: 0,
// adjustMultilineComment: true
// }
// },
// comment: true
//});

code = insertSourceCode(code, grunt.file.read('lib/blocks/jsdebug.js'));
code = insertSourceCode(code, 'blocks.debug.queries = ' + toValueString(queries));
var targetCode = escodegen.generate(parsed.parseTree(), {
format: {
indent: {
style: ' ',
base: 0,
adjustMultilineComment: true
}
},
comment: true
});

grunt.file.write('dist/blocks.js', code);
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)
targetCode = insertSourceCode(targetCode, 'blocks.debug.enabled = true;', true);
grunt.file.write('dist/blocks.js', targetCode);
});

function insertSourceCode(baseCode, insertCode) {
function insertSourceCode(baseCode, insertCode, source) {
var sourceCodeLocation;
var result = baseCode;

sourceCodeLocation = result.indexOf('// @debug-code');
sourceCodeLocation = result.indexOf(source ? '// @source-code' : '// @debug-code');
result = result.substring(0, sourceCodeLocation) + '\n' + getSourceCodeWrap(insertCode) + result.substring(sourceCodeLocation);

return result;
}

function getSourceCodeWrap(code) {
return '(function () {\n' + code + '\n})();'
return '(function () {\n' + code + '\n})();';
}

function toValueString(value, options) {
Expand Down
20 changes: 11 additions & 9 deletions lib/blocks/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,15 @@
* Unwraps a jQuery instance and returns the first element
*
* @param {*} element - If jQuery element is specified it will be unwraped
* @param {Function} [callback] - Callback the element will be passed to.
* @param {*} [thisArg] - The context the callback will be executed with
* @returns {*} - The unwraped value
*
* @example {javascript}
* var articles = $('.article');
* blocks.$unwrap()
* blocks.$unwrap(articles, function (article) {
* console.log(artice.textContent);
* });
*/
$unwrap: function(element, callback, thisArg) {
callback = parseCallback(callback, thisArg);
Expand Down Expand Up @@ -516,7 +520,7 @@
* If the value could not be parsed to a number it is not converted
*
* @memberof blocks
* @param {[type]} value - The value to be converted to the specified unit
* @param {String|Number} value - The value to be converted to the specified unit
* @param {String} [unit='px'] - Optionally provide a unit to convert to.
* Default value is 'px'
*
Expand Down Expand Up @@ -682,7 +686,7 @@
* Determines if the specified value is an object
*
* @memberof blocks
* @param {[type]} obj - The value to check for if it is an object
* @param {*} obj - The value to check for if it is an object
* @returns {boolean} - Returns whether the value is an object
*/
isObject: function(obj) {
Expand Down Expand Up @@ -1062,13 +1066,11 @@
return callback;
}

(function () {
// @debug-code
})();

(function () {
// @source-code
})();
// @debug-code

// @source-code


/* @if DEBUG */
(function() {
Expand Down
Loading

0 comments on commit 29b4ac2

Please sign in to comment.