|
1 | 1 | package com.semmle.js.extractor;
|
2 | 2 |
|
| 3 | +import java.io.File; |
3 | 4 | import java.nio.file.Path;
|
4 | 5 | import java.util.regex.Pattern;
|
5 | 6 |
|
@@ -55,7 +56,7 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
|
55 | 56 | for (Element elt : src.getAllElements()) {
|
56 | 57 | LoCInfo snippetLoC = null;
|
57 | 58 | if (elt.getName().equals(HTMLElementName.SCRIPT)) {
|
58 |
| - SourceType sourceType = getScriptSourceType(elt); |
| 59 | + SourceType sourceType = getScriptSourceType(elt, textualExtractor.getExtractedFile()); |
59 | 60 | if (sourceType != null) {
|
60 | 61 | // Jericho sometimes misparses empty elements, which will show up as start tags
|
61 | 62 | // ending in "/"; we manually exclude these cases to avoid spurious syntax errors
|
@@ -149,26 +150,31 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
|
149 | 150 | * Deduce the {@link SourceType} with which the given <code>script</code> element should be
|
150 | 151 | * extracted, returning <code>null</code> if it cannot be determined.
|
151 | 152 | */
|
152 |
| - private SourceType getScriptSourceType(Element script) { |
| 153 | + private SourceType getScriptSourceType(Element script, File file) { |
153 | 154 | String scriptType = getAttributeValueLC(script, "type");
|
154 | 155 | String scriptLanguage = getScriptLanguage(script);
|
| 156 | + |
| 157 | + SourceType fallbackSourceType = config.getSourceType(); |
| 158 | + if (file.getName().endsWith(".vue")) { |
| 159 | + fallbackSourceType = SourceType.MODULE; |
| 160 | + } |
155 | 161 |
|
156 |
| - if (isTypeScriptTag(script)) return config.getSourceType(); |
| 162 | + if (isTypeScriptTag(script)) return fallbackSourceType; |
157 | 163 |
|
158 | 164 | // if `type` and `language` are both either missing, contain the
|
159 | 165 | // string "javascript", or if `type` is the string "text/jsx", this is a plain script
|
160 | 166 | if ((scriptType == null || scriptType.contains("javascript") || "text/jsx".equals(scriptType))
|
161 | 167 | && (scriptLanguage == null || scriptLanguage.contains("javascript")))
|
162 | 168 | // use default source type
|
163 |
| - return config.getSourceType(); |
| 169 | + return fallbackSourceType; |
164 | 170 |
|
165 | 171 | // if `type` is "text/babel", the source type depends on the `data-plugins` attribute
|
166 | 172 | if ("text/babel".equals(scriptType)) {
|
167 | 173 | String plugins = getAttributeValueLC(script, "data-plugins");
|
168 | 174 | if (plugins != null && plugins.contains("transform-es2015-modules-umd")) {
|
169 | 175 | return SourceType.MODULE;
|
170 | 176 | }
|
171 |
| - return config.getSourceType(); |
| 177 | + return fallbackSourceType; |
172 | 178 | }
|
173 | 179 |
|
174 | 180 | // if `type` is "module", extract as module
|
@@ -214,7 +220,7 @@ private LoCInfo extractSnippet(
|
214 | 220 | boolean isTypeScript) {
|
215 | 221 | if (isTypeScript) {
|
216 | 222 | 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()); |
218 | 224 | VirtualSourceRoot vroot = config.getVirtualSourceRoot();
|
219 | 225 | // Vue files are special in that they can be imported as modules, and may only contain one <script> tag.
|
220 | 226 | // For .vue files we omit the usual snippet decoration to ensure the TypeScript compiler can find it.
|
|
0 commit comments