mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
Refactor of var scope code.
This commit is contained in:
parent
e4852bcb15
commit
d36955ccf8
@ -25,6 +25,11 @@
|
||||
|
||||
name = name? (''+name).replace(/\.prototype\.?/g, '#') : '';
|
||||
|
||||
// member of a var in an outer scope?
|
||||
if (name && !memberof && doclet.meta.code && doclet.meta.code.funcscope) {
|
||||
name = doclet.longname = doclet.meta.code.funcscope + '~' + name;
|
||||
}
|
||||
|
||||
if (memberof) { // @memberof tag given
|
||||
memberof = memberof.replace(/\.prototype\.?/g, '#');
|
||||
|
||||
@ -47,14 +52,7 @@
|
||||
doclet.name = about.name;
|
||||
}
|
||||
|
||||
if (doclet.name && !memberof) {
|
||||
if (doclet.meta.code && doclet.meta.code.funcscope) {
|
||||
about.memberof = doclet.meta.code.funcscope;
|
||||
doclet.longname = about.memberof + '~' + name;
|
||||
about.scope = '~';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (about.memberof) {
|
||||
doclet.setMemberof(about.memberof);
|
||||
|
||||
@ -30,7 +30,7 @@
|
||||
@fires fileBegin
|
||||
@fires fileComplete
|
||||
*/
|
||||
Parser.prototype.parse = function(sourceFiles, encoding) {
|
||||
exports.Parser.prototype.parse = function(sourceFiles, encoding) {
|
||||
const SCHEMA = 'javascript:';
|
||||
var sourceCode = '',
|
||||
filename = '';
|
||||
@ -56,7 +56,6 @@
|
||||
currentParser = this;
|
||||
this._parseSourceCode(sourceCode, filename);
|
||||
currentParser = null;
|
||||
pragmas = {}; // always resets at end of file
|
||||
}
|
||||
|
||||
return this._resultBuffer;
|
||||
@ -66,14 +65,14 @@
|
||||
@method module:jsdoc/src/parser.Parser#results
|
||||
@returns {Array<Doclet>} The accumulated results of any calls to parse.
|
||||
*/
|
||||
Parser.prototype.results = function() {
|
||||
exports.Parser.prototype.results = function() {
|
||||
return this._resultBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
@method module:jsdoc/src/parser.Parser#addResult
|
||||
*/
|
||||
Parser.prototype.addResult = function(o) {
|
||||
exports.Parser.prototype.addResult = function(o) {
|
||||
this._resultBuffer.push(o);
|
||||
}
|
||||
|
||||
@ -81,7 +80,7 @@
|
||||
Empty any accumulated results of calls to parse.
|
||||
@method module:jsdoc/src/parser.Parser#clear
|
||||
*/
|
||||
Parser.prototype.clear = function() {
|
||||
exports.Parser.prototype.clear = function() {
|
||||
currentParser = null;
|
||||
currentSourceName = '';
|
||||
this._resultBuffer = [];
|
||||
@ -90,7 +89,7 @@
|
||||
/**
|
||||
@private
|
||||
*/
|
||||
Parser.prototype._parseSourceCode = function(sourceCode, sourceName) {
|
||||
exports.Parser.prototype._parseSourceCode = function(sourceCode, sourceName) {
|
||||
currentSourceName = sourceName;
|
||||
|
||||
// merge adjacent doclets
|
||||
@ -117,7 +116,7 @@
|
||||
/**
|
||||
Given a node, determine what the node is a member of.
|
||||
*/
|
||||
Parser.prototype.astnodeToMemberof = function(astnode) {
|
||||
exports.Parser.prototype.astnodeToMemberof = function(astnode) {
|
||||
var memberof = {};
|
||||
|
||||
if (astnode.type === Token.VAR || astnode.type === Token.FUNCTION) {
|
||||
@ -141,11 +140,11 @@
|
||||
/**
|
||||
Resolve what "this" refers too, relative to a node
|
||||
*/
|
||||
Parser.prototype.resolveThis = function(astnode) {
|
||||
exports.Parser.prototype.resolveThis = function(node) {
|
||||
var memberof = {};
|
||||
|
||||
if (astnode.enclosingFunction) {
|
||||
memberof.id = 'astnode'+astnode.enclosingFunction.hashCode();
|
||||
if (node.enclosingFunction) {
|
||||
memberof.id = 'astnode'+node.enclosingFunction.hashCode();
|
||||
memberof.doclet = this.refs[memberof.id];
|
||||
|
||||
if (!memberof.doclet) {
|
||||
@ -164,14 +163,14 @@
|
||||
return memberof.doclet.longname||memberof.doclet.name;
|
||||
}
|
||||
else {
|
||||
if (astnode.enclosingFunction){
|
||||
return this.resolveThis(astnode.enclosingFunction/*memberof.doclet.meta.code.val*/);
|
||||
if (node.enclosingFunction){
|
||||
return this.resolveThis(node.enclosingFunction/*memberof.doclet.meta.code.val*/);
|
||||
}
|
||||
else return ''; // TODO handle global this?
|
||||
}
|
||||
}
|
||||
else if (astnode.parent) {
|
||||
var parent = astnode.parent;
|
||||
else if (node.parent) {
|
||||
var parent = node.parent;
|
||||
if (parent.type === Token.COLON) parent = parent.parent; // go up one more
|
||||
|
||||
memberof.id = 'astnode'+parent.hashCode();
|
||||
@ -191,30 +190,19 @@
|
||||
@param {astnode} node
|
||||
@param {string} basename The leftmost name in the long name: in foo.bar.zip the basename is foo.
|
||||
*/
|
||||
Parser.prototype.resolveVar = function(node, basename) {
|
||||
var memberof = {};
|
||||
exports.Parser.prototype.resolveVar = function(node, basename) {
|
||||
var doclet,
|
||||
enclosingFunction = node.enclosingFunction;
|
||||
|
||||
if (node.enclosingFunction) {
|
||||
memberof.id = 'astnode'+node.enclosingFunction.hashCode();
|
||||
memberof.doclet = this.refs[memberof.id];
|
||||
if (!enclosingFunction) { return ''; } // global
|
||||
|
||||
if (!memberof.doclet) {
|
||||
return this.resolveVar(node.enclosingFunction, basename);
|
||||
doclet = this.refs['astnode'+enclosingFunction.hashCode()];
|
||||
|
||||
if ( doclet && doclet.vars && ~doclet.vars.indexOf(basename) ) {
|
||||
return doclet.longname;
|
||||
}
|
||||
|
||||
if (memberof.doclet.vars && ~memberof.doclet.vars.indexOf(basename) ) {
|
||||
return memberof.doclet.longname;
|
||||
}
|
||||
else {
|
||||
if (memberof.doclet.meta.code.node){
|
||||
return this.resolveVar(memberof.doclet.meta.code.node, basename);
|
||||
}
|
||||
else return ''; // TODO handle global this?
|
||||
}
|
||||
}
|
||||
else {
|
||||
return ''; // global?
|
||||
}
|
||||
return this.resolveVar(enclosingFunction, basename);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -7,7 +7,10 @@ function Message(to) {
|
||||
headers.to = to;
|
||||
|
||||
(function() {
|
||||
var headers = {};
|
||||
var headers = {
|
||||
/** document me */
|
||||
cache: {}
|
||||
};
|
||||
|
||||
/** document me */
|
||||
headers.from = '';
|
||||
|
||||
@ -1,11 +1,21 @@
|
||||
(function() {
|
||||
var docSet = testhelpers.getDocSetFromFile('test/cases/innerscope2.js'),
|
||||
to = docSet.getByLongname('Message~headers.to'),
|
||||
from = docSet.getByLongname('<anonymous>~headers.from');
|
||||
from = docSet.getByLongname('<anonymous>~headers.from'),
|
||||
cache = docSet.getByLongname('<anonymous>~headers.cache');
|
||||
|
||||
//dump(docSet);
|
||||
|
||||
test('When a var is declared in a function.', function() {
|
||||
assert.equal(cache.length, 1, 'It is like Inner~member.');
|
||||
});
|
||||
|
||||
test('When a var is masked by an inner var and a member of the inner is documented.', function() {
|
||||
assert.equal(from.length, 1, 'It is still like Inner~inner.member.');
|
||||
assert.equal(from.length, 1, 'It is like Inner~inner.member.');
|
||||
});
|
||||
|
||||
test('When a documented member is assigned to a var that masks an outer var.', function() {
|
||||
assert.equal(from[0].name, 'from');
|
||||
assert.equal(from[0].memberof, '<anonymous>~headers');
|
||||
});
|
||||
})();
|
||||
Loading…
x
Reference in New Issue
Block a user