Skip to content

Commit 3da5e12

Browse files
gkdncopybara-github
authored andcommitted
Add some missing APIs base.js.
I refactored most other stuff to stop depending base.js but couple of things left that require more work; particularly: - goog.global: is used in different places incl. open-source so the refactoring of this is a larger task so better drop it later on. - goog.setTestOnly: I don't think there is a compiler level alternative and I didn't want to drop whole from open-source right now without replacemen so postponed any refactorings around it. - goog.getObjectByName: we still rely on it though we can probably drop it later on when fully migrate out of globals for System.getProperty. PiperOrigin-RevId: 718092702
1 parent 88dd3e6 commit 3da5e12

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

lib/base.js

+47-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ var COMPILED = false;
3636
var goog = goog || {};
3737

3838

39+
/**
40+
* Reference to the global object.
41+
* https://www.ecma-international.org/ecma-262/9.0/index.html#sec-global-object
42+
*
43+
* More info on this implementation here:
44+
* https://docs.google.com/document/d/1NAeW4Wk7I7FV0Y2tcUFvQdGMc89k2vdgSXInw8_nvCI/edit
45+
*
46+
* @const
47+
* @suppress {undefinedVars} self won't be referenced unless `this` is falsy.
48+
* @type {!Global}
49+
* @deprecated Use `globalThis` directly; this version will be deleted.
50+
*/
51+
goog.global = globalThis;
52+
53+
3954
/**
4055
* A hook for overriding the define values in uncompiled or compiled mode.
4156
*
@@ -203,6 +218,12 @@ goog.DEBUG = goog.define('goog.DEBUG', true);
203218
*/
204219
goog.LOCALE = goog.define('goog.LOCALE', 'en'); // default to en
205220

221+
/**
222+
* @define {boolean} Whether code that calls {@link goog.setTestOnly} should
223+
* be disallowed in the compilation unit.
224+
*/
225+
goog.DISALLOW_TEST_ONLY_CODE =
226+
goog.define('goog.DISALLOW_TEST_ONLY_CODE', COMPILED && !goog.DEBUG);
206227

207228
/**
208229
* Defines a global namespace in Closure.
@@ -260,7 +281,7 @@ goog.constructNamespace_ = function(name, object, overwriteImplicit) {
260281

261282
var namespace = name;
262283
while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) {
263-
if (goog.getObjectByName_(namespace)) {
284+
if (goog.getObjectByName(namespace)) {
264285
break;
265286
}
266287
goog.implicitNamespaces_[namespace] = true;
@@ -368,7 +389,7 @@ goog.module.getInternal_ = function(name) {
368389
if (name in goog.loadedModules_) {
369390
return goog.loadedModules_[name];
370391
} else if (!goog.implicitNamespaces_[name]) {
371-
var ns = goog.getObjectByName_(name);
392+
var ns = goog.getObjectByName(name);
372393
return ns != null ? ns : null;
373394
}
374395
}
@@ -415,6 +436,27 @@ goog.module.declareLegacyNamespace = function() {
415436
};
416437

417438

439+
/**
440+
* Marks that the current file should only be used for testing, and never for
441+
* live code in production.
442+
*
443+
* In the case of unit tests, the message may optionally be an exact namespace
444+
* for the test (e.g. 'goog.stringTest'). The linter will then ignore the extra
445+
* provide (if not explicitly defined in the code).
446+
*
447+
* @param {string=} opt_message Optional message to add to the error that's
448+
* raised when used in production code.
449+
*/
450+
goog.setTestOnly = function(opt_message) {
451+
if (goog.DISALLOW_TEST_ONLY_CODE) {
452+
opt_message = opt_message || '';
453+
throw new Error(
454+
'Importing test-only code into non-debug environment' +
455+
(opt_message ? ': ' + opt_message : '.'));
456+
}
457+
};
458+
459+
418460
/**
419461
* Forward declares a symbol. This is an indication to the compiler that the
420462
* symbol may be used in the source yet is not required and may not be provided
@@ -449,7 +491,7 @@ if (!COMPILED) {
449491
goog.isProvided_ = function(name) {
450492
return (name in goog.loadedModules_) ||
451493
(!goog.implicitNamespaces_[name] &&
452-
goog.getObjectByName_(name) != null);
494+
goog.getObjectByName(name) != null);
453495
};
454496

455497
/**
@@ -479,9 +521,9 @@ if (!COMPILED) {
479521
* @param {!Object=} opt_obj The object within which to look; default is
480522
* globalThis.
481523
* @return {?} The value (object or primitive) or, if not found, null.
482-
* @private
524+
* @deprecated Prefer non-reflective access.
483525
*/
484-
goog.getObjectByName_ = function(name, opt_obj) {
526+
goog.getObjectByName = function(name, opt_obj) {
485527
var parts = name.split('.');
486528
var cur = opt_obj || globalThis;
487529
for (var i = 0; i < parts.length; i++) {

0 commit comments

Comments
 (0)