Skip to content

Commit 2c1567a

Browse files
committed
JS: Don't extract TypeScript from HTML
1 parent 805deb1 commit 2c1567a

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
5959

6060
Segment content = elt.getContent();
6161
String source = content.toString();
62+
boolean isTypeScript = isTypeScriptTag(elt);
6263

6364
/*
6465
* Script blocks in XHTML files may wrap (parts of) their code inside CDATA sections.
@@ -81,7 +82,8 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
8182
textualExtractor,
8283
source,
8384
contentStart.getRow(),
84-
contentStart.getColumn());
85+
contentStart.getColumn(),
86+
isTypeScript);
8587
}
8688
}
8789
} else {
@@ -103,7 +105,8 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
103105
textualExtractor,
104106
source,
105107
valueStart.getRow(),
106-
valueStart.getColumn());
108+
valueStart.getColumn(),
109+
false /* isTypeScript */);
107110
} else if (source.startsWith("javascript:")) {
108111
source = source.substring(11);
109112
snippetLoC =
@@ -114,7 +117,8 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
114117
textualExtractor,
115118
source,
116119
valueStart.getRow(),
117-
valueStart.getColumn() + 11);
120+
valueStart.getColumn() + 11,
121+
false /* isTypeScript */);
118122
}
119123
}
120124
}
@@ -143,11 +147,9 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
143147
*/
144148
private SourceType getScriptSourceType(Element script) {
145149
String scriptType = getAttributeValueLC(script, "type");
146-
String scriptLanguage = getAttributeValueLC(script, "language");
150+
String scriptLanguage = getScriptLanguage(script);
147151

148-
if (scriptLanguage == null) { // Vue templates use 'lang' instead of 'language'.
149-
scriptLanguage = getAttributeValueLC(script, "lang");
150-
}
152+
if (isTypeScriptTag(script)) return config.getSourceType();
151153

152154
// if `type` and `language` are both either missing, contain the
153155
// string "javascript", or if `type` is the string "text/jsx", this is a plain script
@@ -171,6 +173,23 @@ private SourceType getScriptSourceType(Element script) {
171173
return null;
172174
}
173175

176+
private String getScriptLanguage(Element script) {
177+
String scriptLanguage = getAttributeValueLC(script, "language");
178+
179+
if (scriptLanguage == null) { // Vue templates use 'lang' instead of 'language'.
180+
scriptLanguage = getAttributeValueLC(script, "lang");
181+
}
182+
return scriptLanguage;
183+
}
184+
185+
private boolean isTypeScriptTag(Element script) {
186+
String language = getScriptLanguage(script);
187+
if ("ts".equals(language) || "typescript".equals(language)) return true;
188+
String type = getAttributeValueLC(script, "type");
189+
if (type != null && type.contains("typescript")) return true;
190+
return false;
191+
}
192+
174193
/**
175194
* Get the value of attribute <code>attr</code> of element <code>elt</code> in lower case; if the
176195
* attribute has no value, <code>null</code> is returned.
@@ -187,7 +206,10 @@ private LoCInfo extractSnippet(
187206
TextualExtractor textualExtractor,
188207
String source,
189208
int line,
190-
int column) {
209+
int column,
210+
boolean isTypeScript) {
211+
if (isTypeScript)
212+
return null; // not supported right now
191213
TrapWriter trapwriter = textualExtractor.getTrapwriter();
192214
LocationManager locationManager = textualExtractor.getLocationManager();
193215
LocationManager scriptLocationManager =

0 commit comments

Comments
 (0)