symbol.js

来自「JsDoc Toolkit 是一个把js描述格式化成文档的工具。开发者只需按Js」· JavaScript 代码 · 共 601 行 · 第 1/2 页

JS
601
字号
if (typeof JSDOC == "undefined") JSDOC = {};/**	Create a new Symbol.	@class Represents a symbol in the source code. */JSDOC.Symbol = function() {	this.init();	if (arguments.length) this.populate.apply(this, arguments);}JSDOC.Symbol.prototype.init = function() {	this.$args = [];	this.addOn = "";	this.alias = "";	this.augments = [];	this.author = "";	this.classDesc = "";	this.comment = {};	this.defaultValue = undefined;	this.deprecated = "";	this.desc = "";	this.events = [];	this.example = [];	this.exceptions = [];	this.inherits = [];	this.inheritsFrom = [];	this.isa = "OBJECT";	this.isEvent = false;	this.isConstant = false;	this.isIgnored = false;	this.isInner = false;	this.isNamespace = false;	this.isPrivate = false;	this.isStatic = false;	this.memberOf = "";	this.methods = [];	this._name = "";	this._params = [];	this.properties = [];	this.requires = [];	this.returns = [];	this.see = [];	this.since = "";	this.srcFile = {};	this.type = "";	this.version = "";}JSDOC.Symbol.prototype.serialize = function() {	var keys = [];	for (var p in this) {		keys.push (p);	}	keys = keys.sort();		var out = "";	for (var i in keys) {		if (typeof this[keys[i]] == "function") continue;		out += keys[i]+" => "+Dumper.dump(this[keys[i]])+",\n";	}	return "\n{\n" + out + "}\n";}JSDOC.Symbol.prototype.clone = function() {	var clone = new JSDOC.Symbol();	clone.populate.apply(clone, this.$args); // repopulate using the original arguments	clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made	return clone;}JSDOC.Symbol.prototype.__defineSetter__("name",	function(n) { n = n.replace(/^_global_[.#-]/, ""); n = n.replace(/\.prototype\.?/g, '#'); this._name = n; });JSDOC.Symbol.prototype.__defineGetter__("name",	function() { return this._name; });JSDOC.Symbol.prototype.__defineSetter__("params", 	function(v) {		for (var i = 0, l = v.length; i < l; i++) {			if (v[i].constructor != JSDOC.DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}				this._params[i] = new JSDOC.DocTag("param"+((v[i].type)?" {"+v[i].type+"}":"")+" "+v[i].name);			}			else {				this._params[i] = v[i];			}		}	});JSDOC.Symbol.prototype.__defineGetter__("params",	function() { return this._params; });JSDOC.Symbol.prototype.populate = function(		/** String */ name,		/** Object[] */ params,		/** String */ isa,		/** JSDOC.DocComment */ comment) {	this.$args = arguments;		this.name = name;	this.alias = this.name;	this.params = params;	this.isa = (isa == "VIRTUAL")? "OBJECT":isa;	this.comment = comment || new JSDOC.DocComment("");	this.srcFile = JSDOC.Symbol.srcFile;		if (this.is("FILE") && !this.alias) this.alias = this.srcFile;	this.setTags();		if (typeof JSDOC.PluginManager != "undefined") {		JSDOC.PluginManager.run("onSymbol", this);	}}JSDOC.Symbol.prototype.setTags = function() {	// @author	var authors = this.comment.getTag("author");	if (authors.length) {		this.author = authors.map(function($){return $.desc;}).join(", ");	}		/*t:		plan(34, "testing JSDOC.Symbol");				requires("../lib/JSDOC/DocComment.js");		requires("../frame/String.js");		requires("../lib/JSDOC/DocTag.js");		var sym = new JSDOC.Symbol("foo", [], "OBJECT", new JSDOC.DocComment("/**@author Joe Smith*"+"/"));		is(sym.author, "Joe Smith", "@author tag, author is found.");	*/		// @desc	var descs = this.comment.getTag("desc");	if (descs.length) {		this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "OBJECT", new JSDOC.DocComment("/**@desc This is a description.*"+"/"));		is(sym.desc, "This is a description.", "@desc tag, description is found.");	*/		// @overview	if (this.is("FILE")) {		if (!this.alias) this.alias = this.srcFile;				var overviews = this.comment.getTag("overview");		if (overviews.length) {			this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");		}	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@overview This is an overview.*"+"/"));		is(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");	*/		// @since	var sinces = this.comment.getTag("since");	if (sinces.length) {		this.since = sinces.map(function($){return $.desc;}).join(", ");	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@since 1.01*"+"/"));		is(sym.since, "1.01", "@since tag, description is found.");	*/		// @constant	if (this.comment.getTag("constant").length) {		this.isConstant = true;	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@constant*"+"/"));		is(sym.isConstant, true, "@constant tag, isConstant set.");	*/		// @version	var versions = this.comment.getTag("version");	if (versions.length) {		this.version = versions.map(function($){return $.desc;}).join(", ");	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@version 2.0x*"+"/"));		is(sym.version, "2.0x", "@version tag, version is found.");	*/		// @deprecated	var deprecateds = this.comment.getTag("deprecated");	if (deprecateds.length) {		this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@deprecated Use other method.*"+"/"));		is(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");	*/		// @example	var examples = this.comment.getTag("example");	if (examples.length) {		this.example = examples.map(			// trim trailing whitespace			function($) {				$.desc = $.desc.replace(/\s+$/, "");				return $;			}		);	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@example This\n  is an example. \n*"+"/"));		isnt(typeof sym.example[0], "undefined", "@example tag, creates sym.example array.");		is(sym.example[0], "This\n  is an example.", "@example tag, desc is found.");	*/		// @see	var sees = this.comment.getTag("see");	if (sees.length) {		var thisSee = this.see;		sees.map(function($){thisSee.push($.desc);});	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "FILE", new JSDOC.DocComment("/**@see The other thing.*"+"/"));		is(sym.see, "The other thing.", "@see tag, desc is found.");	*/		// @class	var classes = this.comment.getTag("class");	if (classes.length) {		this.isa = "CONSTRUCTOR";		this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "OBJECT", new JSDOC.DocComment("/**@class This describes the class.*"+"/"));		is(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");		is(sym.classDesc, "This describes the class.", "@class tag, class description is found.");	*/		// @namespace	var namespaces = this.comment.getTag("namespace");	if (namespaces.length) {		this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.		this.isNamespace = true;	}		/*t:		var sym = new JSDOC.Symbol("foo", [], "OBJECT", new JSDOC.DocComment("/**@namespace This describes the namespace.*"+"/"));		is(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");	*/		// @param	var params = this.comment.getTag("param");	if (params.length) {		// user-defined params overwrite those with same name defined by the parser		var thisParams = this.params;		if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in			this.params = params;		}		else { // need to overlay these user-defined params on to existing parser-defined params			for (var i = 0, l = params.length; i < l; i++) {				if (thisParams[i]) {					if (params[i].type) thisParams[i].type = params[i].type;					thisParams[i].name = params[i].name;					thisParams[i].desc = params[i].desc;					thisParams[i].isOptional = params[i].isOptional;					thisParams[i].defaultValue = params[i].defaultValue;				}				else thisParams[i] = params[i];			}		}	}		/*t:		var sym = new JSDOC.Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new JSDOC.DocComment("/**Description.*"+"/"));		is(sym.params.length, 1, "parser defined param is found.");				sym = new JSDOC.Symbol("foo", [], "FUNCTION", new JSDOC.DocComment("/**Description.\n@param {array} pages*"+"/"));		is(sym.params.length, 1, "user defined param is found.");		is(sym.params[0].type, "array", "user defined param type is found.");		is(sym.params[0].name, "pages", "user defined param name is found.");				sym = new JSDOC.Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new JSDOC.DocComment("/**Description.\n@param {string} uid*"+"/"));		is(sym.params.length, 1, "user defined param overwrites parser defined param.");		is(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");		is(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");			sym = new JSDOC.Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new JSDOC.DocComment("/**Description.\n@param {string} uid*"+"/"));		is(sym.params.length, 2, "user defined params  overlay parser defined params.");		is(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");		is(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?