Skip to content

Commit 50b5a3b

Browse files
authored
Merge pull request #151 from asger-semmle/ts-ambient-toplevel
Approved by xiemaisi
2 parents 6c1098d + 4e9c52a commit 50b5a3b

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

change-notes/1.18/analysis-javascript.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
- [xss](https://github.com/leizongmin/js-xss)
8686
- [xtend](https://github.com/Raynos/xtend)
8787

88+
* Handling of ambient TypeScript code has been improved. As a result, fewer false positives will be reported in `.d.ts` files.
89+
8890
## New queries
8991

9092
| **Query** | **Tags** | **Purpose** |

javascript/ql/src/Expressions/SuspiciousInvocation.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ private import semmle.javascript.dataflow.InferredTypes
1515

1616
from InvokeExpr invk, DataFlow::AnalyzedNode callee
1717
where callee.asExpr() = invk.getCallee() and
18-
forex (InferredType tp | tp = callee.getAType() | tp != TTFunction() and tp != TTClass())
18+
forex (InferredType tp | tp = callee.getAType() | tp != TTFunction() and tp != TTClass()) and
19+
not invk.isAmbient()
1920
select invk, "Callee is not a function: it has type " + callee.ppTypes() + "."

javascript/ql/src/Expressions/SuspiciousPropAccess.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ predicate namespaceOrConstEnumAccess(VarAccess e) {
3131
from PropAccess pacc, DataFlow::AnalyzedNode base
3232
where base.asExpr() = pacc.getBase() and
3333
forex (InferredType tp | tp = base.getAType() | tp = TTNull() or tp = TTUndefined()) and
34-
not namespaceOrConstEnumAccess(pacc.getBase())
34+
not namespaceOrConstEnumAccess(pacc.getBase()) and
35+
not pacc.isAmbient()
3536
select pacc, "The base expression of this property access is always " + base.ppTypes() + "."

javascript/ql/src/semmle/javascript/AST.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ class TopLevel extends @toplevel, StmtContainer {
208208
override string toString() {
209209
result = "<toplevel>"
210210
}
211+
212+
override predicate isAmbient() {
213+
getFile().getFileType().isTypeScript() and
214+
getFile().getBaseName().matches("%.d.ts")
215+
}
211216
}
212217

213218
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Subclass extends BaseClass {} // OK - ambient context
2+
3+
export class BaseClass {}

0 commit comments

Comments
 (0)