Search in sources :

Example 6 with Comment

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;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ParserRunner(com.google.javascript.jscomp.parsing.ParserRunner) Config(com.google.javascript.jscomp.parsing.Config) ErrorReporter(com.google.javascript.rhino.ErrorReporter) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) InputId(com.google.javascript.rhino.InputId) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 7 with Comment

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;
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) Node(com.google.javascript.rhino.Node) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 8 with Comment

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;
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment)

Example 9 with Comment

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;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ParserRunner(com.google.javascript.jscomp.parsing.ParserRunner) CheckLevel(com.google.javascript.jscomp.CheckLevel) Config(com.google.javascript.jscomp.parsing.Config) Node(com.google.javascript.rhino.Node) ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) GatherModuleMetadata(com.google.javascript.jscomp.GatherModuleMetadata) JSError(com.google.javascript.jscomp.JSError) GatherModuleMetadata(com.google.javascript.jscomp.GatherModuleMetadata) BasicErrorManager(com.google.javascript.jscomp.BasicErrorManager) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) InputId(com.google.javascript.rhino.InputId) SourceFile(com.google.javascript.jscomp.SourceFile)

Aggregations

Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)9 Node (com.google.javascript.rhino.Node)4 NonJSDocComment (com.google.javascript.rhino.NonJSDocComment)3 Compiler (com.google.javascript.jscomp.Compiler)2 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)2 SourceFile (com.google.javascript.jscomp.SourceFile)2 Config (com.google.javascript.jscomp.parsing.Config)2 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)2 InputId (com.google.javascript.rhino.InputId)2 FormatString (com.google.errorprone.annotations.FormatString)1 BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 GatherModuleMetadata (com.google.javascript.jscomp.GatherModuleMetadata)1 JSError (com.google.javascript.jscomp.JSError)1 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 Parser (com.google.javascript.jscomp.parsing.parser.Parser)1 SourceFile (com.google.javascript.jscomp.parsing.parser.SourceFile)1 ProgramTree (com.google.javascript.jscomp.parsing.parser.trees.ProgramTree)1 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)1