chore(docs): refactor traceur usage

This commit is contained in:
Peter Bacon Darwin 2014-12-06 11:25:17 +00:00
parent 94b541a3e8
commit b4772fc79b
8 changed files with 75 additions and 33 deletions

View File

@ -13,7 +13,13 @@ module.exports = new Package('angular', [jsdocPackage, nunjucksPackage])
// Register the services and file readers
.factory(require('./services/atParser'))
.factory(require('./services/getJSDocComment'))
.factory(require('./services/SourceFile'))
.factory(require('./services/TraceurParser'))
.factory(require('./services/traceurOptions'))
.factory(require('./services/ParseTreeVisitor'))
.factory(require('./services/AttachCommentTreeVisitor'))
.factory(require('./services/ExportTreeVisitor'))
.factory(require('./readers/atScript'))
.factory(require('./readers/ngdoc'))

View File

@ -0,0 +1,42 @@
module.exports = function AttachCommentTreeVisitor(ParseTreeVisitor, log) {
function AttachCommentTreeVisitorImpl() {
ParseTreeVisitor.call(this);
}
AttachCommentTreeVisitorImpl.prototype = {
__proto__: ParseTreeVisitor.prototype,
visit: function(tree, comments) {
this.comments = comments;
this.index = 0;
this.currentComment = this.comments[this.index];
if (this.currentComment) log.silly('comment: ' +
this.currentComment.range.start.line + ' - ' +
this.currentComment.range.end.line);
ParseTreeVisitor.prototype.visit.call(this, tree);
},
// Really we ought to subclass ParseTreeVisitor but this is fiddly in ES5 so
// it is easier to simply override the prototype's method on the instance
visitAny: function(tree) {
if (tree && tree.location && tree.location.start && this.currentComment) {
if (this.currentComment.range.end.offset < tree.location.start.offset) {
log.silly('tree: ' + tree.constructor.name + ' - ' + tree.location.start.line);
tree.commentBefore = this.currentComment;
this.currentComment.treeAfter = tree;
this.index++;
this.currentComment = this.comments[this.index];
if (this.currentComment) log.silly('comment: ' + this.currentComment.range.start.line + ' - ' + this.currentComment.range.end.line);
}
}
return ParseTreeVisitor.prototype.visitAny.call(this, tree);
}
};
return AttachCommentTreeVisitorImpl;
};

View File

@ -1,7 +1,4 @@
var traceur = require('traceur/src/node/traceur.js');
var ParseTreeVisitor = System.get("traceur@0.0.74/src/syntax/ParseTreeVisitor").ParseTreeVisitor;
module.exports = function ExportTreeVisitor(log) {
module.exports = function ExportTreeVisitor(ParseTreeVisitor, log) {
function ExportTreeVisitorImpl() {
ParseTreeVisitor.call(this);

View File

@ -0,0 +1,5 @@
var traceur = require('traceur/src/node/traceur.js');
module.exports = function ParseTreeVisitor() {
return System.get(System.map.traceur + '/src/syntax/ParseTreeVisitor').ParseTreeVisitor;
};

View File

@ -0,0 +1,5 @@
var traceur = require('traceur/src/node/traceur.js');
module.exports = function SourceFile() {
return System.get(System.map.traceur + '/src/syntax/SourceFile').SourceFile;
};

View File

@ -0,0 +1,5 @@
var traceur = require('traceur/src/node/traceur.js');
module.exports = function TraceurParser() {
return System.get(System.map.traceur + '/src/syntax/Parser').Parser;
};

View File

@ -1,10 +1,8 @@
var traceur = require('traceur/src/node/traceur.js');
var ParseTreeVisitor = System.get(System.map.traceur + '/src/syntax/ParseTreeVisitor').ParseTreeVisitor;
var file2modulename = require('../../../tools/build/file2modulename');
/**
* Wrapper around traceur that can parse the contents of a file
*/
module.exports = function atParser(log) {
module.exports = function atParser(AttachCommentTreeVisitor, SourceFile, TraceurParser, traceurOptions, log) {
var service = {
/**
@ -33,16 +31,16 @@ module.exports = function atParser(log) {
function parseModule(fileInfo) {
var moduleName = file2modulename(fileInfo.relativePath);
var sourceFile = new traceur.syntax.SourceFile(moduleName, fileInfo.content);
var parser = new traceur.syntax.Parser(sourceFile);
var sourceFile = new SourceFile(moduleName, fileInfo.content);
var comments = [];
var moduleTree;
var parser = new TraceurParser(sourceFile);
// Configure the parser
parser.handleComment = function(range) {
comments.push({ range: range });
};
traceur.options.setFromObject(service.traceurOptions);
traceurOptions.setFromObject(service.traceurOptions);
try {
// Parse the file as a module, attaching the comments
@ -69,29 +67,8 @@ module.exports = function atParser(log) {
// attach the comments to their nearest code tree
function attachComments(tree, comments) {
var visitor = new ParseTreeVisitor();
var index = 0;
var currentComment = comments[index];
if (currentComment) log.silly('comment: ' + currentComment.range.start.line + ' - ' + currentComment.range.end.line);
// Really we ought to subclass ParseTreeVisitor but this is fiddly in ES5 so
// it is easier to simply override the prototype's method on the instance
visitor.visitAny = function(tree) {
if (tree && tree.location && tree.location.start && currentComment) {
if (currentComment.range.end.offset < tree.location.start.offset) {
log.silly('tree: ' + tree.constructor.name + ' - ' + tree.location.start.line);
tree.commentBefore = currentComment;
currentComment.treeAfter = tree;
index++;
currentComment = comments[index];
if (currentComment) log.silly('comment: ' + currentComment.range.start.line + ' - ' + currentComment.range.end.line);
}
}
return ParseTreeVisitor.prototype.visitAny.call(this, tree);
};
var visitor = new AttachCommentTreeVisitor();
// Visit every node of the tree using our custom method
visitor.visit(tree);
visitor.visit(tree, comments);
}
};

View File

@ -0,0 +1,5 @@
var traceur = require('traceur/src/node/traceur.js');
module.exports = function traceurOptions() {
return traceur.options;
};