Search in sources :

Example 1 with GatherModuleMetadata

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;
}
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

BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 Compiler (com.google.javascript.jscomp.Compiler)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 GatherModuleMetadata (com.google.javascript.jscomp.GatherModuleMetadata)1 JSError (com.google.javascript.jscomp.JSError)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)1 Config (com.google.javascript.jscomp.parsing.Config)1 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 InputId (com.google.javascript.rhino.InputId)1 Node (com.google.javascript.rhino.Node)1