Пример #1
0
 protected CompilerOptions getCompilerOptions() {
   CompilerOptions options = new CompilerOptions();
   options.sourceMapOutputPath = "testcode_source_map.out";
   options.sourceMapFormat = getSourceMapFormat();
   options.sourceMapDetailLevel = detailLevel;
   return options;
 }
Пример #2
0
  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;

    JSSourceFile[] inputs = {JSSourceFile.fromCode(fileName1, js1)};

    if (js2 != null && fileName2 != null) {
      JSSourceFile[] multiple = {
        JSSourceFile.fromCode(fileName1, js1), JSSourceFile.fromCode(fileName2, js2)
      };
      inputs = multiple;
    }

    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");
    } catch (IOException e) {
      throw new RuntimeException("unexpected exception", e);
    }

    RunResult rr = new RunResult();
    rr.generatedSource = source;
    rr.sourceMap = result.sourceMap;
    rr.sourceMapFileContent = sb.toString();
    return rr;
  }
 @Override
 void apply(CompilerOptions options, boolean value) {
   if (value) {
     options.setSyntheticBlockStartMarker("start");
     options.setSyntheticBlockEndMarker("end");
   } else {
     options.setSyntheticBlockStartMarker(null);
     options.setSyntheticBlockEndMarker(null);
   }
 }
Пример #4
0
  private CompilerOptions createCompilerOptions() {
    CompilerOptions options = new CompilerOptions();

    if (this.debugOptions) {
      this.compilationLevel.setDebugOptionsForCompilationLevel(options);
    } else {
      this.compilationLevel.setOptionsForCompilationLevel(options);
    }

    this.warningLevel.setOptionsForWarningLevel(options);
    options.setManageClosureDependencies(manageDependencies);
    return options;
  }
