Skip to content

Commit 56deae6

Browse files
committed
JS: Deprecate PathExpr and related classes
1 parent 46cdb17 commit 56deae6

24 files changed

+118
-96
lines changed

javascript/ql/examples/snippets/importfrom.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
import javascript
1212

1313
from ImportDeclaration id
14-
where id.getImportedPath().getValue() = "react"
14+
where id.getImportedPathString() = "react"
1515
select id

javascript/ql/lib/definitions.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private predicate importLookup(AstNode path, Module target, string kind) {
7070
kind = "I" and
7171
(
7272
exists(Import i |
73-
path = i.getImportedPath() and
73+
path = i.getImportedPathExpr() and
7474
target = i.getImportedModule()
7575
)
7676
or

javascript/ql/lib/semmle/javascript/AMD.qll

+18-10
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ class AmdModuleDefinition extends CallExpr instanceof AmdModuleDefinition::Range
6161
result = this.getArgument(1)
6262
}
6363

64+
/** DEPRECATED. Use `getDependencyExpr` instead. */
65+
deprecated PathExpr getDependency(int i) { result = this.getDependencyExpr(i) }
66+
67+
/** DEPRECATED. Use `getADependencyExpr` instead. */
68+
deprecated PathExpr getADependency() { result = this.getADependencyExpr() }
69+
6470
/** Gets the `i`th dependency of this module definition. */
65-
PathExpr getDependency(int i) {
71+
Expr getDependencyExpr(int i) {
6672
exists(Expr expr |
6773
expr = this.getDependencies().getElement(i) and
6874
not isPseudoDependency(expr.getStringValue()) and
@@ -71,8 +77,8 @@ class AmdModuleDefinition extends CallExpr instanceof AmdModuleDefinition::Range
7177
}
7278

7379
/** Gets a dependency of this module definition. */
74-
PathExpr getADependency() {
75-
result = this.getDependency(_) or
80+
Expr getADependencyExpr() {
81+
result = this.getDependencyExpr(_) or
7682
result = this.getARequireCall().getAnArgument()
7783
}
7884

@@ -233,7 +239,7 @@ private class AmdDependencyPath extends PathExprCandidate {
233239
}
234240

235241
/** A constant path element appearing in an AMD dependency expression. */
236-
private class ConstantAmdDependencyPathElement extends PathExpr, ConstantString {
242+
deprecated private class ConstantAmdDependencyPathElement extends PathExpr, ConstantString {
237243
ConstantAmdDependencyPathElement() { this = any(AmdDependencyPath amd).getAPart() }
238244

239245
override string getValue() { result = this.getStringValue() }
@@ -261,11 +267,13 @@ private predicate amdModuleTopLevel(AmdModuleDefinition def, TopLevel tl) {
261267
* An AMD dependency, viewed as an import.
262268
*/
263269
private class AmdDependencyImport extends Import {
264-
AmdDependencyImport() { this = any(AmdModuleDefinition def).getADependency() }
270+
AmdDependencyImport() { this = any(AmdModuleDefinition def).getADependencyExpr() }
265271

266-
override Module getEnclosingModule() { this = result.(AmdModule).getDefine().getADependency() }
272+
override Module getEnclosingModule() {
273+
this = result.(AmdModule).getDefine().getADependencyExpr()
274+
}
267275

268-
override PathExpr getImportedPath() { result = this }
276+
override Expr getImportedPathExpr() { result = this }
269277

270278
/**
271279
* Gets a file that looks like it might be the target of this import.
@@ -274,7 +282,7 @@ private class AmdDependencyImport extends Import {
274282
* adding well-known JavaScript file extensions like `.js`.
275283
*/
276284
private File guessTarget() {
277-
exists(PathString imported, string abspath, string dirname, string basename |
285+
exists(FilePath imported, string abspath, string dirname, string basename |
278286
this.targetCandidate(result, abspath, imported, dirname, basename)
279287
|
280288
abspath.regexpMatch(".*/\\Q" + imported + "\\E")
@@ -296,9 +304,9 @@ private class AmdDependencyImport extends Import {
296304
* `dirname` and `basename` to the dirname and basename (respectively) of `imported`.
297305
*/
298306
private predicate targetCandidate(
299-
File f, string abspath, PathString imported, string dirname, string basename
307+
File f, string abspath, FilePath imported, string dirname, string basename
300308
) {
301-
imported = this.getImportedPath().getValue() and
309+
imported = this.getImportedPathString() and
302310
f.getStem() = imported.getStem() and
303311
f.getAbsolutePath() = abspath and
304312
dirname = imported.getDirName() and

javascript/ql/lib/semmle/javascript/ES2015Modules.qll

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ private predicate hasDefaultExport(ES2015Module mod) {
9292
class ImportDeclaration extends Stmt, Import, @import_declaration {
9393
override ES2015Module getEnclosingModule() { result = this.getTopLevel() }
9494

95-
override PathExpr getImportedPath() { result = this.getChildExpr(-1) }
96-
9795
/**
9896
* INTERNAL USE ONLY. DO NOT USE.
9997
*/
10098
string getRawImportPath() { result = this.getChildExpr(-1).getStringValue() }
10199

100+
override Expr getImportedPathExpr() { result = this.getChildExpr(-1) }
101+
102102
/**
103103
* Gets the object literal passed as part of the `with` (or `assert`) clause in this import declaration.
104104
*
@@ -155,7 +155,7 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
155155
}
156156

157157
/** A literal path expression appearing in an `import` declaration. */
158-
private class LiteralImportPath extends PathExpr, ConstantString {
158+
deprecated private class LiteralImportPath extends PathExpr, ConstantString {
159159
LiteralImportPath() { exists(ImportDeclaration req | this = req.getChildExpr(-1)) }
160160

161161
override string getValue() { result = this.getStringValue() }
@@ -736,7 +736,7 @@ abstract class ReExportDeclaration extends ExportDeclaration {
736736
}
737737

738738
/** A literal path expression appearing in a re-export declaration. */
739-
private class LiteralReExportPath extends PathExpr, ConstantString {
739+
deprecated private class LiteralReExportPath extends PathExpr, ConstantString {
740740
LiteralReExportPath() { exists(ReExportDeclaration bred | this = bred.getImportedPath()) }
741741

742742
override string getValue() { result = this.getStringValue() }

javascript/ql/lib/semmle/javascript/Expr.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -2821,7 +2821,7 @@ class DynamicImportExpr extends @dynamic_import, Expr, Import {
28212821
result = this.getSource().getFirstControlFlowNode()
28222822
}
28232823

2824-
override PathExpr getImportedPath() { result = this.getSource() }
2824+
override Expr getImportedPathExpr() { result = this.getSource() }
28252825

28262826
/**
28272827
* Gets the second "argument" to the import expression, that is, the `Y` in `import(X, Y)`.
@@ -2852,7 +2852,7 @@ class DynamicImportExpr extends @dynamic_import, Expr, Import {
28522852
}
28532853

28542854
/** A literal path expression appearing in a dynamic import. */
2855-
private class LiteralDynamicImportPath extends PathExpr, ConstantString {
2855+
deprecated private class LiteralDynamicImportPath extends PathExpr, ConstantString {
28562856
LiteralDynamicImportPath() {
28572857
exists(DynamicImportExpr di | this.getParentExpr*() = di.getSource())
28582858
}

javascript/ql/lib/semmle/javascript/HTML.qll

+8-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ module HTML {
214214
result = path.regexpCapture("file://(/.*)", 1)
215215
or
216216
not path.regexpMatch("(\\w+:)?//.*") and
217-
result = this.getSourcePath().(ScriptSrcPath).resolve(this.getSearchRoot()).toString()
217+
result = ResolveScriptSrc::resolve(this.getSearchRoot(), this.getSourcePath()).toString()
218218
)
219219
}
220220

@@ -274,10 +274,16 @@ module HTML {
274274
)
275275
}
276276

277+
private module ResolverConfig implements Folder::ResolveSig {
278+
predicate shouldResolve(Container base, string path) { scriptSrc(path, base) }
279+
}
280+
281+
private module ResolveScriptSrc = Folder::Resolve<ResolverConfig>;
282+
277283
/**
278284
* A path string arising from the `src` attribute of a `script` tag.
279285
*/
280-
private class ScriptSrcPath extends PathString {
286+
deprecated private class ScriptSrcPath extends PathString {
281287
ScriptSrcPath() { scriptSrc(this, _) }
282288

283289
override Folder getARootFolder() { scriptSrc(this, result) }

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ abstract class Module extends TopLevel {
6969
* This predicate is not part of the public API, it is only exposed to allow
7070
* overriding by subclasses.
7171
*/
72-
predicate searchRoot(PathExpr path, Folder searchRoot, int priority) {
72+
deprecated predicate searchRoot(PathExpr path, Folder searchRoot, int priority) {
7373
path.getEnclosingModule() = this and
7474
priority = 0 and
7575
exists(string v | v = path.getValue() |
@@ -90,7 +90,7 @@ abstract class Module extends TopLevel {
9090
* resolves to a folder containing a main module (such as `index.js`), then
9191
* that file is the result.
9292
*/
93-
File resolve(PathExpr path) {
93+
deprecated File resolve(PathExpr path) {
9494
path.getEnclosingModule() = this and
9595
(
9696
// handle the case where the import path is complete
@@ -123,8 +123,14 @@ abstract class Import extends AstNode {
123123
/** Gets the module in which this import appears. */
124124
abstract Module getEnclosingModule();
125125

126+
/** DEPRECATED. Use `getImportedPathExpr` instead. */
127+
deprecated PathExpr getImportedPath() { result = this.getImportedPathExpr() }
128+
126129
/** Gets the (unresolved) path that this import refers to. */
127-
abstract PathExpr getImportedPath();
130+
abstract Expr getImportedPathExpr();
131+
132+
/** Gets the imported path as a string. */
133+
final string getImportedPathString() { result = this.getImportedPathExpr().getStringValue() }
128134

129135
/**
130136
* Gets an externs module the path of this import resolves to.
@@ -133,7 +139,7 @@ abstract class Import extends AstNode {
133139
* path is assumed to be a possible target of the import.
134140
*/
135141
Module resolveExternsImport() {
136-
result.isExterns() and result.getName() = this.getImportedPath().getValue()
142+
result.isExterns() and result.getName() = this.getImportedPathString()
137143
}
138144

139145
/**
@@ -144,7 +150,7 @@ abstract class Import extends AstNode {
144150
/**
145151
* Gets the module the path of this import resolves to.
146152
*/
147-
File getTargetFile() { result = ImportPathResolver::resolveExpr(this.getImportedPath()) }
153+
File getTargetFile() { result = ImportPathResolver::resolveExpr(this.getImportedPathExpr()) }
148154

149155
/**
150156
* DEPRECATED. Use `getImportedModule()` instead.

javascript/ql/lib/semmle/javascript/NodeJS.qll

+10-22
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class NodeModule extends Module {
146146
)
147147
}
148148

149-
override predicate searchRoot(PathExpr path, Folder searchRoot, int priority) {
149+
deprecated override predicate searchRoot(PathExpr path, Folder searchRoot, int priority) {
150150
path.getEnclosingModule() = this and
151151
exists(string pathval | pathval = path.getValue() |
152152
// paths starting with `./` or `../` are resolved relative to the importing
@@ -236,11 +236,6 @@ private class RequireVariable extends Variable {
236236
}
237237
}
238238

239-
/**
240-
* Holds if module `m` is in file `f`.
241-
*/
242-
private predicate moduleInFile(Module m, File f) { m.getFile() = f }
243-
244239
private predicate isModuleModule(EarlyStageNode nd) {
245240
exists(ImportDeclaration imp | imp.getRawImportPath() = "module" |
246241
nd = TDestructuredModuleImportNode(imp)
@@ -328,24 +323,17 @@ private predicate isRequire(EarlyStageNode nd) {
328323
class Require extends CallExpr, Import {
329324
Require() { isRequire(TValueNode(this.getCallee())) }
330325

331-
override PathExpr getImportedPath() { result = this.getArgument(0) }
326+
override Expr getImportedPathExpr() { result = this.getArgument(0) }
332327

333328
override Module getEnclosingModule() { this = result.getAnImport() }
334329

335-
override Module resolveImportedPath() {
336-
moduleInFile(result, this.load(min(int prio | moduleInFile(_, this.load(prio)))))
337-
or
338-
not moduleInFile(_, this.load(_)) and
339-
result = Import.super.resolveImportedPath()
340-
}
341-
342330
/**
343331
* Gets the file that is imported by this `require`.
344332
*
345333
* The result can be a JavaScript file, a JSON file or a `.node` file.
346334
* Externs files are not treated differently from other files by this predicate.
347335
*/
348-
File getImportedFile() { result = this.load(min(int prio | exists(this.load(prio)))) }
336+
deprecated File getImportedFile() { result = this.load(min(int prio | exists(this.load(prio)))) }
349337

350338
/**
351339
* Gets the file that this `require` refers to (which may not be a JavaScript file),
@@ -402,8 +390,8 @@ class Require extends CallExpr, Import {
402390
* predicate `tryExtensions` that handles the repeated distinction between
403391
* `.js`, `.json` and `.node`.
404392
*/
405-
private File load(int priority) {
406-
exists(int r | this.getEnclosingModule().searchRoot(this.getImportedPath(), _, r) |
393+
deprecated private File load(int priority) {
394+
exists(int r | this.getEnclosingModule().searchRoot(this.getImportedPathExpr(), _, r) |
407395
result = loadAsFile(this, r, priority - prioritiesPerCandidate() * r) or
408396
result =
409397
loadAsDirectory(this, r,
@@ -415,7 +403,7 @@ class Require extends CallExpr, Import {
415403
}
416404

417405
/** An argument to `require` or `require.resolve`, considered as a path expression. */
418-
private class RequirePath extends PathExprCandidate {
406+
deprecated private class RequirePath extends PathExprCandidate {
419407
RequirePath() {
420408
this = any(Require req).getArgument(0)
421409
or
@@ -428,14 +416,14 @@ private class RequirePath extends PathExprCandidate {
428416
}
429417

430418
/** A constant path element appearing in a call to `require` or `require.resolve`. */
431-
private class ConstantRequirePathElement extends PathExpr, ConstantString {
419+
deprecated private class ConstantRequirePathElement extends PathExpr, ConstantString {
432420
ConstantRequirePathElement() { this = any(RequirePath rp).getAPart() }
433421

434422
override string getValue() { result = this.getStringValue() }
435423
}
436424

437425
/** A `__dirname` path expression. */
438-
private class DirNamePath extends PathExpr, VarAccess {
426+
deprecated private class DirNamePath extends PathExpr, VarAccess {
439427
DirNamePath() {
440428
this.getName() = "__dirname" and
441429
this.getVariable().getScope() instanceof ModuleScope
@@ -445,7 +433,7 @@ private class DirNamePath extends PathExpr, VarAccess {
445433
}
446434

447435
/** A `__filename` path expression. */
448-
private class FileNamePath extends PathExpr, VarAccess {
436+
deprecated private class FileNamePath extends PathExpr, VarAccess {
449437
FileNamePath() {
450438
this.getName() = "__filename" and
451439
this.getVariable().getScope() instanceof ModuleScope
@@ -458,7 +446,7 @@ private class FileNamePath extends PathExpr, VarAccess {
458446
* A path expression of the form `path.join(p, "...")` where
459447
* `p` is also a path expression.
460448
*/
461-
private class JoinedPath extends PathExpr, @call_expr {
449+
deprecated private class JoinedPath extends PathExpr, @call_expr {
462450
JoinedPath() {
463451
exists(MethodCallExpr call | call = this |
464452
call.getReceiver().(VarAccess).getName() = "path" and

0 commit comments

Comments
 (0)