Package com.google.javascript.jscomp

Examples of com.google.javascript.jscomp.Compiler


  @Test
  public void testInsertArguments_notFirstArgument() {
    String before = "goog.dom.classes.add(foo, ";
    String after = "bar);";
    Compiler compiler = getCompiler(before + after);
    Node root = compileToScriptRoot(compiler);
    SuggestedFix fix = new SuggestedFix.Builder()
        .insertArguments(root.getFirstChild().getFirstChild(), 1, "baz")
        .build();
    CodeReplacement replacement = new CodeReplacement(before.length(), 0, "baz, ");
View Full Code Here


  @Test
  public void testInsertArguments_lastArgument() {
    String before = "goog.dom.classes.add(foo, bar";
    String after = ");";
    Compiler compiler = getCompiler(before + after);
    Node root = compileToScriptRoot(compiler);
    SuggestedFix fix = new SuggestedFix.Builder()
        .insertArguments(root.getFirstChild().getFirstChild(), 2, "baz")
        .build();
    CodeReplacement replacement = new CodeReplacement(before.length(), 0, ", baz");
View Full Code Here

        "goog.provide('js.Foo');\n"
        + "goog.require('abc.def');\n"
        + "\n"
        + "/** @private */\n"
        + "function foo_() {};\n";
    Compiler compiler = getCompiler(input);
    Node root = compileToScriptRoot(compiler);
    Match match = new Match(root.getFirstChild(), new NodeMetadata(compiler));
    SuggestedFix fix = new SuggestedFix.Builder()
        .addGoogRequire(match, "abc.def")
        .build();
View Full Code Here

    SetMultimap<String, CodeReplacement> replacementMap = fix.getReplacements();
    assertEquals(0, replacementMap.size());
  }

  private void assertAddGoogRequire(String before, String after, String namespace) {
    Compiler compiler = getCompiler(before + after);
    Node root = compileToScriptRoot(compiler);
    Match match = new Match(root.getFirstChild(), new NodeMetadata(compiler));
    SuggestedFix fix = new SuggestedFix.Builder()
        .addGoogRequire(match, namespace)
        .build();
View Full Code Here

    }
  }

  public static Result compile(String program, int num) {
    SourceFile input = SourceFile.fromCode(""+num, program);
    Compiler compiler = new Compiler();
    Result result = compiler.compile(extern, input, options);
    return result;
  }
View Full Code Here

    }

    Compiler.setLoggingLevel(Level.OFF);

    CompilerOptions options = createCompilerOptions();
    Compiler compiler = createCompiler(options);

    List<SourceFile> externs = findExternFiles();
    List<SourceFile> sources = findSourceFiles();

    if (isStale() || forceRecompile) {
      log("Compiling " + sources.size() + " file(s) with " +
          externs.size() + " extern(s)");

      Result result = compiler.compile(externs, sources, options);

      if (result.success) {
        StringBuilder source = new StringBuilder(compiler.toSource());

        if (this.outputWrapperFile != null) {
          try {
            this.outputWrapper = Files.toString(this.outputWrapperFile, UTF_8);
          } catch (Exception e) {
View Full Code Here

    return success;
  }

  private Compiler createCompiler(CompilerOptions options) {
    Compiler compiler = new Compiler();
    MessageFormatter formatter =
        options.errorFormat.toFormatter(compiler, false);
    AntErrorManager errorManager = new AntErrorManager(formatter, this);
    compiler.setErrorManager(errorManager);
    return compiler;
  }
View Full Code Here

*/
public class Compressor {

    public static String compile(List<FragmentDescriptor> scripts, IRequestProxy request,
                                 CompressorSettings settings) throws JSCompileException {
        Compiler compiler = new Compiler();

        CompilerOptions options = new CompilerOptions();
        options.markAsCompiled = true;
        if (settings.getLocale() != null)
            options.locale = settings.getLocale();
        options.prettyPrint = settings.isFormatPrettyPrint();
        options.printInputDelimiter = settings.isFormatPrintInputDelimiter();
        if (settings.getOptimization() != null) {
            if (settings.getOptimization().equalsIgnoreCase(CompressorSettings.ADVANCED_OPTIMIZATIONS_VALUE))
                CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
            else if (settings.getOptimization().equalsIgnoreCase(CompressorSettings.WHITESPACE_ONLY_VALUE))
                CompilationLevel.WHITESPACE_ONLY.setOptionsForCompilationLevel(options);
            else CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
        }

        List<JSSourceFile> sources = new ArrayList<JSSourceFile>();
        for (FragmentDescriptor sd : scripts) {
            JSSourceFile input;
            if (sd instanceof ExternalFragment)
                input = JSSourceFile.fromFile(request.getRealPath(((ExternalFragment) sd).getFilePath()));
            else
                input = JSSourceFile.fromCode("fragment" + Integer.toString(scripts.indexOf(sd)),
                        ((InternalFragment) sd).getText());
            sources.add(input);
        }

        Result res = compiler.compile(new JSSourceFile[]{}, sources.toArray(new JSSourceFile[]{}), options);
        if (!res.success)
            throw new JSCompileException();
        String licenses = "";
        try {
            licenses = getLicenses(scripts, settings, request);
        } catch (IOException e) {
            throw new JSCompileException(e);
        }

        // The compiler is responsible for generating the compiled code; it is not
        // accessible via the Result.
        if (settings.getOutputWrapper() == null)
            return licenses + compiler.toSource();
        else
            return licenses + settings.getOutputWrapper().replace(settings.getOutputWrapperMarker(),
                    compiler.toSource());
    }
View Full Code Here

            }
            log.info("Compiler Options:" + compilerOptions);

            compilationLevel.setOptionsForCompilationLevel(compilerOptions);

            Compiler compiler = new Compiler();
            Result result = compiler.compile(externs, source, compilerOptions);

            // TODO: should log results to a file if desired.
            for (JSError warning : result.warnings) {
                getLog().warn(warning.toString());
            }

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

            if (!result.success) {
                // TODO: better info?
                throw new MojoFailureException("Compilation failure");
            }

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

        CompilerOptions options = new CompilerOptions();
        options.setCodingConvention(new ClosureCodingConvention());
        options.setOutputCharset(OUTPUT_CHARSET);
        options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);

        Compiler compiler = new Compiler();

        compiler.disableThreads();

        SourceFile input = SourceFile.fromInputStream(resource.toString(), resource.openStream());

        List<SourceFile> inputs = Collections.singletonList(input);

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

        if (result.success)
        {
            return IOUtils.toInputStream(compiler.toSource(), OUTPUT_CHARSET);
        }

        throw new RuntimeException(String.format("Compilation failed: %s.",
                InternalUtils.join(CollectionFactory.newList(result.errors), ";")));
    }
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.