Search in sources :

Example 1 with TestExternsBuilder

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);", "}")));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 2 with TestExternsBuilder

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);", "}")));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 3 with TestExternsBuilder

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());"));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 4 with TestExternsBuilder

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);"));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 5 with TestExternsBuilder

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);
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Aggregations

TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)107 Test (org.junit.Test)106 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)58 Node (com.google.javascript.rhino.Node)17 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)17 Compiler (com.google.javascript.jscomp.Compiler)8 SourceFile (com.google.javascript.jscomp.SourceFile)7 Color (com.google.javascript.jscomp.colors.Color)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)1 ClosureReverseAbstractInterpreter (com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)1 SemanticReverseAbstractInterpreter (com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 File (java.io.File)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1