From c89fa6dbba05e9ccac0cf03323e8fe284edd53ed Mon Sep 17 00:00:00 2001 From: Michael Mathews Date: Tue, 11 Oct 2011 20:13:30 +0100 Subject: [PATCH] Added new event named "beforeParse". Closes #45. --- plugins/commentConvert.js | 18 ++++++++++++++++++ rhino_modules/jsdoc/src/parser.js | 18 ++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 plugins/commentConvert.js diff --git a/plugins/commentConvert.js b/plugins/commentConvert.js new file mode 100644 index 00000000..b601166d --- /dev/null +++ b/plugins/commentConvert.js @@ -0,0 +1,18 @@ +/** + @overview Demonstrate how to modify the source code before the parser sees it. + @module plugins/comentConvert + @author Michael Mathews + */ + + /// + /// Convert ///-style comments into jsdoc comments. + /// @param e + /// @param e.filename + /// @param e.source + /// +exports.beforeParse = function(e) { + e.source = e.source.replace(/(\n[ \t]*\/\/\/[^\n]*)+/g, function($) { + var replacement = '\n/**' + $.replace(/^[ \t]*\/\/\//mg, '').replace(/(\n$|$)/, '*/$1'); + return replacement; + }); +}; \ No newline at end of file diff --git a/rhino_modules/jsdoc/src/parser.js b/rhino_modules/jsdoc/src/parser.js index 0e04d3f3..8ad6e573 100644 --- a/rhino_modules/jsdoc/src/parser.js +++ b/rhino_modules/jsdoc/src/parser.js @@ -93,16 +93,18 @@ exports.Parser.prototype.clear = function() { /** @private */ exports.Parser.prototype._parseSourceCode = function(sourceCode, sourceName) { - currentSourceName = sourceName; - - sourceCode = pretreat(sourceCode); - - var ast = parserFactory().parse(sourceCode, sourceName, 1); - - var e = {filename: currentSourceName}; + var e = {filename: sourceName}; this.fire('fileBegin', e); if (!e.defaultPrevented) { + e = {filename: sourceName, source: sourceCode}; + this.fire('beforeParse', e); + sourceCode = e.source; + currentSourceName = sourceName = e.filename; + + sourceCode = pretreat(e.source); + + var ast = parserFactory().parse(sourceCode, sourceName, 1); ast.visit( new Packages.org.mozilla.javascript.ast.NodeVisitor({ visit: visitNode @@ -120,7 +122,7 @@ function pretreat(code) { // merge adjacent doclets .replace(/\*\/\/\*\*+/g, '@also') // make lent objectliterals documentable by giving them a dummy name - .replace(/(\/\*\*[\s\S]*?@lends\b[\s\S]*?\*\/\s*)\{/g, '$1____ = {') + .replace(/(\/\*\*[\s\S]*?@lends\b[\s\S]*?\*\/\s*)\{/g, '$1 ____ = {') // make starbangstar comments look like real jsdoc comments .replace(/\/\*\!\*/g, '/**'); }