use of com.google.javascript.jscomp.GatherModuleMetadata in project closure-compiler by google.
the class JsFileFullParser method parse.
/**
* Parses a JavaScript file for dependencies and annotations
*
* @return empty info if a syntax error was encountered
*/
public static FileInfo parse(String code, String filename, Reporter reporter) {
DelegatingReporter errorReporter = new DelegatingReporter(reporter);
Compiler compiler = new Compiler(new BasicErrorManager() {
@Override
public void println(CheckLevel level, JSError error) {
if (level == CheckLevel.ERROR) {
errorReporter.error(error.getDescription(), error.getSourceName(), error.getLineNumber(), error.getCharno());
} else if (level == CheckLevel.WARNING) {
errorReporter.warning(error.getDescription(), error.getSourceName(), error.getLineNumber(), error.getCharno());
}
}
@Override
protected void printSummary() {
}
});
SourceFile source = SourceFile.fromCode(filename, code);
CompilerOptions options = new CompilerOptions();
options.setChecksOnly(true);
compiler.init(ImmutableList.of(), ImmutableList.of(source), options);
Config config = ParserRunner.createConfig(// TODO(sdh): ES8 STRICT, with a non-strict fallback - then give warnings.
Config.LanguageMode.ES_NEXT, Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE, Config.RunMode.STOP_AFTER_ERROR, /* extraAnnotationNames */
ImmutableSet.<String>of(), /* parseInlineSourceMaps */
true, Config.StrictMode.SLOPPY);
FileInfo info = new FileInfo();
ParserRunner.ParseResult parsed = ParserRunner.parse(source, code, config, errorReporter);
if (errorReporter.seenError) {
return info;
}
parsed.ast.setInputId(new InputId(filename));
String version = parsed.features.version();
if (!version.equals("es3")) {
info.loadFlags.put("lang", version);
}
for (Comment comment : parsed.comments) {
if (comment.type == Comment.Type.JSDOC) {
parseComment(comment, info);
}
}
GatherModuleMetadata gatherModuleMetadata = new GatherModuleMetadata(compiler, /* processCommonJsModules= */
false, ResolutionMode.BROWSER);
gatherModuleMetadata.process(new Node(Token.ROOT), parsed.ast);
compiler.generateReport();
if (compiler.getModuleMetadataMap().getModulesByPath().size() != 1) {
// Avoid potential crashes due to assumptions of the code below being violated.
return info;
}
ModuleMetadata module = Iterables.getOnlyElement(compiler.getModuleMetadataMap().getModulesByPath().values());
if (module.isEs6Module()) {
info.loadFlags.put("module", "es6");
} else if (module.isGoogModule()) {
info.loadFlags.put("module", "goog");
}
switch(module.moduleType()) {
case GOOG_PROVIDE:
info.moduleType = FileInfo.ModuleType.GOOG_PROVIDE;
break;
case GOOG_MODULE:
case LEGACY_GOOG_MODULE:
info.moduleType = FileInfo.ModuleType.GOOG_MODULE;
break;
case ES6_MODULE:
info.moduleType = FileInfo.ModuleType.ES_MODULE;
break;
case COMMON_JS:
case SCRIPT:
// Treat these as unknown for now; we can extend the enum if we care about these.
info.moduleType = FileInfo.ModuleType.UNKNOWN;
break;
}
info.goog = module.usesClosure();
recordModuleMetadata(info, module);
return info;
}
Aggregations