Skip to content

Commit c8508a6

Browse files
authored
Update copy of reflect.js with the one from Closure Compiler (bazelbuild#653)
1 parent ecad10f commit c8508a6

1 file changed

Lines changed: 12 additions & 68 deletions

File tree

closure/library/reflect.js

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
/**
22
* @license
33
* Copyright The Closure Library Authors.
4+
* Copyright The Closure Compiler Authors.
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

78
/**
8-
* @fileoverview Useful compiler idioms.
9+
* @fileoverview Useful compiler idioms related to renaming.
910
*/
1011

1112
goog.provide('goog.reflect');
1213

1314

1415
/**
1516
* Syntax for object literal casts.
16-
* @see http://go/jscompiler-renaming
1717
* @see https://goo.gl/CRs09P
1818
*
1919
* Use this if you have an object literal whose keys need to have the same names
2020
* as the properties of some class even after they are renamed by the compiler.
2121
*
2222
* @param {!Function} type Type to cast to.
23-
* @param {Object} object Object literal to cast.
24-
* @return {Object} The object literal.
23+
* @param {!Object} object Object literal to cast.
24+
* @return {!Object} The object literal.
2525
*/
2626
goog.reflect.object = function(type, object) {
2727
'use strict';
2828
return object;
2929
};
3030

31+
3132
/**
3233
* Syntax for renaming property strings.
33-
* @see http://go/jscompiler-renaming
3434
* @see https://goo.gl/CRs09P
3535
*
3636
* Use this if you have an need to access a property as a string, but want
@@ -49,13 +49,15 @@ goog.reflect.objectProperty = function(prop, object) {
4949
return prop;
5050
};
5151

52+
5253
/**
5354
* To assert to the compiler that an operation is needed when it would
5455
* otherwise be stripped. For example:
55-
* <code>
56-
* // Force a layout
57-
* goog.reflect.sinkValue(dialog.offsetHeight);
58-
* </code>
56+
*
57+
* ```javascript
58+
* // Force a layout
59+
* goog.reflect.sinkValue(dialog.offsetHeight);
60+
* ```
5961
* @param {T} x
6062
* @return {T}
6163
* @template T
@@ -71,62 +73,4 @@ goog.reflect.sinkValue = function(x) {
7173
* The compiler should optimize this function away iff no one ever uses
7274
* goog.reflect.sinkValue.
7375
*/
74-
goog.reflect.sinkValue[' '] = function() {};
75-
76-
77-
/**
78-
* Check if a property can be accessed without throwing an exception.
79-
* @param {Object} obj The owner of the property.
80-
* @param {string} prop The property name.
81-
* @return {boolean} Whether the property is accessible. Will also return true
82-
* if obj is null.
83-
*/
84-
goog.reflect.canAccessProperty = function(obj, prop) {
85-
'use strict';
86-
try {
87-
goog.reflect.sinkValue(obj[prop]);
88-
return true;
89-
} catch (e) {
90-
}
91-
return false;
92-
};
93-
94-
95-
/**
96-
* Retrieves a value from a cache given a key. The compiler provides special
97-
* consideration for this call such that it is generally considered side-effect
98-
* free. However, if the `opt_keyFn` or `valueFn` have side-effects
99-
* then the entire call is considered to have side-effects.
100-
*
101-
* Conventionally storing the value on the cache would be considered a
102-
* side-effect and preclude unused calls from being pruned, ie. even if
103-
* the value was never used, it would still always be stored in the cache.
104-
*
105-
* Providing a side-effect free `valueFn` and `opt_keyFn`
106-
* allows unused calls to `goog.reflect.cache` to be pruned.
107-
*
108-
* @param {!Object<K, V>} cacheObj The object that contains the cached values.
109-
* @param {?} key The key to lookup in the cache. If it is not string or number
110-
* then a `opt_keyFn` should be provided. The key is also used as the
111-
* parameter to the `valueFn`.
112-
* @param {function(?):V} valueFn The value provider to use to calculate the
113-
* value to store in the cache. This function should be side-effect free
114-
* to take advantage of the optimization.
115-
* @param {function(?):K=} opt_keyFn The key provider to determine the cache
116-
* map key. This should be used if the given key is not a string or number.
117-
* If not provided then the given key is used. This function should be
118-
* side-effect free to take advantage of the optimization.
119-
* @return {V} The cached or calculated value.
120-
* @template K
121-
* @template V
122-
*/
123-
goog.reflect.cache = function(cacheObj, key, valueFn, opt_keyFn) {
124-
'use strict';
125-
const storedKey = opt_keyFn ? opt_keyFn(key) : key;
126-
127-
if (Object.prototype.hasOwnProperty.call(cacheObj, storedKey)) {
128-
return cacheObj[storedKey];
129-
}
130-
131-
return (cacheObj[storedKey] = valueFn(key));
132-
};
76+
goog.reflect.sinkValue[' '] = function() {};

0 commit comments

Comments
 (0)