Skip to content

Commit b4772fc

Browse files
chore(docs): refactor traceur usage
1 parent 94b541a commit b4772fc

8 files changed

Lines changed: 75 additions & 33 deletions

File tree

docs/dgeni-package/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ module.exports = new Package('angular', [jsdocPackage, nunjucksPackage])
1313
// Register the services and file readers
1414
.factory(require('./services/atParser'))
1515
.factory(require('./services/getJSDocComment'))
16+
.factory(require('./services/SourceFile'))
17+
.factory(require('./services/TraceurParser'))
18+
.factory(require('./services/traceurOptions'))
19+
.factory(require('./services/ParseTreeVisitor'))
20+
.factory(require('./services/AttachCommentTreeVisitor'))
1621
.factory(require('./services/ExportTreeVisitor'))
22+
1723
.factory(require('./readers/atScript'))
1824
.factory(require('./readers/ngdoc'))
1925

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = function AttachCommentTreeVisitor(ParseTreeVisitor, log) {
2+
3+
function AttachCommentTreeVisitorImpl() {
4+
ParseTreeVisitor.call(this);
5+
}
6+
7+
AttachCommentTreeVisitorImpl.prototype = {
8+
9+
__proto__: ParseTreeVisitor.prototype,
10+
11+
12+
visit: function(tree, comments) {
13+
this.comments = comments;
14+
this.index = 0;
15+
this.currentComment = this.comments[this.index];
16+
17+
if (this.currentComment) log.silly('comment: ' +
18+
this.currentComment.range.start.line + ' - ' +
19+
this.currentComment.range.end.line);
20+
21+
ParseTreeVisitor.prototype.visit.call(this, tree);
22+
},
23+
24+
// Really we ought to subclass ParseTreeVisitor but this is fiddly in ES5 so
25+
// it is easier to simply override the prototype's method on the instance
26+
visitAny: function(tree) {
27+
if (tree && tree.location && tree.location.start && this.currentComment) {
28+
if (this.currentComment.range.end.offset < tree.location.start.offset) {
29+
log.silly('tree: ' + tree.constructor.name + ' - ' + tree.location.start.line);
30+
tree.commentBefore = this.currentComment;
31+
this.currentComment.treeAfter = tree;
32+
this.index++;
33+
this.currentComment = this.comments[this.index];
34+
if (this.currentComment) log.silly('comment: ' + this.currentComment.range.start.line + ' - ' + this.currentComment.range.end.line);
35+
}
36+
}
37+
return ParseTreeVisitor.prototype.visitAny.call(this, tree);
38+
}
39+
};
40+
41+
return AttachCommentTreeVisitorImpl;
42+
};

docs/dgeni-package/services/ExportTreeVisitor.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
var traceur = require('traceur/src/node/traceur.js');
2-
var ParseTreeVisitor = System.get("traceur@0.0.74/src/syntax/ParseTreeVisitor").ParseTreeVisitor;
3-
4-
module.exports = function ExportTreeVisitor(log) {
1+
module.exports = function ExportTreeVisitor(ParseTreeVisitor, log) {
52

63
function ExportTreeVisitorImpl() {
74
ParseTreeVisitor.call(this);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var traceur = require('traceur/src/node/traceur.js');
2+
3+
module.exports = function ParseTreeVisitor() {
4+
return System.get(System.map.traceur + '/src/syntax/ParseTreeVisitor').ParseTreeVisitor;
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var traceur = require('traceur/src/node/traceur.js');
2+
3+
module.exports = function SourceFile() {
4+
return System.get(System.map.traceur + '/src/syntax/SourceFile').SourceFile;
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var traceur = require('traceur/src/node/traceur.js');
2+
3+
module.exports = function TraceurParser() {
4+
return System.get(System.map.traceur + '/src/syntax/Parser').Parser;
5+
};

docs/dgeni-package/services/atParser.js

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
var traceur = require('traceur/src/node/traceur.js');
2-
var ParseTreeVisitor = System.get(System.map.traceur + '/src/syntax/ParseTreeVisitor').ParseTreeVisitor;
31
var file2modulename = require('../../../tools/build/file2modulename');
42
/**
53
* Wrapper around traceur that can parse the contents of a file
64
*/
7-
module.exports = function atParser(log) {
5+
module.exports = function atParser(AttachCommentTreeVisitor, SourceFile, TraceurParser, traceurOptions, log) {
86

97
var service = {
108
/**
@@ -33,16 +31,16 @@ module.exports = function atParser(log) {
3331
function parseModule(fileInfo) {
3432

3533
var moduleName = file2modulename(fileInfo.relativePath);
36-
var sourceFile = new traceur.syntax.SourceFile(moduleName, fileInfo.content);
37-
var parser = new traceur.syntax.Parser(sourceFile);
34+
var sourceFile = new SourceFile(moduleName, fileInfo.content);
3835
var comments = [];
3936
var moduleTree;
37+
var parser = new TraceurParser(sourceFile);
4038

4139
// Configure the parser
4240
parser.handleComment = function(range) {
4341
comments.push({ range: range });
4442
};
45-
traceur.options.setFromObject(service.traceurOptions);
43+
traceurOptions.setFromObject(service.traceurOptions);
4644

4745
try {
4846
// Parse the file as a module, attaching the comments
@@ -69,29 +67,8 @@ module.exports = function atParser(log) {
6967
// attach the comments to their nearest code tree
7068
function attachComments(tree, comments) {
7169

72-
var visitor = new ParseTreeVisitor();
73-
var index = 0;
74-
var currentComment = comments[index];
75-
76-
if (currentComment) log.silly('comment: ' + currentComment.range.start.line + ' - ' + currentComment.range.end.line);
77-
78-
// Really we ought to subclass ParseTreeVisitor but this is fiddly in ES5 so
79-
// it is easier to simply override the prototype's method on the instance
80-
visitor.visitAny = function(tree) {
81-
if (tree && tree.location && tree.location.start && currentComment) {
82-
if (currentComment.range.end.offset < tree.location.start.offset) {
83-
log.silly('tree: ' + tree.constructor.name + ' - ' + tree.location.start.line);
84-
tree.commentBefore = currentComment;
85-
currentComment.treeAfter = tree;
86-
index++;
87-
currentComment = comments[index];
88-
if (currentComment) log.silly('comment: ' + currentComment.range.start.line + ' - ' + currentComment.range.end.line);
89-
}
90-
}
91-
return ParseTreeVisitor.prototype.visitAny.call(this, tree);
92-
};
93-
70+
var visitor = new AttachCommentTreeVisitor();
9471
// Visit every node of the tree using our custom method
95-
visitor.visit(tree);
72+
visitor.visit(tree, comments);
9673
}
9774
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var traceur = require('traceur/src/node/traceur.js');
2+
3+
module.exports = function traceurOptions() {
4+
return traceur.options;
5+
};

0 commit comments

Comments
 (0)