Skip to content

Instantly share code, notes, and snippets.

@nerevar
Created January 7, 2016 20:37
Show Gist options
  • Save nerevar/17e2f6069b21f621da60 to your computer and use it in GitHub Desktop.
Save nerevar/17e2f6069b21f621da60 to your computer and use it in GitHub Desktop.
Добавить шапочку jsdoc в файлы для определённых паттернов (функций вызовов счётчиков)
var fs = require('fs'),
_ = require('lodash'),
glob = require('glob'),
esprima = require('esprima'),
walk = require('esprima-walk'),
omitDeep = require('omit-deep');
var counterStrings = [
'w',
'c.hit',
'c.hitDelayed'
];
var counterASTNodes = [];
_.forEach(counterStrings, function(counter) {
counterASTNodes.push(esprima.parse(counter).body[0].expression);
});
function addComment(lines, loc) {
var jsdoc = _.map([
'/**',
' * @counter',
' * @TODO Добавить описание счётчика',
' */'
], function(line) {
// добавляем отступ
return (new Array(loc.column + 1)).join(' ') + line;
});
Array.prototype.splice.apply(lines, [loc.line - 1, 0].concat(jsdoc));
}
_.forEach(glob.sync('images/blocks-*/**/*.js'), function(filename) {
var code = fs.readFileSync(filename, 'utf-8');
var ast = esprima.parse(code, { loc: true });
var found = [];
walk(ast, function(node) {
_.forEach(counterASTNodes, function(counterNode) {
if (_.isEqual(omitDeep(node, 'loc'), counterNode)) {
// console.log('\n\n', node);
found.push(node.loc.start);
}
});
});
if (_.isEmpty(found)) {
process.stdout.write('·');
return;
} else {
console.log('\n✓ ' + found.length + ' ' + filename);
}
found.sort(function(a, b) {
return a.line < b.line ? 1 : a.line > b.line ? -1 : 0;
});
var lines = code.split('\n');
_.forEach(found, function(loc) {
// добавляем jsdoc-шапочку с конца, чтобы не сдвигать нумерацию
addComment(lines, loc);
});
// fs.writeFileSync(filename + '.new', lines.join('\n'), 'utf-8');
console.log(lines.join('\n'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment