DRY out visitNode()

This commit is contained in:
Jeff Williams 2012-11-01 13:31:12 -07:00
parent 10cd4a077d
commit 09ffe2ec78

View File

@ -304,9 +304,57 @@ function getBasename(name) {
return name; return name;
} }
/** @private
* @memberof module:src/parser.Parser
*/
function makeEvent(node, extras) {
extras = extras || {};
// fill in default values as needed. if we're overriding a property, don't execute the default
// code for that property, since it might blow up.
var result = {
id: extras.id || 'astnode' + node.hashCode(),
comment: extras.comment || String(node.getJsDoc() || '@undocumented'),
lineno: extras.lineno || node.left.getLineno(),
filename: extras.filename || currentSourceName,
astnode: extras.astnode || node,
code: extras.code || aboutNode(node),
event: extras.event || 'symbolFound',
finishers: extras.finishers || [currentParser.addDocletRef]
};
// make sure the result includes extras that don't have default values
for (var prop in extras) {
if ( hasOwnProp.call(extras, prop) ) {
result[prop] = extras[prop];
}
}
return result;
}
/** @private
* @memberof module:src/parser.Parser
*/
function trackVars(node, e) {
// keep track of vars in a function or global scope
var func = "__global__";
var funcDoc = null;
if (node.enclosingFunction) {
func = 'astnode'+node.enclosingFunction.hashCode();
}
funcDoc = currentParser.refs[func];
if (funcDoc) {
funcDoc.meta.vars = funcDoc.meta.vars || {};
funcDoc.meta.vars[e.code.name] = false;
e.finishers.push(makeVarsFinisher(funcDoc));
}
}
/** @private */ /** @private */
function visitNode(node) { function visitNode(node) {
var e, var e,
extras,
nodeComments, nodeComments,
comment, comment,
commentSrc, commentSrc,
@ -341,16 +389,7 @@ function visitNode(node) {
e = null; e = null;
} }
else if (node.type === Token.ASSIGN) { else if (node.type === Token.ASSIGN) {
e = { e = makeEvent(node);
id: 'astnode'+node.hashCode(), // the id of the ASSIGN node
comment: String(node.getJsDoc()||'@undocumented'),
lineno: node.left.getLineno(),
filename: currentSourceName,
astnode: node,
code: aboutNode(node),
event: "symbolFound",
finishers: [currentParser.addDocletRef]
};
basename = getBasename(e.code.name); basename = getBasename(e.code.name);
@ -359,28 +398,17 @@ function visitNode(node) {
} }
} }
else if (node.type === Token.COLON) { // assignment within an object literal else if (node.type === Token.COLON) { // assignment within an object literal
e = { extras = {
id: 'astnode'+node.hashCode(), // the id of the COLON node
comment: String(node.left.getJsDoc() || '@undocumented'), comment: String(node.left.getJsDoc() || '@undocumented'),
lineno: node.left.getLineno(),
filename: currentSourceName,
astnode: node,
code: aboutNode(node),
event: "symbolFound",
finishers: [currentParser.addDocletRef, currentParser.resolveEnum] finishers: [currentParser.addDocletRef, currentParser.resolveEnum]
}; };
e = makeEvent(node, extras);
} }
else if (node.type === Token.GET || node.type === Token.SET) { // assignment within an object literal else if (node.type === Token.GET || node.type === Token.SET) { // assignment within an object literal
e = { extras = {
id: 'astnode'+node.hashCode(), // the id of the GET/SET node comment: String(node.left.getJsDoc() || '@undocumented')
comment: String(node.left.getJsDoc()||'@undocumented'),
lineno: node.left.getLineno(),
filename: currentSourceName,
astnode: node,
code: aboutNode(node),
event: "symbolFound",
finishers: [currentParser.addDocletRef]
}; };
e = makeEvent(node, extras);
} }
else if (node.type == Token.VAR || node.type == Token.LET || node.type == Token.CONST) { else if (node.type == Token.VAR || node.type == Token.LET || node.type == Token.CONST) {
@ -394,57 +422,22 @@ function visitNode(node) {
//node.jsDoc = node.parent.jsDoc; //node.jsDoc = node.parent.jsDoc;
} }
e = { extras = {
id: 'astnode'+node.hashCode(), // the id of the VARIABLE node lineno: node.getLineno()
comment: String(node.getJsDoc()||'@undocumented'),
lineno: node.getLineno(),
filename: currentSourceName,
astnode: node,
code: aboutNode(node),
event: "symbolFound",
finishers: [currentParser.addDocletRef]
}; };
e = makeEvent(node, extras);
// keep track of vars in a function or global scope trackVars(node, e);
func = "__global__";
funcDoc = null;
if (node.enclosingFunction) {
func = 'astnode'+node.enclosingFunction.hashCode();
}
funcDoc = currentParser.refs[func];
if (funcDoc) {
funcDoc.meta.vars = funcDoc.meta.vars || {};
funcDoc.meta.vars[e.code.name] = false;
e.finishers.push(makeVarsFinisher(funcDoc));
}
} }
else if (node.type == Token.FUNCTION || node.type == tkn.NAMEDFUNCTIONSTATEMENT) { else if (node.type == Token.FUNCTION || node.type == tkn.NAMEDFUNCTIONSTATEMENT) {
e = { extras = {
id: 'astnode'+node.hashCode(), // the id of the COLON node lineno: node.getLineno()
comment: String(node.getJsDoc()||'@undocumented'),
lineno: node.getLineno(),
filename: currentSourceName,
astnode: node,
code: aboutNode(node),
event: "symbolFound",
finishers: [currentParser.addDocletRef]
}; };
e = makeEvent(node, extras);
e.code.name = (node.type == tkn.NAMEDFUNCTIONSTATEMENT)? '' : String(node.name) || ''; e.code.name = (node.type == tkn.NAMEDFUNCTIONSTATEMENT)? '' : String(node.name) || '';
//console.log(':: e.code.name is', e.code.name);
// keep track of vars in a function or global scope trackVars(node, e);
func = "__global__";
funcDoc = null;
if (node.enclosingFunction) {
func = 'astnode'+node.enclosingFunction.hashCode();
}
funcDoc = currentParser.refs[func];
if (funcDoc) {
funcDoc.meta.vars = funcDoc.meta.vars || {};
funcDoc.meta.vars[e.code.name] = false;
e.finishers.push(makeVarsFinisher(funcDoc));
}
basename = getBasename(e.code.name); basename = getBasename(e.code.name);
e.code.funcscope = currentParser.resolveVar(node, basename); e.code.funcscope = currentParser.resolveVar(node, basename);