Skip to content

Commit bed5142

Browse files
committed
CLI: With null/undefined eliminated by constructors and .create, document message fields as non-optional where applicable (ideally used with TS & strictNullChecks), see protobufjs#743
1 parent a66f764 commit bed5142

File tree

10 files changed

+437
-432
lines changed

10 files changed

+437
-432
lines changed

cli/targets/static.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ function toJsType(field) {
333333
type = "*"; // should not happen
334334
break;
335335
}
336-
return field.repeated ? "Array.<" + type + ">"
337-
: field.map ? "Object.<string," + type + ">"
338-
: type;
336+
if (field.map)
337+
return "Object.<string," + type + ">";
338+
if (field.repeated)
339+
return "Array.<" + type + ">";
340+
return type;
339341
}
340342

341343
function buildType(ref, type) {
@@ -374,9 +376,12 @@ function buildType(ref, type) {
374376
var prop = util.safeProp(field.name);
375377
if (config.comments) {
376378
push("");
379+
var jsType = toJsType(field);
380+
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type)
381+
jsType = "(" + jsType + "|null)";
377382
pushComment([
378383
field.comment || type.name + " " + field.name + ".",
379-
"@type {" + toJsType(field) + (field.optional ? "|undefined" : "") + "}"
384+
"@type {" + jsType + "}"
380385
]);
381386
} else if (firstField) {
382387
push("");

tests/data/comments.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ $root.Test1 = (function() {
3939

4040
/**
4141
* Field with a comment.
42-
* @type {string|undefined}
42+
* @type {string}
4343
*/
4444
Test1.prototype.field1 = "";
4545

4646
/**
4747
* Test1 field2.
48-
* @type {number|undefined}
48+
* @type {number}
4949
*/
5050
Test1.prototype.field2 = 0;
5151

5252
/**
5353
* Field with a comment and a <a href="http://example.com/foo/">link</a>
54-
* @type {boolean|undefined}
54+
* @type {boolean}
5555
*/
5656
Test1.prototype.field3 = false;
5757

tests/data/convert.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,55 +46,55 @@ $root.Message = (function() {
4646

4747
/**
4848
* Message stringVal.
49-
* @type {string|undefined}
49+
* @type {string}
5050
*/
5151
Message.prototype.stringVal = "";
5252

5353
/**
5454
* Message stringRepeated.
55-
* @type {Array.<string>|undefined}
55+
* @type {Array.<string>}
5656
*/
5757
Message.prototype.stringRepeated = $util.emptyArray;
5858

5959
/**
6060
* Message uint64Val.
61-
* @type {number|Long|undefined}
61+
* @type {number|Long}
6262
*/
6363
Message.prototype.uint64Val = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
6464

6565
/**
6666
* Message uint64Repeated.
67-
* @type {Array.<number|Long>|undefined}
67+
* @type {Array.<number|Long>}
6868
*/
6969
Message.prototype.uint64Repeated = $util.emptyArray;
7070

7171
/**
7272
* Message bytesVal.
73-
* @type {Uint8Array|undefined}
73+
* @type {Uint8Array}
7474
*/
7575
Message.prototype.bytesVal = $util.newBuffer([]);
7676

7777
/**
7878
* Message bytesRepeated.
79-
* @type {Array.<Uint8Array>|undefined}
79+
* @type {Array.<Uint8Array>}
8080
*/
8181
Message.prototype.bytesRepeated = $util.emptyArray;
8282

8383
/**
8484
* Message enumVal.
85-
* @type {Message.SomeEnum|undefined}
85+
* @type {Message.SomeEnum}
8686
*/
8787
Message.prototype.enumVal = 1;
8888

8989
/**
9090
* Message enumRepeated.
91-
* @type {Array.<Message.SomeEnum>|undefined}
91+
* @type {Array.<Message.SomeEnum>}
9292
*/
9393
Message.prototype.enumRepeated = $util.emptyArray;
9494

9595
/**
9696
* Message int64Map.
97-
* @type {Object.<string,number|Long>|undefined}
97+
* @type {Object.<string,number|Long>}
9898
*/
9999
Message.prototype.int64Map = $util.emptyObject;
100100

tests/data/mapbox/vector_tile.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $root.vector_tile = (function() {
4343

4444
/**
4545
* Tile layers.
46-
* @type {Array.<vector_tile.Tile.Layer$Properties>|undefined}
46+
* @type {Array.<vector_tile.Tile.Layer$Properties>}
4747
*/
4848
Tile.prototype.layers = $util.emptyArray;
4949

@@ -259,43 +259,43 @@ $root.vector_tile = (function() {
259259

260260
/**
261261
* Value stringValue.
262-
* @type {string|undefined}
262+
* @type {string}
263263
*/
264264
Value.prototype.stringValue = "";
265265

266266
/**
267267
* Value floatValue.
268-
* @type {number|undefined}
268+
* @type {number}
269269
*/
270270
Value.prototype.floatValue = 0;
271271

272272
/**
273273
* Value doubleValue.
274-
* @type {number|undefined}
274+
* @type {number}
275275
*/
276276
Value.prototype.doubleValue = 0;
277277

278278
/**
279279
* Value intValue.
280-
* @type {number|Long|undefined}
280+
* @type {number|Long}
281281
*/
282282
Value.prototype.intValue = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
283283

284284
/**
285285
* Value uintValue.
286-
* @type {number|Long|undefined}
286+
* @type {number|Long}
287287
*/
288288
Value.prototype.uintValue = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
289289

290290
/**
291291
* Value sintValue.
292-
* @type {number|Long|undefined}
292+
* @type {number|Long}
293293
*/
294294
Value.prototype.sintValue = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
295295

296296
/**
297297
* Value boolValue.
298-
* @type {boolean|undefined}
298+
* @type {boolean}
299299
*/
300300
Value.prototype.boolValue = false;
301301

@@ -595,25 +595,25 @@ $root.vector_tile = (function() {
595595

596596
/**
597597
* Feature id.
598-
* @type {number|Long|undefined}
598+
* @type {number|Long}
599599
*/
600600
Feature.prototype.id = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
601601

602602
/**
603603
* Feature tags.
604-
* @type {Array.<number>|undefined}
604+
* @type {Array.<number>}
605605
*/
606606
Feature.prototype.tags = $util.emptyArray;
607607

608608
/**
609609
* Feature type.
610-
* @type {vector_tile.Tile.GeomType|undefined}
610+
* @type {vector_tile.Tile.GeomType}
611611
*/
612612
Feature.prototype.type = 0;
613613

614614
/**
615615
* Feature geometry.
616-
* @type {Array.<number>|undefined}
616+
* @type {Array.<number>}
617617
*/
618618
Feature.prototype.geometry = $util.emptyArray;
619619

@@ -932,25 +932,25 @@ $root.vector_tile = (function() {
932932

933933
/**
934934
* Layer features.
935-
* @type {Array.<vector_tile.Tile.Feature$Properties>|undefined}
935+
* @type {Array.<vector_tile.Tile.Feature$Properties>}
936936
*/
937937
Layer.prototype.features = $util.emptyArray;
938938

939939
/**
940940
* Layer keys.
941-
* @type {Array.<string>|undefined}
941+
* @type {Array.<string>}
942942
*/
943943
Layer.prototype.keys = $util.emptyArray;
944944

945945
/**
946946
* Layer values.
947-
* @type {Array.<vector_tile.Tile.Value$Properties>|undefined}
947+
* @type {Array.<vector_tile.Tile.Value$Properties>}
948948
*/
949949
Layer.prototype.values = $util.emptyArray;
950950

951951
/**
952952
* Layer extent.
953-
* @type {number|undefined}
953+
* @type {number}
954954
*/
955955
Layer.prototype.extent = 4096;
956956

tests/data/package.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,109 +57,109 @@ $root.Package = (function() {
5757

5858
/**
5959
* Package name.
60-
* @type {string|undefined}
60+
* @type {string}
6161
*/
6262
Package.prototype.name = "";
6363

6464
/**
6565
* Package version.
66-
* @type {string|undefined}
66+
* @type {string}
6767
*/
6868
Package.prototype.version = "";
6969

7070
/**
7171
* Package versionScheme.
72-
* @type {string|undefined}
72+
* @type {string}
7373
*/
7474
Package.prototype.versionScheme = "";
7575

7676
/**
7777
* Package description.
78-
* @type {string|undefined}
78+
* @type {string}
7979
*/
8080
Package.prototype.description = "";
8181

8282
/**
8383
* Package author.
84-
* @type {string|undefined}
84+
* @type {string}
8585
*/
8686
Package.prototype.author = "";
8787

8888
/**
8989
* Package license.
90-
* @type {string|undefined}
90+
* @type {string}
9191
*/
9292
Package.prototype.license = "";
9393

9494
/**
9595
* Package repository.
96-
* @type {Package.Repository$Properties|undefined}
96+
* @type {(Package.Repository$Properties|null)}
9797
*/
9898
Package.prototype.repository = null;
9999

100100
/**
101101
* Package bugs.
102-
* @type {string|undefined}
102+
* @type {string}
103103
*/
104104
Package.prototype.bugs = "";
105105

106106
/**
107107
* Package homepage.
108-
* @type {string|undefined}
108+
* @type {string}
109109
*/
110110
Package.prototype.homepage = "";
111111

112112
/**
113113
* Package keywords.
114-
* @type {Array.<string>|undefined}
114+
* @type {Array.<string>}
115115
*/
116116
Package.prototype.keywords = $util.emptyArray;
117117

118118
/**
119119
* Package main.
120-
* @type {string|undefined}
120+
* @type {string}
121121
*/
122122
Package.prototype.main = "";
123123

124124
/**
125125
* Package bin.
126-
* @type {Object.<string,string>|undefined}
126+
* @type {Object.<string,string>}
127127
*/
128128
Package.prototype.bin = $util.emptyObject;
129129

130130
/**
131131
* Package scripts.
132-
* @type {Object.<string,string>|undefined}
132+
* @type {Object.<string,string>}
133133
*/
134134
Package.prototype.scripts = $util.emptyObject;
135135

136136
/**
137137
* Package dependencies.
138-
* @type {Object.<string,string>|undefined}
138+
* @type {Object.<string,string>}
139139
*/
140140
Package.prototype.dependencies = $util.emptyObject;
141141

142142
/**
143143
* Package optionalDependencies.
144-
* @type {Object.<string,string>|undefined}
144+
* @type {Object.<string,string>}
145145
*/
146146
Package.prototype.optionalDependencies = $util.emptyObject;
147147

148148
/**
149149
* Package devDependencies.
150-
* @type {Object.<string,string>|undefined}
150+
* @type {Object.<string,string>}
151151
*/
152152
Package.prototype.devDependencies = $util.emptyObject;
153153

154154
/**
155155
* Package types.
156-
* @type {string|undefined}
156+
* @type {string}
157157
*/
158158
Package.prototype.types = "";
159159

160160
/**
161161
* Package cliDependencies.
162-
* @type {Array.<string>|undefined}
162+
* @type {Array.<string>}
163163
*/
164164
Package.prototype.cliDependencies = $util.emptyArray;
165165

@@ -688,13 +688,13 @@ $root.Package = (function() {
688688

689689
/**
690690
* Repository type.
691-
* @type {string|undefined}
691+
* @type {string}
692692
*/
693693
Repository.prototype.type = "";
694694

695695
/**
696696
* Repository url.
697-
* @type {string|undefined}
697+
* @type {string}
698698
*/
699699
Repository.prototype.url = "";
700700

tests/data/rpc-es6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const MyRequest = $root.MyRequest = (() => {
8989

9090
/**
9191
* MyRequest path.
92-
* @type {string|undefined}
92+
* @type {string}
9393
*/
9494
MyRequest.prototype.path = "";
9595

@@ -263,7 +263,7 @@ export const MyResponse = $root.MyResponse = (() => {
263263

264264
/**
265265
* MyResponse status.
266-
* @type {number|undefined}
266+
* @type {number}
267267
*/
268268
MyResponse.prototype.status = 0;
269269

0 commit comments

Comments
 (0)