use of com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata in project closure-compiler by google.
the class GlobalNamespaceTest method moduleLevelNamesAreCaptured_esExportDecl.
@Test
public void moduleLevelNamesAreCaptured_esExportDecl() {
GlobalNamespace namespace = parseAndGatherModuleData("export const x = 0;");
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByPath().get("test.js");
Name x = namespace.getNameFromModule(metadata, "x");
assertThat(x).isNotNull();
assertNode(x.getDeclaration().getNode().getParent()).hasToken(Token.CONST);
}
use of com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata 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;
}
use of com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata in project closure-compiler by google.
the class GlobalNamespaceTest method moduleLevelNamesAreCaptured_esExportClassDecl.
@Test
public void moduleLevelNamesAreCaptured_esExportClassDecl() {
GlobalNamespace namespace = parseAndGatherModuleData("export class Foo {}");
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByPath().get("test.js");
Name x = namespace.getNameFromModule(metadata, "Foo");
assertThat(x).isNotNull();
assertNode(x.getDeclaration().getNode().getParent()).hasToken(Token.CLASS);
}
use of com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata in project closure-compiler by google.
the class GlobalNamespaceTest method assignToGlobalNameInLoadModule_doesNotCreateModuleName.
@Test
public void assignToGlobalNameInLoadModule_doesNotCreateModuleName() {
GlobalNamespace namespace = parseAndGatherModuleData(lines("class Foo {}", "goog.loadModule(function(exports) {", " goog.module('m');", " Foo.Bar = 0", " return exports;", "});"));
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByGoogNamespace().get("m");
assertThat(namespace.getNameFromModule(metadata, "Foo.Bar")).isNull();
assertThat(namespace.getSlot("Foo.Bar")).isNotNull();
}
use of com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata in project closure-compiler by google.
the class GlobalNamespaceTest method googModule_containsExports_explicitAssign.
@Test
public void googModule_containsExports_explicitAssign() {
GlobalNamespace namespace = parseAndGatherModuleData("goog.module('m'); const x = 0; exports = {x};");
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByGoogNamespace().get("m");
Name exports = namespace.getNameFromModule(metadata, "exports");
assertThat(exports.getGlobalSets()).isEqualTo(1);
assertThat(namespace.getNameFromModule(metadata, "x").getGlobalSets()).isEqualTo(1);
}
Aggregations