Skip to content

Commit 4916bed

Browse files
authored
Merge pull request #2433 from asger-semmle/import-js-file
Approved by max-schaefer
2 parents 9ca4f6a + e3e15a6 commit 4916bed

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

change-notes/1.24/analysis-javascript.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- [react](https://www.npmjs.com/package/react)
77
- [Handlebars](https://www.npmjs.com/package/handlebars)
88

9+
- Imports with the `.js` extension can now be resolved to a TypeScript file,
10+
when the import refers to a file generated by TypeScript.
11+
912
## New queries
1013

1114
| **Query** | **Tags** | **Purpose** |

javascript/ql/src/semmle/javascript/Modules.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ abstract class Module extends TopLevel {
8383
result = c.(Folder).getJavaScriptFile("index")
8484
)
8585
or
86-
// handle the case where the import path is missing an extension
86+
// handle the case where the import path is missing the extension
8787
exists(Folder f | f = path.resolveUpTo(path.getNumComponent() - 1) |
8888
result = f.getJavaScriptFile(path.getBaseName())
89+
or
90+
// If a js file was not found look for a file that compiles to js
91+
not exists(f.getJavaScriptFile(path.getBaseName())) and
92+
result = f.getJavaScriptFile(path.getStem())
8993
)
9094
)
9195
}

javascript/ql/src/semmle/javascript/Paths.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ abstract class PathExpr extends PathExprBase {
205205
/** Gets the base name of the folder or file this path refers to. */
206206
string getBaseName() { result = getValue().(PathString).getBaseName() }
207207

208+
/** Gets the stem, that is, base name without extension, of the folder or file this path refers to. */
209+
string getStem() { result = getValue().(PathString).getStem() }
210+
208211
/**
209212
* Gets the file or folder that the first `n` components of this path refer to
210213
* when resolved relative to the root folder of the given `priority`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from "./f.js";
2+
3+
foo();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ test_ImportSpecifiers
66
| f.ts:1:8:1:8 | g | f.ts:1:8:1:8 | g |
77
| g.ts:1:9:1:11 | foo | g.ts:1:9:1:11 | foo |
88
| import-in-mjs.mjs:1:8:1:24 | exported_from_mjs | import-in-mjs.mjs:1:8:1:24 | exported_from_mjs |
9+
| import-ts-with-js-extension.ts:1:10:1:12 | foo | import-ts-with-js-extension.ts:1:10:1:12 | foo |
910
| m/c.js:1:8:1:13 | * as b | m/c.js:1:13:1:13 | b |
1011
| tst.html:5:10:5:10 | f | tst.html:5:10:5:10 | f |
1112
| unresolved.js:1:8:1:8 | f | unresolved.js:1:8:1:8 | f |
@@ -43,6 +44,7 @@ test_getImportedName
4344
| f.ts:1:8:1:8 | g | default |
4445
| g.ts:1:9:1:11 | foo | foo |
4546
| import-in-mjs.mjs:1:8:1:24 | exported_from_mjs | default |
47+
| import-ts-with-js-extension.ts:1:10:1:12 | foo | foo |
4648
| tst.html:5:10:5:10 | f | default |
4749
| unresolved.js:1:8:1:8 | f | default |
4850
test_ExportDeclarations
@@ -65,6 +67,7 @@ test_getAnImportedModule
6567
| es2015_require.js | d.js |
6668
| f.ts | e.js |
6769
| g.ts | f.ts |
70+
| import-ts-with-js-extension.ts | f.ts |
6871
| m/c.js | b.js |
6972
test_getSourceNode
7073
| a.js:1:1:3:1 | export ... n 23;\\n} | default | a.js:1:16:3:1 | functio ... n 23;\\n} |
@@ -87,13 +90,15 @@ test_Imports
8790
| f.ts:1:1:1:19 | import g from './e' | f.ts:1:15:1:19 | './e' | 1 |
8891
| g.ts:1:1:1:23 | import ... m './f' | g.ts:1:19:1:23 | './f' | 1 |
8992
| import-in-mjs.mjs:1:1:1:46 | import ... n-mjs'; | import-in-mjs.mjs:1:31:1:45 | 'export-in-mjs' | 1 |
93+
| import-ts-with-js-extension.ts:1:1:1:29 | import ... /f.js"; | import-ts-with-js-extension.ts:1:21:1:28 | "./f.js" | 1 |
9094
| m/c.js:1:1:1:26 | import ... '../b'; | m/c.js:1:20:1:25 | '../b' | 1 |
9195
| tst.html:5:3:5:20 | import f from 'a'; | tst.html:5:17:5:19 | 'a' | 1 |
9296
| unresolved.js:1:1:1:18 | import f from 'a'; | unresolved.js:1:15:1:17 | 'a' | 1 |
9397
test_NamedImportSpecifier
9498
| d.js:1:10:1:21 | default as g |
9599
| d.js:1:24:1:29 | x as y |
96100
| g.ts:1:9:1:11 | foo |
101+
| import-ts-with-js-extension.ts:1:10:1:12 | foo |
97102
test_GlobalVariableRef
98103
| a.js:5:31:5:31 | o |
99104
| exports.js:3:9:3:15 | exports |

0 commit comments

Comments
 (0)