Skip to content

Commit 56c9ee3

Browse files
committed
JS: Deprecate PathExpr, PathString, and Path
1 parent 1f3d3c7 commit 56c9ee3

File tree

14 files changed

+71
-69
lines changed

14 files changed

+71
-69
lines changed

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

Lines changed: 16 additions & 8 deletions
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,9 +267,11 @@ 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

268276
override Expr getImportedPathExpr() { result = this }
269277

@@ -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(StringAsPath imported, string abspath, string dirname, string basename |
278286
this.targetCandidate(result, abspath, imported, dirname, basename)
279287
|
280288
abspath.regexpMatch(".*/\\Q" + imported + "\\E")
@@ -296,7 +304,7 @@ 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, StringAsPath imported, string dirname, string basename
300308
) {
301309
imported = this.getImportedPathString() and
302310
f.getStem() = imported.getStem() and

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
150150
}
151151

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

156156
override string getValue() { result = this.getStringValue() }
@@ -731,7 +731,7 @@ abstract class ReExportDeclaration extends ExportDeclaration {
731731
}
732732

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/Modules.qll

Lines changed: 3 additions & 3 deletions
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
@@ -149,7 +149,7 @@ abstract class Import extends AstNode {
149149
/**
150150
* Gets the module the path of this import resolves to.
151151
*/
152-
File getTargetFile() { result = Resolver::resolveExpr(this.getImportedPathExpr()) }
152+
File getTargetFile() { result = ImportPathResolver::resolveExpr(this.getImportedPathExpr()) }
153153

154154
/**
155155
* DEPRECATED. Use `getImportedModule()` instead.

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

Lines changed: 8 additions & 8 deletions
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
@@ -333,7 +333,7 @@ class Require extends CallExpr, Import {
333333
* The result can be a JavaScript file, a JSON file or a `.node` file.
334334
* Externs files are not treated differently from other files by this predicate.
335335
*/
336-
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)))) }
337337

