Skip to content

Commit b9a2d06

Browse files
committed
Squash stuff
1 parent 60ad54a commit b9a2d06

File tree

63 files changed

+733
-461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+733
-461
lines changed

javascript/extractor/src/com/semmle/js/extractor/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ public void setupMatchers(ArgsParser ap) {
301301
// only extract HTML and JS by default
302302
addIncludesFor(includes, FileType.HTML);
303303
addIncludesFor(includes, FileType.JS);
304+
includes.add("**/.babelrc*.json");
305+
304306

305307
// extract TypeScript if `--typescript` or `--typescript-full` was specified
306308
if (getTypeScriptMode(ap) != TypeScriptMode.NONE) {

javascript/extractor/src/com/semmle/js/parser/JSONParser.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ private JSONObject readObject(int startoff, Position start) throws ParseError {
152152
char c = next();
153153
switch (c) {
154154
case '}':
155-
if (!needsComma) {
156-
raise("Trailing commas are not allowed in JSON.");
157-
}
158155
break out;
159156
case ',':
160157
if (!needsComma) {

javascript/ql/lib/definitions.qll

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 10 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,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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javascript
44
private import semmle.javascript.internal.CachedStages
5+
private import semmle.javascript.internal.paths.PathExprResolver
56

67
/**
78
* An ECMAScript 2015 module.
@@ -91,7 +92,12 @@ private predicate hasDefaultExport(ES2015Module mod) {
9192
class ImportDeclaration extends Stmt, Import, @import_declaration {
9293
override ES2015Module getEnclosingModule() { result = this.getTopLevel() }
9394

94-
override PathExpr getImportedPath() { result = this.getChildExpr(-1) }
95+
/**
96+
* Internal use only. Use `getImportedPathExpr` instead.
97+
*/
98+
string getRawImportPath() { result = this.getChildExpr(-1).getStringValue() }
99+
100+
override Expr getImportedPathExpr() { result = this.getChildExpr(-1) }
95101

96102
/**
97103
* Gets the object literal passed as part of the `with` (or `assert`) clause in this import declaration.
@@ -149,7 +155,7 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
149155
}
150156

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

155161
override string getValue() { result = this.getStringValue() }
@@ -725,27 +731,12 @@ abstract class ReExportDeclaration extends ExportDeclaration {
725731
cached
726732
Module getReExportedModule() {
727733
Stages::Imports::ref() and
728-
result.getFile() = this.getEnclosingModule().resolve(this.getImportedPath())
729-
or
730-
result = this.resolveFromTypeRoot()
731-
}
732-
733-
/**
734-
* Gets a module in a `node_modules/@types/` folder that matches the imported module name.
735-
*/
736-
private Module resolveFromTypeRoot() {
737-
result.getFile() =
738-
min(TypeRootFolder typeRoot |
739-
|
740-
typeRoot.getModuleFile(this.getImportedPath().getStringValue())
741-
order by
742-
typeRoot.getSearchPriority(this.getFile().getParentContainer())
743-
)
734+
result.getFile() = ImportPathResolver::resolveExpr(this.getImportedPath())
744735
}
745736
}
746737

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

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

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 8 additions & 2 deletions
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

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

77
import javascript
88
private import semmle.javascript.internal.CachedStages
9-
private import semmle.javascript.internal.paths.PathExprResolver as PathExprResolver
9+
private import semmle.javascript.internal.paths.PathExprResolver
1010

1111
/**
1212
* A module, which may either be an ECMAScript 2015-style module,
@@ -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() { none() }
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,40 +139,18 @@ 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
/**
140146
* Gets the module the path of this import resolves to.
141147
*/
142-
Module resolveImportedPath() {
143-
result.getFile() = PathExprResolver::resolvePathExpr(this.getImportedPath())
144-
}
145-
146-
/**
147-
* Gets a module with a `@providesModule` JSDoc tag that matches
148-
* the imported path.
149-
*/
150-
private Module resolveAsProvidedModule() {
151-
exists(JSDocTag tag |
152-
tag.getTitle() = "providesModule" and
153-
tag.getParent().getComment().getTopLevel() = result and
154-
tag.getDescription().trim() = this.getImportedPath().getValue()
155-
)
156-
}
148+
Module resolveImportedPath() { result.getFile() = this.getTargetFile() }
157149

158150
/**
159-
* Gets a module in a `node_modules/@types/` folder that matches the imported module name.
151+
* Gets the module the path of this import resolves to.
160152
*/
161-
private Module resolveFromTypeRoot() {
162-
result.getFile() =
163-
min(TypeRootFolder typeRoot |
164-
|
165-
typeRoot.getModuleFile(this.getImportedPath().getValue())
166-
order by
167-
typeRoot.getSearchPriority(this.getFile().getParentContainer())
168-
)
169-
}
153+
File getTargetFile() { result = ImportPathResolver::resolveExpr(this.getImportedPathExpr()) }
170154

171155
/**
172156
* DEPRECATED. Use `getImportedModule()` instead.
@@ -191,11 +175,7 @@ abstract class Import extends AstNode {
191175
Stages::Imports::ref() and
192176
if exists(this.resolveExternsImport())
193177
then result = this.resolveExternsImport()
194-
else (
195-
result = this.resolveAsProvidedModule() or
196-
result = this.resolveImportedPath() or
197-
result = this.resolveFromTypeRoot()
198-
)
178+
else result = this.resolveImportedPath()
199179
}
200180

201181
/**

javascript/ql/lib/semmle/javascript/NPM.qll

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javascript
66
private import NodeModuleResolutionImpl
7+
private import semmle.javascript.internal.paths.PackageJsonEx
78

89
/** A `package.json` configuration object. */
910
class PackageJson extends JsonObject {
@@ -93,7 +94,10 @@ class PackageJson extends JsonObject {
9394
* `module` paths to be exported under the relative path `"."`.
9495
*/
9596
string getExportedPath(string relativePath) {
96-
result = MainModulePath::of(this, relativePath).getValue()
97+
this.(PackageJsonEx).hasExactPathMapping(relativePath, result)
98+
or
99+
relativePath = "." and
100+
result = this.(PackageJsonEx).getMainPath()
97101
}
98102

99103
/** Gets the path of a command defined for this package. */
@@ -220,20 +224,18 @@ class PackageJson extends JsonObject {
220224
/**
221225
* Gets the main module of this package.
222226
*/
223-
Module getMainModule() { result = this.getExportedModule(".") }
227+
Module getMainModule() { result.getFile() = this.(PackageJsonEx).getMainFileOrBestGuess() }
224228

225229
/**
226230
* Gets the module exported under the given relative path.
227231
*
228232
* The main module is considered exported under the path `"."`.
229233
*/
230234
Module getExportedModule(string relativePath) {
231-
result =
232-
min(Module m, int prio |
233-
m.getFile() = resolveMainModule(this, prio, relativePath)
234-
|
235-
m order by prio
236-
)
235+
this.(PackageJsonEx).hasExactPathMappingTo(relativePath, result.getFile())
236+
or
237+
relativePath = "." and
238+
result = this.getMainModule()
237239
}
238240

239241
/**
@@ -245,19 +247,7 @@ class PackageJson extends JsonObject {
245247
* Gets the file containing the typings of this package, which can either be from the `types` or
246248
* `typings` field, or derived from the `main` or `module` fields.
247249
*/
248-
File getTypingsFile() {
249-
result =
250-
TypingsModulePathString::of(this).resolve(this.getFile().getParentContainer()).getContainer()
251-
or
252-
not exists(TypingsModulePathString::of(this)) and
253-
exists(File mainFile |
254-
mainFile = this.getMainModule().getFile() and
255-
result =
256-
mainFile
257-
.getParentContainer()
258-
.getFile(mainFile.getStem().regexpReplaceAll("\\.d$", "") + ".d.ts")
259-
)
260-
}
250+
File getTypingsFile() { none() } // implemented in PackageJsonEx
261251

262252
/**
263253
* Gets the module containing the typings of this package, which can either be from the `types` or

0 commit comments

Comments
 (0)