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);
}
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');");
}
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))"));
}
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'));");
}
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))"));
}
Aggregations