Search in sources :

Example 1 with ErrorManager

use of com.google.javascript.jscomp.ErrorManager in project divolte-collector by divolte.

the class JavaScriptResource method compile.

private static Compiler compile(final String filename, final InputStream javascript, final ImmutableMap<String, Object> scriptConstants, final boolean debugMode) throws IOException {
    final CompilerOptions options = new CompilerOptions();
    COMPILATION_LEVEL.setOptionsForCompilationLevel(options);
    COMPILATION_LEVEL.setTypeBasedOptimizationOptions(options);
    options.setEnvironment(CompilerOptions.Environment.BROWSER);
    options.setLanguageIn(ECMASCRIPT5_STRICT);
    options.setLanguageOut(ECMASCRIPT5_STRICT);
    WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
    // can be related more easily to the original JavaScript source.
    if (debugMode) {
        options.setPrettyPrint(true);
        COMPILATION_LEVEL.setDebugOptionsForCompilationLevel(options);
    }
    options.setDefineReplacements(scriptConstants);
    final SourceFile source = SourceFile.fromInputStream(filename, javascript, StandardCharsets.UTF_8);
    final Compiler compiler = new Compiler();
    final ErrorManager errorManager = new Slf4jErrorManager(compiler);
    compiler.setErrorManager(errorManager);
    // TODO: Use an explicit list of externs instead of the default browser set, to control compatibility.
    final List<SourceFile> externs = CommandLineRunner.getBuiltinExterns(options.getEnvironment());
    compiler.compile(externs, ImmutableList.of(source), options);
    return compiler;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) ErrorManager(com.google.javascript.jscomp.ErrorManager) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 2 with ErrorManager

use of com.google.javascript.jscomp.ErrorManager in project ow by vtst.

the class ClosureBuilder method compileJavaScriptFile.

/**
 * Compile a JavaScript file.
 */
private void compileJavaScriptFile(IFile file, CompilerOptions options, boolean fileIsOpen, boolean stripIncludedFiles, boolean doNotKeepCompilationResultsOfClosedFilesInMemory, boolean force) throws CoreException {
    OwJsDev.log("Compiling %s", file.getFullPath().toOSString());
    CompilableJSUnit unit = ResourceProperties.getJSUnit(file);
    if (unit == null)
        return;
    ErrorManager errorManager = new ErrorManagerForFileBuild(unit, file);
    boolean keepCompilationResultsInMemory = !doNotKeepCompilationResultsOfClosedFilesInMemory || fileIsOpen;
    boolean force2 = force || editorRegistry.isNewlyOpenedFile(file);
    CompilerRun run = unit.fullCompile(options, errorManager, keepCompilationResultsInMemory, stripIncludedFiles, force2);
    run.setErrorManager(new NullErrorManager());
}
Also used : ErrorManager(com.google.javascript.jscomp.ErrorManager) NullErrorManager(net.vtst.ow.closure.compiler.util.NullErrorManager) CompilerRun(net.vtst.ow.closure.compiler.compile.CompilerRun) CompilableJSUnit(net.vtst.ow.closure.compiler.compile.CompilableJSUnit) NullErrorManager(net.vtst.ow.closure.compiler.util.NullErrorManager)

Example 3 with ErrorManager

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

the class DefaultDependencyResolver method parseRequires.

/**
 * Parses a block of code for goog.require statements and extracts the required symbols.
 */
private static Collection<String> parseRequires(String code, boolean addClosureBase) {
    ErrorManager errorManager = new LoggerErrorManager(logger);
    JsFileParser parser = new JsFileParser(errorManager);
    DependencyInfo deps = parser.parseFile("<unknown path>", "<unknown path>", code);
    List<String> requires = new ArrayList<>();
    if (addClosureBase) {
        requires.add(CLOSURE_BASE_PROVIDE);
    }
    requires.addAll(deps.getRequiredSymbols());
    errorManager.generateReport();
    return requires;
}
Also used : LoggerErrorManager(com.google.javascript.jscomp.LoggerErrorManager) ErrorManager(com.google.javascript.jscomp.ErrorManager) ArrayList(java.util.ArrayList) LoggerErrorManager(com.google.javascript.jscomp.LoggerErrorManager)

Example 4 with ErrorManager

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

the class DependencyFile method loadGraph.

/**
 * Loads the dependency graph.
 */
