@Override public String process(final String filename, final String source, final Config conf, final ClassLoader loader) throws Exception { final CompilerOptions copts = new CompilerOptions(); copts.setCodingConvention(new ClosureCodingConvention()); copts.setOutputCharset(StandardCharsets.UTF_8); copts.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING); CompilationLevel level = level(get("level")); level.setOptionsForCompilationLevel(copts); Compiler.setLoggingLevel(Level.SEVERE); Compiler compiler = new Compiler(); compiler.disableThreads(); compiler.initOptions(copts); List<SourceFile> externs = externs(copts); Result result = compiler.compile(externs, ImmutableList.of(SourceFile.fromCode(filename, source)), copts); if (result.success) { return compiler.toSource(); } List<AssetProblem> errors = Arrays.stream(result.errors) .map(error -> new AssetProblem(error.sourceName, error.lineNumber, error.getCharno(), error.description, null)) .collect(Collectors.toList()); throw new AssetException(name(), errors); }
private List<SourceFile> externs(final CompilerOptions coptions) throws IOException { List<SourceFile> externs = CommandLineRunner.getBuiltinExterns(coptions.getEnvironment()); List<String> local = get("externs"); if (local != null) { Class<?> loader = getClass(); for (String js : local) { String path = js.startsWith("/") ? js : "/" + js; try (InputStream stream = loader.getResourceAsStream(path)) { if (stream == null) { throw new FileNotFoundException(path); } externs.add(SourceFile.fromInputStream(path, stream, StandardCharsets.UTF_8)); } } } return externs; }
@Override public SourceFile apply(String filename) { return SourceFile.fromFile(filename); } };
if (file.flag == JsSourceType.JS_ZIP) { if (!"-".equals(filename)) { List<SourceFile> newFiles = SourceFile.fromZipFile(filename, inputCharset); inputs.addAll(newFiles); if (jsModuleSpec != null) { SourceFile newFile = SourceFile.fromFile(filename, inputCharset); inputs.add(newFile); } else { inputs.add(SourceFile.fromInputStream("stdin", this.in, inputCharset)); usingStdin = true; inputs.add(SourceFile.fromCode(jsonFile.getPath(), jsonFile.getSrc()));
/** * For a given //# sourceMappingUrl, this locates the appropriate sourcemap on disk. This is use * for sourcemap merging (--apply_input_source_maps) and for error resolution. * * @param parseInlineSourceMaps Whether to parse Base64 encoded source maps. */ static SourceFile extractSourceMap( SourceFile jsFile, String sourceMapURL, boolean parseInlineSourceMaps) { if (parseInlineSourceMaps && sourceMapURL.startsWith(BASE64_URL_PREFIX)) { String extractedString = extractBase64String(sourceMapURL); if (extractedString != null) { return SourceFile.fromCode(jsFile.getName() + ".inline.map", extractedString); } return null; } // TODO(tdeegan): Handle absolute urls here. The compiler needs to come up with a scheme for // properly resolving absolute urls from http:// or the root /some/abs/path/... See b/62544959. if (isAbsolute(sourceMapURL)) { return null; } // If not absolute, its relative. // TODO(tdeegan): Handle urls relative to //# sourceURL. See the sourcemap spec. return getRelativePath(jsFile.getName(), sourceMapURL); }
static void lint(Path path, Compiler compiler) throws IOException { SourceFile file = SourceFile.fromFile(path.toString()); CompilerOptions options = new CompilerOptions(); options.setLanguage(LanguageMode.ECMASCRIPT_NEXT); compiler.setPassConfig(new LintPassConfig(options)); compiler.disableThreads(); SourceFile externs = SourceFile.fromCode("<Linter externs>", ""); compiler.compile(ImmutableList.<SourceFile>of(externs), ImmutableList.of(file), options);
/** * Gets the original location of this sourcemap file on disk. */ public String getOriginalPath() { return sourceFile.getOriginalPath(); } }
String closureRelativePath = PathUtil.makeRelative( closurePathAbs, PathUtil.makeAbsolute(file.getName())); logger.fine("Closure-relative path: " + closureRelativePath); DependencyInfo depInfo = jsParser.parseFile( file.getName(), closureRelativePath, file.getCode()); depInfo = new LazyParsedDependencyInfo(depInfo, new JsAst(file), compiler); file.clearCachedSource(); parsedFiles.put(closureRelativePath, depInfo);
@Override public void setSourceFile(SourceFile file) { checkState(fileName.equals(file.getName())); sourceFile = file; }
/** * Transforms JSON files to a module export that closure compiler can process and keeps track of * any "main" entries in package.json files. */ Map<String, String> processJsonInputs(Iterable<CompilerInput> inputsToProcess) { RewriteJsonToModule rewriteJson = new RewriteJsonToModule(this); for (CompilerInput input : inputsToProcess) { if (!input.getSourceFile().getOriginalPath().endsWith(".json")) { continue; } input.setCompiler(this); try { // JSON objects need wrapped in parens to parse properly input.getSourceFile().setCode("(" + input.getSourceFile().getCode() + ")"); } catch (IOException e) { continue; } Node root = checkNotNull(input.getAstRoot(this)); input.setJsModuleType(CompilerInput.ModuleType.JSON); rewriteJson.process(null, root); } return rewriteJson.getPackageJsonMainEntries(); }
protected String getSourceCode(StaticSourceFile file, int line) { if (file instanceof SourceFile) { String code = ((SourceFile)file).getLine(line); return code; } return "no source line found"; }
ParserRunner.ParseResult result = ParserRunner.parse( sourceFile, sourceFile.getCode(), compiler.getParserConfig(sourceFile.isExtern() ? AbstractCompiler.ConfigContext.EXTERNS : AbstractCompiler.ConfigContext.DEFAULT), compiler.addComments(sourceFile.getName(), result.comments); SourceMapResolver.extractSourceMap(sourceFile, result.sourceMapURL, parseInline); if (sourceMapSourceFile != null) { compiler.addInputSourceMap(sourceFile.getName(), new SourceMapInput(sourceMapSourceFile)); JSError.make(AbstractCompiler.READ_ERROR, sourceFile.getName(), e.getMessage()));
private synchronized void addFilesToSourceMap(Iterable<? extends SourceFile> files) { // synchronized annotation guards concurrent access to sourceMap during parsing. if (getOptions().sourceMapIncludeSourcesContent && getSourceMap() != null) { for (SourceFile file : files) { try { getSourceMap().addSourceFile(file.getName(), file.getCode()); } catch (IOException e) { throw new RuntimeException("Cannot read code of a source map's source file.", e); } } } }
private void findLineOffsets() { if (lineOffsets != null) { return; } try { String[] sourceLines = getCode().split("\n", -1); lineOffsets = new int[sourceLines.length]; for (int ii = 1; ii < sourceLines.length; ++ii) { lineOffsets[ii] = lineOffsets[ii - 1] + sourceLines[ii - 1].length() + 1; } } catch (IOException e) { lineOffsets = new int[1]; lineOffsets[0] = 0; } }
/** * Gets the source map, reading from disk and parsing if necessary. Returns null if the sourcemap * cannot be resolved or is malformed. */ public synchronized @Nullable SourceMapConsumerV3 getSourceMap(ErrorManager errorManager) { if (!cached) { // Avoid re-reading or reparsing files. cached = true; String sourceMapPath = sourceFile.getOriginalPath(); try { String sourceMapContents = sourceFile.getCode(); SourceMapConsumerV3 consumer = new SourceMapConsumerV3(); consumer.parse(sourceMapContents); parsedSourceMap = consumer; } catch (IOException e) { JSError error = JSError.make(SourceMapInput.SOURCEMAP_RESOLVE_FAILED, sourceMapPath, e.getMessage()); errorManager.report(error.getDefaultLevel(), error); } catch (SourceMapParseException e) { JSError error = JSError.make(SourceMapInput.SOURCEMAP_PARSE_FAILED, sourceMapPath, e.getMessage()); errorManager.report(error.getDefaultLevel(), error); } } return parsedSourceMap; }
List<DependencyInfo> depInfos = depsParser.parseFileReader( file.getName(), file.getCodeReader()); if (depInfos.isEmpty()) { reportNoDepsInDepsFile(file.getName()); } else { for (DependencyInfo info : depInfos) { if ((new File(src.getName())).exists() && !shouldSkipDepsFile(src)) { List<DependencyInfo> srcInfos = depsParser.parseFileReader(src.getName(), src.getCodeReader()); for (DependencyInfo info : srcInfos) { depsFiles.put(info.getPathRelativeToClosureBase(), info);
if (file.flag == JsSourceType.JS_ZIP) { if (!"-".equals(filename)) { List<SourceFile> newFiles = SourceFile.fromZipFile(filename, inputCharset); String zipEntryName = zipEntry.getName(); checkState(zipEntryName.contains(filename)); String zipmap = zipEntryName.replace(filename, rootFilename); SourceFile newFile = SourceFile.fromFile(filename, inputCharset, kind); inputs.add(newFile); } else { inputs.add(SourceFile.fromInputStream("stdin", this.in, inputCharset)); usingStdin = true; inputs.add(SourceFile.fromCode(jsonFile.getPath(), jsonFile.getSrc()));
/** * Gets the original location of this sourcemap file on disk. */ public String getOriginalPath() { return sourceFile.getOriginalPath(); } }
String closureRelativePath = PathUtil.makeRelative( closurePathAbs, PathUtil.makeAbsolute(file.getName())); if (logger.isLoggable(Level.FINE)) { logger.fine("Closure-relative path: " + closureRelativePath); DependencyInfo depInfo = jsParser.parseFile( file.getName(), closureRelativePath, file.getCode()); depInfo = new LazyParsedDependencyInfo(depInfo, new JsAst(file), compiler); file.clearCachedSource(); parsedFiles.put(closureRelativePath, depInfo);