|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * Add paths to each comment, making it possible to generate permalinks |
5 |
| - * that differentiate between instance functions with the same name but |
6 |
| - * different `@memberof` values. |
7 |
| - * |
8 |
| - * Person#say // the instance method named "say." |
9 |
| - * Person.say // the static method named "say." |
10 |
| - * Person~say // the inner method named "say." |
11 |
| - * |
12 |
| - * @param {Object} comment the jsdoc comment |
13 |
| - * @param {Array<string>} prefix an array of strings representing names |
14 |
| - * @param {string} namepath the namepath so far |
15 |
| - * @returns {undefined} changes its input by reference. |
16 |
| - */ |
17 |
| -function addPath(comment, prefix, namepath) { |
18 |
| - comment.path = prefix.concat([comment.name]); |
19 |
| - comment.members.instance.forEach(function (member) { |
20 |
| - addPath(member, comment.path, comment.namepath); |
21 |
| - }); |
22 |
| - comment.members.static.forEach(function (member) { |
23 |
| - addPath(member, comment.path, namepath); |
24 |
| - }); |
25 |
| -} |
26 |
| - |
27 | 3 | /**
|
28 | 4 | * @param {Array<Object>} comments an array of parsed comments
|
29 | 5 | * @returns {Array<Object>} nested comments, with only root comments
|
30 | 6 | * at the top level.
|
31 | 7 | */
|
32 |
| -function inferHierarchy(comments) { |
33 |
| - var nameIndex = {}, i; |
34 |
| - |
35 |
| - // We're going to iterate comments in reverse to generate the memberships so |
36 |
| - // to avoid reversing the sort order we reverse the array for the name index. |
37 |
| - comments.reverse(); |
38 |
| - |
39 |
| - // First, create a fast lookup index of Namespace names |
40 |
| - // that might be used in memberof tags, and let all objects |
41 |
| - // have members |
42 |
| - for (i = 0; i < comments.length; i++) { |
43 |
| - nameIndex[comments[i].name] = comments[i]; |
44 |
| - comments[i].members = { instance: [], static: [] }; |
45 |
| - } |
| 8 | +module.exports = function (comments) { |
| 9 | + var id = 0, |
| 10 | + root = { |
| 11 | + members: { |
| 12 | + instance: {}, |
| 13 | + static: {} |
| 14 | + } |
| 15 | + }; |
46 | 16 |
|
47 |
| - for (i = comments.length - 1; i >= 0; i--) { |
48 |
| - var comment = comments[i]; |
| 17 | + comments.forEach(function (comment) { |
| 18 | + var path = []; |
49 | 19 |
|
50 |
| - if (!comment.memberof) { |
51 |
| - continue; |
| 20 | + if (comment.memberof) { |
| 21 | + // TODO: full namepath parsing |
| 22 | + path = comment.memberof |
| 23 | + .split('.') |
| 24 | + .map(function (segment) { |
| 25 | + return ['static', segment]; |
| 26 | + }); |
52 | 27 | }
|
53 | 28 |
|
54 |
| - var memberOfTag = comment.tags.filter(function (tag) { |
55 |
| - return tag.title === 'memberof' |
56 |
| - })[0]; |
57 |
| - var memberOfTagLineNumber = (memberOfTag && memberOfTag.lineNumber) || 0; |
58 |
| - |
59 |
| - var parent = nameIndex[comment.memberof]; |
60 |
| - |
61 |
| - if (!parent) { |
| 29 | + if (!comment.name) { |
62 | 30 | comment.errors.push({
|
63 |
| - message: 'memberof reference to ' + comment.memberof + ' not found', |
64 |
| - commentLineNumber: memberOfTagLineNumber |
| 31 | + message: 'could not determine @name for hierarchy' |
65 | 32 | });
|
66 |
| - continue; |
67 | 33 | }
|
68 | 34 |
|
69 |
| - parent.members[comment.scope || 'static'].push(comment); |
| 35 | + path.push([ |
| 36 | + comment.scope || 'static', |
| 37 | + comment.name || ('unknown_' + id++) |
| 38 | + ]); |
70 | 39 |
|
71 |
| - // remove non-root nodes from the lowest level: these are reachable |
72 |
| - // as members of other docs. |
73 |
| - comments.splice(i, 1); |
74 |
| - } |
| 40 | + var node = root; |
75 | 41 |
|
76 |
| - // Now the members are in the right order but the root comments are reversed |
77 |
| - // so we reverse once more. |
78 |
| - comments.reverse(); |
| 42 | + while (path.length) { |
| 43 | + var segment = path.shift(), |
| 44 | + scope = segment[0], |
| 45 | + name = segment[1]; |
79 | 46 |
|
80 |
| - for (i = 0; i < comments.length; i++) { |
81 |
| - addPath(comments[i], [], ''); |
82 |
| - } |
| 47 | + if (!node.members[scope][name]) { |
| 48 | + node.members[scope][name] = { |
| 49 | + comments: [], |
| 50 | + members: { |
| 51 | + instance: {}, |
| 52 | + static: {} |
| 53 | + } |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + node = node.members[scope][name]; |
| 58 | + } |
| 59 | + |
| 60 | + node.comments.push(comment); |
| 61 | + }); |
| 62 | + |
| 63 | + /* |
| 64 | + * Massage the hierarchy into a format more suitable for downstream consumers: |
| 65 | + * |
| 66 | + * * Individual top-level scopes are collapsed to a single array |
| 67 | + * * Members at intermediate nodes are copied over to the corresponding comments, |
| 68 | + * with multisignature comments allowed. |
| 69 | + * * Intermediate nodes without corresponding comments indicate an undefined |
| 70 | + * @memberof reference. Emit an error, and reparent the offending comment to |
| 71 | + * the root. |
| 72 | + * * Add paths to each comment, making it possible to generate permalinks |
| 73 | + * that differentiate between instance functions with the same name but |
| 74 | + * different `@memberof` values. |
| 75 | + * |
| 76 | + * Person#say // the instance method named "say." |
| 77 | + * Person.say // the static method named "say." |
| 78 | + * Person~say // the inner method named "say." |
| 79 | + */ |
| 80 | + function toComments(nodes, root, hasUndefinedParent, path) { |
| 81 | + var result = [], scope; |
| 82 | + |
| 83 | + path = path || []; |
| 84 | + |
| 85 | + for (var name in nodes) { |
| 86 | + var node = nodes[name]; |
| 87 | + |
| 88 | + for (scope in node.members) { |
| 89 | + node.members[scope] = toComments(node.members[scope], root || result, |
| 90 | + !node.comments.length, |
| 91 | + node.comments.length ? path.concat(node.comments[0]) : []); |
| 92 | + } |
| 93 | + |
| 94 | + for (var i = 0; i < node.comments.length; i++) { |
| 95 | + var comment = node.comments[i]; |
| 96 | + |
| 97 | + comment.members = {}; |
| 98 | + for (scope in node.members) { |
| 99 | + comment.members[scope] = node.members[scope]; |
| 100 | + } |
83 | 101 |
|
84 |
| - return comments; |
85 |
| -} |
| 102 | + comment.path = path.map(function (n) { |
| 103 | + return n.name; |
| 104 | + }).concat(comment.name); |
| 105 | + |
| 106 | + if (hasUndefinedParent) { |
| 107 | + var memberOfTag = comment.tags.filter(function (tag) { |
| 108 | + return tag.title === 'memberof' |
| 109 | + })[0]; |
| 110 | + var memberOfTagLineNumber = (memberOfTag && memberOfTag.lineNumber) || 0; |
| 111 | + |
| 112 | + comment.errors.push({ |
| 113 | + message: '@memberof reference to ' + comment.memberof + ' not found', |
| 114 | + commentLineNumber: memberOfTagLineNumber |
| 115 | + }); |
| 116 | + |
| 117 | + root.push(comment); |
| 118 | + } else { |
| 119 | + result.push(comment); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return result; |
| 125 | + } |
86 | 126 |
|
87 |
| -module.exports = inferHierarchy; |
| 127 | + return toComments(root.members.static); |
| 128 | +}; |
0 commit comments