private void loadGraph() throws ServiceException {
    dependencies.clear();
    logger.info("Loading dependency graph");
    // Parse the deps.js file.
    ErrorManager errorManager = new LoggerErrorManager(logger);
    DepsFileParser parser = new DepsFileParser(errorManager);
    List<DependencyInfo> depInfos = parser.parseFile(getName(), getContent());
    // Ensure the parse succeeded.
    if (!parser.didParseSucceed()) {
        throw new ServiceException("Problem parsing " + getName() + ". See logs for details.");
    }
    // Incorporate the dependencies into our maps.
    for (DependencyInfo depInfo : depInfos) {
        for (String provide : depInfo.getProvides()) {
            DependencyInfo existing = dependencies.get(provide);
            if (existing != null && !existing.equals(depInfo)) {
                throw new ServiceException("Duplicate provide of " + provide + ". Was provided by " + existing.getPathRelativeToClosureBase() + " and " + depInfo.getPathRelativeToClosureBase());
            }
            dependencies.put(provide, depInfo);
        }
    }
    List<String> provides = new ArrayList<>();
    provides.add(CLOSURE_BASE_PROVIDE);
    // Add implicit base.js entry.
    dependencies.put(CLOSURE_BASE_PROVIDE, SimpleDependencyInfo.builder(CLOSURE_BASE, CLOSURE_BASE).setProvides(provides).build());
    errorManager.generateReport();
    logger.info("Dependencies loaded");
}
Also used : LoggerErrorManager(com.google.javascript.jscomp.LoggerErrorManager) ErrorManager(com.google.javascript.jscomp.ErrorManager) ArrayList(java.util.ArrayList) LoggerErrorManager(com.google.javascript.jscomp.LoggerErrorManager)

Example 5 with ErrorManager

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

the class RefasterJs method doMain.

private void doMain(String[] args) throws Exception {
    CmdLineParser parser = new CmdLineParser(this);
    parser.parseArgument(args);
    if (args.length < 1 || displayHelp) {
        CmdLineParser p = new CmdLineParser(this);
        p.printUsage(System.out);
        return;
    }
    checkArgument(!Strings.isNullOrEmpty(refasterJsTemplate), "--refasterjs_template must be provided");
    List<String> fileInputs = getInputs();
    checkArgument(!fileInputs.isEmpty(), "At least one input must be provided in the --inputs flag.");
    for (String input : fileInputs) {
        Preconditions.checkArgument(new File(input).exists(), "Input file %s does not exist.", input);
    }
    if (!verbose) {
        // This is done here instead of using the Compiler#setLoggingLevel function since the
        // Compiler is created and then run inside of RefactoringDriver.
        Logger errorManagerLogger = Logger.getLogger("com.google.javascript.jscomp");
        errorManagerLogger.setLevel(Level.OFF);
    }
    RefasterJsScanner scanner = new RefasterJsScanner();
    scanner.setTypeMatchingStrategy(typeMatchingStrategy);
    scanner.loadRefasterJsTemplate(refasterJsTemplate);
    CompilerOptions options = new CompilerOptions();
    options.setEnvironment(environment);
    RefactoringDriver driver = new RefactoringDriver.Builder().addExterns(CommandLineRunner.getBuiltinExterns(environment)).addExternsFromFile(getExterns()).addInputsFromFile(fileInputs).build();
    System.out.println("Compiling JavaScript code and searching for suggested fixes.");
    // TODO(bangert): allow picking a non-default choice in RefasterJS, e.g. via a switch.
    List<SuggestedFix> fixes = driver.drive(scanner);
    if (!verbose) {
        // When running in quiet mode, the Compiler's error manager will not have printed
        // this information itself.
        ErrorManager errorManager = driver.getCompiler().getErrorManager();
        System.out.println("Compiler results: " + errorManager.getErrorCount() + " errors and " + errorManager.getWarningCount() + " warnings.");
    }
    System.out.println("Found " + fixes.size() + " suggested fixes.");
    if (dryRun) {
        if (!fixes.isEmpty()) {
            System.out.println("SuggestedFixes: " + fixes);
        }
    } else {
        Set<String> affectedFiles = new TreeSet<>();
        for (SuggestedFix fix : fixes) {
            affectedFiles.addAll(fix.getReplacements().keySet());
        }
        System.out.println("Modifying affected files: " + affectedFiles);
        ApplySuggestedFixes.applySuggestedFixesToFiles(fixes);
    }
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) ErrorManager(com.google.javascript.jscomp.ErrorManager) Logger(java.util.logging.Logger) TreeSet(java.util.TreeSet) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) File(java.io.File)

Aggregations

ErrorManager (com.google.javascript.jscomp.ErrorManager)5 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)2 LoggerErrorManager (com.google.javascript.jscomp.LoggerErrorManager)2 ArrayList (java.util.ArrayList)2 Compiler (com.google.javascript.jscomp.Compiler)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 File (java.io.File)1 TreeSet (java.util.TreeSet)1 Logger (java.util.logging.Logger)1 CompilableJSUnit (net.vtst.ow.closure.compiler.compile.CompilableJSUnit)1 CompilerRun (net.vtst.ow.closure.compiler.compile.CompilerRun)1 NullErrorManager (net.vtst.ow.closure.compiler.util.NullErrorManager)1 CmdLineParser (org.kohsuke.args4j.CmdLineParser)1