Search in sources :

Example 1 with NoninjectingCompiler

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

the class AdvancedOptimizationsIntegrationTest method testArrayValuesIsPolyfilledForEs2015Out.

@Test
public void testArrayValuesIsPolyfilledForEs2015Out() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setTypeBasedOptimizationOptions(options);
    options.setRewritePolyfills(true);
    options.setLanguageOut(LanguageMode.ECMASCRIPT_2015);
    useNoninjectingCompiler = true;
    compile(options, new String[] { "for (const x of [1, 2, 3].values()) { alert(x); }" });
    assertThat(lastCompiler.getResult().errors).isEmpty();
    assertThat(lastCompiler.getResult().warnings).isEmpty();
    assertThat(((NoninjectingCompiler) lastCompiler).getInjected()).containsExactly("es6/array/values");
}
Also used : NoninjectingCompiler(com.google.javascript.jscomp.testing.NoninjectingCompiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) Test(org.junit.Test)

Example 2 with NoninjectingCompiler

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

the class AdvancedOptimizationsIntegrationTest method testBug123583793.

@Test
public void testBug123583793() {
    // Avoid including the transpilation library
    useNoninjectingCompiler = true;
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setTypeBasedOptimizationOptions(options);
    options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setRewritePolyfills(true);
    options.setPrettyPrint(true);
    externs = ImmutableList.of(new TestExternsBuilder().addObject().addConsole().buildExternsFile("externs.js"));
    test(options, lines("function foo() {", "  const {a, ...rest} = {a: 1, b: 2, c: 3};", "  return {a, rest};", "};", "console.log(foo());", ""), lines("var a = console,", "    b = a.log,", "    c = {a:1, b:2, c:3},", "    d = Object.assign({}, c),", "    e = c.a,", "    f = (delete d.a, d);", "b.call(a, {a:e, d:f});", ""));
    assertThat(((NoninjectingCompiler) lastCompiler).getInjected()).contains("es6/object/assign");
}
Also used : NoninjectingCompiler(com.google.javascript.jscomp.testing.NoninjectingCompiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 3 with NoninjectingCompiler

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

the class RewritePolyfillsTest method createCompiler.

@Override
protected Compiler createCompiler() {
    return new NoninjectingCompiler() {

        Node lastInjected = null;

        @Override
        public Node ensureLibraryInjected(String library, boolean force) {
            if (getInjected().contains(library)) {
                // already injected
                return lastInjected;
            } else {
                // super method just records library in `injected`
                super.ensureLibraryInjected(library, force);
                Node parent = getNodeForCodeInsertion(null);
                Node ast = parseSyntheticCode(library, injectableLibraries.get(library));
                Node lastChild = ast.getLastChild();
                Node firstChild = ast.removeChildren();
                // Any newly added functions must be marked as changed.
                for (Node child = firstChild; child != null; child = child.getNext()) {
                    NodeUtil.markNewScopesChanged(child, this);
                }
                if (lastInjected == null) {
                    parent.addChildrenToFront(firstChild);
                } else {
                    parent.addChildrenAfter(firstChild, lastInjected);
                }
                return lastInjected = lastChild;
            }
        }
    };
}
Also used : NoninjectingCompiler(com.google.javascript.jscomp.testing.NoninjectingCompiler) Node(com.google.javascript.rhino.Node)

Example 4 with NoninjectingCompiler

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

the class InjectTranspilationRuntimeLibrariesTest method parseAndRunInjectionPass.

/**
 * Parses the given code and runs the {@link InjectTranspilationRuntimeLibraries} pass over the
 * resulting AST
 *
 * @return the set of paths to all the injected libraries
 */
private Set<String> parseAndRunInjectionPass(String js) {
    NoninjectingCompiler compiler = new NoninjectingCompiler();
    CompilerOptions options = new CompilerOptions();
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    compiler.init(ImmutableList.of(SourceFile.fromCode("externs", "")), ImmutableList.of(SourceFile.fromCode("testcode", js)), options);
    compiler.parse();
    new InjectTranspilationRuntimeLibraries(compiler).process(compiler.getExternsRoot(), compiler.getJsRoot());
    return compiler.getInjected();
}
Also used : NoninjectingCompiler(com.google.javascript.jscomp.testing.NoninjectingCompiler)

Aggregations

NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)4 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)2 Test (org.junit.Test)2 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)1 Node (com.google.javascript.rhino.Node)1