Search in sources :

Example 61 with Compiler

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

the class PolymerIntegrationTest method testPolymerExportPolicyExportAllClassBased.

@Test
public void testPolymerExportPolicyExportAllClassBased() {
    CompilerOptions options = createCompilerOptions();
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setPolymerVersion(2);
    options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
    addPolymerExterns();
    options.setRenamingPolicy(VariableRenamingPolicy.ALL, PropertyRenamingPolicy.ALL_UNQUOTED);
    options.setRemoveUnusedPrototypeProperties(true);
    options.setPolymerExportPolicy(PolymerExportPolicy.EXPORT_ALL);
    options.setGenerateExports(true);
    options.setExportLocalPropertyDefinitions(true);
    Compiler compiler = compile(options, lines(EXPORT_PROPERTY_DEF, "class FooElement extends PolymerElement {", "  static get properties() {", "    return {", "      longUnusedProperty: String,", "    }", "  }", "  longUnusedMethod() {", "    return this.longUnusedProperty;", "  }", "}"));
    String source = compiler.toSource();
    // If we see these identifiers anywhere in the output source, we know that we successfully
    // protected it against removal and renaming.
    assertThat(source).contains("longUnusedProperty");
    assertThat(source).contains("longUnusedMethod");
    assertThat(compiler.getErrors()).isEmpty();
    assertThat(compiler.getWarnings()).isEmpty();
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) Test(org.junit.Test)

Example 62 with Compiler

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

the class PolymerIntegrationTest method testPolymerElementImportedFromEsModule.

@Test
public void testPolymerElementImportedFromEsModule() {
    CompilerOptions options = createCompilerOptions();
    options.setPolymerVersion(2);
    options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    addPolymerExterns();
    Compiler compiler = compile(options, new String[] { lines("export class PolymerElement {};"), lines("import {PolymerElement} from './i0.js';", "class Foo extends PolymerElement {", "  get is() { return 'foo-element'; }", "  static get properties() { return { fooProp: String }; }", "}", "const foo = new Foo();", // had successfully parsed the element definition.
    "foo.fooProp;") });
    assertThat(compiler.getErrors()).isEmpty();
    assertThat(compiler.getWarnings()).isEmpty();
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) Test(org.junit.Test)

Example 63 with Compiler

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

the class ProductionCoverageInstrumentationPassIntegrationTest method testInstrumentationMappingIsCreated.

@Test
public void testInstrumentationMappingIsCreated() {
    CompilerOptions options = createCompilerOptions();
    declareIstArrExtern();
    String source = lines(// 
    "function foo() { ", "   console.log('Hello');", "}");
    Compiler compiledSourceCode = compile(options, source);
    VariableMap variableParamMap = compiledSourceCode.getInstrumentationMapping();
    ImmutableMap<String, String> paramMap = variableParamMap.getOriginalNameToNewNameMap();
    assertThat(paramMap).hasSize(4);
    assertWithMessage("FunctionNames in the parameter mapping are not properly set").that(paramMap.get(" FunctionNames")).isEqualTo("[\"foo\"]");
    assertWithMessage("FileNames in the parameter mapping are not properly set").that(paramMap.get(" FileNames")).isEqualTo("[\"i0.js\"]");
    assertWithMessage("Types in the parameter mapping are not properly set").that(paramMap.get(" Types")).isEqualTo("[\"FUNCTION\"]");
    assertWithMessage("Array index encoding is not performed properly").that(paramMap.get("C")).isEqualTo("AAACA");
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) VariableMap(com.google.javascript.jscomp.VariableMap) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) Test(org.junit.Test)

Example 64 with Compiler

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

the class IntegrationTestCase method test.

/**
 * Asserts that when compiling with the given compiler options, there is an error or warning.
 */
protected void test(CompilerOptions options, String[] original, String[] compiled, DiagnosticGroup warnings) {
    Compiler compiler = compile(options, original);
    checkUnexpectedErrorsOrWarnings(compiler, 1);
    if (compiled != null) {
        Node root = compiler.getRoot().getLastChild();
        Node expectedRoot = parseExpectedCode(compiled, options);
        assertNode(root).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
    }
    DiagnosticType diagnostic;
    if (compiler.getErrorCount() == 1) {
        diagnostic = compiler.getErrors().get(0).getType();
    } else {
        diagnostic = compiler.getWarnings().get(0).getType();
    }
    assertWithMessage("Error not in expected diagnostic group. Error: " + diagnostic.key + "\nExpected group: " + warnings).that(warnings.matches(diagnostic)).isTrue();
}
Also used : NoninjectingCompiler(com.google.javascript.jscomp.testing.NoninjectingCompiler) Compiler(com.google.javascript.jscomp.Compiler) DiagnosticType(com.google.javascript.jscomp.DiagnosticType) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node)

Example 65 with Compiler

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

the class IntegrationTestCase method testParseError.

/**
 * Asserts that there is at least one parse error.
 */
protected void testParseError(CompilerOptions options, String original, String compiled) {
    Compiler compiler = compile(options, original);
    for (JSError error : compiler.getErrors()) {
        if (!DiagnosticGroups.PARSING.matches(error)) {
            assertWithMessage("Found unexpected error type " + error.getType() + ":\n" + error).fail();
        }
    }
    assertWithMessage("Unexpected warnings: " + Joiner.on("\n").join(compiler.getWarnings())).that(compiler.getWarnings().size()).isEqualTo(0);
    if (compiled != null) {
        Node root = compiler.getRoot().getLastChild();
        Node expectedRoot = parseExpectedCode(new String[] { compiled }, options);
        assertNode(root).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
    }
}
Also used : NoninjectingCompiler(com.google.javascript.jscomp.testing.NoninjectingCompiler) Compiler(com.google.javascript.jscomp.Compiler) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) JSError(com.google.javascript.jscomp.JSError)

Aggregations

Compiler (com.google.javascript.jscomp.Compiler)172 Test (org.junit.Test)132 Node (com.google.javascript.rhino.Node)116 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)50 SourceFile (com.google.javascript.jscomp.SourceFile)22 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)16 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)9 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)8 Result (com.google.javascript.jscomp.Result)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 File (java.io.File)4 BlackHoleErrorManager (com.google.javascript.jscomp.BlackHoleErrorManager)3 JSError (com.google.javascript.jscomp.JSError)3 InputId (com.google.javascript.rhino.InputId)3 GwtIncompatible (com.google.common.annotations.GwtIncompatible)2 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)2 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSChunk (com.google.javascript.jscomp.JSChunk)2 JSModule (com.google.javascript.jscomp.JSModule)2