Skip to content

Commit 2dcae25

Browse files
committed
CLI: Several improvements to tsd-jsdoc emitted comments
1 parent ff85800 commit 2dcae25

File tree

7 files changed

+188
-294
lines changed

7 files changed

+188
-294
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# [6.8.0](https://github.com/dcodeIO/protobuf.js/releases/tag/6.8.0)
22

33
## Breaking
4+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/ff858003f525db542cbb270777b6fab3a230c9bb) Replaced Buffer and Long types with interfaces and removed stubs<br />
5+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/22f907c49adbbdf09b72bde5299271dbe0ee9cbe) Removed Message#toObject in favor of having just the static version (unnecessary static code otherwise)<br />
46
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c97b61811248df002f1fb93557b982bc0aa27309) Everything uses interfaces now instead of typedefs (SomethingProperties is now ISomething)<br />
57
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/b9f179064f3ddf683f13e0d4e17840301be64010) ReflectionObject#toJSON properly omits explicit undefined values<br />
68
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/7a6f98b5e74f9e9142f9be3ba0683caeaff916c4) Initial implementation of TypeScript decorators<br />
@@ -19,6 +21,9 @@
1921
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/3939667ef1f37b025bd7f9476015890496d50e00) Added 'json' conversion option for proto3 JSON mapping compatibility of NaN and Infinity + additional documentation of util.toJSONOptions, see [#351](https://github.com/dcodeIO/protobuf.js/issues/351)<br />
2022

2123
## CLI
24+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/ff858003f525db542cbb270777b6fab3a230c9bb) Further TypeScript definition improvements<br />
25+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/22f907c49adbbdf09b72bde5299271dbe0ee9cbe) Relieved tsd files from unnecessary comments<br />
26+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/22f907c49adbbdf09b72bde5299271dbe0ee9cbe) Generate TS namespaces for vars and functions with properties<br />
2227
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/b355115e619c6595ac9d91897cfe628ef0e46054) Prefer @tstype over @type when generating typedefs (tsd-jsdoc)<br />
2328

2429
## Docs
@@ -28,6 +33,7 @@
2833
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/35a663757efe188bea552aef017837bc6c6a481a) Additional docs on TS/decorators usage<br />
2934

