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, '#') : '';
|
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
|
if (memberof) { // @memberof tag given
|
||||||
memberof = memberof.replace(/\.prototype\.?/g, '#');
|
memberof = memberof.replace(/\.prototype\.?/g, '#');
|
||||||
|
|
||||||
@ -47,14 +52,7 @@
|
|||||||
doclet.name = about.name;
|
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) {
|
if (about.memberof) {
|
||||||
doclet.setMemberof(about.memberof);
|
doclet.setMemberof(about.memberof);
|
||||||
|
|||||||
@ -30,7 +30,7 @@
|
|||||||
@fires fileBegin
|
@fires fileBegin
|
||||||
@fires fileComplete
|
@fires fileComplete
|
||||||
*/
|
*/
|
||||||
Parser.prototype.parse = function(sourceFiles, encoding) {
|
exports.Parser.prototype.parse = function(sourceFiles, encoding) {
|
||||||
const SCHEMA = 'javascript:';
|
const SCHEMA = 'javascript:';
|
||||||
var sourceCode = '',
|
var sourceCode = '',
|
||||||
filename = '';
|
filename = '';
|
||||||
@ -56,7 +56,6 @@
|
|||||||
currentParser = this;
|
currentParser = this;
|
||||||
this._parseSourceCode(sourceCode, filename);
|
this._parseSourceCode(sourceCode, filename);
|
||||||
currentParser = null;
|
currentParser = null;
|
||||||
pragmas = {}; // always resets at end of file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._resultBuffer;
|
return this._resultBuffer;
|
||||||
@ -66,14 +65,14 @@
|
|||||||
@method module:jsdoc/src/parser.Parser#results
|
@method module:jsdoc/src/parser.Parser#results
|
||||||
@returns {Array<Doclet>} The accumulated results of any calls to parse.
|
@returns {Array<Doclet>} The accumulated results of any calls to parse.
|
||||||
*/
|
*/
|
||||||
Parser.prototype.results = function() {
|
exports.Parser.prototype.results = function() {
|
||||||
return this._resultBuffer;
|
return this._resultBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@method module:jsdoc/src/parser.Parser#addResult
|
@method module:jsdoc/src/parser.Parser#addResult
|
||||||
*/
|
*/
|
||||||
Parser.prototype.addResult = function(o) {
|
exports.Parser.prototype.addResult = function(o) {
|
||||||
this._resultBuffer.push(o);
|
this._resultBuffer.push(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +80,7 @@
|
|||||||
Empty any accumulated results of calls to parse.
|
Empty any accumulated results of calls to parse.
|
||||||
@method module:jsdoc/src/parser.Parser#clear
|
@method module:jsdoc/src/parser.Parser#clear
|
||||||
*/
|
*/
|
||||||
Parser.prototype.clear = function() {
|
exports.Parser.prototype.clear = function() {
|
||||||
currentParser = null;
|
currentParser = null;
|
||||||
currentSourceName = '';
|
currentSourceName = '';
|
||||||
this._resultBuffer = [];
|
this._resultBuffer = [];
|
||||||
@ -90,7 +89,7 @@
|
|||||||
/**
|
/**
|
||||||
@private
|
@private
|
||||||
*/
|
*/
|
||||||
Parser.prototype._parseSourceCode = function(sourceCode, sourceName) {
|
exports.Parser.prototype._parseSourceCode = function(sourceCode, sourceName) {
|
||||||
currentSourceName = sourceName;
|
currentSourceName = sourceName;
|
||||||
|
|
||||||
// merge adjacent doclets
|
// merge adjacent doclets
|
||||||
@ -117,7 +116,7 @@
|
|||||||
/**
|
/**
|
||||||
Given a node, determine what the node is a member of.
|
Given a node, determine what the node is a member of.
|
||||||
*/
|
*/
|
||||||
Parser.prototype.astnodeToMemberof = function(astnode) {
|
exports.Parser.prototype.astnodeToMemberof = function(astnode) {
|
||||||
var memberof = {};
|
var memberof = {};
|
||||||
|
|
||||||
if (astnode.type === Token.VAR || astnode.type === Token.FUNCTION) {
|
if (astnode.type === Token.VAR || astnode.type === Token.FUNCTION) {
|
||||||
@ -141,11 +140,11 @@
|
|||||||
/**
|
/**
|
||||||
Resolve what "this" refers too, relative to a node
|
Resolve what "this" refers too, relative to a node
|
||||||
*/
|
*/
|
||||||
Parser.prototype.resolveThis = function(astnode) {
|
exports.Parser.prototype.resolveThis = function(node) {
|
||||||
var memberof = {};
|
var memberof = {};
|
||||||
|
|
||||||
if (astnode.enclosingFunction) {
|
if (node.enclosingFunction) {
|
||||||
memberof.id = 'astnode'+astnode.enclosingFunction.hashCode();
|
memberof.id = 'astnode'+node.enclosingFunction.hashCode();
|
||||||
memberof.doclet = this.refs[memberof.id];
|
memberof.doclet = this.refs[memberof.id];
|
||||||
|
|
||||||
if (!memberof.doclet) {
|
if (!memberof.doclet) {
|
||||||
@ -164,14 +163,14 @@
|
|||||||
return memberof.doclet.longname||memberof.doclet.name;
|
return memberof.doclet.longname||memberof.doclet.name;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (astnode.enclosingFunction){
|
if (node.enclosingFunction){
|
||||||
return this.resolveThis(astnode.enclosingFunction/*memberof.doclet.meta.code.val*/);
|
return this.resolveThis(node.enclosingFunction/*memberof.doclet.meta.code.val*/);
|
||||||
}
|
}
|
||||||
else return ''; // TODO handle global this?
|
else return ''; // TODO handle global this?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (astnode.parent) {
|
else if (node.parent) {
|
||||||
var parent = astnode.parent;
|
var parent = node.parent;
|
||||||
if (parent.type === Token.COLON) parent = parent.parent; // go up one more
|
if (parent.type === Token.COLON) parent = parent.parent; // go up one more
|
||||||
|
|
||||||
memberof.id = 'astnode'+parent.hashCode();
|
memberof.id = 'astnode'+parent.hashCode();
|
||||||
@ -191,30 +190,19 @@
|
|||||||
@param {astnode} node
|
@param {astnode} node
|
||||||
@param {string} basename The leftmost name in the long name: in foo.bar.zip the basename is foo.
|
@param {string} basename The leftmost name in the long name: in foo.bar.zip the basename is foo.
|
||||||
*/
|
*/
|
||||||
Parser.prototype.resolveVar = function(node, basename) {
|
exports.Parser.prototype.resolveVar = function(node, basename) {
|
||||||
var memberof = {};
|
var doclet,
|
||||||
|
enclosingFunction = node.enclosingFunction;
|
||||||
|
|
||||||
if (node.enclosingFunction) {
|
if (!enclosingFunction) { return ''; } // global
|
||||||
memberof.id = 'astnode'+node.enclosingFunction.hashCode();
|
|
||||||
memberof.doclet = this.refs[memberof.id];
|
doclet = this.refs['astnode'+enclosingFunction.hashCode()];
|
||||||
|
|
||||||
if (!memberof.doclet) {
|
if ( doclet && doclet.vars && ~doclet.vars.indexOf(basename) ) {
|
||||||
return this.resolveVar(node.enclosingFunction, 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;
|
headers.to = to;
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var headers = {};
|
var headers = {
|
||||||
|
/** document me */
|
||||||
|
cache: {}
|
||||||
|
};
|
||||||
|
|
||||||
/** document me */
|
/** document me */
|
||||||
headers.from = '';
|
headers.from = '';
|
||||||
|
|||||||
@ -1,11 +1,21 @@
|
|||||||
(function() {
|
(function() {
|
||||||
var docSet = testhelpers.getDocSetFromFile('test/cases/innerscope2.js'),
|
var docSet = testhelpers.getDocSetFromFile('test/cases/innerscope2.js'),
|
||||||
to = docSet.getByLongname('Message~headers.to'),
|
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);
|
//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() {
|
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