Skip to content

Commit 7dd7463

Browse files
authored
Merge pull request #2169 from erik-krogh/importMeta
JS: add initial support for import.meta expressions in TypeScript
2 parents 4b27b2a + ab42b5d commit 7dd7463

File tree

17 files changed

+2399
-10
lines changed

17 files changed

+2399
-10
lines changed

change-notes/1.23/analysis-javascript.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
* The call graph has been improved to resolve method calls in more cases. This may produce more security alerts.
1414

15+
* TypeScript 3.6 features are supported.
16+
17+
1518
## New queries
1619

1720
| **Query** | **Tags** | **Purpose** |

javascript/extractor/lib/typescript/src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ function prepareNextFile() {
162162
}
163163
}
164164

165-
function handleParseCommand(command: ParseCommand) {
165+
function handleParseCommand(command: ParseCommand, checkPending = true) {
166166
let filename = command.filename;
167167
let expectedFilename = state.pendingFiles[state.pendingFileIndex];
168-
if (expectedFilename !== filename) {
168+
if (expectedFilename !== filename && checkPending) {
169169
throw new Error("File requested out of order. Expected '" + expectedFilename + "' but got '" + filename + "'");
170170
}
171171
++state.pendingFileIndex;
@@ -515,13 +515,13 @@ if (process.argv.length > 2) {
515515
handleParseCommand({
516516
command: "parse",
517517
filename: sf.fileName,
518-
});
518+
}, false);
519519
}
520520
} else if (pathlib.extname(argument) === ".ts" || pathlib.extname(argument) === ".tsx") {
521521
handleParseCommand({
522522
command: "parse",
523523
filename: argument,
524-
});
524+
}, false);
525525
} else {
526526
console.error("Unrecognized file or flag: " + argument);
527527
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ public Integer visit(ComprehensionBlock nd, Void q) {
249249
@Override
250250
public Integer visit(MetaProperty nd, Void c) {
251251
if (nd.getMeta().getName().equals("new")) return 82; // @newtargetexpr
252+
if (nd.getMeta().getName().equals("import")) return 115; // @importmetaexpr
252253
return 93; // @functionsentexpr
253254
}
254255

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,10 +1584,12 @@ private Node convertMappedType(JsonObject node, SourceLocation loc) throws Parse
15841584

15851585
private Node convertMetaProperty(JsonObject node, SourceLocation loc) throws ParseError {
15861586
Position metaStart = loc.getStart();
1587+
String keywordKind = syntaxKinds.get(node.getAsJsonPrimitive("keywordToken").getAsInt() + "").getAsString();
1588+
String identifier = keywordKind.equals("ImportKeyword") ? "import" : "new";
15871589
Position metaEnd =
1588-
new Position(metaStart.getLine(), metaStart.getColumn() + 3, metaStart.getOffset() + 3);
1589-
SourceLocation metaLoc = new SourceLocation("new", metaStart, metaEnd);
1590-
Identifier meta = new Identifier(metaLoc, "new");
1590+
new Position(metaStart.getLine(), metaStart.getColumn() + identifier.length(), metaStart.getOffset() + identifier.length());
1591+
SourceLocation metaLoc = new SourceLocation(identifier, metaStart, metaEnd);
1592+
Identifier meta = new Identifier(metaLoc, identifier);
15911593
return new MetaProperty(loc, meta, convertChild(node, "name"));
15921594
}
15931595

javascript/ql/src/semmle/javascript/ES2015Modules.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,4 +641,4 @@ class OriginalExportDeclaration extends ExportDeclaration {
641641
result = this.(ExportDefaultDeclaration).getSourceNode(name) or
642642
result = this.(ExportNamedDeclaration).getSourceNode(name)
643643
}
644-
}
644+
}

javascript/ql/src/semmle/javascript/Expr.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,3 +2645,15 @@ class OptionalChainRoot extends ChainElem {
26452645
*/
26462646
OptionalUse getAnOptionalUse() { result = optionalUse }
26472647
}
2648+
2649+
/**
2650+
* An `import.meta` expression.
2651+
*
2652+
* Example:
2653+
* ```js
2654+
* let url = import.meta.url;
2655+
* ```
2656+
*/
2657+
class ImportMetaExpr extends @importmetaexpr, Expr {
2658+
override predicate isImpure() { none() }
2659+
}

javascript/ql/src/semmle/javascript/dataflow/DataFlow.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,8 @@ module DataFlow {
14151415
or
14161416
e instanceof NewTargetExpr
14171417
or
1418+
e instanceof ImportMetaExpr
1419+
or
14181420
e instanceof FunctionBindExpr
14191421
or
14201422
e instanceof TaggedTemplateExpr

javascript/ql/src/semmle/javascript/dataflow/Sources.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ module SourceNode {
235235
astNode instanceof FunctionSentExpr or
236236
astNode instanceof FunctionBindExpr or
237237
astNode instanceof DynamicImportExpr or
238-
astNode instanceof ImportSpecifier
238+
astNode instanceof ImportSpecifier or
239+
astNode instanceof ImportMetaExpr
239240
)
240241
or
241242
DataFlow::parameterNode(this, _)

javascript/ql/src/semmlecode.javascript.dbscheme

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ case @expr.kind of
349349
| 112 = @e4x_xml_static_qualident
350350
| 113 = @e4x_xml_dynamic_qualident
351351
| 114 = @e4x_xml_dotdotexpr
352+
| 115 = @importmetaexpr
352353
;
353354

354355
@varaccess = @proper_varaccess | @export_varaccess;
@@ -1169,4 +1170,4 @@ extraction_data(
11691170
varchar(900) cacheFile: string ref,
11701171
boolean fromCache: boolean ref,
11711172
int length: int ref
1172-
)
1173+
)

javascript/ql/src/semmlecode.javascript.dbscheme.stats

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@
514514
<v>1</v>
515515
</e>
516516
<e>
517+
<k>@importmetaexpr</k>
518+
<v>1</v>
519+
</e>
520+
<e>
517521
<k>@namedimportspecifier</k>
518522
<v>4</v>
519523
</e>

0 commit comments

Comments
 (0)