|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var typeParserPatched = false; |
| 4 | +var typeLinkerPatched = false; |
| 5 | +var typeScriptTypePattern = /[&;]|\?:|=>|\bkeyof\b|\btypeof\b/; |
| 6 | + |
| 7 | +function normalizeType(type) { |
| 8 | + return type.replace(/\r?\n|\r/g, "\n"); |
| 9 | +} |
| 10 | + |
| 11 | +function patchTypeExpressionParser(dictionary) { |
| 12 | + var type = require("jsdoc/tag/type"); |
| 13 | + var parse = type.parse, |
| 14 | + typeTag = dictionary.lookUp("type"); |
| 15 | + |
| 16 | + if (typeTag && !typeTag.protobufjsPatched) { |
| 17 | + typeTag.onTagText = function onTypeTagText(text) { |
| 18 | + var openIdx = text.indexOf("{"), |
| 19 | + closeIdx = text.indexOf("}"); |
| 20 | + |
| 21 | + if (openIdx !== 0 || closeIdx <= openIdx + 1) |
| 22 | + text = "{" + text + "}"; |
| 23 | + |
| 24 | + return text; |
| 25 | + }; |
| 26 | + typeTag.protobufjsPatched = true; |
| 27 | + } |
| 28 | + |
| 29 | + if (typeParserPatched) |
| 30 | + return; |
| 31 | + typeParserPatched = true; |
| 32 | + |
| 33 | + type.parse = function parseTypeScriptType(tagValue, canHaveName, canHaveType) { |
| 34 | + try { |
| 35 | + return parse(tagValue, canHaveName, canHaveType); |
| 36 | + } catch (e) { |
| 37 | + if (!canHaveType) |
| 38 | + throw e; |
| 39 | + |
| 40 | + var text = String(tagValue || ""); |
| 41 | + var open = text.indexOf("{"); |
| 42 | + if (open < 0) |
| 43 | + throw e; |
| 44 | + |
| 45 | + var depth = 0; |
| 46 | + var close = -1; |
| 47 | + for (var i = open; i < text.length; ++i) { |
| 48 | + var ch = text.charAt(i); |
| 49 | + if (ch === "{") |
| 50 | + ++depth; |
| 51 | + else if (ch === "}" && --depth === 0) { |
| 52 | + close = i; |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + if (close < 0) |
| 57 | + throw e; |
| 58 | + |
| 59 | + var expression = normalizeType(text.substring(open + 1, close)).trim(); |
| 60 | + if (!typeScriptTypePattern.test(expression) && !/^\s*\{/.test(expression)) |
| 61 | + throw e; |
| 62 | + |
| 63 | + var name = canHaveName ? text.substring(close + 1).trim() : ""; |
| 64 | + return { |
| 65 | + type: [ expression ], |
| 66 | + typeExpression: expression, |
| 67 | + name: name |
| 68 | + }; |
| 69 | + } |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +function patchTypeExpressionLinks() { |
| 74 | + if (typeLinkerPatched) |
| 75 | + return; |
| 76 | + typeLinkerPatched = true; |
| 77 | + |
| 78 | + var helper = require("jsdoc/util/templateHelper"); |
| 79 | + var linkto = helper.linkto; |
| 80 | + |
| 81 | + helper.linkto = function linkTypeScriptType(longname, linkText, cssClass, fragmentId) { |
| 82 | + var stripped = longname ? String(longname).replace(/^<|>$/g, "") : ""; |
| 83 | + if (stripped && typeScriptTypePattern.test(stripped) && |
| 84 | + !/^(http|ftp)s?:\/\//.test(stripped) && |
| 85 | + /\{@.+\}/.test(stripped) === false && |
| 86 | + /^<[\s\S]+>/.test(stripped) === false) |
| 87 | + return linkText || helper.htmlsafe(longname); |
| 88 | + |
| 89 | + return linkto.call(this, longname, linkText, cssClass, fragmentId); |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +exports.normalizeType = normalizeType; |
| 94 | + |
| 95 | +exports.defineTags = function(dictionary) { |
| 96 | + patchTypeExpressionParser(dictionary); |
| 97 | + patchTypeExpressionLinks(); |
| 98 | +}; |
0 commit comments