@@ -1310,6 +1310,174 @@ public void tesNonLegacyGoogModule_inScript_required_noWarning() {
1310
1310
"goog.provide('test.b'); goog.require('test.a'); console.log(test.a.x);" );
1311
1311
}
1312
1312
1313
+ @ Test
1314
+ public void testWarning_googScope_googModuleGet_noRequire () {
1315
+ checkRequireForGoogModuleGetWarning (
1316
+ "foo.bar.Baz" ,
1317
+ lines (
1318
+ "goog.module('foo.bar.Baz');" , //
1319
+ "/** @constructor */" ,
1320
+ "exports = function() {}" ),
1321
+ lines (
1322
+ "goog.provide('test');" , //
1323
+ "" ,
1324
+ "goog.scope(function() {" ,
1325
+ "const Baz = goog.module.get('foo.bar.Baz');" ,
1326
+ "exports.fn = function() { new Baz(); }" ,
1327
+ "});" ));
1328
+ }
1329
+
1330
+ @ Test
1331
+ public void testWarning_script_googModuleGet_inProvideInitialization_noRequire () {
1332
+ checkRequireForGoogModuleGetWarning (
1333
+ "foo.bar.Baz" ,
1334
+ lines (
1335
+ "goog.module('foo.bar.Baz');" , //
1336
+ "/** @constructor */" ,
1337
+ "exports.Boo = function() {}" ),
1338
+ lines (
1339
+ "goog.provide('test');" , //
1340
+ "" ,
1341
+ "test.boo = new (goog.module.get('foo.bar.Baz').Boo)();" ));
1342
+ }
1343
+
1344
+ @ Test
1345
+ public void testWarning_script_googModuleGet_inScriptIfBlock_noRequire () {
1346
+ checkRequireForGoogModuleGetWarning (
1347
+ "foo.bar.Baz" ,
1348
+ lines (
1349
+ "goog.module('foo.bar.Baz');" , //
1350
+ "/** @constructor */" ,
1351
+ "exports.Boo = function() {}" ),
1352
+ lines (
1353
+ "goog.provide('test');" , //
1354
+ "" ,
1355
+ "if (true) { console.log(new (goog.module.get('foo.bar.Baz').Boo)()); }" ));
1356
+ }
1357
+
1358
+ @ Test
1359
+ public void testWarning_script_googModuleGet_inScript_inTopLevelIIFE_noRequire () {
1360
+ checkRequireForGoogModuleGetWarning (
1361
+ "foo.bar.Baz" ,
1362
+ lines (
1363
+ "goog.module('foo.bar.Baz');" , //
1364
+ "/** @constructor */" ,
1365
+ "exports.Boo = function() {}" ),
1366
+ lines (
1367
+ "goog.provide('test');" , //
1368
+ "" ,
1369
+ "(() => console.log(new (goog.module.get('foo.bar.Baz').Boo)()))();" ));
1370
+ }
1371
+
1372
+ @ Test
1373
+ public void testWarning_googScope_googModuleGet_withPropAccess_noRequire () {
1374
+ checkRequireForGoogModuleGetWarning (
1375
+ "foo.bar.Baz" ,
1376
+ lines (
1377
+ "goog.module('foo.bar.Baz');" , //
1378
+ "/** @constructor */" ,
1379
+ "exports.Boo = function() {}" ),
1380
+ lines (
1381
+ "goog.provide('test');" , //
1382
+ "" ,
1383
+ "goog.scope(function() {" ,
1384
+ "const Woo = goog.module.get('foo.bar.Baz').Boo.Goo.Woo;" ,
1385
+ "exports.fn = function() { new Woo(); }" ,
1386
+ "});" ));
1387
+ }
1388
+
1389
+ @ Test
1390
+ public void testNoWarning_googScope_googModuleGet_hasRequire () {
1391
+ checkNoWarning (
1392
+ lines (
1393
+ "goog.module('foo.bar.Baz');" , //
1394
+ "/** @constructor */" ,
1395
+ "exports = function() {}" ),
1396
+ lines (
1397
+ "goog.provide('test');" , //
1398
+ "goog.require('foo.bar.Baz');" ,
1399
+ "" ,
1400
+ "goog.scope(function() {" ,
1401
+ "const Baz = goog.module.get('foo.bar.Baz');" ,
1402
+ "function fn() { new Baz(); }" ,
1403
+ "});" ));
1404
+ }
1405
+
1406
+ @ Test
1407
+ public void testWarning_googScope_googModuleGet_IIFE_noRequire () {
1408
+ checkRequireForGoogModuleGetWarning (
1409
+ "foo.bar.Baz" ,
1410
+ lines (
1411
+ "goog.module('foo.bar.Baz');" , //
1412
+ "/** @constructor */" ,
1413
+ "exports.Boo = function() {}" ),
1414
+ lines (
1415
+ "goog.provide('test');" , //
1416
+ "" ,
1417
+ "goog.scope(function() {" ,
1418
+ " (() => console.log(new (goog.module.get('foo.bar.Baz').Boo)()))();" ,
1419
+ "});" ));
1420
+ }
1421
+
1422
+ @ Test
1423
+ public void testWarning_script_googModuleGet_inFunctionInScript_noRequire_noWarning () {
1424
+ checkNoWarning (
1425
+ lines (
1426
+ "goog.module('foo.bar.Baz');" , //
1427
+ "/** @constructor */" ,
1428
+ "exports.Boo = function() {}" ),
1429
+ lines (
1430
+ "goog.provide('test');" , //
1431
+ "" ,
1432
+ // We allow this goog.module.get despite the lack of goog.require: it's possible that
1433
+ // the foo.bar.Baz module will have been loaded by the time test.fn is called. It's up
1434
+ // to the caller to ensure that it's loaded.
1435
+ "test.fn = function() {" ,
1436
+ " console.log(new (goog.module.get('foo.bar.Baz').Boo)());" ,
1437
+ "}" ));
1438
+ }
1439
+
1440
+ @ Test
1441
+ public void
1442
+ testWarning_script_googModuleGet_inFunctionInScript_inNonTopLevelIIFE_noRequire_noWarning () {
1443
+ checkNoWarning (
1444
+ lines (
1445
+ "goog.module('foo.bar.Baz');" , //
1446
+ "/** @constructor */" ,
1447
+ "exports.Boo = function() {}" ),
1448
+ lines (
1449
+ "goog.provide('test');" , //
1450
+ "" ,
1451
+ // We allow this goog.module.get despite the lack of goog.require: it's possible that
1452
+ // the foo.bar.Baz module will have been loaded by the time test.fn is called. It's up
1453
+ // to the caller to ensure that it's loaded.
1454
+ "test.fn = function() {" ,
1455
+ " (() => console.log(new (goog.module.get('foo.bar.Baz').Boo)()))();" ,
1456
+ "}" ));
1457
+ }
1458
+
1459
+ @ Test
1460
+ public void testNoWarning_nestedInGoogScope_googModuleGet_noRequire () {
1461
+ checkNoWarning (
1462
+ lines (
1463
+ "goog.module('foo.bar.Baz');" , //
1464
+ "/** @constructor */" ,
1465
+ "exports = function() {}" ),
1466
+ lines (
1467
+ "goog.provide('test');" , //
1468
+ "" ,
1469
+ "goog.scope(function() {" ,
1470
+ "test.fn = function() {" ,
1471
+ // We allow this goog.module.get despite the lack of goog.require: it's possible that
1472
+ // the foo.bar.Baz module will have been loaded by the time test.fn is called. It's up
1473
+ // to the caller to ensure that it's loaded.
1474
+ " const Baz = goog.module.get('foo.bar.Baz');" ,
1475
+ " new Baz();" ,
1476
+ " new goog.module.get('foo.bar.Baz');" ,
1477
+ "}" ,
1478
+ "});" ));
1479
+ }
1480
+
1313
1481
private void checkNoWarning (String ... js ) {
1314
1482
testSame (srcs (js ));
1315
1483
}
@@ -1368,4 +1536,11 @@ private void checkIncorrectNamespaceAliasRequireTypeWarning(String namespace, St
1368
1536
warning (CheckMissingRequires .INCORRECT_NAMESPACE_ALIAS_REQUIRE_TYPE )
1369
1537
.withMessageContaining ("'" + namespace + "'" ));
1370
1538
}
1539
+
1540
+ private void checkRequireForGoogModuleGetWarning (String namespace , String ... js ) {
1541
+ testSame (
1542
+ srcs (js ),
1543
+ warning (CheckMissingRequires .MISSING_REQUIRE_FOR_GOOG_MODULE_GET )
1544
+ .withMessageContaining ("'" + namespace + "'" ));
1545
+ }
1371
1546
}
0 commit comments