Added new event named "beforeParse". Closes #45.

This commit is contained in:
Michael Mathews 2011-10-11 20:13:30 +01:00
parent 5c47302493
commit c89fa6dbba
2 changed files with 28 additions and 8 deletions

18
plugins/commentConvert.js Normal file
View File

@ -0,0 +1,18 @@
/**
@overview Demonstrate how to modify the source code before the parser sees it.
@module plugins/comentConvert
@author Michael Mathews <micmath@gmail.com>
*/
///
/// 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;
});
};

View File

@ -93,16 +93,18 @@ exports.Parser.prototype.clear = function() {
/** @private */ /** @private */
exports.Parser.prototype._parseSourceCode = function(sourceCode, sourceName) { exports.Parser.prototype._parseSourceCode = function(sourceCode, sourceName) {
currentSourceName = sourceName; var e = {filename: sourceName};
sourceCode = pretreat(sourceCode);
var ast = parserFactory().parse(sourceCode, sourceName, 1);
var e = {filename: currentSourceName};
this.fire('fileBegin', e); this.fire('fileBegin', e);
if (!e.defaultPrevented) { 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( ast.visit(
new Packages.org.mozilla.javascript.ast.NodeVisitor({ new Packages.org.mozilla.javascript.ast.NodeVisitor({
visit: visitNode visit: visitNode
@ -120,7 +122,7 @@ function pretreat(code) {
// merge adjacent doclets // merge adjacent doclets
.replace(/\*\/\/\*\*+/g, '@also') .replace(/\*\/\/\*\*+/g, '@also')
// make lent objectliterals documentable by giving them a dummy name // 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 // make starbangstar comments look like real jsdoc comments
.replace(/\/\*\!\*/g, '/**'); .replace(/\/\*\!\*/g, '/**');
} }