From b4772fc79b0645a8417b5b20067c38bfbda7ac92 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Sat, 6 Dec 2014 11:25:17 +0000 Subject: [PATCH] chore(docs): refactor traceur usage --- docs/dgeni-package/index.js | 6 +++ .../services/AttachCommentTreeVisitor.js | 42 +++++++++++++++++++ .../services/ExportTreeVisitor.js | 5 +-- .../services/ParseTreeVisitor.js | 5 +++ docs/dgeni-package/services/SourceFile.js | 5 +++ docs/dgeni-package/services/TraceurParser.js | 5 +++ docs/dgeni-package/services/atParser.js | 35 +++------------- docs/dgeni-package/services/traceurOptions.js | 5 +++ 8 files changed, 75 insertions(+), 33 deletions(-) create mode 100644 docs/dgeni-package/services/AttachCommentTreeVisitor.js create mode 100644 docs/dgeni-package/services/ParseTreeVisitor.js create mode 100644 docs/dgeni-package/services/SourceFile.js create mode 100644 docs/dgeni-package/services/TraceurParser.js create mode 100644 docs/dgeni-package/services/traceurOptions.js diff --git a/docs/dgeni-package/index.js b/docs/dgeni-package/index.js index b509f5232f..b19da4f563 100644 --- a/docs/dgeni-package/index.js +++ b/docs/dgeni-package/index.js @@ -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')) diff --git a/docs/dgeni-package/services/AttachCommentTreeVisitor.js b/docs/dgeni-package/services/AttachCommentTreeVisitor.js new file mode 100644 index 0000000000..9325f0b4f0 --- /dev/null +++ b/docs/dgeni-package/services/AttachCommentTreeVisitor.js @@ -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; +}; \ No newline at end of file diff --git a/docs/dgeni-package/services/ExportTreeVisitor.js b/docs/dgeni-package/services/ExportTreeVisitor.js index b8750ce8ca..ec26898748 100644 --- a/docs/dgeni-package/services/ExportTreeVisitor.js +++ b/docs/dgeni-package/services/ExportTreeVisitor.js @@ -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); diff --git a/docs/dgeni-package/services/ParseTreeVisitor.js b/docs/dgeni-package/services/ParseTreeVisitor.js new file mode 100644 index 0000000000..9929727e7f --- /dev/null +++ b/docs/dgeni-package/services/ParseTreeVisitor.js @@ -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; +}; \ No newline at end of file diff --git a/docs/dgeni-package/services/SourceFile.js b/docs/dgeni-package/services/SourceFile.js new file mode 100644 index 0000000000..d8af7b48d6 --- /dev/null +++ b/docs/dgeni-package/services/SourceFile.js @@ -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; +}; \ No newline at end of file diff --git a/docs/dgeni-package/services/TraceurParser.js b/docs/dgeni-package/services/TraceurParser.js new file mode 100644 index 0000000000..344aef9530 --- /dev/null +++ b/docs/dgeni-package/services/TraceurParser.js @@ -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; +}; \ No newline at end of file diff --git a/docs/dgeni-package/services/atParser.js b/docs/dgeni-package/services/atParser.js index 58366aef9b..60afd5a343 100644 --- a/docs/dgeni-package/services/atParser.js +++ b/docs/dgeni-package/services/atParser.js @@ -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); } }; \ No newline at end of file diff --git a/docs/dgeni-package/services/traceurOptions.js b/docs/dgeni-package/services/traceurOptions.js new file mode 100644 index 0000000000..6991a7e96b --- /dev/null +++ b/docs/dgeni-package/services/traceurOptions.js @@ -0,0 +1,5 @@ +var traceur = require('traceur/src/node/traceur.js'); + +module.exports = function traceurOptions() { + return traceur.options; +}; \ No newline at end of file