Пример #5
0
  /** core method */
  private void writeJsTo(Writer writer) throws IOException {
    Compiler compiler;
    CompilerOptions options;
    List<SourceFile> externals;
    List<SourceFile> sources;
    Result result;
    boolean first;
    int i;
    Node node;

    compiler = new Compiler(new LoggerErrorManager());
    options = new CompilerOptions();
    options.setOutputCharset(Engine.ENCODING);
    sources = new ArrayList<>();
    externals = new ArrayList<>();
    for (i = 0; i < nodes.size(); i++) {
      node = nodes.get(i);
      sources.add(
          SourceFile.fromCode(
              location(node) + /* to get unique names, which is checked by the compiler: */ "_" + i,
              readString(node)));
    }
    result = compiler.compile(externals, sources, options);
    if (!result.success) {
      if (result.errors.length < 1) {
        throw new IllegalStateException();
      }
      throw new IOException(
          result.errors[0].sourceName
              + ":"
              + result.errors[0].lineNumber
              + ":"
              + result.errors[0].description);
    }
    if (overallMinimize) {
      writer.write(compiler.toSource());
    } else {
      first = true;
      for (SourceFile source : sources) {
        if (first) {
          first = false;
        } else {
          writer.write(LF);
        }
        if (!overallMinimize) {
          writer.write(type.comment(source.getName()));
        }
        writer.write(source.getCode());
      }
    }
  }
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setLanguageOut(
       value
           ? CompilerOptions.LanguageMode.ECMASCRIPT5
           : CompilerOptions.LanguageMode.NO_TRANSPILE);
 }
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setLanguageIn(
       value
           ? CompilerOptions.LanguageMode.ECMASCRIPT6
           : CompilerOptions.LanguageMode.ECMASCRIPT5);
 }
 @Override
 void apply(CompilerOptions options, boolean value) {
   if (value) {
     for (String groupName : new DiagnosticGroups().getRegisteredGroups().keySet()) {
       options.setWarningLevel(groupName, CheckLevel.WARNING);
     }
   }
 }
 private String compile(HtmlLibrary library, ClosureOptimizationLevel opt, InputStream js)
     throws IOException {
   CompilationLevel compilationLevel = opt.toCompilationLevel();
   if (null == compilationLevel) {
     // return original input
     return IOUtils.toString(js);
   }
   SourceFile input = SourceFile.fromInputStream(getLibraryName(library), js);
   // TODO externs not supported, should avoid ADVANCED compilation
   SourceFile extern = SourceFile.fromCode("TODO", StringUtils.EMPTY);
   CompilerOptions options = new CompilerOptions();
   compilationLevel.setOptionsForCompilationLevel(options);
   // ES5 assumption to allow getters/setters
   options.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT5);
   Compiler compiler = new Compiler();
   compiler.compile(extern, input, options);
   return compiler.toSource();
 }
 public void handle(CompilerOptions compilerOptions) {
   compilerOptions.setCheckEventfulObjectDisposalPolicy(AGGRESSIVE);
   compilerOptions.assumeStrictThis();
   compilerOptions.setCheckMissingReturn(CheckLevel.ERROR);
   compilerOptions.setAggressiveVarCheck(CheckLevel.ERROR);
   compilerOptions.setCheckDeterminism(true);
   compilerOptions.setBrokenClosureRequiresLevel(CheckLevel.ERROR);
   compilerOptions.setCheckTypes(true);
   compilerOptions.setInferConst(true);
   CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(compilerOptions);
   CompilationLevel.ADVANCED_OPTIMIZATIONS.setTypeBasedOptimizationOptions(compilerOptions);
 }
 /** Converts {@code <entrypoint/>} nested elements into Compiler entrypoint replacements. */
 private void convertEntryPointParameters(CompilerOptions options) {
   List<String> entryPoints = Lists.newLinkedList();
   for (Parameter p : entryPointParams) {
     String key = p.getName();
     entryPoints.add(key);
   }
   if (this.manageDependencies) {
     options.setManageClosureDependencies(entryPoints);
   }
 }
  /**
   * Maps Ant-style values (e.g., from Properties) into expected Closure {@code @define} literals
   *
   * @return True if the {@code @define} replacement succeeded, false if the variable's value could
   *     not be mapped properly.
   */
  private boolean setDefine(CompilerOptions options, String key, Object value) {
    boolean success = false;

    if (value instanceof String) {
      final boolean isTrue = "true".equals(value);
      final boolean isFalse = "false".equals(value);

      if (isTrue || isFalse) {
        options.setDefineToBooleanLiteral(key, isTrue);
      } else {
        try {
          double dblTemp = Double.parseDouble((String) value);
          options.setDefineToDoubleLiteral(key, dblTemp);
        } catch (NumberFormatException nfe) {
          // Not a number, assume string
          options.setDefineToStringLiteral(key, (String) value);
        }
      }

      success = true;
    } else if (value instanceof Boolean) {
      options.setDefineToBooleanLiteral(key, (Boolean) value);
      success = true;
    } else if (value instanceof Integer) {
      options.setDefineToNumberLiteral(key, (Integer) value);
      success = true;
    } else if (value instanceof Double) {
      options.setDefineToDoubleLiteral(key, (Double) value);
      success = true;
    }

    return success;
  }
Пример #13
0
  @VisibleForTesting
  static CompilerOptions getCompilerOptions() {
    CompilerOptions options = new CompilerOptions();

    DependencyOptions deps = new DependencyOptions();
    deps.setDependencySorting(true);
    options.setDependencyOptions(deps);

    options.setIdeMode(true);
    options.setCheckSuspiciousCode(true);
    options.setCheckSymbols(true);
    options.setCheckTypes(true);
    options.setClosurePass(true);
    options.setPreserveGoogRequires(true);

    options.setWarningLevel(DiagnosticGroups.MISSING_REQUIRE, CheckLevel.ERROR);

    return options;
  }
