Search in sources :

Example 71 with TestExternsBuilder

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

Example 72 with TestExternsBuilder

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

Example 73 with TestExternsBuilder

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

Example 74 with TestExternsBuilder

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

Example 75 with TestExternsBuilder

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