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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 1 1 1 1 584 1 584 584 584 584 584 584 584 436 436 436 3988 2 2 1 1 436 436 3988 435 435 435 3553 584 584 1 594 594 594 594 594 594 594 584 584 584 594 337 337 337 594 584 584 5 584 594 1 594 327 327 68 327 63 63 594 1 491 491 491 491 491 8 8 435 435 1 1 22 5 7 5 5 17 22 25 54 25 491 1 594 594 594 159 435 435 3 432 432 432 864 28 432 1 432 1 1 594 594 594 594 1188 591 591 | /** * @module jsdoc/tag/type * * @author Michael Mathews <micmath@gmail.com> * @author Jeff Williams <jeffrey.l.williams@gmail.com> * @license Apache License 2.0 - See file 'LICENSE.md' in this project. */ 'use strict'; var catharsis = require('catharsis'); var jsdoc = { name: require('jsdoc/name'), tag: { inline: require('jsdoc/tag/inline') }, util: { cast: require('jsdoc/util/cast') } }; var util = require('util'); /** * Information about a type expression extracted from tag text. * * @typedef TypeExpressionInfo * @memberof module:jsdoc/tag/type * @property {string} expression - The type expression. * @property {string} text - The updated tag text. */ /** @private */ function unescapeBraces(text) { return text.replace(/\\\{/g, '{') .replace(/\\\}/g, '}'); } /** * Extract a type expression from the tag text. * * @private * @param {string} string - The tag text. * @return {module:jsdoc/tag/type.TypeExpressionInfo} The type expression and updated tag text. */ function extractTypeExpression(string) { var completeExpression; var count = 0; var position = 0; var expression = ''; var startIndex = string.search(/\{[^@]/); var textStartIndex; if (startIndex !== -1) { // advance to the first character in the type expression position = textStartIndex = startIndex + 1; count++; while (position < string.length) { switch (string[position]) { case '\\': // backslash is an escape character, so skip the next character position++; break; case '{': count++; break; case '}': count--; break; default: // do nothing } if (count === 0) { completeExpression = string.slice(startIndex, position + 1); expression = string.slice(textStartIndex, position).trim(); break; } position++; } } string = completeExpression ? string.replace(completeExpression, '') : string; return { expression: unescapeBraces(expression), newString: string.trim() }; } /** @private */ function getTagInfo(tagValue, canHaveName, canHaveType) { var name = ''; var typeExpression = ''; var text = tagValue; var expressionAndText; var nameAndDescription; var typeOverride; if (canHaveType) { expressionAndText = extractTypeExpression(text); typeExpression = expressionAndText.expression; text = expressionAndText.newString; } if (canHaveName) { nameAndDescription = jsdoc.name.splitName(text); name = nameAndDescription.name; text = nameAndDescription.description; } // an inline @type tag, like {@type Foo}, overrides the type expression if (canHaveType) { typeOverride = jsdoc.tag.inline.extractInlineTag(text, 'type'); if (typeOverride.tags && typeOverride.tags[0]) { typeExpression = typeOverride.tags[0].text; } text = typeOverride.newString; } return { name: name, typeExpression: typeExpression, text: text }; } /** * Information provided in a JSDoc tag. * * @typedef {Object} TagInfo * @memberof module:jsdoc/tag/type * @property {string} TagInfo.defaultvalue - The default value of the member. * @property {string} TagInfo.name - The name of the member (for example, `myParamName`). * @property {boolean} TagInfo.nullable - Indicates whether the member can be set to `null` or * `undefined`. * @property {boolean} TagInfo.optional - Indicates whether the member is optional. * @property {string} TagInfo.text - Descriptive text for the member (for example, `The user's email * address.`). * @property {Array.<string>} TagInfo.type - The type or types that the member can contain (for * example, `string` or `MyNamespace.MyClass`). * @property {string} TagInfo.typeExpression - The type expression that was parsed to identify the * types. * @property {boolean} TagInfo.variable - Indicates whether the number of members that are provided * can vary (for example, in a function that accepts any number of parameters). */ // TODO: move to module:jsdoc/name? /** * Extract JSDoc-style type information from the name specified in the tag info, including the * member name; whether the member is optional; and the default value of the member. * * @private * @param {module:jsdoc/tag/type.TagInfo} tagInfo - Information contained in the tag. * @return {module:jsdoc/tag/type.TagInfo} Updated information from the tag. */ function parseName(tagInfo) { // like '[foo]' or '[ foo ]' or '[foo=bar]' or '[ foo=bar ]' or '[ foo = bar ]' // or 'foo=bar' or 'foo = bar' if ( /^(\[)?\s*(.+?)\s*(\])?$/.test(tagInfo.name) ) { tagInfo.name = RegExp.$2; // were the "optional" brackets present? if (RegExp.$1 && RegExp.$3) { tagInfo.optional = true; } // like 'foo=bar' or 'foo = bar' if ( /^(.+?)\s*=\s*(.+)$/.test(tagInfo.name) ) { tagInfo.name = RegExp.$1; tagInfo.defaultvalue = jsdoc.util.cast.cast(RegExp.$2); } } return tagInfo; } /** @private */ function getTypeStrings(parsedType, isOutermostType) { var applications; var typeString; var types = []; var TYPES = catharsis.Types; switch (parsedType.type) { case TYPES.AllLiteral: types.push('*'); break; case TYPES.FunctionType: types.push('function'); break; case TYPES.NameExpression: types.push(parsedType.name); break; case TYPES.NullLiteral: types.push('null'); break; case TYPES.RecordType: types.push('Object'); break; case TYPES.TypeApplication: // if this is the outermost type, we strip the modifiers; otherwise, we keep them if (isOutermostType) { applications = parsedType.applications.map(function(application) { return catharsis.stringify(application); }).join(', '); typeString = util.format( '%s.<%s>', getTypeStrings(parsedType.expression), applications ); types.push(typeString); } else { types.push( catharsis.stringify(parsedType) ); } break; case TYPES.TypeUnion: parsedType.elements.forEach(function(element) { types = types.concat( getTypeStrings(element) ); }); break; case TYPES.UndefinedLiteral: types.push('undefined'); break; case TYPES.UnknownLiteral: types.push('?'); break; default: // this shouldn't happen throw new Error( util.format('unrecognized type %s in parsed type: %j', parsedType.type, parsedType) ); } return types; } /** * Extract JSDoc-style and Closure Compiler-style type information from the type expression * specified in the tag info. * * @private * @param {module:jsdoc/tag/type.TagInfo} tagInfo - Information contained in the tag. * @return {module:jsdoc/tag/type.TagInfo} Updated information from the tag. */ function parseTypeExpression(tagInfo) { var errorMessage; var parsedType; // don't try to parse empty type expressions if (!tagInfo.typeExpression) { return tagInfo; } try { parsedType = catharsis.parse(tagInfo.typeExpression, {jsdoc: true}); } catch (e) { // always re-throw so the caller has a chance to report which file was bad throw new Error( util.format('Invalid type expression "%s": %s', tagInfo.typeExpression, e.message) ); } tagInfo.type = tagInfo.type.concat( getTypeStrings(parsedType, true) ); tagInfo.parsedType = parsedType; // Catharsis and JSDoc use the same names for 'optional' and 'nullable'... ['optional', 'nullable'].forEach(function(key) { if (parsedType[key] !== null && parsedType[key] !== undefined) { tagInfo[key] = parsedType[key]; } }); // ...but not 'variable'. if (parsedType.repeatable !== null && parsedType.repeatable !== undefined) { tagInfo.variable = parsedType.repeatable; } return tagInfo; } // TODO: allow users to add/remove type parsers (perhaps via plugins) var typeParsers = [parseName, parseTypeExpression]; /** * Parse the value of a JSDoc tag. * * @param {string} tagValue - The value of the tag. For example, the tag `@param {string} name` has * a value of `{string} name`. * @param {boolean} canHaveName - Indicates whether the value can include a symbol name. * @param {boolean} canHaveType - Indicates whether the value can include a type expression that * describes the symbol. * @return {module:jsdoc/tag/type.TagInfo} Information obtained from the tag. * @throws {Error} Thrown if a type expression cannot be parsed. */ exports.parse = function(tagValue, canHaveName, canHaveType) { Iif (typeof tagValue !== 'string') { tagValue = ''; } var tagInfo = getTagInfo(tagValue, canHaveName, canHaveType); tagInfo.type = tagInfo.type || []; typeParsers.forEach(function(parser) { tagInfo = parser.call(this, tagInfo); }); // if we wanted a type, but the parsers didn't add any type names, use the type expression Iif (canHaveType && !tagInfo.type.length && tagInfo.typeExpression) { tagInfo.type = [tagInfo.typeExpression]; } return tagInfo; }; |