Skip to content

Commit 5d9d23e

Browse files
authored
Merge pull request #1110 from xiemaisi/js/yield-in-non-generator
Approved by asger-semmle
2 parents 7513bcf + 69c6311 commit 5d9d23e

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)