Пример #14
0
  /**
   * Add all the check pass that are possibly relevant to a non googler.
   *
   * @param options The CompilerOptions object to set the options on.
   */
  private static void addVerboseWarnings(CompilerOptions options) {
    addDefaultWarnings(options);

    // checkSuspiciousCode needs to be enabled for CheckGlobalThis to get run.
    options.checkSuspiciousCode = true;
    options.checkGlobalThisLevel = CheckLevel.WARNING;
    options.checkSymbols = true;
    options.checkMissingReturn = CheckLevel.WARNING;

    // checkTypes has the side-effect of asserting that the
    // correct number of arguments are passed to a function.
    // Because the CodingConvention used with the web service does not provide a
    // way for optional arguments to be specified, these warnings may result in
    // false positives.
    options.checkTypes = true;
    options.checkGlobalNamesLevel = CheckLevel.WARNING;
    options.aggressiveVarCheck = CheckLevel.WARNING;
    options.setWarningLevel(DiagnosticGroups.MISSING_PROPERTIES, CheckLevel.WARNING);
    options.setWarningLevel(DiagnosticGroups.DEPRECATED, CheckLevel.WARNING);
  }
Пример #15
0
  /**
   * Minimize the output using google closure compiler.
   *
   * @param output The input file to minimize.
   * @throws IOException If something goes wrong.
   * @throws MojoFailureException If something goes wrong.
   */
  private void minimize(final File output) throws IOException, MojoFailureException {
    final CompilerOptions options = new CompilerOptions();
    options.setCodingConvention(new ClosureCodingConvention());
    options.setOutputCharset("UTF-8");
    options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
    CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

    Compiler.setLoggingLevel(Level.SEVERE);
    Compiler compiler = new Compiler();
    compiler.disableThreads();
    compiler.initOptions(options);

    Result result =
        compiler.compile(
            Collections.<SourceFile>emptyList(),
            Arrays.asList(SourceFile.fromFile(output)),
            options);
    if (result.success) {
      FileUtils.fileWrite(output, compiler.toSource());
    } else {
      JSError[] errors = result.errors;
      throw new MojoFailureException(errors[0].toString());
    }
  }
Пример #16
0
  @VisibleForTesting
  static CompilerOptions createOptions(
      FileSystem fileSystem, TypeRegistry typeRegistry, DossierCompiler compiler) {
    CompilerOptions options = new CompilerOptions();

    options.setCodingConvention(new ClosureCodingConvention());
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setTypeBasedOptimizationOptions(options);

    // IDE mode must be enabled or all of the jsdoc info will be stripped from the AST.
    options.setIdeMode(true);

    // For easier debugging.
    options.setPrettyPrint(true);

    ProvidedSymbolsCollectionPass providedNamespacesPass =
        new ProvidedSymbolsCollectionPass(compiler, typeRegistry, fileSystem);
    options.addCustomPass(CustomPassExecutionTime.BEFORE_CHECKS, providedNamespacesPass);
    options.addCustomPass(
        CustomPassExecutionTime.BEFORE_OPTIMIZATIONS,
        new DocPass(compiler, typeRegistry, fileSystem));

    return options;
  }
Пример #17
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setPolymerPass(value);
 }
Пример #18
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setMoveFunctionDeclarations(value);
 }
Пример #19
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setGenerateExports(value);
 }
Пример #20
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setCheckSymbols(value);
 }
Пример #21
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   if (value) {
     options.setWarningLevel(DiagnosticGroups.LINT_CHECKS, CheckLevel.WARNING);
   }
 }
Пример #22
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setSkipNonTranspilationPasses(value);
 }
Пример #23
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setPrettyPrint(value);
 }
Пример #24
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setClosurePass(value);
 }
Пример #25
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setOptimizeParameters(value);
 }
Пример #26
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setCrossModuleMethodMotion(value);
 }
Пример #27
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setExportLocalPropertyDefinitions(value);
 }
Пример #28
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setMarkNoSideEffectCalls(value);
 }
Пример #29
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setPreserveTypeAnnotations(value);
 }
Пример #30
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setOptimizeReturns(value);
 }