Search in sources :

Example 16 with TestExternsBuilder

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

the class AdvancedOptimizationsIntegrationTest method testBigIntStringConcatenation.

@Test
public void testBigIntStringConcatenation() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguage(LanguageMode.ECMASCRIPT_NEXT);
    externs = ImmutableList.of(new TestExternsBuilder().addBigInt().buildExternsFile("externs.js"));
    test(options, "1n + 'asdf'", "'1nasdf'");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 17 with TestExternsBuilder

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

the class AdvancedOptimizationsIntegrationTest method nullishCoalesceTranspiledOutput.

@Test
public void nullishCoalesceTranspiledOutput() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT_IN);
    options.setLanguageOut(LanguageMode.ECMASCRIPT_2019);
    externs = ImmutableList.of(new TestExternsBuilder().addExtra("var x, y").buildExternsFile("externs"));
    test(options, "x ?? y", "let a; null != (a = x) ? a : y");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 18 with TestExternsBuilder

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

the class AdvancedOptimizationsIntegrationTest method testDisambiguationOfForwardReferencedAliasedInterface.

@Test
public void testDisambiguationOfForwardReferencedAliasedInterface() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageOut(LanguageMode.NO_TRANSPILE);
    options.setDisambiguateProperties(true);
    options.setPrettyPrint(true);
    options.setPreserveTypeAnnotations(true);
    externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs.js"));
    test(options, lines("", // but it does have a matching `foo()` method.
    "class MyBaseClass {", "    /** @return {void} */", "    foo() {", "        console.log('I should exist');", "    }", "}", "", // Note that `AliasedInterface` isn't defined yet.
    "/** @implements {AliasedInterface} */", "class MyClass extends MyBaseClass {", "}", "", // code doesn't appear to do this.
    "/** @record */", "function OriginalInterface() { }", "/** @return {void} */", "OriginalInterface.prototype.foo = function () { };", "", // error.
    "/** @const */", "const AliasedInterface = OriginalInterface;", "", // `MyClass.prototype.foo`.
    "/** @return {!AliasedInterface} */", "function magicFactory() {", "    return new MyClass();", "}", "", "magicFactory().foo();"), // up inlined as the only output statement.
    "console.log('I should exist');");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 19 with TestExternsBuilder

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

the class AdvancedOptimizationsIntegrationTest method testBug173319540.

@Test
public void testBug173319540() {
    // 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.ECMASCRIPT_2017);
    options.setPrettyPrint(true);
    options.setGeneratePseudoNames(true);
    externs = ImmutableList.of(new TestExternsBuilder().addAsyncIterable().addConsole().addExtra(// fake async iterator to use in the test code
    "/** @type {!AsyncIterator<Array<String>>} */", "var asyncIterator;", "", // Externs to take the place of the injected library code
    "const $jscomp = {};", "", "/**", " * @param {", " *     string|!AsyncIterable<T>|!Iterable<T>|!Iterator<T>|!Arguments", " *   } iterable", " * @return {!AsyncIteratorIterable<T>}", " * @template T", " * @suppress {reportUnknownTypes}", " */", "$jscomp.makeAsyncIterator = function(iterable) {};", "").buildExternsFile("externs.js"));
    test(options, lines(// 
    "", "async function foo() {", "  for await (const [key, value] of asyncIterator) {", "    console.log(key,value);", "  }", "}", "foo();"), lines(// 
    "", "(async function() {", "  for (const $$jscomp$forAwait$tempIterator0$$ =", "           $jscomp.makeAsyncIterator(asyncIterator);;) {", "    const $$jscomp$forAwait$tempResult0$$ =", "        await $$jscomp$forAwait$tempIterator0$$.next();", "    if ($$jscomp$forAwait$tempResult0$$.done) {", "      break;", "    }", "    const [$key$$, $value$jscomp$2$$] = $$jscomp$forAwait$tempResult0$$.value;", "    console.log($key$$, $value$jscomp$2$$);", "  }", "})();", ""));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 20 with TestExternsBuilder

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

the class AdvancedOptimizationsIntegrationTest method testReferenceToInternalClassName.

@Test
public void testReferenceToInternalClassName() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageOut(LanguageMode.NO_TRANSPILE);
    // 'use strict'; is just noise here
    options.setEmitUseStrict(false);
    options.setPrettyPrint(true);
    options.setGeneratePseudoNames(true);
    externs = ImmutableList.of(new TestExternsBuilder().addExtra("function use(x) {}").buildExternsFile("externs"));
    test(options, new String[] { TestExternsBuilder.getClosureExternsAsSource(), lines("goog.module('a.b.c');", "exports = class Foo {", // (CollapseProperties) to get full optimization and avoid generating broken code.
    "  method() { return Foo.EventType.E1; }", "};", "", "/** @enum {number} */", "exports.EventType = {", "  E1: 1,", "};", ""), lines(// 
    "", "goog.module('a.b.d');", "const Foo = goog.require('a.b.c');", "use(new Foo().method());", "") }, new String[] { "", "", lines(// 
    "new class {};", "use(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