Skip to content

Commit 69c6311

Browse files
author
Max Schaefer
committed
JavaScript: Teach Function.isGenerator to check for yield.
1 parent 6baf526 commit 69c6311

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

javascript/ql/src/LanguageFeatures/YieldInNonGenerator.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ import javascript
1515
from YieldExpr yield, Function f
1616
where
1717
f = yield.getEnclosingFunction() and
18-
not f.isGenerator()
18+
not isGenerator(f)
1919
select yield, "This yield expression is contained in $@ which is not marked as a generator.",
2020
f.getFirstToken(), f.describe()

javascript/ql/src/semmle/javascript/Functions.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
7979
TypeExpr getReturnTypeAnnotation() { typeexprs(result, _, this, -3, _) }
8080

8181
/** Holds if this function is a generator function. */
82-
predicate isGenerator() { isGenerator(this) }
82+
predicate isGenerator() {
83+
isGenerator(this)
84+
or
85+
// we also support `yield` in non-generator functions
86+
exists(YieldExpr yield | this = yield.getEnclosingFunction())
87+
}
8388

8489
/** Holds if the last parameter of this function is a rest parameter. */
8590
predicate hasRestParameter() { hasRestParameter(this) }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function outer() {
2+
function inner() {
3+
yield 1;
4+
}
5+
inner().next()
6+
}
7+
8+
// semmle-extractor-options: --experimental

0 commit comments

Comments
 (0)