1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | var jsdoc = {
|
18 | tag: {
|
19 | dictionary: require('jsdoc/tag/dictionary'),
|
20 | validator: require('jsdoc/tag/validator'),
|
21 | type: require('jsdoc/tag/type')
|
22 | },
|
23 | util: {
|
24 | logger: require('jsdoc/util/logger')
|
25 | }
|
26 | };
|
27 | var path = require('jsdoc/path');
|
28 | var util = require('util');
|
29 |
|
30 |
|
31 |
|
32 | function mustPreserveWhitespace(text, meta) {
|
33 | return meta && meta.code && meta.code.name === text && text.match(/(?:^\s+)|(?:\s+$)/);
|
34 | }
|
35 |
|
36 | function trim(text, opts, meta) {
|
37 | var indentMatcher;
|
38 | var match;
|
39 |
|
40 | opts = opts || {};
|
41 | text = String(typeof text !== 'undefined' ? text : '');
|
42 |
|
43 | if ( mustPreserveWhitespace(text, meta) ) {
|
44 | text = util.format('"%s"', text);
|
45 | }
|
46 | else if (opts.keepsWhitespace) {
|
47 | text = text.replace(/^[\n\r\f]+|[\n\r\f]+$/g, '');
|
48 | if (opts.removesIndent) {
|
49 | match = text.match(/^([ \t]+)/);
|
50 | if (match && match[1]) {
|
51 | indentMatcher = new RegExp('^' + match[1], 'gm');
|
52 | text = text.replace(indentMatcher, '');
|
53 | }
|
54 | }
|
55 | }
|
56 | else {
|
57 | text = text.replace(/^\s+|\s+$/g, '');
|
58 | }
|
59 |
|
60 | return text;
|
61 | }
|
62 |
|
63 | function addHiddenProperty(obj, propName, propValue) {
|
64 | Object.defineProperty(obj, propName, {
|
65 | value: propValue,
|
66 | writable: true,
|
67 | enumerable: !!global.env.opts.debug,
|
68 | configurable: true
|
69 | });
|
70 | }
|
71 |
|
72 | function parseType(tag, tagDef, meta) {
|
73 | try {
|
74 | return jsdoc.tag.type.parse(tag.text, tagDef.canHaveName, tagDef.canHaveType);
|
75 | }
|
76 | catch (e) {
|
77 | jsdoc.util.logger.error(
|
78 | 'Unable to parse a tag\'s type expression%s with tag title "%s" and text "%s": %s',
|
79 | meta.filename ? ( ' for source file ' + path.join(meta.path, meta.filename) ) : '',
|
80 | tag.originalTitle,
|
81 | tag.text,
|
82 | e.message
|
83 | );
|
84 | return {};
|
85 | }
|
86 | }
|
87 |
|
88 | function processTagText(tag, tagDef, meta) {
|
89 | var tagType;
|
90 |
|
91 | if (tagDef.onTagText) {
|
92 | tag.text = tagDef.onTagText(tag.text);
|
93 | }
|
94 |
|
95 | if (tagDef.canHaveType || tagDef.canHaveName) {
|
96 |
|
97 | tag.value = {};
|
98 |
|
99 | tagType = parseType(tag, tagDef, meta);
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | if (tagType.type) {
|
105 | if (tagType.type.length) {
|
106 | tag.value.type = {
|
107 | names: tagType.type
|
108 | };
|
109 | addHiddenProperty(tag.value.type, 'parsedType', tagType.parsedType);
|
110 | }
|
111 |
|
112 | ['optional', 'nullable', 'variable', 'defaultvalue'].forEach(function(prop) {
|
113 | if (typeof tagType[prop] !== 'undefined') {
|
114 | tag.value[prop] = tagType[prop];
|
115 | }
|
116 | });
|
117 | }
|
118 |
|
119 | if (tagType.text && tagType.text.length) {
|
120 | tag.value.description = tagType.text;
|
121 | }
|
122 |
|
123 | if (tagDef.canHaveName) {
|
124 |
|
125 | if (tagType.name && tagType.name !== '-') { tag.value.name = tagType.name; }
|
126 | }
|
127 | }
|
128 | else {
|
129 | tag.value = tag.text;
|
130 | }
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | exports._replaceDictionary = function _replaceDictionary(dict) {
|
143 | jsdoc.tag.dictionary = dict;
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | var Tag = exports.Tag = function(tagTitle, tagBody, meta) {
|
155 | var tagDef;
|
156 | var trimOpts;
|
157 |
|
158 | meta = meta || {};
|
159 |
|
160 | this.originalTitle = trim(tagTitle);
|
161 |
|
162 |
|
163 | this.title = jsdoc.tag.dictionary.normalise(this.originalTitle);
|
164 |
|
165 | tagDef = jsdoc.tag.dictionary.lookUp(this.title);
|
166 | trimOpts = {
|
167 | keepsWhitespace: tagDef.keepsWhitespace,
|
168 | removesIndent: tagDef.removesIndent
|
169 | };
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | this.text = trim(tagBody, trimOpts, meta);
|
190 |
|
191 | if (this.text) {
|
192 | processTagText(this, tagDef, meta);
|
193 | }
|
194 |
|
195 | jsdoc.tag.validator.validate(this, tagDef, meta);
|
196 | };
|