Skip to content

Commit f94f3ce

Browse files
committed
Parser options are now properties of a Parser instance
1 parent 206badc commit f94f3ce

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

lib/jsdocs/js-doc.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,20 @@ function getSourceFiles(paths, extensions, exclude, depth) {
8989
* @param {Path[]} files Files to be parsed
9090
*/
9191
function parseSourceFiles(options) {
92-
var parser = new Parser(options);
92+
var parser = Parser(options);
9393
files = options.files, l = files.length;
9494
while (l--) {
9595
var file = files[l];
96-
var path = file.absolute();
9796
console.debug("Parsing file: " + file.toString());
9897
try {
99-
var src = file.read().toString();
98+
var source = file.read().toString();
10099
} catch(e) {
101100
console.warn("Can't read source file '" + file.toString() + "': " + e.message);
102101
continue;
103102
}
104-
var tr = new TokenReader();
105-
var ts = new TokenStream(tr.tokenize(new TextStream(src)));
106-
parser.parse(ts, path);
103+
var tokenReader = new TokenReader();
104+
var ts = new TokenStream(tokenReader.tokenize(new TextStream(source)));
105+
parser.parse(ts, file.toString());
107106
}
108107
var symbols = parser.symbols;
109108
parser.finish();

lib/jsdocs/parser.js

+50-18
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,49 @@ var console = require("system").log;
1111
@requires DocComment
1212
*/
1313
var Parser = exports.Parser = function Parser(options) {
14-
this.conf = options;
15-
this.init();
14+
if (!(this instanceof Parser)) return new Parser(options);
15+
for (var key in options) this[key] = options[key];
16+
this.symbols = new SymbolSet(this);
17+
this.walker = new Walker(this);
1618
};
1719
Parser.prototype = {
1820
constructor: Parser,
19-
init: function init() {
20-
this.symbols = new SymbolSet(this);
21-
this.walker = new Walker(this);
22-
},
21+
/**
22+
* Identify secure modules while parseing source code
23+
* @type {Boolean}
24+
*/
25+
securemodules: true,
26+
/**
27+
* If true will include all functions, even anonymus ones.
28+
* @type {Boolean}
29+
*/
30+
ignoreAnonymous: true,
31+
/**
32+
* Treat properties starting with underscore as privates.
33+
* @type {Boolean}
34+
*/
35+
treatUnderscoredAsPrivate: true,
36+
/**
37+
* Include symbols tagged as private, underscored and inner symbols.
38+
* @type {Boolean}
39+
*/
40+
includePrivates: true,
41+
/**
42+
* Dump detail on found symbols
43+
* @type {Boolean}
44+
*/
45+
explain: false,
46+
/**
47+
* Include all functions, even undocumented ones.
48+
* @type {Boolean}
49+
*/
50+
allfunctions: false,
51+
/**
52+
* Ignore all code, only document comments with @name tags.
53+
* @type {Boolean}
54+
*/
55+
ignoreCode: false,
2356
addSymbol: function addSymbol(symbol) {
24-
var conf = this.conf;
2557
var rename = this.rename;
2658
if (rename) {
2759
for (var name in rename) {
@@ -33,7 +65,7 @@ Parser.prototype = {
3365
}
3466
}
3567
}
36-
if (conf.securemodules) {
68+
if (this.securemodules) {
3769
if (typeof this.secureModules == "undefined") this.secureModules = {};
3870
if (/^exports\./.test(symbol.alias)) {
3971
symbol.srcFile.match(/(^|[\\\/])([^\\\/]+)\.js/i);
@@ -62,27 +94,26 @@ Parser.prototype = {
6294
// if a symbol alias is documented more than once the last one with the user docs wins
6395
var symbols = this.symbols;
6496
if (symbols.hasSymbol(symbol.alias)) {
65-
var oldSymbol = symbols.getSymbol(symbol.alias);
97+
var oldSymbol = symbols.getSymbol(symbol.alias);
6698
if (oldSymbol.comment.isUserComment) {
6799
if (symbol.comment.isUserComment) { // old and new are both documented
68100
console.warn("The symbol '" + symbol.alias + "' is documented more than once.");
69-
}
70-
else { // old is documented but new isn't
101+
} else { // old is documented but new isn't
71102
return;
72103
}
73104
}
74105
}
75106

76107
// we don't document anonymous things
77-
if (conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
108+
if (this.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
78109

79110
// uderscored things may be treated as if they were marked private, this cascades
80-
if (conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
111+
if (this.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
81112
if (!symbol.comment.getTag("public").length > 0) symbol.isPrivate = true;
82113
}
83114

84115
// -p flag is required to document private things
85-
if (!conf.includePrivates && symbol.isPrivate) return; // issue #161 fixed by mcbain.asm
116+
if (!this.includePrivates && symbol.isPrivate) return; // issue #161 fixed by mcbain.asm
86117

87118
// ignored things are not documented, this doesn't cascade
88119
if (symbol.isIgnored) return;
@@ -101,7 +132,7 @@ Parser.prototype = {
101132
symbolsSet.relate();
102133

103134
// make a litle report about what was found
104-
if (this.conf.explain) {
135+
if (this.explain) {
105136
var symbols = symbolsSet.toArray();
106137
var srcFile = "";
107138
for (var i = 0, l = symbols.length; i < l; i++) {
@@ -115,14 +146,15 @@ Parser.prototype = {
115146
print("-------------------\n");
116147
}
117148
},
149+
/**
150+
* Parses specified source. Optionally specidies source uri.
151+
*/
118152
parse: function pase(/**TokenStream*/ts, /**String*/srcPath) {
119-
var conf = this.conf;
120153
var symbols = this.symbols;
121154
// TODO: Check why we need to add this to the calsses
122155
Symbol.srcFile = (srcPath || "");
123156
DocComment.shared = ""; // shared comments don't cross file boundaries
124157

125-
if (!this.walker) this.init();
126158
this.walker.walk(ts); // adds to our symbols
127159

128160
// filter symbols by option
@@ -132,7 +164,7 @@ Parser.prototype = {
132164
if (symbol.is("FILE") || symbol.is("GLOBAL")) {
133165
continue;
134166
}
135-
else if (!conf.allfunctions && !symbol.comment.isUserComment) {
167+
else if (!this.allfunctions && !symbol.comment.isUserComment) {
136168
symbols.deleteSymbol(symbol.alias);
137169
}
138170
if (/#$/.test(symbol.alias)) { // we don't document prototypes

lib/jsdocs/walker.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ var console = require("system").log;
77
var Walker = exports.Walker = function Walker(/**Parser*/parser, /**TokenStream*/ts) {
88
this.parser = parser;
99
this.init();
10-
if (typeof ts != "undefined") {
11-
this.walk(ts);
12-
}
13-
}
10+
if (typeof ts != "undefined") this.walk(ts);
11+
};
1412
Walker.prototype = {
1513
constructor: Walker,
1614
init: function() {
@@ -83,9 +81,9 @@ Walker.prototype = {
8381
} else if (doc.meta) { // it's a meta doclet
8482
if (doc.meta == "@+") DocComment.shared = doc.src;
8583
else if (doc.meta == "@-") DocComment.shared = "";
86-
else if (doc.meta == "nocode+") Parser.conf.ignoreCode = true;
87-
else if (doc.meta == "nocode-") Parser.conf.ignoreCode = JSDOC.opt.n;
88-
else throw "Unrecognized meta comment: "+doc.meta;
84+
else if (doc.meta == "nocode+") this.parser.ignoreCode = true;
85+
else if (doc.meta == "nocode-") this.parser.ignoreCode = false;
86+
else throw "Unrecognized meta comment: " + doc.meta;
8987
this.lastDoc = null;
9088
return true;
9189
} else if (doc.getTag("overview").length > 0) { // it's a file overview
@@ -97,7 +95,7 @@ Walker.prototype = {
9795
this.lastDoc = doc;
9896
return false;
9997
}
100-
} else if (!this.parser.conf.ignoreCode) { // it's code
98+
} else if (!this.parser.ignoreCode) { // it's code
10199
if (this.token.is("NAME")) { // it's the name of something
102100
var symbol;
103101
var name = this.token.data;

0 commit comments

Comments
 (0)