3035
## Other
36+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/22f907c49adbbdf09b72bde5299271dbe0ee9cbe) Regenerated dist files<br />
3137
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/5bc3541d2da19e2857dc884f743d37c27e8e21f2) Even more documentation and typings for ext/descriptor<br />
3238
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/773e6347b57e4a5236b1ef0bb8d361e4b233caf7) ext/descriptor docs<br />
3339
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/773e6347b57e4a5236b1ef0bb8d361e4b233caf7) Decorators coverage<br />

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ The npm package depends on [@types/node](https://www.npmjs.com/package/@types/no
469469

470470
#### Using the JS API
471471

472-
The API shown above works pretty much the same with TypeScript. However, because everything is typed, accessing fields on instances of dynamically generated message classes requires either (1) using bracket-notation (i.e. `message["awesomeField"]`), (2) explicit casts or (3) the use of a [typings file generated for its static counterpart](#pbts-for-typescript).
472+
The API shown above works pretty much the same with TypeScript. However, because everything is typed, accessing fields on instances of dynamically generated message classes requires either using bracket-notation (i.e. `message["awesomeField"]`) or explicit casts. Alternatively, it is possible to use a [typings file generated for its static counterpart](#pbts-for-typescript).
473473

474474
```ts
475475
import { load } from "protobufjs"; // respectively "./node_modules/protobufjs"
@@ -509,7 +509,7 @@ let decoded = AwesomeMessage.decode(buffer);
509509

510510
The library also includes an early implementation of [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
511511

512-
**Note** that this API is rather new in protobuf.js (and probably buggy) and that decorators are an experimental feature in TypeScript. Also note that declaration order is important depending on the JS target. For example, `@Field.d(2, AwesomeArrayMessage)` requires that `AwesomeArrayMessage` has been defined earlier when targeting `ES5`.
512+
**Note** that decorators are an experimental feature in TypeScript and that declaration order is important depending on the JS target. For example, `@Field.d(2, AwesomeArrayMessage)` requires that `AwesomeArrayMessage` has been defined earlier when targeting `ES5`.
513513

514514
```ts
515515
import { Message, Type, Field, OneOf } from "protobufjs/light"; // respectively "./node_modules/protobufjs/light.js"

cli/lib/tsd-jsdoc/publish.js

+81-23
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,38 @@ function writeln() {
134134
indentWritten = false;
135135
}
136136

137-
var skipTagsRe = /@(type|tstype|const|memberof|name|exports|interface|extends|implements|constructor|function|template|property|this|typedef|variation|example|returns \{undefined})[^@$]*/g,
138-
skipTypeRe = /(@[^ ]+) \{[^\s$]+}/g;
137+
var keepTags = [
138+
"param",
139+
"returns",
140+
"throws",
141+
"see"
142+
];
143+
144+
// parses a comment into text and tags
145+
function parseComment(comment) {
146+
var lines = comment.replace(/^ *\/\*\* *|^ *\*\/| *\*\/ *$|^ *\* */mg, "").trim().split(/\r?\n/g);
147+
var desc;
148+
var text = [];
149+
var tags = null;
150+
for (var i = 0; i < lines.length; ++i) {
151+
var match = /^@(\w+)\b/.exec(lines[i]);
152+
if (match) {
153+
if (!tags) {
154+
tags = [];
155+
desc = text;
156+
}
157+
text = [];
158+
tags.push({ name: match[1], text: text });
159+
lines[i] = lines[i].substring(match[1].length + 1).trim();
160+
}
161+
if (lines[i].length || text.length)
162+
text.push(lines[i]);
163+
}
164+
return {
165+
text: desc || text,
166+
tags: tags || []
167+
};
168+
}
139169

140170
// writes a comment
141171
function writeComment(comment, otherwiseNewline) {
@@ -144,26 +174,40 @@ function writeComment(comment, otherwiseNewline) {
144174
writeln();
145175
return;
146176
}
147-
comment = comment.replace(skipTagsRe, "").trim();
148-
if (comment.charAt(comment.length - 1) !== "/")
149-
comment += "/";
150-
var lines = comment.split(/\r?\n/g);
151-
if (lines.length === 3) {
152-
writeln();
153-
writeln("/*" + lines[1].trim() + " */");
154-
} else {
155-
var first = true;
156-
lines.forEach(function(line) {
157-
line = line.trim().replace(/^\*/, " *").replace(skipTypeRe, "$1");
158-
if (line.length) {
159-
if (first) {
160-
writeln();
161-
first = false;
177+
178+
if (typeof comment !== "object")
179+
comment = parseComment(comment);
180+
comment.tags = comment.tags.filter(function(tag) {
181+
return keepTags.indexOf(tag.name) > -1 && (tag.name !== "returns" || tag.text[0] !== "{undefined}");
182+
});
183+
writeln();
184+
if (!comment.tags.length && comment.text.length < 2) {
185+
writeln("/** " + comment.text[0] + " */");
186+
return;
187+
}
188+
writeln("/**");
189+
comment.text.forEach(function(line) {
190+
writeln(" * ", line);
191+
});
192+
comment.tags.forEach(function(tag) {
193+
var started = false;
194+
if (tag.text.length) {
195+
tag.text.forEach(function(line, i) {
196+
if (i > 0)
197+
write(" * ");
198+
else if (tag.name !== "throws")
199+
line = line.replace(/^\{[^\s]*} ?/, "");
200+
if (!line.length)
201+
return;
202+
if (!started) {
203+
write(" * @", tag.name, " ");
204+
started = true;
162205
}
163206
writeln(line);
164-
}
165-
});
166-
}
207+
});
208+
}
209+
});
210+
writeln(" */");
167211
}
168212

169213
// recursively replaces all occurencies of re's match
@@ -184,12 +228,17 @@ function replaceRecursive(name, re, fn) {
184228

185229
// tests if an element is considered to be a class or class-like
186230
function isClassLike(element) {
187-
return element && (element.kind === "class" || element.kind === "interface" || element.kind === "mixin");
231+
return isClass(element) || isInterface(element);
232+
}
233+
234+
// tests if an element is considered to be a class
235+
function isClass(element) {
236+
return element && element.kind === "class";
188237
}
189238

190239
// tests if an element is considered to be an interface
191240
function isInterface(element) {
192-
return element && element.kind === "interface";
241+
return element && (element.kind === "interface" || element.kind === "mixin");
193242
}
194243

195244
// tests if an element is considered to be a namespace
@@ -254,7 +303,16 @@ function getTypeOf(element) {
254303
// begins writing the definition of the specified element
255304
function begin(element, is_interface) {
256305
if (!seen[element.longname]) {
257-
writeComment(element.comment, is_interface || isInterface(element) || isClassLike(element) || isNamespace(element) || element.isEnum || element.scope === "global");
306+
if (isClass(element)) {
307+
var comment = parseComment(element.comment);
308+
var classdesc = comment.tags.find(function(tag) { return tag.name === "classdesc"; });
309+
if (classdesc) {
310+
comment.text = classdesc.text;
311+
comment.tags = [];
312+
}
313+
writeComment(comment, true);
314+
} else
315+
writeComment(element.comment, is_interface || isClassLike(element) || isNamespace(element) || element.isEnum || element.scope === "global");
258316
seen[element.longname] = element;
259317
} else
260318
writeln();

cli/targets/static.js

+1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ function buildType(ref, type) {
371371
"Constructs a new " + type.name + ".",
372372
type.comment ? "@classdesc " + type.comment : null,
373373
"@constructor",
374+
// "@extends $protobuf.Message<" + exportName(type) + ">",
374375
"@param {" + exportName(type, true) + "=} [" + (config.beautify ? "properties" : "p") + "] Properties to set",
375376
type.parent instanceof protobuf.Root ? "@name " + escapeName(type.name) : "@memberof " + exportName(type.parent)
376377
]);

0 commit comments

Comments
 (0)