Skip to content

Instantly share code, notes, and snippets.

@pabigot
Forked from micmath/plugins_local.js
Last active March 14, 2018 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabigot/800dece1ee38066d4e03 to your computer and use it in GitHub Desktop.
Save pabigot/800dece1ee38066d4e03 to your computer and use it in GitHub Desktop.
Possible implementation of a "local" tag.
/**
@overview Make references to local things local.
@module plugins/local
@author Michael Mathews <micmath@gmail.com>
@author Peter A. Bigot <pab@pabigot.com>
@see {@link https://github.com/jsdoc3/jsdoc/issues/101|issue #101}
*/
var thisModule = '',
registry = {};
function reset() {
thisModule = '';
registry = {};
}
exports.defineTags = function(dictionary) {
dictionary.defineTag('local', {
onTagged: function(doclet, tag) {
registry[tag.text] = true;
}
});
}
function buildRE(prefix, tag) {
var pat = '(' + prefix + ')\\b(' + tag + ')\\b';
return new RegExp(pat, 'g');
}
exports.handlers = {
jsdocCommentFound: function(e) {
if (thisModule) for (var local in registry) {
/* Handle {@link local} => {@link module~local|local} (across EOL) */
var re = new RegExp('({@link\\s*\\*?\\s*)\\b(' + local + '\\b[^|}]*)}', 'g');
e.comment = e.comment.replace(re,
'$1' + thisModule + '~$2\|$2}');
/* Handle {local} => {thisModule~local}. Brace reference
* doesn't support providing alternative text. */
e.comment = e.comment.replace(buildRE('{', local),
'$1' + thisModule + '~$2');
/* Handle `@cmd local` => `@cmd thisModule~local` for
* certain commands (across EOL) */
e.comment = e.comment.replace(buildRE('@(event|link|memberof|name)\\s*\\*?\\s*', local),
'$1' + thisModule + '~$3');
}
},
newDoclet: function(e) {
if (e.doclet.kind === 'module') {
thisModule = e.doclet.longname;
}
else {
if (thisModule) for (var local in registry) {
var augment;
if (e.doclet.augments) {
for (var i = 0, len = e.doclet.augments.length; i < len; i++) {
augment = e.doclet.augments[i];
if (augment && augment.indexOf(local) === 0) {
e.doclet.augments[i] = thisModule+'~'+e.doclet.augments[i];
}
}
}
if (e.doclet.longname.indexOf(local) === 0) {
e.doclet.longname = thisModule+'~'+e.doclet.longname;
}
}
}
},
fileComplete: function(e) {
reset();
}
};
/**
* @fileoverview This is a test case.
* @module test
* @local BaseClass
* @local ChildClass
*/
/**
* This is a base class.
*
* It emits {@link BaseClass#event:evt|evt}.
*
* @constructor
*/
function BaseClass(x) {
/** document me */
this.x = x;
}
/** An event emitted by BaseClass
*
* @event BaseClass#evt */
/**
* Gets X.
* @return {number} The value of X.
*/
BaseClass.prototype.getX = function() {
return this.x;
};
/**
* This is a class that inherits from the {@link BaseClass|base class}
* AKA {@link BaseClass}.
*
* It uses {@link BaseClass#getX} aka {@link BaseClass#getX|getX()}.
* Across comment lines {@link
* BaseClass} works as it does with {@link
* BaseClass|specific text}.
*
* @constructor
* @extends {BaseClass}
*/
function ChildClass(x, y) {
BaseClass.call(this, x);
this.y = y;
}
ChildClass.prototype = Object.create(BaseClass.prototype);
ChildClass.prototype.constructor = ChildClass;
/**
* Returns a copy of this ChildClass.
* @return {ChildClass} The copy.
*/
ChildClass.prototype.copy = function() {
return new ChildClass(this.x, this.y);
};
/**
* Returns a copy of this ChildClass.
* @param {BaseClass} base - parameter
* @return {BaseClass} `base`.
*/
ChildClass.prototype.useBase = function(base) {
return base;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment