JS: address comments

This commit is contained in:
Asger F
2019-01-24 11:43:38 +00:00
parent 9fd4e81f20
commit 6bcae5e7c2
7 changed files with 37 additions and 18 deletions

View File

@@ -306,7 +306,7 @@ public class ASTExtractor {
public V(Platform platform, SourceType sourceType) {
this.platform = platform;
this.sourceType = sourceType;
this.isStrict = sourceType == SourceType.ES6_MODULE || sourceType == SourceType.CLOSURE_MODULE;
this.isStrict = sourceType == SourceType.MODULE || sourceType == SourceType.CLOSURE_MODULE;
}
private Label visit(INode child, Label parent, int childIndex) {
@@ -557,7 +557,7 @@ public class ASTExtractor {
if (!".mjs".equals(locationManager.getSourceFileExtension()))
scopeManager.addVariables("require", "module", "exports", "__filename", "__dirname", "arguments");
trapwriter.addTuple("isModule", toplevelLabel);
} else if (sourceType == SourceType.ES6_MODULE || sourceType == SourceType.CLOSURE_MODULE) {
} else if (sourceType == SourceType.MODULE || sourceType == SourceType.CLOSURE_MODULE) {
Label moduleScopeKey = trapwriter.globalID("module;{" + locationManager.getFileLabel() + "}," + locationManager.getStartLine() + "," + locationManager.getStartColumn());
scopeManager.enterScope(3, moduleScopeKey, toplevelLabel);
if (sourceType == SourceType.CLOSURE_MODULE) {
@@ -572,8 +572,8 @@ public class ASTExtractor {
visitAll(nd.getBody(), toplevelLabel);
// if we're extracting a Node.js/ES2015 module, leave its scope
if (platform == Platform.NODE || sourceType == SourceType.ES6_MODULE || sourceType == SourceType.CLOSURE_MODULE)
// if we're extracting a module, leave its scope
if (platform == Platform.NODE || sourceType == SourceType.MODULE || sourceType == SourceType.CLOSURE_MODULE)
scopeManager.leaveScope();
contextManager.leaveContainer();

View File

@@ -5,6 +5,7 @@ import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import com.semmle.js.parser.JcornWrapper;
import com.semmle.util.data.StringUtil;
import com.semmle.util.exception.UserError;
@@ -50,8 +51,26 @@ public class ExtractorConfig {
}
};
/**
* The type of a source file, which together with the {@link Platform}
* determines how the top-level scope of the file behaves, and whether ES2015
* module syntax should be allowed.
* <p>
* Note that the names of these enum members are depended on by {@link Main},
* {@link AutoBuild}, and {@link JcornWrapper}.
*/
public static enum SourceType {
SCRIPT, ES6_MODULE, CLOSURE_MODULE, AUTO;
/** A script executed in the global scope. */
SCRIPT,
/** An ES2015 module. */
MODULE,
/** A Closure-Library module, defined using `goog.module()`. */
CLOSURE_MODULE,
/** Automatically determined source type. */
AUTO;
@Override
public String toString() {

View File

@@ -140,14 +140,14 @@ public class HTMLExtractor implements IExtractor {
if ("text/babel".equals(scriptType)) {
String plugins = getAttributeValueLC(script, "data-plugins");
if (plugins != null && plugins.contains("transform-es2015-modules-umd")) {
return SourceType.ES6_MODULE;
return SourceType.MODULE;
}
return config.getSourceType();
}
// if `type` is "module", extract as module
if ("module".equals(scriptType))
return SourceType.ES6_MODULE;
return SourceType.MODULE;
return null;
}

View File

@@ -71,7 +71,7 @@ public class JSExtractor {
if (config.getEcmaVersion().compareTo(ECMAVersion.ECMA2015) >= 0) {
Matcher m = containsModuleIndicator.matcher(source);
if (m.find() && (allowLeadingWS || m.group(1).isEmpty())) {
return m.group(2).startsWith("goog") ? SourceType.CLOSURE_MODULE : SourceType.ES6_MODULE;
return m.group(2).startsWith("goog") ? SourceType.CLOSURE_MODULE : SourceType.MODULE;
}
}
return SourceType.SCRIPT;
@@ -126,7 +126,7 @@ public class JSExtractor {
if (config.isExterns())
textualExtractor.getTrapwriter().addTuple("isExterns", toplevelLabel);
if (platform == Platform.NODE && sourceType != SourceType.ES6_MODULE && sourceType != SourceType.CLOSURE_MODULE)
if (platform == Platform.NODE && sourceType != SourceType.MODULE && sourceType != SourceType.CLOSURE_MODULE)
textualExtractor.getTrapwriter().addTuple("isNodejs", toplevelLabel);
return Pair.make(toplevelLabel, loc);

View File

@@ -52,7 +52,7 @@ public class ScriptExtractor implements IExtractor {
// Some file extensions are interpreted as modules by default.
if (isAlwaysModule(locationManager.getSourceFileExtension())) {
if (config.getSourceType() == SourceType.AUTO)
config = config.withSourceType(SourceType.ES6_MODULE);
config = config.withSourceType(SourceType.MODULE);
}
ScopeManager scopeManager = new ScopeManager(textualExtractor.getTrapwriter(), config.getEcmaVersion());