JS: recognize Closure modules files as modules

This commit is contained in:
Asger F
2019-01-22 18:15:43 +00:00
parent e195ac996e
commit f00b16e500
3 changed files with 5 additions and 4 deletions

View File

@@ -560,6 +560,7 @@ public class ASTExtractor {
} else if (sourceType == SourceType.MODULE) {
Label moduleScopeKey = trapwriter.globalID("module;{" + locationManager.getFileLabel() + "}," + locationManager.getStartLine() + "," + locationManager.getStartColumn());
scopeManager.enterScope(3, moduleScopeKey, toplevelLabel);
scopeManager.addVariables("exports"); // needed for Closure modules - spuriously added for ES6 modules
trapwriter.addTuple("isModule", toplevelLabel);
}

View File

@@ -29,8 +29,8 @@ public class JSExtractor {
this.config = config;
}
// heuristic: if `import` or `export` appears at the beginning of a line, it's probably a module
private static final Pattern containsImportOrExport = Pattern.compile("(?m)^([ \t]*)(import|export)\\b");
// heuristic: if `import`, `export`, or `goog.module` appears at the beginning of a line, it's probably a module
private static final Pattern containsModuleIndicator = Pattern.compile("(?m)^([ \t]*)(import|export|goog\\.module)\\b");
public Pair<Label, LoCInfo> extract(TextualExtractor textualExtractor, String source, int toplevelKind, ScopeManager scopeManager) throws ParseError {
// if the file starts with `{ "<string>":` it won't parse as JavaScript; try parsing as JSON instead
@@ -69,7 +69,7 @@ public class JSExtractor {
if (sourceType != SourceType.AUTO)
return sourceType;
if (config.getEcmaVersion().compareTo(ECMAVersion.ECMA2015) >= 0) {
Matcher m = containsImportOrExport.matcher(source);
Matcher m = containsModuleIndicator.matcher(source);
if (m.find() && (allowLeadingWS || m.group(1).isEmpty()))
return SourceType.MODULE;
}

View File

@@ -41,7 +41,7 @@ public class Main {
* such a way that it may produce different tuples for the same file under the same
* {@link ExtractorConfig}.
*/
public static final String EXTRACTOR_VERSION = "2019-01-29";
public static final String EXTRACTOR_VERSION = "2019-02-04";
public static final Pattern NEWLINE = Pattern.compile("\n");