Skip to content

Commit e5ca5c8

Browse files
More minor cleanup, lint fixes, and coverage improvements
1 parent a84409b commit e5ca5c8

13 files changed

+76
-68
lines changed

index.d.ts

-3
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ export class Enum extends ReflectionObject {
182182
/** Resolved values features, if any */
183183
public _valuesFeatures?: { [k: string]: { [k: string]: any } };
184184

185-
/** Unresolved values features, if any */
186-
public _valuesProtoFeatures?: { [k: string]: { [k: string]: any } };
187-
188185
/** Reserved ranges, if any. */
189186
public reserved: (number[]|string)[];
190187

src/enum.js

+3-24
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
6262
*/
6363
this._valuesFeatures = {};
6464

65-
/**
66-
* Unresolved values features, if any
67-
* @type {Object<string, Object<string, *>>|undefined}
68-
*/
69-
this._valuesProtoFeatures = {};
70-
7165
/**
7266
* Reserved ranges, if any.
7367
* @type {Array.<number[]|string>}
@@ -88,12 +82,12 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
8882
* @override
8983
*/
9084
Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
91-
var edition = this._edition || edition;
85+
edition = this._edition || edition;
9286
ReflectionObject.prototype._resolveFeatures.call(this, edition);
9387

94-
Object.keys(this._valuesProtoFeatures).forEach(key => {
88+
Object.keys(this.values).forEach(key => {
9589
var parentFeaturesCopy = Object.assign({}, this._features);
96-
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._valuesProtoFeatures[key] || {});
90+
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this.valuesOptions && this.valuesOptions[key] && this.valuesOptions[key].features);
9791
});
9892

9993
return this;
@@ -179,21 +173,6 @@ Enum.prototype.add = function add(name, id, comment, options) {
179173
if (this.valuesOptions === undefined)
180174
this.valuesOptions = {};
181175
this.valuesOptions[name] = options || null;
182-
183-
for (var key of Object.keys(this.valuesOptions)) {
184-
var features = Array.isArray(this.valuesOptions[key]) ? this.valuesOptions[key].find(x => {return Object.prototype.hasOwnProperty.call(x, "features");}) : this.valuesOptions[key] === "features";
185-
if (features) {
186-
this._valuesProtoFeatures[key] = features.features;
187-
} else {
188-
this._valuesProtoFeatures[key] = {};
189-
}
190-
}
191-
}
192-
193-
for (var enumValue of Object.keys(this.values)) {
194-
if (!this._valuesProtoFeatures[enumValue]) {
195-
this._valuesProtoFeatures[enumValue] = {};
196-
}
197176
}
198177

199178
this.comments[name] = comment || null;

src/field.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Field.prototype.resolve = function resolve() {
366366
*/
367367
Field.prototype._inferLegacyProtoFeatures = function _inferLegacyProtoFeatures(edition) {
368368
if (edition !== "proto2" && edition !== "proto3") {
369-
return;
369+
return {};
370370
}
371371

372372
var features = {};

src/index-light.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protobuf.types = require("./types");
9898
protobuf.util = require("./util");
9999

100100
// Set up possibly cyclic reflection dependencies
101-
protobuf.ReflectionObject._configure(protobuf.Root, protobuf.Namespace);
101+
protobuf.ReflectionObject._configure(protobuf.Root);
102102
protobuf.Namespace._configure(protobuf.Type, protobuf.Service, protobuf.Enum);
103103
protobuf.Root._configure(protobuf.Type);
104104
protobuf.Field._configure(protobuf.Type);

src/namespace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ Namespace.prototype.resolveAll = function resolveAll() {
324324
* @override
325325
*/
326326
Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
327-
var edition = this._edition || edition;
327+
edition = this._edition || edition;
328328

329329
ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);
330330
this.nestedArray.forEach(nested => {

src/object.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ReflectionObject.className = "ReflectionObject";
66
const OneOf = require("./oneof");
77
var util = require("./util");
88

9-
var Root, Namespace; // cyclic
9+
var Root; // cyclic
1010

1111
/* eslint-disable no-warning-comments */
1212
// TODO: Replace with embedded proto.
@@ -196,6 +196,7 @@ ReflectionObject.prototype._resolveFeaturesRecursive = function _resolveFeatures
196196
ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition) {
197197
var defaults = {};
198198

199+
/* istanbul ignore if */
199200
if (!edition) {
200201
throw new Error("Unknown edition for " + this.fullName);
201202
}
@@ -205,6 +206,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
205206

206207
if (this._edition) {
207208
// For a namespace marked with a specific edition, reset defaults.
209+
/* istanbul ignore else */
208210
if (edition === "proto2") {
209211
defaults = Object.assign({}, proto2Defaults);
210212
} else if (edition === "proto3") {
@@ -220,6 +222,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
220222

221223
// fields in Oneofs aren't actually children of them, so we have to
222224
// special-case it
225+
/* istanbul ignore else */
223226
if (this.partOf instanceof OneOf) {
224227
var lexicalParentFeaturesCopy = Object.assign({}, this.partOf._features);
225228
this._features = Object.assign(lexicalParentFeaturesCopy, protoFeatures || {});
@@ -229,7 +232,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
229232
var parentFeaturesCopy = Object.assign({}, this.parent._features);
230233
this._features = Object.assign(parentFeaturesCopy, protoFeatures || {});
231234
} else {
232-
this._features = Object.assign({}, protoFeatures);
235+
throw new Error("Unable to find a parent for " + this.fullName);
233236
}
234237
if (this.extensionField) {
235238
// Sister fields should have the same features as their extensions.
@@ -268,13 +271,7 @@ ReflectionObject.prototype.getOption = function getOption(name) {
268271
ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {
269272
if (!this.options)
270273
this.options = {};
271-
if (name === "features") {
272-
if (ifNotSet) {
273-
this.options.features = Object.assign(Object.assign({}, value), this.options.features || {});
274-
} else {
275-
this.options.features = Object.assign(this.options.features || {}, value);
276-
}
277-
} else if (/^features\./.test(name)) {
274+
if (/^features\./.test(name)) {
278275
util.setProperty(this.options, name, value, ifNotSet);
279276
} else if (!ifNotSet || this.options[name] === undefined) {
280277
if (this.getOption(name) !== value) this.resolved = false;
@@ -295,7 +292,6 @@ ReflectionObject.prototype.setParsedOption = function setParsedOption(name, valu
295292
if (!this.parsedOptions) {
296293
this.parsedOptions = [];
297294
}
298-
var isFeature = /^features$/.test(name);
299295
var parsedOptions = this.parsedOptions;
300296
if (propName) {
301297
// If setting a sub property of an option then try to merge it
@@ -360,10 +356,9 @@ ReflectionObject.prototype._editionToJSON = function _editionToJSON() {
360356
return undefined;
361357
}
362358
return this._edition;
363-
}
359+
};
364360

365361
// Sets up cyclic dependencies (called in index-light)
366-
ReflectionObject._configure = function(Root_, Namespace_) {
362+
ReflectionObject._configure = function(Root_) {
367363
Root = Root_;
368-
Namespace = Namespace_;
369364
};

src/parse.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function parse(source, root, options) {
9696
obj._edition = edition;
9797
Object.keys(topLevelOptions).forEach(opt => {
9898
if (obj.getOption(opt) !== undefined) return;
99-
obj.setOption(opt, topLevelOptions[opt]);
99+
obj.setOption(opt, topLevelOptions[opt], true);
100100
});
101101
});
102102
}
@@ -628,18 +628,13 @@ function parse(source, root, options) {
628628
dummy = {
629629
options: undefined
630630
};
631+
dummy.getOption = function(name) {
632+
return this.options[name];
633+
};
631634
dummy.setOption = function(name, value) {
632-
if (this.options === undefined)
633-
this.options = {};
634-
635-
this.options[name] = value;
635+
ReflectionObject.prototype.setOption.call(dummy, name, value);
636636
};
637-
dummy.setParsedOption = function(name, value, propName) {
638-
// In order to not change existing behavior, only calling
639-
// this for features
640-
if (/^features$/.test(name)) {
641-
return ReflectionObject.prototype.setParsedOption.call(dummy, name, value, propName);
642-
}
637+
dummy.setParsedOption = function() {
643638
return undefined;
644639
};
645640
ifBlock(dummy, function parseEnumValue_block(token) {

src/service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ Service.prototype.resolveAll = function resolveAll() {
121121
* @override
122122
*/
123123
Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
124-
var edition = this._edition || edition;
124+
edition = this._edition || edition;
125125

126126
Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
127127
this.methodsArray.forEach(method => {
128-
method._resolveFeaturesRecursive(edition);
128+
method._resolveFeaturesRecursive(edition);
129129
});
130130
return this;
131131
};

src/type.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function clearCache(type) {
234234
* @param {IType} json Message type descriptor
235235
* @returns {Type} Created message type
236236
*/
237-
Type.fromJSON = function fromJSON(name, json, nested) {
237+
Type.fromJSON = function fromJSON(name, json) {
238238
var type = new Type(name, json.options);
239239
type.extensions = json.extensions;
240240
type.reserved = json.reserved;
@@ -317,14 +317,14 @@ Type.prototype.resolveAll = function resolveAll() {
317317
* @override
318318
*/
319319
Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
320-
var edition = this._edition || edition;
320+
edition = this._edition || edition;
321321

322322
Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
323323
this.oneofsArray.forEach(oneof => {
324-
oneof._resolveFeatures(edition);
324+
oneof._resolveFeatures(edition);
325325
});
326326
this.fieldsArray.forEach(field => {
327-
field._resolveFeatures(edition);
327+
field._resolveFeatures(edition);
328328
});
329329
return this;
330330
};

tests/api_enum.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tape.test("reflected enums", function(test) {
66

77
var enm = new protobuf.Enum("Test", {
88
a: 1,
9-
b: 2
9+
b: 2,
1010
});
1111

1212
var enm_allow_alias = new protobuf.Enum( 'AliasTest',
@@ -86,6 +86,17 @@ tape.test("reflected enums", function(test) {
8686
'(test_option)': 'test_value'
8787
}
8888
});
89+
enm.remove("e");
90+
test.same( enm.valuesOptions, {}, "should clean up value options");
91+
92+
enm.reserved = [[100,200], "BAD_NAME"];
93+
test.throws(function() {
94+
enm.add("d", 101);
95+
}, Error, "should throw if id is a reserved number");
96+
97+
test.throws(function() {
98+
enm.add("BAD_NAME", 5);
99+
}, Error, "should throw if id is a reserved name");
89100

90101
test.end();
91102
});

tests/comp_packed-repeated.js

+14
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,17 @@ tape.test("packed repeated values encode", function(top) {
9595

9696
top.end()
9797
});
98+
99+
tape.test("packed unpackable fields", function(test) {
100+
var root = protobuf.parse(`message Test {
101+
repeated Test a = 1 [packed = true];
102+
repeated Test b = 2 [packed = true, foo = true];
103+
}`).root.resolveAll();
104+
var Test = root.lookup("Test");
105+
106+
test.equal(Test.fields.a.options, undefined, "should have no options")
107+
test.equal(Test.fields.b.options.packed, undefined, "should have no packed option")
108+
test.equal(Test.fields.b.options.foo, true, "should retain other option")
109+
110+
test.end();
111+
});

tests/data/uncommon.proto

+11
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ enum Test4_1{
7171
OPTION = 1;
7272
}
7373

74+
enum ReservedEnum{
75+
RESERVED_UNKNOWN = 0;
76+
77+
reserved 1 to 5;
78+
reserved "abc", "INVALID";
79+
reserved 10;
80+
reserved 11 to 20 {
81+
option foo = true;
82+
}
83+
}
84+
7485
service Test5;
7586

7687
service Test6 { option (custom).bar = "";

tests/feature_resolution_editions.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,16 @@ tape.test("unresolved feature options", function(test) {
115115
tape.test("aggregate feature parsing", function(test) {
116116
var rootEditionsOverriden = protobuf.parse(`edition = "2023";
117117
option features = {
118+
utf8_validation: VERIFY
118119
json_format: LEGACY_BEST_EFFORT
119120
field_presence: IMPLICIT
120121
};
121122
122123
message Message {
123-
option features = { utf8_validation: NONE };
124+
option features = {
125+
utf8_validation: NONE
126+
enum_type: OPEN
127+
};
124128
string string_val = 1;
125129
string string_repeated = 2 [features = { enum_type: CLOSED field_presence: LEGACY_REQUIRED }];
126130
}`).root.resolveAll();
@@ -557,15 +561,17 @@ tape.test("feature resolution inferred proto2 presence", function(test) {
557561
message Message {
558562
optional int32 default = 1;
559563
required int32 required = 2;
564+
repeated int32 repeated = 3;
560565
}`).root.resolveAll();
561566

562567
var msg = root.lookup("Message");
563-
test.ok(msg.fields.default.optional)
564-
test.notOk(msg.fields.default.required)
565-
test.equal(msg.fields.default._features.field_presence, "EXPLICIT")
566-
test.notOk(msg.fields.required.optional)
567-
test.ok(msg.fields.required.required)
568-
test.equal(msg.fields.required._features.field_presence, "LEGACY_REQUIRED")
568+
test.ok(msg.fields.default.optional);
569+
test.notOk(msg.fields.default.required);
570+
test.ok(msg.fields.default.hasPresence);
571+
test.notOk(msg.fields.required.optional);
572+
test.ok(msg.fields.required.required);
573+
test.equal(msg.fields.required._features.field_presence, "LEGACY_REQUIRED");
574+
test.notOk(msg.fields.repeated.hasPresence, "repeated fields never have presence");
569575

570576
test.end();
571577
});

0 commit comments

Comments
 (0)