Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (38 sloc) 1.32 KB
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define(['../shaper', '../comments'], function(Shaper, Comments) {
"use strict"; "use restrict";
Shaper("annotater", function(root) {
Shaper.traverse(root, {pre: function(node, ref) {
// collect leading comments (whitespace excluded)
var comments = [];
var split = Comments.split(node.leadingComment);
for (var i = 0; i < split.length; i++) {
var str = split[i];
if (Comments.isComment(str)) {
comments.push(str);
}
else if (!Comments.isBlankString(str)) {
break;
}
}
// match comments with annotater matchers
for (i = 0; i < Annotater.matchers.length; i++) {
var matcher = Annotater.matchers[i];
for (var j = 0; j < comments.length; j++) {
var comment = comments[j];
var annotation = comment.match(matcher.re);
if (annotation === null) {
continue;
}
var fn = matcher.applyfn;
fn(node, annotation);
}
}
}});
});
function Annotater(re, applyfn) {
Annotater.matchers.push({re: re, applyfn: applyfn});
}
Annotater.matchers = [];
return Annotater;
});