use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.
the class JsfileParser method parse.
/**
* Internal implementation to produce the {@link FileInfo} object.
*/
private static FileInfo parse(String code, String filename, @Nullable Reporter reporter) {
ErrorReporter errorReporter = new DelegatingReporter(reporter);
Compiler compiler = new Compiler();
compiler.init(ImmutableList.<SourceFile>of(), ImmutableList.<SourceFile>of(), new CompilerOptions());
Config config = ParserRunner.createConfig(// TODO(sdh): ES8 STRICT, with a non-strict fallback - then give warnings.
Config.LanguageMode.ECMASCRIPT8, Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE, Config.RunMode.KEEP_GOING, /* extraAnnotationNames */
ImmutableSet.<String>of(), /* parseInlineSourceMaps */
true, Config.StrictMode.SLOPPY);
SourceFile source = SourceFile.fromCode(filename, code);
FileInfo info = new FileInfo(errorReporter);
ParserRunner.ParseResult parsed = ParserRunner.parse(source, code, config, errorReporter);
parsed.ast.setInputId(new InputId(filename));
String version = parsed.features.version();
if (!version.equals("es3")) {
info.loadFlags.add(JsArray.of("lang", version));
}
for (Comment comment : parsed.comments) {
if (comment.type == Comment.Type.JSDOC) {
parseComment(comment, info);
}
}
NodeTraversal.traverseEs6(compiler, parsed.ast, new Traverser(info));
return info;
}
use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.
the class IRFactory method transformTree.
public static IRFactory transformTree(ProgramTree tree, StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) {
IRFactory irFactory = new IRFactory(sourceString, sourceFile, config, errorReporter, tree.sourceComments);
// don't call transform as we don't want standard jsdoc handling.
Node n = irFactory.transformDispatcher.process(tree);
irFactory.setSourceInfo(n, tree);
if (tree.sourceComments != null) {
for (Comment comment : tree.sourceComments) {
if ((comment.type == Comment.Type.JSDOC || comment.type == Comment.Type.IMPORTANT) && !irFactory.parsedComments.contains(comment)) {
irFactory.handlePossibleFileOverviewJsDoc(comment);
} else if (comment.type == Comment.Type.BLOCK) {
irFactory.handleBlockComment(comment);
}
}
}
irFactory.setFileOverviewJsDoc(n);
irFactory.validateAll(n);
irFactory.resultNode = n;
return irFactory;
}
use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.
the class IRFactory method getJsDoc.
private Comment getJsDoc(SourceRange location) {
Comment closestPreviousComment = null;
while (hasPendingCommentBefore(location)) {
closestPreviousComment = currentComment;
currentComment = skipNonJsDoc(nextCommentIter);
}
return closestPreviousComment;
}
use of com.google.javascript.jscomp.parsing.parser.trees.Comment 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