Package com.google.javascript.jscomp

Examples of com.google.javascript.jscomp.Compiler


  }

  @Test
  public void testJsDocType() {
    String input = "/** @type {number} */ var foo = 1;";
    Compiler compiler = getCompiler(input);
    Node root = compileToScriptRoot(compiler);
    Node node = root.getFirstChild().getFirstChild();
    assertTrue(Matchers.jsDocType("number").matches(node, new NodeMetadata(compiler)));
    assertFalse(Matchers.jsDocType("string").matches(node, new NodeMetadata(compiler)));
  }
View Full Code Here


  }

  @Test
  public void testPropertyAccess() {
    String input = "foo.bar.method();";
    Compiler compiler = getCompiler(input);
    Node root = compileToScriptRoot(compiler);
    Node methodNode = root.getFirstChild().getFirstChild().getFirstChild();
    Node barNode = methodNode.getFirstChild();
    assertTrue(Matchers.propertyAccess().matches(methodNode, new NodeMetadata(compiler)));
    assertTrue(Matchers.propertyAccess().matches(barNode, new NodeMetadata(compiler)));
View Full Code Here

    String externs = ""
        + "/** @constructor */\n"
        + "function Foo() {};\n"
        + "Foo.prototype.bar = 3;\n";
    String input = "var foo = new Foo(); foo.bar;";
    Compiler compiler = getCompiler(externs, input);
    Node root = compileToScriptRoot(compiler);
    Node node = root.getLastChild().getFirstChild();
    assertTrue(Matchers.propertyAccess().matches(node, new NodeMetadata(compiler)));
    assertTrue(Matchers.propertyAccess("Foo.prototype.bar").matches(
          node, new NodeMetadata(compiler)));
View Full Code Here

  private Compiler getCompiler(String jsInput) {
    return getCompiler("", jsInput);
  }

  private Compiler getCompiler(String externs, String jsInput) {
    Compiler compiler = new Compiler();
    compiler.disableThreads();
    CompilerOptions options = RefactoringDriver.getCompilerOptions();
    compiler.compile(
        ImmutableList.of(SourceFile.fromCode("externs", externs)),
        ImmutableList.of(SourceFile.fromCode("test", jsInput)),
        options);
    return compiler;
  }
View Full Code Here

    CompilerInput input = new CompilerInput(new SyntheticAst(script));
    JSModule jsModule = new JSModule("fuzzedModule");
    jsModule.add(input);

    Compiler.setLoggingLevel(level.getLevel());
    Compiler compiler = new Compiler();
    compiler.setTimeout(30);
    compiler.disableThreads();
    return compiler.compileModules(
        CommandLineRunner.getDefaultExterns(),
        Arrays.asList(jsModule), getOptions());
  }
View Full Code Here

    return options;
  }

  protected RunResult compile(
      String js1, String fileName1, String js2, String fileName2) {
    Compiler compiler = new Compiler();
    CompilerOptions options = getCompilerOptions();

    // Turn on IDE mode to get rid of optimizations.
    options.ideMode = true;

    List<SourceFile> inputs =
        ImmutableList.of(SourceFile.fromCode(fileName1, js1));

    if (js2 != null && fileName2 != null) {
      inputs = ImmutableList.of(
          SourceFile.fromCode(fileName1, js1),
          SourceFile.fromCode(fileName2, js2));
    }

    Result result = compiler.compile(EXTERNS, inputs, options);

    assertTrue("compilation failed", result.success);
    String source = compiler.toSource();

    StringBuilder sb = new StringBuilder();
    try {
      result.sourceMap.validate(true);
      result.sourceMap.appendTo(sb, "testcode");
View Full Code Here

  }

  private void compile(final Reader reader, final Writer writer,
      final Map<String, Object> options) throws IOException {
    Compiler.setLoggingLevel(Level.SEVERE);
    final Compiler compiler = new Compiler(new PrintStream(
        LOGGER_OUTPUT_STREAM, false, "UTF-8"));
    final CompilerOptions compilerOptions = new CompilerOptions();
    CompilationLevel.SIMPLE_OPTIMIZATIONS
        .setOptionsForCompilationLevel(compilerOptions);
    compilerOptions.setCodingConvention(new ClosureCodingConvention());
    if (isSourceMappingEnabled(options)) {
      compilerOptions.setSourceMapFormat(Format.V3);
      compilerOptions.setSourceMapDetailLevel(DetailLevel.ALL);
    }
    setupOptions(compilerOptions, options);
    compiler.initOptions(compilerOptions);

    final Result result = compiler.compile(SourceFile.fromCode("externs", ""),
        SourceFile.fromReader("source.js", reader), compilerOptions);
    if (result.success) {
      writer.write(compiler.toSource());
    } else {
      if (result.errors.length > 0) {
        throw new SmallerException("Closure Failed: "
            + result.errors[0].toString());
      }
View Full Code Here

      options.setWarningLevel(dg, CheckLevel.ERROR);
    }

    options.setCodingConvention(new GoogleCodingConvention());

    Compiler compiler = new Compiler();
    MessageFormatter formatter =
        options.errorFormat.toFormatter(compiler, false);
    AntErrorManager errorManager = new AntErrorManager(formatter, task);
    compiler.setErrorManager(errorManager);

    Result r = compiler.compile(externs, jsInputs, options);
    if (!r.success) {
      return null;
    }

    String wrapped = "(function(){" + compiler.toSource() + "})();\n";
    return wrapped;
  }
View Full Code Here

  }

  public void compile(OutputStream outputStream)
      throws IOException, JavascriptCompilationException {
    PrintStream printStream = new PrintStream(outputStream);
    Compiler compiler = new Compiler(System.err);

    List<JSSourceFile> inputs = filesToJsSourceFiles(inputFiles);
    List<JSSourceFile> externs = CommandLineRunner.getDefaultExterns();
    externs.addAll(filesToJsSourceFiles(externFiles));

    compilerOptions.setManageClosureDependencies(entryPoints);

    Result result = compiler.compile(externs, inputs, compilerOptions);

    if (result.success) {
      printStream.println(compiler.toSource());
    } else {
      for (JSError error : result.errors) {
        printError(printStream, "ERROR",  error);
      }
      for (JSError warning : result.warnings) {
View Full Code Here

             */
            getLog().info("Skipping JavaScript compilation. The compiled JavaScript file is up to date.");
            return;
        }

        Compiler compiler = new Compiler();
        compiler.setLoggingLevel(Level.WARNING);
        Result result = compiler.compile(new ArrayList<JSSourceFile>(), sources, compilerOptions);

        for (JSError warning : result.warnings) {
            getLog().warn(warning.toString());
        }

        for (JSError error : result.errors) {
            getLog().error(error.toString());
        }

        if (!result.success) {
            throw new MojoFailureException("Compilation failure");
        }

        try {
            Files.createParentDirs(outputFile);
            Files.touch(outputFile);
            Files.write(compiler.toSource(), outputFile, Charsets.UTF_8);
        } catch (IOException e) {
            throw new MojoFailureException(outputFile != null ? outputFile.toString() : e.getMessage());
        }
    }
View Full Code Here

TOP

Related Classes of com.google.javascript.jscomp.Compiler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.