|
| 1 | +var Syntax = require('doctrine').Syntax, |
| 2 | + globalsDocs = require('globals-docs'), |
| 3 | + u = require('unist-builder'); |
| 4 | + |
| 5 | +function t(text) { |
| 6 | + return u('text', text); |
| 7 | +} |
| 8 | + |
| 9 | +function link(text) { |
| 10 | + var docs = globalsDocs.getDoc(text); |
| 11 | + if (docs) { |
| 12 | + return u('link', { href: docs }, [u('text', text)]); |
| 13 | + } |
| 14 | + return u('text', text); |
| 15 | +} |
| 16 | + |
| 17 | +function commaList(getNamedLink, items, start, end, sep) { |
| 18 | + var res = []; |
| 19 | + if (start) { |
| 20 | + res.push(t(start)); |
| 21 | + } |
| 22 | + for (var i = 0, iz = items.length; i < iz; ++i) { |
| 23 | + res = res.concat(formatType(items[i], getNamedLink)); |
| 24 | + if (i + 1 !== iz) { |
| 25 | + res.push(t(sep || ', ')); |
| 26 | + } |
| 27 | + } |
| 28 | + if (end) { |
| 29 | + res.push(t(end)); |
| 30 | + } |
| 31 | + return res; |
| 32 | +} |
| 33 | + |
| 34 | +function decorate(formatted, str, prefix) { |
| 35 | + if (prefix) { |
| 36 | + return [t(str)].concat(formatted); |
| 37 | + } |
| 38 | + return formatted.concat(t(str)); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Helper used to format JSDoc-style type definitions into HTML or Markdown. |
| 43 | + * |
| 44 | + * @name formatType |
| 45 | + * @param {Object} node type object in doctrine style |
| 46 | + * @param {function(text): text} getNamedLink a function that tries |
| 47 | + * to find a URL to point a named link to |
| 48 | + * @returns {string} string |
| 49 | + * @example |
| 50 | + * var x = { type: 'NameExpression', name: 'String' }; |
| 51 | + * // in template |
| 52 | + * // {{ type x }} |
| 53 | + * // generates String |
| 54 | + */ |
| 55 | +function formatType(node, getNamedLink) { |
| 56 | + var result = []; |
| 57 | + |
| 58 | + if (!node) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + switch (node.type) { |
| 63 | + case Syntax.NullableLiteral: |
| 64 | + return [t('?')]; |
| 65 | + case Syntax.AllLiteral: |
| 66 | + return [t('Any')]; |
| 67 | + case Syntax.NullLiteral: |
| 68 | + return [t('null')]; |
| 69 | + case Syntax.VoidLiteral: |
| 70 | + return [t('void')]; |
| 71 | + case Syntax.UndefinedLiteral: |
| 72 | + return [link('undefined')]; |
| 73 | + case Syntax.NameExpression: |
| 74 | + return [link(node.name)]; |
| 75 | + case Syntax.ParameterType: |
| 76 | + return [t(node.name + ': ')].concat(formatType(node.expression, getNamedLink)); |
| 77 | + |
| 78 | + case Syntax.TypeApplication: |
| 79 | + return formatType(node.expression, getNamedLink) |
| 80 | + .concat(commaList(getNamedLink, node.applications, '.<', '>')); |
| 81 | + case Syntax.UnionType: |
| 82 | + return commaList(getNamedLink, node.elements, '(', ')', '|'); |
| 83 | + case Syntax.ArrayType: |
| 84 | + return commaList(getNamedLink, node.elements, '[', ']'); |
| 85 | + case Syntax.RecordType: |
| 86 | + return commaList(getNamedLink, node.fields, '{', '}'); |
| 87 | + |
| 88 | + case Syntax.FieldType: |
| 89 | + if (node.value) { |
| 90 | + return [t(node.key + ': ')].concat(formatType(node.value, getNamedLink)); |
| 91 | + } |
| 92 | + return [t(node.key)]; |
| 93 | + |
| 94 | + case Syntax.FunctionType: |
| 95 | + result = [t('function (')]; |
| 96 | + |
| 97 | + if (node['this']) { |
| 98 | + if (node['new']) { |
| 99 | + result.push(t('new: ')); |
| 100 | + } else { |
| 101 | + result.push(t('this: ')); |
| 102 | + } |
| 103 | + |
| 104 | + result = result.concat(formatType(node['this'], getNamedLink)); |
| 105 | + |
| 106 | + if (node.params.length !== 0) { |
| 107 | + result.push(t(', ')); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + result = result.concat(commaList(getNamedLink, node.params, '', ')')); |
| 112 | + |
| 113 | + if (node.result) { |
| 114 | + result = result.concat([t(': ')].concat(formatType(node.result, getNamedLink))); |
| 115 | + } |
| 116 | + return result; |
| 117 | + |
| 118 | + case Syntax.RestType: |
| 119 | + // note that here we diverge from doctrine itself, which |
| 120 | + // lets the expression be omitted. |
| 121 | + return decorate(formatType(node.expression, getNamedLink), '...', true); |
| 122 | + case Syntax.OptionalType: |
| 123 | + return decorate(formatType(node.expression, getNamedLink), '='); |
| 124 | + case Syntax.NonNullableType: |
| 125 | + return decorate(formatType(node.expression, getNamedLink), '!', node.prefix); |
| 126 | + case Syntax.NullableType: |
| 127 | + return decorate(formatType(node.expression, getNamedLink), '?', node.prefix); |
| 128 | + |
| 129 | + default: |
| 130 | + throw new Error('Unknown type ' + node.type); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +module.exports.formatType = formatType; |
0 commit comments