Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30ba7ae

Browse files
committed
JS: split SourceType.MODULE into two
1 parent f00b16e commit 30ba7ae

11 files changed

Lines changed: 29 additions & 11 deletions

File tree

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private class V extends DefaultVisitor<Context, Label> {
306306
public V(Platform platform, SourceType sourceType) {
307307
this.platform = platform;
308308
this.sourceType = sourceType;
309-
this.isStrict = sourceType == SourceType.MODULE;
309+
this.isStrict = sourceType == SourceType.ES6_MODULE || sourceType == SourceType.CLOSURE_MODULE;
310310
}
311311

312312
private Label visit(INode child, Label parent, int childIndex) {
@@ -557,10 +557,12 @@ public Label visit(Program nd, Context c) {
557557
if (!".mjs".equals(locationManager.getSourceFileExtension()))
558558
scopeManager.addVariables("require", "module", "exports", "__filename", "__dirname", "arguments");
559559
trapwriter.addTuple("isModule", toplevelLabel);
560-
} else if (sourceType == SourceType.MODULE) {
560+
} else if (sourceType == SourceType.ES6_MODULE || sourceType == SourceType.CLOSURE_MODULE) {
561561
Label moduleScopeKey = trapwriter.globalID("module;{" + locationManager.getFileLabel() + "}," + locationManager.getStartLine() + "," + locationManager.getStartColumn());
562562
scopeManager.enterScope(3, moduleScopeKey, toplevelLabel);
563-
scopeManager.addVariables("exports"); // needed for Closure modules - spuriously added for ES6 modules
563+
if (sourceType == SourceType.CLOSURE_MODULE) {
564+
scopeManager.addVariables("exports");
565+
}
564566
trapwriter.addTuple("isModule", toplevelLabel);
565567
}
566568

@@ -571,7 +573,7 @@ public Label visit(Program nd, Context c) {
571573
visitAll(nd.getBody(), toplevelLabel);
572574

573575
// if we're extracting a Node.js/ES2015 module, leave its scope
574-
if (platform == Platform.NODE || sourceType == SourceType.MODULE)
576+
if (platform == Platform.NODE || sourceType == SourceType.ES6_MODULE || sourceType == SourceType.CLOSURE_MODULE)
575577
scopeManager.leaveScope();
576578

577579
contextManager.leaveContainer();

javascript/extractor/src/com/semmle/js/extractor/ExtractorConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static ECMAVersion of(int version) {
5151
};
5252

5353
public static enum SourceType {
54-
SCRIPT, MODULE, AUTO;
54+
SCRIPT, ES6_MODULE, CLOSURE_MODULE, AUTO;
5555

5656
@Override
5757
public String toString() {

javascript/extractor/src/com/semmle/js/extractor/HTMLExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ private SourceType getScriptSourceType(Element script) {
140140
if ("text/babel".equals(scriptType)) {
141141
String plugins = getAttributeValueLC(script, "data-plugins");
142142
if (plugins != null && plugins.contains("transform-es2015-modules-umd")) {
143-
return SourceType.MODULE;
143+
return SourceType.ES6_MODULE;
144144
}
145145
return config.getSourceType();
146146
}
147147

148148
// if `type` is "module", extract as module
149149
if ("module".equals(scriptType))
150-
return SourceType.MODULE;
150+
return SourceType.ES6_MODULE;
151151

152152
return null;
153153
}

javascript/extractor/src/com/semmle/js/extractor/JSExtractor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ public SourceType establishSourceType(String source, boolean allowLeadingWS) {
7070
return sourceType;
7171
if (config.getEcmaVersion().compareTo(ECMAVersion.ECMA2015) >= 0) {
7272
Matcher m = containsModuleIndicator.matcher(source);
73-
if (m.find() && (allowLeadingWS || m.group(1).isEmpty()))
74-
return SourceType.MODULE;
73+
if (m.find() && (allowLeadingWS || m.group(1).isEmpty())) {
74+
return m.group(2).startsWith("goog") ? SourceType.CLOSURE_MODULE : SourceType.ES6_MODULE;
75+
}
7576
}
7677
return SourceType.SCRIPT;
7778
}
@@ -125,7 +126,7 @@ public Pair<Label, LoCInfo> extract(TextualExtractor textualExtractor,
125126

126127
if (config.isExterns())
127128
textualExtractor.getTrapwriter().addTuple("isExterns", toplevelLabel);
128-
if (platform == Platform.NODE && sourceType != SourceType.MODULE)
129+
if (platform == Platform.NODE && sourceType != SourceType.ES6_MODULE && sourceType != SourceType.CLOSURE_MODULE)
129130
textualExtractor.getTrapwriter().addTuple("isNodejs", toplevelLabel);
130131

131132
return Pair.make(toplevelLabel, loc);

javascript/extractor/src/com/semmle/js/extractor/ScriptExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
5252
// Some file extensions are interpreted as modules by default.
5353
if (isAlwaysModule(locationManager.getSourceFileExtension())) {
5454
if (config.getSourceType() == SourceType.AUTO)
55-
config = config.withSourceType(SourceType.MODULE);
55+
config = config.withSourceType(SourceType.ES6_MODULE);
5656
}
5757

5858
ScopeManager scopeManager = new ScopeManager(textualExtractor.getTrapwriter(), config.getEcmaVersion());
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| a.js:5:31:5:31 | o |
2+
| exports.js:3:9:3:15 | exports |
3+
| tst.html:6:3:6:7 | alert |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import javascript
2+
3+
from VarAccess access
4+
where access.getVariable() instanceof GlobalVariable
5+
select access
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
| exports.js:1:8:1:17 | * as dummy |
12
| m/c.js:1:8:1:13 | * as b |

javascript/ql/test/library-tests/Modules/ImportSpecifiers.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| b.js:1:8:1:8 | f | b.js:1:8:1:8 | f |
22
| d.js:1:10:1:21 | default as g | d.js:1:21:1:21 | g |
33
| d.js:1:24:1:29 | x as y | d.js:1:29:1:29 | y |
4+
| exports.js:1:8:1:17 | * as dummy | exports.js:1:13:1:17 | dummy |
45
| f.ts:1:8:1:8 | g | f.ts:1:8:1:8 | g |
56
| g.ts:1:9:1:11 | foo | g.ts:1:9:1:11 | foo |
67
| import-in-mjs.mjs:1:8:1:24 | exported_from_mjs | import-in-mjs.mjs:1:8:1:24 | exported_from_mjs |

javascript/ql/test/library-tests/Modules/Imports.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| b.js:1:1:1:20 | import f from './a'; | b.js:1:15:1:19 | './a' | 1 |
22
| d.js:1:1:1:43 | import ... './a'; | d.js:1:38:1:42 | './a' | 2 |
33
| d.js:2:1:2:13 | import './b'; | d.js:2:8:2:12 | './b' | 0 |
4+
| exports.js:1:1:1:31 | import ... dummy'; | exports.js:1:24:1:30 | 'dummy' | 1 |
45
| f.ts:1:1:1:19 | import g from './e' | f.ts:1:15:1:19 | './e' | 1 |
56
| g.ts:1:1:1:23 | import ... m './f' | g.ts:1:19:1:23 | './f' | 1 |
67
| import-in-mjs.mjs:1:1:1:46 | import ... n-mjs'; | import-in-mjs.mjs:1:31:1:45 | 'export-in-mjs' | 1 |

0 commit comments

Comments
 (0)