kclean
Version:
A build tool that converts KISSY module code to standard JavaScript.
61 lines (47 loc) • 1.84 kB
JavaScript
var errorMsgs = require('./errorMsgs'),
convertDefinesAndRequires = require('./convertDefinesAndRequires'),
_ = require('lodash'),
estraverse = require('estraverse');
module.exports = function traverseAndUpdateAst(obj) {
var amdclean = this,
options = amdclean.options,
ast = obj.ast;
if(!_.isPlainObject(obj)) {
throw new Error(errorMsgs.invalidObject('traverseAndUpdateAst'));
}
if(!ast) {
throw new Error(errorMsgs.emptyAst('traverseAndUpdateAst'));
}
if(!_.isPlainObject(estraverse) || !_.isFunction(estraverse.replace)) {
throw new Error(errorMsgs.estraverse);
}
estraverse.replace(ast, {
'enter': function(node, parent) {
var ignoreComments;
if(node.type === 'Program') {
ignoreComments = (function() {
var arr = [],
currentLineNumber;
amdclean.comments = node.comments;
_.each(node.comments, function(currentComment) {
var currentCommentValue = (currentComment.value).trim();
if(currentCommentValue === options.commentCleanName) {
arr.push(currentComment);
}
});
return arr;
}());
_.each(ignoreComments, function(currentComment) {
currentLineNumber = currentComment.loc.start.line;
amdclean.matchingCommentLineNumbers[currentLineNumber] = true;
});
return node;
}
return convertDefinesAndRequires.call(amdclean, node, parent);
},
'leave': function(node) {
return node;
}
});
return ast;
};