Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools/jsdoc-toolkit/app/lib/JSDOC/DocComment.js
blob: c6c8d7d4f842728e5fc3ab04b9e5b055038fa3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
if (typeof JSDOC == "undefined") JSDOC = {};

/**
	Create a new DocComment. This takes a raw documentation comment,
	and wraps it in useful accessors.
	@class Represents a documentation comment object.
 */ 
JSDOC.DocComment = function(/**String*/comment) {
	this.init();
	if (typeof comment != "undefined") {
		this.parse(comment);
	}
}

JSDOC.DocComment.prototype.init = function() {
	this.isUserComment = true;
	this.src           = "";
	this.meta          = "";
	this.tagTexts      = [];
	this.tags          = [];
}

/**
	@requires JSDOC.DocTag
 */
JSDOC.DocComment.prototype.parse = function(/**String*/comment) {
	if (comment == "") {
		comment = "/** @desc */";
		this.isUserComment = false;
	}
	
	this.src = JSDOC.DocComment.unwrapComment(comment);
	
	this.meta = "";
	if (this.src.indexOf("#") == 0) {
		this.src.match(/#(.+[+-])([\s\S]*)$/);
		if (RegExp.$1) this.meta = RegExp.$1;
		if (RegExp.$2) this.src = RegExp.$2;
	}
	
	if (typeof JSDOC.PluginManager != "undefined") {
		JSDOC.PluginManager.run("onDocCommentSrc", this);
	}
	
	this.fixDesc();

	this.src = JSDOC.DocComment.shared+"\n"+this.src;
	
	this.tagTexts = 
		this.src
		.split(/(^|[\r\n])\s*@/)
		.filter(function($){return $.match(/\S/)});
	
	/**
		The tags found in the comment.
		@type JSDOC.DocTag[]
	 */
	this.tags = this.tagTexts.map(function($){return new JSDOC.DocTag($)});
	
	if (typeof JSDOC.PluginManager != "undefined") {
		JSDOC.PluginManager.run("onDocCommentTags", this);
	}
}

/*t:
	plan(5, "testing JSDOC.DocComment");
	requires("../frame/String.js");
	requires("../lib/JSDOC/DocTag.js");
	
	var com = new JSDOC.DocComment("/**@foo some\n* comment here*"+"/");
	is(com.tagTexts[0], "foo some\ncomment here", "first tag text is found.");
	is(com.tags[0].title, "foo", "the title is found in a comment with one tag.");
	
	var com = new JSDOC.DocComment("/** @foo first\n* @bar second*"+"/");
	is(com.getTag("bar").length, 1, "getTag() returns one tag by that title.");
	
	JSDOC.DocComment.shared = "@author John Smith";
	var com = new JSDOC.DocComment("/**@foo some\n* comment here*"+"/");
	is(com.tags[0].title, "author", "shared comment is added.");
	is(com.tags[1].title, "foo", "shared comment is added to existing tag.");
*/

/**
	If no @desc tag is provided, this function will add it.
 */
JSDOC.DocComment.prototype.fixDesc = function() {
	if (this.meta && this.meta != "@+") return;
	if (/^\s*[^@\s]/.test(this.src)) {				
		this.src = "@desc "+this.src;
	}
}

/*t:
	plan(5, "testing JSDOC.DocComment#fixDesc");
	
	var com = new JSDOC.DocComment();
	
	com.src = "this is a desc\n@author foo";
	com.fixDesc();
	is(com.src, "@desc this is a desc\n@author foo", "if no @desc tag is provided one is added.");

	com.src = "x";
	com.fixDesc();
	is(com.src, "@desc x", "if no @desc tag is provided one is added to a single character.");

	com.src = "\nx";
	com.fixDesc();
	is(com.src, "@desc \nx", "if no @desc tag is provided one is added to return and character.");
	
	com.src = " ";
	com.fixDesc();
	is(com.src, " ", "if no @desc tag is provided one is not added to just whitespace.");

	com.src = "";
	com.fixDesc();
	is(com.src, "", "if no @desc tag is provided one is not added to empty.");
*/

/**
	Remove slash-star comment wrapper from a raw comment string.
	@type String
 */
JSDOC.DocComment.unwrapComment = function(/**String*/comment) {
	if (!comment) return "";
	var unwrapped = comment.replace(/(^\/\*\*|\*\/$)/g, "").replace(/^\s*\* ?/gm, "");
	return unwrapped;
}

/*t:
	plan(5, "testing JSDOC.DocComment.unwrapComment");
	
	var com = "/**x*"+"/";
	var unwrapped = JSDOC.DocComment.unwrapComment(com);
	is(unwrapped, "x", "a single character jsdoc is found.");
	
	com = "/***x*"+"/";
	unwrapped = JSDOC.DocComment.unwrapComment(com);
	is(unwrapped, "x", "three stars are allowed in the opener.");
	
	com = "/****x*"+"/";
	unwrapped = JSDOC.DocComment.unwrapComment(com);
	is(unwrapped, "*x", "fourth star in the opener is kept.");
	
	com = "/**x\n * y\n*"+"/";
	unwrapped = JSDOC.DocComment.unwrapComment(com);
	is(unwrapped, "x\ny\n", "leading stars and spaces are trimmed.");
	
	com = "/**x\n *   y\n*"+"/";
	unwrapped = JSDOC.DocComment.unwrapComment(com);
	is(unwrapped, "x\n  y\n", "only first space after leading stars are trimmed.");
*/

/**
	Provides a printable version of the comment.
	@type String
 */
JSDOC.DocComment.prototype.toString = function() {
	return this.src;
}

/*t:
	plan(1, "testing JSDOC.DocComment#fixDesc");
	var com = new JSDOC.DocComment();
	com.src = "foo";
	is(""+com, "foo", "stringifying a comment returns the unwrapped src.");
*/

/**
	Given the title of a tag, returns all tags that have that title.
	@type JSDOC.DocTag[]
 */
JSDOC.DocComment.prototype.getTag = function(/**String*/tagTitle) {
	return this.tags.filter(function($){return $.title == tagTitle});
}

/*t:
	plan(1, "testing JSDOC.DocComment#getTag");
	requires("../frame/String.js");
	requires("../lib/JSDOC/DocTag.js");
	
	var com = new JSDOC.DocComment("/**@foo some\n* @bar\n* @bar*"+"/");
	is(com.getTag("bar").length, 2, "getTag returns expected number of tags.");
*/

/**
	Used to store the currently shared tag text.
*/
JSDOC.DocComment.shared = "";

/*t:
	plan(2, "testing JSDOC.DocComment.shared");
	requires("../frame/String.js");
	requires("../lib/JSDOC/DocTag.js");
	
	JSDOC.DocComment.shared = "@author Michael";
	
	var com = new JSDOC.DocComment("/**@foo\n* @foo*"+"/");
	is(com.getTag("author").length, 1, "getTag returns shared tag.");
	is(com.getTag("foo").length, 2, "getTag returns unshared tags too.");
*/