Skip to content

Commit 09ffe2e

Browse files
committed
DRY out visitNode()
1 parent 10cd4a0 commit 09ffe2e

File tree

1 file changed

+63
-70
lines changed

1 file changed

+63
-70
lines changed

rhino_modules/jsdoc/src/parser.js

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,57 @@ function getBasename(name) {
304304
return name;
305305
}
306306

307+
/** @private
308+
* @memberof module:src/parser.Parser
309+
*/
310+
function makeEvent(node, extras) {
311+
extras = extras || {};
312+
313+
// fill in default values as needed. if we're overriding a property, don't execute the default
314+
// code for that property, since it might blow up.
315+
var result = {
316+
id: extras.id || 'astnode' + node.hashCode(),
317+
comment: extras.comment || String(node.getJsDoc() || '@undocumented'),
318+
lineno: extras.lineno || node.left.getLineno(),
319+
filename: extras.filename || currentSourceName,
320+
astnode: extras.astnode || node,
321+
code: extras.code || aboutNode(node),
322+
event: extras.event || 'symbolFound',
323+
finishers: extras.finishers || [currentParser.addDocletRef]
324+
};
325+
326+
// make sure the result includes extras that don't have default values
327+
for (var prop in extras) {
328+
if ( hasOwnProp.call(extras, prop) ) {
329+
result[prop] = extras[prop];
330+
}
331+
}
332+
333+
return result;
334+
}
335+
336+
/** @private
337+
* @memberof module:src/parser.Parser
338+
*/
339+
function trackVars(node, e) {
340+
// keep track of vars in a function or global scope
341+
var func = "__global__";
342+
var funcDoc = null;
343+
if (node.enclosingFunction) {
344+
func = 'astnode'+node.enclosingFunction.hashCode();
345+
}
346+
funcDoc = currentParser.refs[func];
347+
if (funcDoc) {
348+
funcDoc.meta.vars = funcDoc.meta.vars || {};
349+
funcDoc.meta.vars[e.code.name] = false;
350+
e.finishers.push(makeVarsFinisher(funcDoc));
351+
}
352+
}
353+
307354
/** @private */
308355
function visitNode(node) {
309356
var e,
357+
extras,
310358
nodeComments,
311359
comment,
312360
commentSrc,
@@ -341,16 +389,7 @@ function visitNode(node) {
341389
e = null;
342390
}
343391
else if (node.type === Token.ASSIGN) {
344-
e = {
345-
id: 'astnode'+node.hashCode(), // the id of the ASSIGN node
346-
comment: String(node.getJsDoc()||'@undocumented'),
347-
lineno: node.left.getLineno(),
348-
filename: currentSourceName,
349-
astnode: node,
350-
code: aboutNode(node),
351-
event: "symbolFound",
352-
finishers: [currentParser.addDocletRef]
353-
};
392+
e = makeEvent(node);
354393

355394
basename = getBasename(e.code.name);
356395

@@ -359,28 +398,17 @@ function visitNode(node) {
359398
}
360399
}
361400
else if (node.type === Token.COLON) { // assignment within an object literal
362-
e = {
363-
id: 'astnode'+node.hashCode(), // the id of the COLON node
364-
comment: String(node.left.getJsDoc()||'@undocumented'),
365-
lineno: node.left.getLineno(),
366-
filename: currentSourceName,
367-
astnode: node,
368-
code: aboutNode(node),
369-
event: "symbolFound",
401+
extras = {
402+
comment: String(node.left.getJsDoc() || '@undocumented'),
370403
finishers: [currentParser.addDocletRef, currentParser.resolveEnum]
371404
};
405+
e = makeEvent(node, extras);
372406
}
373407
else if (node.type === Token.GET || node.type === Token.SET) { // assignment within an object literal
374-
e = {
375-
id: 'astnode'+node.hashCode(), // the id of the GET/SET node
376-
comment: String(node.left.getJsDoc()||'@undocumented'),
377-
lineno: node.left.getLineno(),
378-
filename: currentSourceName,
379-
astnode: node,
380-
code: aboutNode(node),
381-
event: "symbolFound",
382-
finishers: [currentParser.addDocletRef]
408+
extras = {
409+
comment: String(node.left.getJsDoc() || '@undocumented')
383410
};
411+
e = makeEvent(node, extras);
384412
}
385413
else if (node.type == Token.VAR || node.type == Token.LET || node.type == Token.CONST) {
386414

@@ -394,57 +422,22 @@ function visitNode(node) {
394422
//node.jsDoc = node.parent.jsDoc;
395423
}
396424

397-
e = {
398-
id: 'astnode'+node.hashCode(), // the id of the VARIABLE node
399-
comment: String(node.getJsDoc()||'@undocumented'),
400-
lineno: node.getLineno(),
401-
filename: currentSourceName,
402-
astnode: node,
403-
code: aboutNode(node),
404-
event: "symbolFound",
405-
finishers: [currentParser.addDocletRef]
425+
extras = {
426+
lineno: node.getLineno()
406427
};
428+
e = makeEvent(node, extras);
407429

408-
// keep track of vars in a function or global scope
409-
func = "__global__";
410-
funcDoc = null;
411-
if (node.enclosingFunction) {
412-
func = 'astnode'+node.enclosingFunction.hashCode();
413-
}
414-
funcDoc = currentParser.refs[func];
415-
if (funcDoc) {
416-
funcDoc.meta.vars = funcDoc.meta.vars || {};
417-
funcDoc.meta.vars[e.code.name] = false;
418-
e.finishers.push(makeVarsFinisher(funcDoc));
419-
}
430+
trackVars(node, e);
420431
}
421432
else if (node.type == Token.FUNCTION || node.type == tkn.NAMEDFUNCTIONSTATEMENT) {
422-
e = {
423-
id: 'astnode'+node.hashCode(), // the id of the COLON node
424-
comment: String(node.getJsDoc()||'@undocumented'),
425-
lineno: node.getLineno(),
426-
filename: currentSourceName,
427-
astnode: node,
428-
code: aboutNode(node),
429-
event: "symbolFound",
430-
finishers: [currentParser.addDocletRef]
433+
extras = {
434+
lineno: node.getLineno()
431435
};
436+
e = makeEvent(node, extras);
432437

433438
e.code.name = (node.type == tkn.NAMEDFUNCTIONSTATEMENT)? '' : String(node.name) || '';
434-
//console.log(':: e.code.name is', e.code.name);
435439

436-
// keep track of vars in a function or global scope
437-
func = "__global__";
438-
funcDoc = null;
439-
if (node.enclosingFunction) {
440-
func = 'astnode'+node.enclosingFunction.hashCode();
441-
}
442-
funcDoc = currentParser.refs[func];
443-
if (funcDoc) {
444-
funcDoc.meta.vars = funcDoc.meta.vars || {};
445-
funcDoc.meta.vars[e.code.name] = false;
446-
e.finishers.push(makeVarsFinisher(funcDoc));
447-
}
440+
trackVars(node, e);
448441

449442
basename = getBasename(e.code.name);
450443
e.code.funcscope = currentParser.resolveVar(node, basename);

0 commit comments

Comments
 (0)