use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class MultiPassTest method testDestructuringInForLoopHeaderWithInitializer.
@Test
public void testDestructuringInForLoopHeaderWithInitializer() {
setDestructuringArrowFunctionOptions();
test(externs(new TestExternsBuilder().addConsole().addJSCompLibraries().build()), srcs(lines("for (var x = 1; x < 3; [x,] = [3,4]){", " console.log(x);", "}")), expected(lines("for (var x = 1; x < 3; function () {", " let $jscomp$destructuring$var0 = [3,4]", " var $jscomp$destructuring$var1 = ", " $jscomp.makeIterator($jscomp$destructuring$var0);", " x = $jscomp$destructuring$var1.next().value;", " return $jscomp$destructuring$var0;", " } ()){", "console.log(x);", "}")));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class MultiPassTest method testDestructuringInForLoopHeaderUsedInBody.
@Test
public void testDestructuringInForLoopHeaderUsedInBody() {
setDestructuringArrowFunctionOptions();
test(externs(new TestExternsBuilder().addJSCompLibraries().addConsole().build()), srcs(lines("var prefix;", "for (;;[, prefix] = /** @type {!Array<string>} */ (/\\.?([^.]+)$/.exec(prefix))){", " console.log(prefix);", "}")), expected(lines("var prefix;", "for (;;function () {", " let $jscomp$destructuring$var0 = ", " /\\.?([^.]+)$/.exec(prefix)", " var $jscomp$destructuring$var1 = ", "$jscomp.makeIterator($jscomp$destructuring$var0);", " $jscomp$destructuring$var1.next();", " prefix = $jscomp$destructuring$var1.next().value;", " return $jscomp$destructuring$var0;", " } ()){", " console.log(prefix);", "}")));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class RemoveUnusedCodeTest method testRemoveUnusedPolyfills_propertyOfGlobalObject_untyped.
@Test
public void testRemoveUnusedPolyfills_propertyOfGlobalObject_untyped() {
final String mapPolyfill = "$jscomp.polyfill('Map', function() {}, 'es6', 'es3');";
PolyfillRemovalTester tester = new PolyfillRemovalTester().addExterns(new TestExternsBuilder().addConsole().addExtra(JSCOMP_POLYFILL, "/** @constructor */ function Map() {}", "/** @const */ var goog = {};", "/** @const {!Global} */ goog.global;", "/** @const */ goog.structs = {};", "/** @constructor */ goog.structs.Map = function() {};", "/** @type {!Global} */ var someGlobal;", "/** @const */ var notGlobal = {};", "/** @constructor */ notGlobal.Map = function() {};", "").build()).addPolyfill(mapPolyfill);
// Global polyfills may be accessed as properties on the global object.
tester.expectNoRemovalTest("console.log(new goog.global.Map());");
// NOTE: Without type information we don't know whether a property access is on the global
// object or not.
tester.expectNoRemovalTest("console.log(new goog.structs.Map());");
tester.expectNoRemovalTest("console.log(new notGlobal.Map());");
tester.expectNoRemovalTest(lines(//
"", "var x = {Map: /** @constructor */ function() {}};", "console.log(new x.Map());"));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class RemoveUnusedCodeTest method testRemoveUnusedPolyfills_unguardedAndGuarded.
@Test
public void testRemoveUnusedPolyfills_unguardedAndGuarded() {
final String mapPolyfill = "$jscomp.polyfill('Map', function() {}, 'es6', 'es3');";
PolyfillRemovalTester tester = new PolyfillRemovalTester().addExterns(new TestExternsBuilder().addConsole().addExtra(JSCOMP_POLYFILL, "/** @constructor */ function Map() {}").build()).addPolyfill(mapPolyfill);
// Map is not removed because it has an unguarded usage.
tester.expectNoRemovalTest(lines(//
"if (typeof Map == 'undefined') {", " console.log(Map);", "}", "console.log(Map);"));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class RemoveUnusedCodeTest method testRemoveUnusedPolyfills_guardedStatics.
@Test
public void testRemoveUnusedPolyfills_guardedStatics() {
final String arrayFromPolyfill = "$jscomp.polyfill('Array.from', function() {}, 'es6', 'es3');";
final String promisePolyfill = "$jscomp.polyfill('Promise', function() {}, 'es6', 'es3');";
final String allSettledPolyfill = "$jscomp.polyfill('Promise.allSettled', function() {}, 'es8', 'es3');";
final String symbolPolyfill = "$jscomp.polyfill('Symbol', function() {}, 'es6', 'es3');";
final String symbolIteratorPolyfill = "$jscomp.polyfill('Symbol.iterator', function() {}, 'es6', 'es3');";
PolyfillRemovalTester tester = new PolyfillRemovalTester().addExterns(new TestExternsBuilder().addConsole().addArray().addPromise().addExtra(JSCOMP_POLYFILL).build()).addPolyfill(arrayFromPolyfill).addPolyfill(promisePolyfill).addPolyfill(allSettledPolyfill).addPolyfill(symbolPolyfill).addPolyfill(symbolIteratorPolyfill);
tester.expectPolyfillsRemovedTest(lines(//
"if (typeof Array.from !== 'undefined') {", " console.log(Array.from);", "}"), // guarded & all others unused
arrayFromPolyfill, promisePolyfill, allSettledPolyfill, symbolPolyfill, symbolIteratorPolyfill);
tester.expectPolyfillsRemovedTest(lines(//
"var a;", "if (Promise && Promise.allSettled) {", " Promise.allSettled(a);", "}"), // guarded
promisePolyfill, // guarded
allSettledPolyfill, // this and following unused
arrayFromPolyfill, symbolPolyfill, symbolIteratorPolyfill);
tester.expectPolyfillsRemovedTest(lines("if (Symbol && Symbol.iterator) {", " console.log(Symbol.iterator);", "}"), // guarded
symbolPolyfill, // guarded
symbolIteratorPolyfill, // this and following unused
arrayFromPolyfill, promisePolyfill, allSettledPolyfill);
}
Aggregations