Search in sources :

Example 6 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class RemoveUnusedCodeTest method testRemoveUnusedPolyfills_staticProperty_withOptionalChain.

@Test
public void testRemoveUnusedPolyfills_staticProperty_withOptionalChain() {
    final String promisePolyfill = "$jscomp.polyfill('Promise', function() {}, 'es6', 'es3');";
    final String allSettledPolyfill = "$jscomp.polyfill('Promise.allSettled', function() {}, 'es6', 'es3');";
    PolyfillRemovalTester tester = new PolyfillRemovalTester().addExterns(new TestExternsBuilder().addConsole().addPromise().addExtra(JSCOMP_POLYFILL, "/** @const */ var goog = {};").build()).addPolyfill(promisePolyfill).addPolyfill(allSettledPolyfill);
    // neither guarded
    tester.expectNoRemovalTest("console.log(Promise.allSettled());");
    // `?.` guards the name on its left, allowing it to be removed
    tester.expectPolyfillsRemovedTest(// allSettled guarded
    "console.log(Promise.allSettled?.());", allSettledPolyfill);
    tester.expectPolyfillsRemovedTest(// allSettled guarded
    "console.log(Promise.allSettled?.(Promise.allSettled));", allSettledPolyfill);
    // TODO(b/163394833): Note that this behavior could result in the Promise polyfill being
    // removed, then the Promise.allSettled polyfill code having nowhere to hang its value.
    // At runtime the `$jscomp.polyfill('Promise.allSettled',...)` call will silently fail
    // to create the polyfill if `Promise` isn't defined.
    // This is consistent with the way guarding with `&&` would behave, as demonstrated by the
    // following test case.
    tester.expectPolyfillsRemovedTest(// Promise guarded
    "console.log(Promise?.allSettled());", promisePolyfill);
    tester.expectPolyfillsRemovedTest(// Promise guarded
    "console.log(Promise && Promise.allSettled());", promisePolyfill);
    tester.expectPolyfillsRemovedTest(// both guarded
    "console.log(Promise?.allSettled?.([Promise.allSettled]));", promisePolyfill, allSettledPolyfill);
    // Access via a (potentially) global variable should behave the same way, even if the
    // global is referenced via optional chaining
    tester.expectNoRemovalTest(// neither guarded
    "console.log(goog.global?.Promise.allSettled());");
    tester.expectPolyfillsRemovedTest(// allSettled guarded
    "console.log(goog.global?.Promise.allSettled?.());", allSettledPolyfill);
    // TODO(b/163394833): Note that this behavior could result in the Promise polyfill being
    // removed, then the Promise.allSettled polyfill code having nowhere to hang its value.
    // At runtime the `$jscomp.polyfill('Promise.allSettled',...)` call will silently fail
    // to create the polyfill if `Promise` isn't defined.
    // This is consistent with the way guarding with `&&` would behave, as demonstrated by the
    // following test case.
    tester.expectPolyfillsRemovedTest(// Promise guarded
    "console.log(goog.global?.Promise?.allSettled());", promisePolyfill);
    tester.expectPolyfillsRemovedTest("console.log(goog.global && goog.global.Promise && goog.global.Promise.allSettled());", promisePolyfill);
    tester.expectPolyfillsRemovedTest(// both guarded
    "console.log(goog.global?.Promise?.allSettled?.());", promisePolyfill, allSettledPolyfill);
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 7 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class AdvancedOptimizationsIntegrationTest method testIsolatePolyfills_noPolyfillsUsed.

@Test
public void testIsolatePolyfills_noPolyfillsUsed() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageIn(LanguageMode.STABLE_IN);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setRewritePolyfills(true);
    options.setIsolatePolyfills(true);
    options.setGeneratePseudoNames(true);
    externs = ImmutableList.of(SourceFile.fromCode("testExterns.js", new TestExternsBuilder().addAlert().build()));
    testSame(options, "alert('hi');");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 8 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class AdvancedOptimizationsIntegrationTest method testInlineRestParam.

@Test
public void testInlineRestParam() {
    CompilerOptions options = createCompilerOptions();
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    useNoninjectingCompiler = true;
    externs = ImmutableList.of(new TestExternsBuilder().addAlert().addFunction().addExtra(// Externs to take the place of the injected library code
    "const $jscomp = {};", "", "/**", " * @this {number}", " * @noinline", " */", "$jscomp.getRestArguments = function() {};", "").buildExternsFile("externs.js"));
    test(options, lines("function foo() {", "  var f = (...args) => args[0];", "  return f(8);", "}", "alert(foo());"), lines("alert(function() {", "  return $jscomp.getRestArguments.apply(0, arguments)[0];", "}(8))"));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 9 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class AdvancedOptimizationsIntegrationTest method testIsolatePolyfills_worksWithoutRewritePolyfills.

@Test
public void testIsolatePolyfills_worksWithoutRewritePolyfills() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageIn(LanguageMode.STABLE_IN);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setRewritePolyfills(false);
    options.setIsolatePolyfills(true);
    options.setForceLibraryInjection(ImmutableList.of("es6/string/startswith"));
    options.setGeneratePseudoNames(true);
    externs = ImmutableList.of(SourceFile.fromCode("testExterns.js", new TestExternsBuilder().addArray().addString().addObject().build()));
    compile(options, "alert('foo'.startsWith('bar'));");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 10 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class AdvancedOptimizationsIntegrationTest method testRestObjectPatternParameters.

@Test
public void testRestObjectPatternParameters() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    useNoninjectingCompiler = true;
    externs = ImmutableList.of(new TestExternsBuilder().addAlert().addArray().addFunction().addExtra(// Externs to take the place of the injected library code
    "const $jscomp = {};", "", "/**", " * @this {number}", " * @noinline", " */", "$jscomp.getRestArguments = function() {};", "").buildExternsFile("externs.js"));
    test(options, lines("function countArgs(x, ...{length}) {", "  return length;", "}", "alert(countArgs(1, 1, 1, 1, 1));"), lines("alert(function (a) {", "  return $jscomp.getRestArguments.apply(1, arguments).length;", "}(1,1,1,1,1))"));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) 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