use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testIsolatePolyfills_noAddedCodeForUnusedPolyfill.
@Test
public void testIsolatePolyfills_noAddedCodeForUnusedPolyfill() {
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().addArray().addString().addObject().build()));
test(options, "const unused = 'foo'.startsWith('bar');", "var $$jscomp$propertyToPolyfillSymbol$$={};");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testNameReferencedInExternsDefinedInCodeNotRenamedButIsInlined.
@Test
public void testNameReferencedInExternsDefinedInCodeNotRenamedButIsInlined() {
// NOTE(lharker): I added this test as a regression test for existing compiler behavior. I'm
// not sure it makes sense conceptually to have 'foobar' be unrenamable but still optimized away
// below.
CompilerOptions options = createCompilerOptions();
externs = ImmutableList.of(new TestExternsBuilder().addAlert().addExtra("/** @fileoverview @suppress {externsValidation} */ foobar").buildExternsFile("externs.js"));
// with just variable renaming on, the code is unchanged becasue of the 'foobar' externs ref.
options.setVariableRenaming(VariableRenamingPolicy.ALL);
test(options, "var foobar = {x: 1}; alert(foobar.x);", "var foobar = {x:1}; alert(foobar.x);");
// with inlining + other advanced optimizations, foobar is still deleted
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
test(options, "var foobar = {x: 1}; alert(foobar.x);", "alert(1);");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testAddFunctionProperties4.
@Test
public void testAddFunctionProperties4() {
externs = ImmutableList.of(new TestExternsBuilder().addAlert().buildExternsFile(// add Closure base.js as srcs
"externs.js"));
String source = lines(TestExternsBuilder.getClosureExternsAsSource(), "/** @constructor */", "var Foo = function() {};", "goog.addSingletonGetter = function(o) {", " o.f = function() {", " return o.i || (o.i = new o);", " };", "};", "goog.addSingletonGetter(Foo);", "alert(Foo.f());");
String expected = lines("function Foo(){}", "Foo.f = function() { return Foo.i || (Foo.i = new Foo()); };", "alert(Foo.f());");
CompilerOptions options = createCompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
options.setRenamingPolicy(VariableRenamingPolicy.OFF, PropertyRenamingPolicy.OFF);
test(options, source, expected);
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testBug196083761.
@Test
public void testBug196083761() {
CompilerOptions options = createCompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
CompilationLevel.ADVANCED_OPTIMIZATIONS.setTypeBasedOptimizationOptions(options);
options.setLanguage(LanguageMode.ECMASCRIPT_2020);
options.setPrettyPrint(true);
options.setGeneratePseudoNames(true);
externs = ImmutableList.of(new TestExternsBuilder().addConsole().addClosureExterns().buildExternsFile("externs.js"));
test(options, new String[] { lines(//
"goog.module('base');", //
"class Base {", " constructor({paramProblemFunc = problemFunc} = {}) {", " /** @public */", " this.prop = paramProblemFunc();", " }", "}", "", "const problemFunc = () => 1;", "Base.problemFunc = problemFunc;", "", "exports = {Base};", ""), lines(//
"goog.module('child');", "", "const {Base} = goog.require('base');", "", "class Child extends Base {", " constructor({paramProblemFunc = Base.problemFunc} = {}) {", " super({paramProblemFunc});", " }", "}", "", "exports = {Child};", ""), lines("goog.module('grandchild');", "", "const {Child} = goog.require('child');", "", "class GrandChild extends Child {", " constructor() {", " super({paramProblemFunc: () => GrandChild.problemFunc() + 1});", " }", "}", "", "console.log(new GrandChild().prop);", "") }, new String[] { "", "", lines("const $module$contents$base_problemFunc$$ = () => 1;", "var $module$exports$base$Base$$ = class {", " constructor(", " {", " $paramProblemFunc$:$paramProblemFunc$$ =", " $module$contents$base_problemFunc$$", " } = {}) {", " this.$a$ = $paramProblemFunc$$();", " }", "}, $module$exports$child$Child$$ = class extends $module$exports$base$Base$$ {", " constructor(", " {", " $paramProblemFunc$:$paramProblemFunc$jscomp$1$$ =", " $module$contents$base_problemFunc$$", " } = {}) {", " super({$paramProblemFunc$:$paramProblemFunc$jscomp$1$$});", " }", "};", "class $module$contents$grandchild_GrandChild$$", " extends $module$exports$child$Child$$ {", " constructor() {", // NOTE the call to `null()` here!
" super({$paramProblemFunc$:() => null() + 1});", " }", "}", "console.log((new $module$contents$grandchild_GrandChild$$).$a$);", "") });
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class ES2021IntegrationTest method logicalAsssignmentsPropRefWithElementCastType_supportedWithTranspilation.
@Test
public void logicalAsssignmentsPropRefWithElementCastType_supportedWithTranspilation() {
CompilerOptions options = fullyOptimizedCompilerOptions();
externs = ImmutableList.of(new TestExternsBuilder().addExtra("let foo, x").buildExternsFile("externs"));
test(options, "/** @type {number} */ (foo[x]) ??= 5", lines(//
"let a, b;", "(a = foo)[b = x] ?? (a[b] = 5)"));
}
Aggregations