338338
/**
339339
* Gets the file that this `require` refers to (which may not be a JavaScript file),
@@ -390,7 +390,7 @@ class Require extends CallExpr, Import {
390390
* predicate `tryExtensions` that handles the repeated distinction between
391391
* `.js`, `.json` and `.node`.
392392
*/
393-
private File load(int priority) {
393+
deprecated private File load(int priority) {
394394
exists(int r | this.getEnclosingModule().searchRoot(this.getImportedPathExpr(), _, r) |
395395
result = loadAsFile(this, r, priority - prioritiesPerCandidate() * r) or
396396
result =
@@ -403,7 +403,7 @@ class Require extends CallExpr, Import {
403403
}
404404

405405
/** An argument to `require` or `require.resolve`, considered as a path expression. */
406-
private class RequirePath extends PathExprCandidate {
406+
deprecated private class RequirePath extends PathExprCandidate {
407407
RequirePath() {
408408
this = any(Require req).getArgument(0)
409409
or
@@ -416,14 +416,14 @@ private class RequirePath extends PathExprCandidate {
416416
}
417417

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

422422
override string getValue() { result = this.getStringValue() }
423423
}
424424

425425
/** A `__dirname` path expression. */
426-
private class DirNamePath extends PathExpr, VarAccess {
426+
deprecated private class DirNamePath extends PathExpr, VarAccess {
427427
DirNamePath() {
428428
this.getName() = "__dirname" and
429429
this.getVariable().getScope() instanceof ModuleScope
@@ -433,7 +433,7 @@ private class DirNamePath extends PathExpr, VarAccess {
433433
}
434434

435435
/** A `__filename` path expression. */
436-
private class FileNamePath extends PathExpr, VarAccess {
436+
deprecated private class FileNamePath extends PathExpr, VarAccess {
437437
FileNamePath() {
438438
this.getName() = "__filename" and
439439
this.getVariable().getScope() instanceof ModuleScope
@@ -446,7 +446,7 @@ private class FileNamePath extends PathExpr, VarAccess {
446446
* A path expression of the form `path.join(p, "...")` where
447447
* `p` is also a path expression.
448448
*/
449-
private class JoinedPath extends PathExpr, @call_expr {
449+
deprecated private class JoinedPath extends PathExpr, @call_expr {
450450
JoinedPath() {
451451
exists(MethodCallExpr call | call = this |
452452
call.getReceiver().(VarAccess).getName() = "path" and

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ int numberOfExtensions() { result = count(getFileExtensionPriority(_)) }
4545
* Gets the resolution target with the given `priority` of `req`
4646
* when resolved from the root with priority `rootPriority`.
4747
*/
48-
File loadAsFile(Require req, int rootPriority, int priority) {
49-
exists(PathExpr path | path = req.getImportedPathExpr() |
48+
deprecated File loadAsFile(Require req, int rootPriority, int priority) {
49+
exists(PathExpr path | path = req.getImportedPath() |
5050
result = path.resolve(rootPriority) and priority = 0
5151
or
5252
exists(Folder encl | encl = path.resolveUpTo(path.getNumComponent() - 1, rootPriority) |
@@ -60,8 +60,8 @@ File loadAsFile(Require req, int rootPriority, int priority) {
6060
* with the given `priority` of `req` when resolved from the root with
6161
* priority `rootPriority`.
6262
*/
63-
File loadAsDirectory(Require req, int rootPriority, int priority) {
64-
exists(Folder dir | dir = req.getImportedPathExpr().(PathExpr).resolve(rootPriority) |
63+
deprecated File loadAsDirectory(Require req, int rootPriority, int priority) {
64+
exists(Folder dir | dir = req.getImportedPath().resolve(rootPriority) |
6565
result = resolveMainModule(dir.(NpmPackage).getPackageJson(), priority, ".") or
6666
result = tryExtensions(dir, "index", priority - (numberOfExtensions() + 1))
6767
)
@@ -99,7 +99,7 @@ private string getStem(string name) {
9999
* Gets a file that a main module from `pkg` exported as `mainPath` with the given `priority`.
100100
* `mainPath` is "." if it's the main module of the package.
101101
*/
102-
private File resolveMainPath(PackageJson pkg, string mainPath, int priority) {
102+
deprecated private File resolveMainPath(PackageJson pkg, string mainPath, int priority) {
103103
exists(PathExpr main | main = MainModulePath::of(pkg, mainPath) |
104104
result = main.resolve() and priority = 0
105105
or
@@ -132,7 +132,7 @@ private File resolveMainPath(PackageJson pkg, string mainPath, int priority) {
132132
/**
133133
* Gets the main module described by `pkg` with the given `priority`.
134134
*/
135-
File resolveMainModule(PackageJson pkg, int priority, string exportPath) {
135+
deprecated File resolveMainModule(PackageJson pkg, int priority, string exportPath) {
136136
result = resolveMainPath(pkg, exportPath, priority)
137137
or
138138
exportPath = "." and
@@ -178,7 +178,7 @@ private string getASrcFolderName() { result = ["ts", "js", "src", "lib"] }
178178
* A JSON string in a `package.json` file specifying the path of one of the exported
179179
* modules of the package.
180180
*/
181-
class MainModulePath extends PathExpr, @json_string {
181+
deprecated class MainModulePath extends PathExpr, @json_string {
182182
PackageJson pkg;
183183

184184
MainModulePath() {
@@ -228,7 +228,7 @@ private string getExportRelativePath(JsonValue part) {
228228
result.matches(".%")
229229
}
230230

231-
module MainModulePath {
231+
deprecated module MainModulePath {
232232
/** Gets the path to the main entry point of `pkg`. */
233233
MainModulePath of(PackageJson pkg) { result = of(pkg, ".") }
234234

@@ -244,7 +244,7 @@ module MainModulePath {
244244
* These files are often imported directly from a client when a "main" module is not specified.
245245
* For performance reasons this only exists if there is no "main" field in the `package.json` file.
246246
*/
247-
private class FilesPath extends PathExpr, @json_string {
247+
deprecated private class FilesPath extends PathExpr, @json_string {
248248
PackageJson pkg;
249249

250250
FilesPath() {
@@ -263,15 +263,15 @@ private class FilesPath extends PathExpr, @json_string {
263263
}
264264
}
265265

266-
private module FilesPath {
266+
deprecated private module FilesPath {
267267
FilesPath of(PackageJson pkg) { result.getPackageJson() = pkg }
268268
}
269269

270270
/**
271271
* A JSON string in a `package.json` file specifying the path of the
272272
* TypeScript typings entry point.
273273
*/
274-
class TypingsModulePathString extends PathString {
274+
deprecated class TypingsModulePathString extends PathString {
275275
PackageJson pkg;
276276

277277
TypingsModulePathString() {
@@ -288,7 +288,7 @@ class TypingsModulePathString extends PathString {
288288
}
289289

290290
/** Companion module to the `TypingsModulePathString` class. */
291-
module TypingsModulePathString {
291+
deprecated module TypingsModulePathString {
292292
/** Get the typings path for the given `package.json` file. */
293293
TypingsModulePathString of(PackageJson pkg) { result.getPackageJson() = pkg }
294294
}

javascript/ql/lib/semmle/javascript/PackageExports.qll

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import javascript
88
private import semmle.javascript.internal.CachedStages
9+
private import semmle.javascript.internal.paths.PackageJsonEx
910

1011
/**
1112
* Gets a parameter that is a library input to a top-level package.
@@ -126,19 +127,12 @@ private DataFlow::Node getAValueExportedByPackage() {
126127
// ....
127128
// }));
128129
// ```
129-
// Such files are not recognized as modules, so we manually use `NodeModule::resolveMainModule` to resolve the file against a `package.json` file.
130+
// Such files are not recognized as modules, so we manually use `PackageJsonEx` to resolve the file against a `package.json` file.
130131
exists(ImmediatelyInvokedFunctionExpr func, DataFlow::ParameterNode factory, int i |
131132
factory.getName() = "factory" and
132133
func.getParameter(i) = factory.getParameter() and
133134
DataFlow::globalVarRef("define").getACall().getAnArgument() = factory.getALocalUse() and
134-
func.getFile() =
135-
min(int j, File f |
136-
f =
137-
NodeModule::resolveMainModule(any(PackageJson pack | exists(pack.getPackageName())), j,
138-
".")
139-
|
140-
f order by j
141-
)
135+
func.getFile() = any(PackageJsonEx pack).getMainFileOrBestGuess()
142136
|
143137
result = func.getInvocation().getArgument(i).flow().getAFunctionValue().getAReturn()
144138
or

0 commit comments

Comments
 (0)