Skip to content

Commit 4772798

Browse files
committed
JS: do not resolve arbitrary extensions to JavaScript files
1 parent c5f73cb commit 4772798

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ abstract class Module extends TopLevel {
8888
result = f.getJavaScriptFile(path.getBaseName())
8989
or
9090
// If a js file was not found look for a file that compiles to js
91+
path.getExtension() = ".js" and
9192
not exists(f.getJavaScriptFile(path.getBaseName())) and
9293
result = f.getJavaScriptFile(path.getStem())
9394
)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ abstract class PathString extends string {
128128
/** Gets the path of the parent folder of the folder or file this path refers to. */
129129
string getDirName() { result = this.regexpCapture(pathRegex(), 1) }
130130

131+
/**
132+
* Gets the extension of the folder or file this path refers to, that is, the suffix of the base name
133+
* starting at the last dot character, if there is one.
134+
*
135+
* Has no result if the base name does not contain a dot.
136+
*/
137+
string getExtension() { result = this.regexpCapture(pathRegex(), 4) }
138+
131139
/**
132140
* Gets the absolute path that the sub-path consisting of the first `n`
133141
* components of this path refers to when resolved relative to the
@@ -208,6 +216,14 @@ abstract class PathExpr extends PathExprBase {
208216
/** Gets the stem, that is, base name without extension, of the folder or file this path refers to. */
209217
string getStem() { result = getValue().(PathString).getStem() }
210218

219+
/**
220+
* Gets the extension of the folder or file this path refers to, that is, the suffix of the base name
221+
* starting at the last dot character, if there is one.
222+
*
223+
* Has no result if the base name does not contain a dot.
224+
*/
225+
string getExtension() { result = getValue().(PathString).getExtension() }
226+
211227
/**
212228
* Gets the file or folder that the first `n` components of this path refer to
213229
* when resolved relative to the root folder of the given `priority`.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ test_getAnImportedModule
7373
| library-tests/Modules/f.ts | library-tests/Modules/e.js |
7474
| library-tests/Modules/g.ts | library-tests/Modules/f.ts |
7575
| library-tests/Modules/import-ts-with-js-extension.ts | library-tests/Modules/f.ts |
76-
| library-tests/Modules/importcss.js | library-tests/Modules/a.js |
7776
| library-tests/Modules/m/c.js | library-tests/Modules/b.js |
7877
test_getSourceNode
7978
| a.js:1:1:3:1 | export ... n 23;\\n} | default | a.js:1:16:3:1 | functio ... n 23;\\n} |

0 commit comments

Comments
 (0)