Skip to content

Commit 1cc8a24

Browse files
committed
CLI: Made sure that static target's replacement regexes don't match fields
1 parent cda5c54 commit 1cc8a24

File tree

13 files changed

+229
-135
lines changed

13 files changed

+229
-135
lines changed

cli/targets/static.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function static_target(root, options, callback) {
4141
buildNamespace(null, root);
4242
push("");
4343
if (config.comments)
44-
push("// Resolve lazy type names to actual types");
44+
push("// Resolve lazy type references to actual types");
4545
push("$util.lazyResolve($root, $lazyTypes);");
4646
return callback(null, out.join("\n"));
4747
} catch (err) {
@@ -172,10 +172,10 @@ function buildFunction(type, functionName, gen, scope) {
172172
var code = gen.str(functionName)
173173
.replace(/\(this.ctor\)/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
174174
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
175-
.replace(/\bWriter\b/g, "$Writer") // use common aliases instead of binding through an iife
176-
.replace(/\bReader\b/g, "$Reader")
177-
.replace(/\butil\b/g, "$util")
178-
.replace(/\btypes\b/g, "$types");
175+
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
176+
.replace(/\b(?!\.)Reader\b/g, "$Reader")
177+
.replace(/\b(?!\.)util\.\b/g, "$util.")
178+
.replace(/\b(?!\.)types\[\b/g, "$types[");
179179

180180
if (config.beautify)
181181
code = beautify(code);
@@ -331,15 +331,21 @@ function buildType(ref, type) {
331331
type.fieldsArray.forEach(function(field, index) {
332332
if (field.resolve().resolvedType) { // including enums!
333333
hasTypes = true;
334-
types.push(index + ":"+JSON.stringify(field.resolvedType.fullName.substring(1)));
334+
types.push(index + ": "+JSON.stringify(field.resolvedType.fullName.substring(1)));
335335
}
336336
});
337337

338338
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
339339
push("");
340340
if (config.comments)
341-
push("// Lazily resolved referenced types");
342-
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
341+
push("// Lazily resolved type references");
342+
push("var $types = {");
343+
++indent;
344+
types.forEach(function(line, i) {
345+
push(line + (i === types.length - 1 ? "" : ","));
346+
});
347+
--indent;
348+
push("}; $lazyTypes.push($types);");
343349
}
344350

345351
if (config.create) {

cli/wrappers/amd.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ define(["protobuf"], function($protobuf) {
33

44
%OUTPUT%
55

6-
$protobuf.roots[%ROOT%] = $root;
7-
8-
return $root;
6+
return $protobuf.roots[%ROOT%] = $root;
97
});

cli/wrappers/commonjs.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ var $protobuf = require(%DEPENDENCY%);
44

55
%OUTPUT%
66

7-
$protobuf.roots[%ROOT%] = $root;
8-
9-
module.exports = $root;
7+
module.exports = $protobuf.roots[%ROOT%] = $root;

cli/wrappers/default.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@
1111

1212
%OUTPUT%
1313

14-
$protobuf.roots[%ROOT%] = $root;
15-
16-
return $root;
14+
return $protobuf.roots[%ROOT%] = $root;
1715
});

tests/data/ambiguous-names.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ $root.B = (function() {
210210
*/
211211
B.prototype.A = null;
212212

213-
// Lazily resolved referenced types
214-
var $types = {0:"A"}; $lazyTypes.push($types);
213+
// Lazily resolved type references
214+
var $types = {
215+
0: "A"
216+
}; $lazyTypes.push($types);
215217

216218
/**
217219
* Creates a new B instance using the specified properties.
@@ -366,9 +368,7 @@ $root.B = (function() {
366368
return B;
367369
})();
368370

369-
// Resolve lazy type names to actual types
371+
// Resolve lazy type references to actual types
370372
$util.lazyResolve($root, $lazyTypes);
371373

372-
$protobuf.roots["test_ambiguous-names"] = $root;
373-
374-
module.exports = $root;
374+
module.exports = $protobuf.roots["test_ambiguous-names"] = $root;

tests/data/comments.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,7 @@ $root.Test3 = (function() {
401401
return values;
402402
})();
403403

404-
// Resolve lazy type names to actual types
404+
// Resolve lazy type references to actual types
405405
$util.lazyResolve($root, $lazyTypes);
406406

407-
$protobuf.roots["test_comments"] = $root;
408-
409-
module.exports = $root;
407+
module.exports = $protobuf.roots["test_comments"] = $root;

tests/data/convert.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ $root.Message = (function() {
8484
*/
8585
Message.prototype.int64Map = $util.emptyObject;
8686

87-
// Lazily resolved referenced types
88-
var $types = {6:"Message.SomeEnum",7:"Message.SomeEnum"}; $lazyTypes.push($types);
87+
// Lazily resolved type references
88+
var $types = {
89+
6: "Message.SomeEnum",
90+
7: "Message.SomeEnum"
91+
}; $lazyTypes.push($types);
8992

9093
/**
9194
* Creates a new Message instance using the specified properties.
@@ -637,9 +640,7 @@ $root.Message = (function() {
637640
return Message;
638641
})();
639642

640-
// Resolve lazy type names to actual types
643+
// Resolve lazy type references to actual types
641644
$util.lazyResolve($root, $lazyTypes);
642645

643-
$protobuf.roots["test_convert"] = $root;
644-
645-
module.exports = $root;
646+
module.exports = $protobuf.roots["test_convert"] = $root;

tests/data/mapbox/vector_tile.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ $root.vector_tile = (function() {
4545
*/
4646
Tile.prototype.layers = $util.emptyArray;
4747

48-
// Lazily resolved referenced types
49-
var $types = {0:"vector_tile.Tile.Layer"}; $lazyTypes.push($types);
48+
// Lazily resolved type references
49+
var $types = {
50+
0: "vector_tile.Tile.Layer"
51+
}; $lazyTypes.push($types);
5052

5153
/**
5254
* Creates a new Tile instance using the specified properties.
@@ -685,8 +687,10 @@ $root.vector_tile = (function() {
685687
*/
686688
Feature.prototype.geometry = $util.emptyArray;
687689

688-
// Lazily resolved referenced types
689-
var $types = {2:"vector_tile.Tile.GeomType"}; $lazyTypes.push($types);
690+
// Lazily resolved type references
691+
var $types = {
692+
2: "vector_tile.Tile.GeomType"
693+
}; $lazyTypes.push($types);
690694

691695
/**
692696
* Creates a new Feature instance using the specified properties.
@@ -1058,8 +1062,11 @@ $root.vector_tile = (function() {
10581062
*/
10591063
Layer.prototype.extent = 4096;
10601064

1061-
// Lazily resolved referenced types
1062-
var $types = {2:"vector_tile.Tile.Feature",4:"vector_tile.Tile.Value"}; $lazyTypes.push($types);
1065+
// Lazily resolved type references
1066+
var $types = {
1067+
2: "vector_tile.Tile.Feature",
1068+
4: "vector_tile.Tile.Value"
1069+
}; $lazyTypes.push($types);
10631070

10641071
/**
10651072
* Creates a new Layer instance using the specified properties.
@@ -1373,9 +1380,7 @@ $root.vector_tile = (function() {
13731380
return vector_tile;
13741381
})();
13751382

1376-
// Resolve lazy type names to actual types
1383+
// Resolve lazy type references to actual types
13771384
$util.lazyResolve($root, $lazyTypes);
13781385

1379-
$protobuf.roots["test_vector_tile"] = $root;
1380-
1381-
module.exports = $root;
1386+
module.exports = $protobuf.roots["test_vector_tile"] = $root;

tests/data/package.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ $root.Package = (function() {
132132
*/
133133
Package.prototype.cliDependencies = $util.emptyArray;
134134

135-
// Lazily resolved referenced types
136-
var $types = {5:"Package.Repository"}; $lazyTypes.push($types);
135+
// Lazily resolved type references
136+
var $types = {
137+
5: "Package.Repository"
138+
}; $lazyTypes.push($types);
137139

138140
/**
139141
* Creates a new Package instance using the specified properties.
@@ -211,8 +213,8 @@ $root.Package = (function() {
211213
writer.uint32(122).fork().uint32(10).string(keys[i]).uint32(18).string(message.devDependencies[keys[i]]).ldelim();
212214
}
213215
}
214-
if (message.$types !== undefined && message.$types !== "") {
215-
writer.uint32(138).string(message.$types);
216+
if (message.types !== undefined && message.types !== "") {
217+
writer.uint32(138).string(message.types);
216218
}
217219
if (message.cliDependencies) {
218220
for (var i = 0; i < message.cliDependencies.length; ++i) {
@@ -340,7 +342,7 @@ $root.Package = (function() {
340342
break;
341343

342344
case 17:
343-
message.$types = reader.string();
345+
message.types = reader.string();
344346
break;
345347

346348
case 18:
@@ -485,9 +487,9 @@ $root.Package = (function() {
485487
}
486488
}
487489
}
488-
if (message.$types !== undefined) {
489-
if (!$util.isString(message.$types)) {
490-
return "$types: string expected";
490+
if (message.types !== undefined) {
491+
if (!$util.isString(message.types)) {
492+
return "types: string expected";
491493
}
492494
}
493495
if (message.cliDependencies !== undefined) {
@@ -573,8 +575,8 @@ $root.Package = (function() {
573575
message.devDependencies[keys[i]] = String(object.devDependencies[keys[i]]);
574576
}
575577
}
576-
if (object.$types !== undefined && object.$types !== null) {
577-
message.$types = String(object.$types);
578+
if (object.types !== undefined && object.types !== null) {
579+
message.types = String(object.types);
578580
}
579581
if (object.cliDependencies) {
580582
message.cliDependencies = [];
@@ -625,7 +627,7 @@ $root.Package = (function() {
625627
object.bugs = "";
626628
object.homepage = "";
627629
object.main = "";
628-
object.$types = "";
630+
object.types = "";
629631
}
630632
for (var keys = Object.keys(message), i = 0; i < keys.length; ++i) {
631633
switch (keys[i]) {
@@ -737,9 +739,9 @@ $root.Package = (function() {
737739
}
738740
break;
739741

740-
case "$types":
741-
if (message.$types !== undefined && message.$types !== null) {
742-
object.$types = message.$types;
742+
case "types":
743+
if (message.types !== undefined && message.types !== null) {
744+
object.types = message.types;
743745
}
744746
break;
745747

@@ -978,9 +980,7 @@ $root.Package = (function() {
978980
return Package;
979981
})();
980982

981-
// Resolve lazy type names to actual types
983+
// Resolve lazy type references to actual types
982984
$util.lazyResolve($root, $lazyTypes);
983985

984-
$protobuf.roots["test_package"] = $root;
985-
986-
module.exports = $root;
986+
module.exports = $protobuf.roots["test_package"] = $root;

tests/data/rpc.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ $root.MyResponse = (function() {
454454
return MyResponse;
455455
})();
456456

457-
// Resolve lazy type names to actual types
457+
// Resolve lazy type references to actual types
458458
$util.lazyResolve($root, $lazyTypes);
459459

460-
$protobuf.roots["test_rpc"] = $root;
461-
462-
module.exports = $root;
460+
module.exports = $protobuf.roots["test_rpc"] = $root;

0 commit comments

Comments
 (0)