Skip to content

Commit 1a16d73

Browse files
committed
JS: Set SourceType correctly
1 parent fcb3651 commit 1a16d73

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.nio.file.Path;
44

5+
import com.semmle.js.extractor.ExtractorConfig.SourceType;
6+
57
/**
68
* Denotes where a code snippet originated from within a file.
79
*/
@@ -10,12 +12,14 @@ public class FileSnippet {
1012
private int line;
1113
private int column;
1214
private int topLevelKind;
15+
private SourceType sourceType;
1316

14-
public FileSnippet(Path originalFile, int line, int column, int topLevelKind) {
17+
public FileSnippet(Path originalFile, int line, int column, int topLevelKind, SourceType sourceType) {
1518
this.originalFile = originalFile;
1619
this.line = line;
1720
this.column = column;
1821
this.topLevelKind = topLevelKind;
22+
this.sourceType = sourceType;
1923
}
2024

2125
public Path getOriginalFile() {
@@ -33,4 +37,8 @@ public int getColumn() {
3337
public int getTopLevelKind() {
3438
return topLevelKind;
3539
}
40+
41+
public SourceType getSourceType() {
42+
return sourceType;
43+
}
3644
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.semmle.js.extractor;
22

3+
import java.io.File;
34
import java.nio.file.Path;
45
import java.util.regex.Pattern;
56

@@ -55,7 +56,7 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
5556
for (Element elt : src.getAllElements()) {
5657
LoCInfo snippetLoC = null;
5758
if (elt.getName().equals(HTMLElementName.SCRIPT)) {
58-
SourceType sourceType = getScriptSourceType(elt);
59+
SourceType sourceType = getScriptSourceType(elt, textualExtractor.getExtractedFile());
5960
if (sourceType != null) {
6061
// Jericho sometimes misparses empty elements, which will show up as start tags
6162
// ending in "/"; we manually exclude these cases to avoid spurious syntax errors
@@ -149,26 +150,31 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
149150
* Deduce the {@link SourceType} with which the given <code>script</code> element should be
150151
* extracted, returning <code>null</code> if it cannot be determined.
151152
*/
152-
private SourceType getScriptSourceType(Element script) {
153+
private SourceType getScriptSourceType(Element script, File file) {
153154
String scriptType = getAttributeValueLC(script, "type");
154155
String scriptLanguage = getScriptLanguage(script);
156+
157+
SourceType fallbackSourceType = config.getSourceType();
158+
if (file.getName().endsWith(".vue")) {
159+
fallbackSourceType = SourceType.MODULE;
160+
}
155161

156-
if (isTypeScriptTag(script)) return config.getSourceType();
162+
if (isTypeScriptTag(script)) return fallbackSourceType;
157163

158164
// if `type` and `language` are both either missing, contain the
159165
// string "javascript", or if `type` is the string "text/jsx", this is a plain script
160166
if ((scriptType == null || scriptType.contains("javascript") || "text/jsx".equals(scriptType))
161167
&& (scriptLanguage == null || scriptLanguage.contains("javascript")))
162168
// use default source type
163-
return config.getSourceType();
169+
return fallbackSourceType;
164170

165171
// if `type` is "text/babel", the source type depends on the `data-plugins` attribute
166172
if ("text/babel".equals(scriptType)) {
167173
String plugins = getAttributeValueLC(script, "data-plugins");
168174
if (plugins != null && plugins.contains("transform-es2015-modules-umd")) {
169175
return SourceType.MODULE;
170176
}
171-
return config.getSourceType();
177+
return fallbackSourceType;
172178
}
173179

174180
// if `type` is "module", extract as module
@@ -214,7 +220,7 @@ private LoCInfo extractSnippet(
214220
boolean isTypeScript) {
215221
if (isTypeScript) {
216222
Path file = textualExtractor.getExtractedFile().toPath();
217-
FileSnippet snippet = new FileSnippet(file, line, column, toplevelKind);
223+
FileSnippet snippet = new FileSnippet(file, line, column, toplevelKind, config.getSourceType());
218224
VirtualSourceRoot vroot = config.getVirtualSourceRoot();
219225
// Vue files are special in that they can be imported as modules, and may only contain one <script> tag.
220226
// For .vue files we omit the usual snippet decoration to ensure the TypeScript compiler can find it.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
2525
ScopeManager scopeManager =
2626
new ScopeManager(textualExtractor.getTrapwriter(), ECMAVersion.ECMA2017);
2727
try {
28-
SourceType sourceType = jsExtractor.establishSourceType(source, false);
2928
FileSnippet snippet = state.getSnippets().get(sourceFile.toPath());
30-
int toplevelKind = snippet == null ? 0 : snippet.getTopLevelKind();
29+
SourceType sourceType = snippet != null ? snippet.getSourceType() : jsExtractor.establishSourceType(source, false);
30+
int toplevelKind = snippet != null ? snippet.getTopLevelKind() : 0;
3131
return jsExtractor.extract(textualExtractor, source, toplevelKind, scopeManager, sourceType, res).snd();
3232
} catch (ParseError e) {
3333
e.setPosition(locationManager.translatePosition(e.getPosition()));

0 commit comments

Comments
 (0)