-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinheritparams.js
151 lines (132 loc) · 4.75 KB
/
inheritparams.js
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
/**
* Constructor parameter inheritance JSdoc plugin. Uses a decent amount of @augments code. Please see inheritparams.md for usage instructions.
* @author Ernst Haagsman <ernsthaagsman@gmail.com>
*/
exports.defineTags = function(dictionary){
dictionary.defineTag('inheritparams', {
mustHaveValue: true,
onTagged: function(doclet, tag) {
if (!doclet.inheritparams) {doclet.inheritparams = [];}
doclet.inheritparams.push(tag.value);
}
});
};
exports.handlers = {
processingComplete: function(doclets) {
var docs = doclets.doclets;
var dependencies = mapDependencies(docs.index);
var sorted = sort(dependencies);
var longnames = [];
// only build the list of longnames if we'll actually need it
if (sorted.length) {
longnames = docs.map(function(doc) {
if (doc.longname) {
return doc.longname;
}
});
}
sorted.forEach(function(name) {
// Copy inherited constructor parameters
var inheritedparams = {};
var doclets = docs.index[name];
var doc, parents, parent, candidates;
var doop = require("jsdoc/util/doop").doop;
if(!doclets) { return; }
for (var i = 0, ii = doclets.length; i < ii; i++) {
doc = doclets[i];
if (!doc.inheritparams) { continue; }
parents = doc.augments;
candidates = [];
if (parents && doc.kind === "class") {
for (var j = 0, jj = parents.length; j < jj; j++) {
parent = docs.index[parents[j]];
if(!parent[0].params) {continue;}
candidates = candidates.concat(doop(parent[0].params));
}
}
candidates = candidates.filter(function(param){
for (var h = 0, hh = doc.inheritparams.length; h < hh; h++)
{
if (param.name.indexOf(doc.inheritparams[h]) !== -1)
{
param.inherited = true;
return true;
}
}
return false;
});
inheritedparams[doc.longname] = candidates;
}
if(Object.keys(inheritedparams).length)
{
for (var k = 0, kk = docs.length; k < kk; k++)
{
if(inheritedparams[docs[k].longname])
{
if(docs[k].params)
{
var existing_param_names = docs[k].params.map(function(param){
return param.name;
});
inheritedparams[docs[k].longname].forEach(function(param){
if(existing_param_names.indexOf(param.name) === -1)
{
docs[k].params.push(param);
}
});
}
else
{
docs[k].params = inheritedparams[docs[k].longname];
}
}
}
}
});
}
};
function mapDependencies(index) {
var doclets, doc, len, dependencies = {};
Object.keys(index).forEach(function(name) {
doclets = index[name];
for (var i = 0, ii = doclets.length; i < ii; ++i) {
doc = doclets[i];
if (doc.kind === "class" || doc.kind === "external") {
dependencies[name] = {};
len = doc.augments && doc.augments.length || 0;
for (var j = 0; j < len; ++j) {
dependencies[name][doc.augments[j]] = true;
}
}
}
});
return dependencies;
}
function sort(dependencies) {
var sorter = new Sorter(dependencies);
return sorter.sort();
}
function Sorter(dependencies) {
this.dependencies = dependencies;
this.visited = {};
this.sorted = [];
}
Sorter.prototype.visit = function(key) {
var self = this;
if (!(key in this.visited)) {
this.visited[key] = true;
if (this.dependencies[key]) {
Object.keys(this.dependencies[key]).forEach(function(path) {
self.visit(path);
});
}
this.sorted.push(key);
}
};
Sorter.prototype.sort = function() {
var self = this;
Object.keys(this.dependencies).forEach(function(key) {
self.visit(key);
});
return this.sorted;
};