Skip to content

Commit de224a0

Browse files
authored
Revert "fix: ensure that types are always resolved (#2068)"
This reverts commit 4b51cb2.
1 parent 4b51cb2 commit de224a0

File tree

6 files changed

+28
-76
lines changed

6 files changed

+28
-76
lines changed

index.d.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,6 @@ export abstract class NamespaceBase extends ReflectionObject {
753753
/** Whether or not objects contained in this namespace need feature resolution. */
754754
protected _needsRecursiveFeatureResolution: boolean;
755755

756-
/** Whether or not objects contained in this namespace need a resolve. */
757-
protected _needsRecursiveResolve: boolean;
758-
759756
/** Nested objects of this namespace as an array for iteration. */
760757
public readonly nestedArray: ReflectionObject[];
761758

@@ -903,6 +900,21 @@ export abstract class ReflectionObject {
903900
/** Unique name within its namespace. */
904901
public name: string;
905902

903+
/** The edition specified for this object. Only relevant for top-level objects. */
904+
public _edition: string;
905+
906+
/**
907+
* The default edition to use for this object if none is specified. For legacy reasons,
908+
* this is proto2 except in the JSON parsing case where it was proto3.
909+
*/
910+
public _defaultEdition: string;
911+
912+
/** Resolved Features. */
913+
public _features: object;
914+
915+
/** Whether or not features have been resolved. */
916+
public _featuresResolved: boolean;
917+
906918
/** Parent namespace. */
907919
public parent: (Namespace|null);
908920

src/namespace.js

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ function Namespace(name, options) {
124124
* @protected
125125
*/
126126
this._needsRecursiveFeatureResolution = true;
127-
128-
/**
129-
* Whether or not objects contained in this namespace need a resolve.
130-
* @type {boolean}
131-
* @protected
132-
*/
133-
this._needsRecursiveResolve = true;
134127
}
135128

136129
function clearCache(namespace) {
@@ -280,13 +273,11 @@ Namespace.prototype.add = function add(object) {
280273
}
281274

282275
this._needsRecursiveFeatureResolution = true;
283-
this._needsRecursiveResolve = true;
284276

285277
// Also clear parent caches, since they need to recurse down.
286278
var parent = this;
287279
while(parent = parent.parent) {
288280
parent._needsRecursiveFeatureResolution = true;
289-
parent._needsRecursiveResolve = true;
290281
}
291282

292283
object.onAdd(this);
@@ -350,16 +341,13 @@ Namespace.prototype.define = function define(path, json) {
350341
* @returns {Namespace} `this`
351342
*/
352343
Namespace.prototype.resolveAll = function resolveAll() {
353-
if (!this._needsRecursiveResolve) return this;
354-
355344
var nested = this.nestedArray, i = 0;
356345
this.resolve();
357346
while (i < nested.length)
358347
if (nested[i] instanceof Namespace)
359348
nested[i++].resolveAll();
360349
else
361350
nested[i++].resolve();
362-
this._needsRecursiveResolve = false;
363351
return this;
364352
};
365353

@@ -401,47 +389,29 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
401389
} else if (!path.length)
402390
return this;
403391

404-
var flatPath = path.join(".");
405-
406392
// Start at root if path is absolute
407393
if (path[0] === "")
408394
return this.root.lookup(path.slice(1), filterTypes);
409395

410-
// Early bailout for objects with matching absolute paths
411-
var found = this.root._fullyQualifiedObjects["." + flatPath];
412-
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
413-
return found;
414-
}
415-
416-
// Do a regular lookup at this namespace and below
417-
found = this._lookupImpl(path, flatPath);
396+
var found = this._lookupImpl(path);
418397
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
419398
return found;
420399
}
421400

422-
if (parentAlreadyChecked)
401+
// If there hasn't been a match, try again at the parent
402+
if (this.parent === null || parentAlreadyChecked)
423403
return null;
424-
425-
// If there hasn't been a match, walk up the tree and look more broadly
426-
var current = this;
427-
while (current.parent) {
428-
found = current.parent._lookupImpl(path, flatPath);
429-
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
430-
return found;
431-
}
432-
current = current.parent;
433-
}
434-
return null;
404+
return this.parent.lookup(path, filterTypes);
435405
};
436406

437407
/**
438408
* Internal helper for lookup that handles searching just at this namespace and below along with caching.
439409
* @param {string[]} path Path to look up
440-
* @param {string} flatPath Flattened version of the path to use as a cache key
441410
* @returns {ReflectionObject|null} Looked up object or `null` if none could be found
442411
* @private
443412
*/
444-
Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
413+
Namespace.prototype._lookupImpl = function lookup(path) {
414+
var flatPath = path.join(".");
445415
if(Object.prototype.hasOwnProperty.call(this._lookupCache, flatPath)) {
446416
return this._lookupCache[flatPath];
447417
}
@@ -452,15 +422,13 @@ Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
452422
if (found) {
453423
if (path.length === 1) {
454424
exact = found;
455-
} else if (found instanceof Namespace) {
456-
path = path.slice(1);
457-
exact = found._lookupImpl(path, path.join("."));
458-
}
425+
} else if (found instanceof Namespace && (found = found._lookupImpl(path.slice(1))))
426+
exact = found;
459427

460428
// Otherwise try each nested namespace
461429
} else {
462430
for (var i = 0; i < this.nestedArray.length; ++i)
463-
if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath)))
431+
if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path)))
464432
exact = found;
465433
}
466434

