Skip to content

Commit 506993a

Browse files
committed
Temp: changes to Module to enable MRVA query
1 parent 201f39e commit 506993a

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

javascript/ql/lib/semmle/javascript/Modules.qll

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ abstract class Import extends AstNode {
140140
* Gets the module the path of this import resolves to.
141141
*/
142142
Module resolveImportedPath() {
143+
result.getFile() = this.getEnclosingModule().resolve(this.getImportedPath())
144+
}
145+
146+
/**
147+
* Gets the module the path of this import resolves to.
148+
*/
149+
Module resolveImportedPathNew() {
143150
result.getFile() = PathExprResolver::resolvePathExpr(this.getImportedPath())
144151
}
145152

@@ -169,9 +176,9 @@ abstract class Import extends AstNode {
169176
}
170177

171178
/**
172-
* DEPRECATED. Use `getImportedModule()` instead.
179+
* Gets the imported module, as determined by the TypeScript compiler, if any.
173180
*/
174-
deprecated Module resolveFromTypeScriptSymbol() {
181+
Module resolveFromTypeScriptSymbol() {
175182
exists(CanonicalName symbol |
176183
ast_node_symbol(this, symbol) and
177184
ast_node_symbol(result, symbol)
@@ -193,8 +200,10 @@ abstract class Import extends AstNode {
193200
then result = this.resolveExternsImport()
194201
else (
195202
result = this.resolveAsProvidedModule() or
196-
result = this.resolveImportedPath() or
197-
result = this.resolveFromTypeRoot()
203+
result = this.resolveImportedPathNew() or
204+
result = this.resolveFromTypeRoot() or
205+
// result = this.resolveFromTypeScriptSymbol() or
206+
result = resolveNeighbourPackage(this.getImportedPath().getValue())
198207
)
199208
}
200209

@@ -203,3 +212,28 @@ abstract class Import extends AstNode {
203212
*/
204213
abstract DataFlow::Node getImportedModuleNode();
205214
}
215+
216+
/**
217+
* Gets a module imported from another package in the same repository.
218+
*
219+
* No support for importing from folders inside the other package.
220+
*/
221+
Module resolveNeighbourPackage(PathString importPath) {
222+
exists(PackageJson json | importPath = json.getPackageName() and result = json.getMainModule())
223+
or
224+
exists(string package |
225+
result.getFile().getParentContainer() = getPackageFolder(package) and
226+
importPath = package + "/" + [result.getFile().getBaseName(), result.getFile().getStem()]
227+
)
228+
}
229+
230+
/**
231+
* Gets the folder for a package that has name `package` according to a package.json file in the resulting folder.
232+
*/
233+
pragma[noinline]
234+
private Folder getPackageFolder(string package) {
235+
exists(PackageJson json |
236+
json.getPackageName() = package and
237+
result = json.getFile().getParentContainer()
238+
)
239+
}

0 commit comments

Comments
 (0)