Skip to content

Commit c8bb0e2

Browse files
committed
JS: Treat d.ts as a single extension in Folder.getJavaScriptFile
1 parent 987a830 commit c8bb0e2

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

javascript/ql/lib/semmle/javascript/Files.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ class Folder extends Container, @folder {
175175
result.getExtension() = extension
176176
}
177177

178+
/** Like `getFile` except `d.ts` is treated as a single extension. */
179+
private File getFileLongExtension(string stem, string extension) {
180+
not (stem.matches("%.d") and extension = "ts") and
181+
result = this.getFile(stem, extension)
182+
or
183+
extension = "d.ts" and
184+
result = this.getFile(stem + ".d", "ts")
185+
}
186+
178187
/**
179188
* Gets the file in this folder that has the given `stem` and any of the supported JavaScript extensions.
180189
*
@@ -188,7 +197,11 @@ class Folder extends Container, @folder {
188197
*/
189198
File getJavaScriptFile(string stem) {
190199
result =
191-
min(int p, string ext | p = getFileExtensionPriority(ext) | this.getFile(stem, ext) order by p)
200+
min(int p, string ext |
201+
p = getFileExtensionPriority(ext)
202+
|
203+
this.getFileLongExtension(stem, ext) order by p
204+
)
192205
}
193206

194207
/** Gets a subfolder contained in this folder. */

javascript/ql/lib/semmle/javascript/NodeModuleResolutionImpl.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ int getFileExtensionPriority(string ext) {
3333
ext = "json" and result = 8
3434
or
3535
ext = "node" and result = 9
36+
or
37+
ext = "d.ts" and result = 10
3638
}
3739

3840
int prioritiesPerCandidate() { result = 3 * (numberOfExtensions() + 1) }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
| main.ts:1:1:1:52 | import ... -file"; | ./only-declaration-file | only-declaration-file.d.ts:3:1:4:0 | <toplevel> |
12
| main.ts:2:1:2:48 | import ... -file"; | ./has-javascript-file | has-javascript-file.js:1:1:2:0 | <toplevel> |

0 commit comments

Comments
 (0)