src/object.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,25 @@ function ReflectionObject(name, options) {
5151
/**
5252
* The edition specified for this object. Only relevant for top-level objects.
5353
* @type {string}
54-
* @private
5554
*/
5655
this._edition = null;
5756

5857
/**
5958
* The default edition to use for this object if none is specified. For legacy reasons,
6059
* this is proto2 except in the JSON parsing case where it was proto3.
6160
* @type {string}
62-
* @private
6361
*/
6462
this._defaultEdition = "proto2";
6563

6664
/**
6765
* Resolved Features.
6866
* @type {object}
69-
* @private
7067
*/
7168
this._features = {};
7269

7370
/**
7471
* Whether or not features have been resolved.
7572
* @type {boolean}
76-
* @private
7773
*/
7874
this._featuresResolved = false;
7975

src/root.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,8 @@ function Root(options) {
3636
*/
3737
this.files = [];
3838

39-
/**
40-
* Edition, defaults to proto2 if unspecified.
41-
* @type {string}
42-
* @private
43-
*/
39+
// Default to proto2 if unspecified.
4440
this._edition = "proto2";
45-
46-
/**
47-
* Global lookup cache of fully qualified names.
48-
* @type {Object.<string,ReflectionObject>}
49-
* @private
50-
*/
51-
this._fullyQualifiedObjects = {};
5241
}
5342

5443
/**
@@ -62,7 +51,7 @@ Root.fromJSON = function fromJSON(json, root) {
6251
root = new Root();
6352
if (json.options)
6453
root.setOptions(json.options);
65-
return root.addJSON(json.nested).resolveAll();
54+
return root.addJSON(json.nested)._resolveFeaturesRecursive();
6655
};
6756

6857
/**
@@ -111,7 +100,7 @@ Root.prototype.load = function load(filename, options, callback) {
111100
// Finishes loading by calling the callback (exactly once)
112101
function finish(err, root) {
113102
if (root) {
114-
root.resolveAll();
103+
root._resolveFeaturesRecursive();
115104
}
116105
/* istanbul ignore if */
117106
if (!callback) {
@@ -230,7 +219,7 @@ Root.prototype.load = function load(filename, options, callback) {
230219
if (resolved = self.resolvePath("", filename[i]))
231220
fetch(resolved);
232221
if (sync) {
233-
self.resolveAll();
222+
self._resolveFeaturesRecursive();
234223
return self;
235224
}
236225
if (!queued) {
@@ -279,8 +268,6 @@ Root.prototype.loadSync = function loadSync(filename, options) {
279268
* @override
280269
*/
281270
Root.prototype.resolveAll = function resolveAll() {
282-
if (!this._needsRecursiveResolve) return this;
283-
284271
if (this.deferred.length)
285272
throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
286273
return "'extend " + field.extend + "' in " + field.parent.fullName;
@@ -348,11 +335,6 @@ Root.prototype._handleAdd = function _handleAdd(object) {
348335
object.parent[object.name] = object; // expose namespace as property of its parent
349336
}
350337

351-
if (object instanceof Type || object instanceof Enum || object instanceof Field) {
352-
// Only store types and enums for quick lookup during resolve.
353-
this._fullyQualifiedObjects[object.fullName] = object;
354-
}
355-
356338
// The above also adds uppercased (and thus conflict-free) nested types, services and enums as
357339
// properties of namespaces just like static code does. This allows using a .d.ts generated for
358340
// a static module with reflection-based solutions where the condition is met.
@@ -393,8 +375,6 @@ Root.prototype._handleRemove = function _handleRemove(object) {
393375
delete object.parent[object.name]; // unexpose namespaces
394376

395377
}
396-
397-
delete this._fullyQualifiedObjects[object.fullName];
398378
};
399379

400380
// Sets up cyclic dependencies (called in index-light)

src/service.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ Service.prototype.get = function get(name) {
110110
* @override
111111
*/
112112
Service.prototype.resolveAll = function resolveAll() {
113-
if (!this._needsRecursiveResolve) return this;
114-
115113
Namespace.prototype.resolve.call(this);
116114
var methods = this.methodsArray;
117115
for (var i = 0; i < methods.length; ++i)

src/type.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
303303
* @override
304304
*/
305305
Type.prototype.resolveAll = function resolveAll() {
306-
if (!this._needsRecursiveResolve) return this;
307-
308306
Namespace.prototype.resolveAll.call(this);
309307
var oneofs = this.oneofsArray; i = 0;
310308
while (i < oneofs.length)

0 commit comments

Comments
 (0)