-
Notifications
You must be signed in to change notification settings - Fork 0
/
references.js
218 lines (186 loc) · 6.98 KB
/
references.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
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
/*
subDoc
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
const RE_TYPE_BRACKETS = /<([^>]*)>/;
var parser = require("./parser");
/*
@name references.Reference
@type class
Reference object containing documentable information about a code entity.
*/
/*
@name references.Reference.name
@type prop <null | String>
Identifier or reference chain of code entity
*/
/*
@name references.Reference.type
@type prop <String>
The type of code entity that is being referenced.
~~~~
First word (item delimited by space) be any of:
* `"function"` for a function (defined by `function` keyword)
* `"class"` for a class (defined by `class` keyword), `"extends"` can be
concatenated after a space to represent a class extension
* `"method"` for a class method
* `"static"` for a static class method (defined by `static` keyword)
* `"prop"` for a class property (typically referenced as
`this.propName`)
* `"const"` for a constant variable (defined by `const` keyword)
* `"var"` for a variable (typically defined by `var` keyword)
* `"generator"` for a generator function (defined by `function*` syntax)
~~~~
May also be anything else, but this is discouraged. If the type is a
`"prop"`, `"const"` or `"var"`, the type's datatype annotation can also
be concatenated after a space.
*/
/*
@name references.Reference.synopsis
@type prop <String>
The synopsis describing code entity.
Leave this blank if there is no synopsis.
*/
/*
@name references.Reference.parameters
@type prop <[references.Parameter]>
A list of parameter references that code entity takes, if any.
*/
/*
@name references.Reference.returns
@type class <references.Return>
A return object that code entity gives, if any.
*/
exports.Reference = class {
constructor() {
this.name = null;
this.type = "function";
this.synopsis = "";
this.parameters = [];
this.returns = new exports.Return("undefined");
}
};
/*
@name references.Parameter
@type class
A reference to a parameter that is part of a code entity.
@param identifier <String> The identifier of the parameter
@param type <String = "*"> The datatype that a value should be of when used as an argument for this parameter
@param description <String = ""> A description of the parameter. Leave blank if there's no description
@param defaultValue <String = "undefined"> The default value the parameter will be if there is no matching argument specified
*/
/*
@name references.Parameter.identifier
@type prop <String>
*/
/*
@name references.Parameter.type
@type prop <String>
*/
/*
@name references.Parameter.description
@type prop <String>
*/
/*
@name references.Parameter.defaultValue
@type prop <String>
*/
exports.Parameter = class {
constructor(identifier, type = "*", description = "", defaultValue = "undefined") {
this.identifier = identifier;
this.type = type;
this.description = description;
this.defaultValue = defaultValue;
}
};
/*
@name references.Return
@type class
A reference to a value that a code entity returns.
@param type <String = "undefined"> The datatype of the value that is returned
@param description <String = ""> A description of the return. Leave blank if there's no description
*/
/*
@name references.Return.type
@type prop <String>
*/
/*
@name references.Return.description
@type prop <String>
*/
exports.Return = class {
constructor(type = "undefined", description = "") {
this.type = type;
this.description = description;
}
};
function parseParameter(comment) {
var newParameter = new exports.Parameter(null);
if ((comment.match(RE_TYPE_BRACKETS) || []).length > 1) {
var typeString = comment.match(RE_TYPE_BRACKETS)[1].split("=");
newParameter.type = typeString[0].trim();
if (typeString.length > 1) {
newParameter.defaultValue = typeString[1].trim();
}
comment = comment.replace(new RegExp(RE_TYPE_BRACKETS, "g"), "");
}
var tokens = comment.split(" ");
if (tokens[0] != "@param") {
throw new parser.ParseError("Parameter comment has not been declared with `@param`");
}
if (tokens.length < 2) {
throw new parser.ParseError("Parameter comment does not specify identifier");
}
newParameter.identifier = tokens[1];
for (var i = 2; i < tokens.length; i++) {
newParameter.description += " " + tokens[i];
}
newParameter.description = newParameter.description.trim();
return newParameter;
}
function parseReturn(comment) {
var newReturn = new exports.Return(null);
if ((comment.match(RE_TYPE_BRACKETS) || []).length > 1) {
newReturn.type = comment.match(RE_TYPE_BRACKETS)[1];
comment = comment.replace(new RegExp(RE_TYPE_BRACKETS, "g"), "");
}
var tokens = comment.split(" ");
if (tokens[0] != "@returns") {
throw new parser.ParseError("Return has not been declared with `@returns`");
}
for (var i = 1; i < tokens.length; i++) {
newReturn.description += " " + tokens[i];
}
newReturn.description = newReturn.description.trim();
return newReturn;
}
/*
@name references.parseComment
Parse a comment's type annotations to generate a code reference.
@param comment <String> Contents of comment to parse, excluding opening and closing comment syntax
@returns <references.Reference> The generated code reference instance
*/
exports.parseComment = function(comment) {
var commentLines = comment.split("\n").filter((item) => item.trim() != "");
var indentCount = Math.max(commentLines[0].search(/[\S\t]/), 0);
var newReferenceData = new exports.Reference();
// For lines that start with an indent, remove that first indent
commentLines = commentLines.map((item) => new RegExp(`[\\s\\t]{${indentCount},}`).test(item) ? item.slice(indentCount) : item);
for (var i = 0; i < commentLines.length; i++) {
if (commentLines[i].startsWith("@name")) {
newReferenceData.name = commentLines[i].split(" ").slice(1).join(" ");
} else if (commentLines[i].startsWith("@type")) {
newReferenceData.type = commentLines[i].split(" ").slice(1).join(" ");
} else if (commentLines[i].startsWith("@param")) {
newReferenceData.parameters.push(parseParameter(commentLines[i]));
} else if (commentLines[i].startsWith("@returns")) {
newReferenceData.returns = parseReturn(commentLines[i]);
} else {
newReferenceData.synopsis += " " + commentLines[i];
}
}
newReferenceData.synopsis = newReferenceData.synopsis.replace(new RegExp(" ".repeat(5), "g"), "\n").replace(/~~~~/g, "\n").trim();
return newReferenceData;
};