From d4eb9716fadad538606a3aed36c7bd8256607596 Mon Sep 17 00:00:00 2001 From: Sam Robson Date: Fri, 6 Nov 2026 09:55:11 +0000 Subject: [PATCH] fix(js): require identifier after await using declaration --- .../src/com/semmle/jcorn/Parser.java | 15 ++++++++--- .../AST/ExplicitResource/printAst.expected | 26 +++++++++++++++++++ .../library-tests/AST/ExplicitResource/tst.js | 4 +++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/javascript/extractor/src/com/semmle/jcorn/Parser.java b/javascript/extractor/src/com/semmle/jcorn/Parser.java index c7c88fef7517..8aea863a0482 100644 --- a/javascript/extractor/src/com/semmle/jcorn/Parser.java +++ b/javascript/extractor/src/com/semmle/jcorn/Parser.java @@ -2598,7 +2598,10 @@ boolean isAsyncFunction() { } boolean isAwaitUsing() { - return isAsyncKeyword("await", "using"); + if (!isAsyncKeyword("await", "using")) return false; + Matcher m = Whitespace.skipWhiteSpace.matcher(this.input); + m.find(this.pos); + return isUsingDecl(m.end() + "using".length()); } // check 'pre [no LineTerminator here] keyword' @@ -2625,11 +2628,15 @@ boolean isUsingDecl() { || this.options.ecmaVersion() < 8 || !this.value.equals("using")) return false; + return isUsingDecl(this.pos); + } + + boolean isUsingDecl(int pos) { Matcher m = Whitespace.skipWhiteSpace.matcher(this.input); - m.find(this.pos); + m.find(pos); int next = m.end(); return this.input.length() > next && - !Whitespace.lineBreakG.matcher(inputSubstring(this.pos, next)).matches() + !Whitespace.lineBreakG.matcher(inputSubstring(pos, next)).matches() && Identifiers.isIdentifierChar(this.input.codePointAt(next), false); } @@ -2809,7 +2816,7 @@ protected Statement parseForStatement(Position startLoc) { if (this.isAwaitUsing() && this.inAsync) { this.next(); // just skip the await and treat it as a `using` statement } - if (this.type == TokenType._var || this.type == TokenType._const || isLet || (this.type == TokenType.name && this.value.equals("using"))) { + if (this.type == TokenType._var || this.type == TokenType._const || isLet || this.isUsingDecl()) { Position initStartLoc = this.startLoc; String kind = isLet ? "let" : String.valueOf(this.value); this.next(); diff --git a/javascript/ql/test/library-tests/AST/ExplicitResource/printAst.expected b/javascript/ql/test/library-tests/AST/ExplicitResource/printAst.expected index 3514287b1f41..11507529fea7 100644 --- a/javascript/ql/test/library-tests/AST/ExplicitResource/printAst.expected +++ b/javascript/ql/test/library-tests/AST/ExplicitResource/printAst.expected @@ -3,6 +3,7 @@ nodes | file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) | | file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) | | file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) | +| file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) | | file://:0:0:0:0 | (Parameters) | semmle.label | (Parameters) | | tst.html:1:5:1:9 | [ExprStmt] using | semmle.label | [ExprStmt] using | | tst.html:1:5:1:9 | [ExprStmt] using | semmle.order | 1 | @@ -69,6 +70,15 @@ nodes | tst.js:28:5:28:16 | [CallExpr] using(using) | semmle.label | [CallExpr] using(using) | | tst.js:28:5:28:17 | [ExprStmt] using(using); | semmle.label | [ExprStmt] using(using); | | tst.js:28:11:28:15 | [VarRef] using | semmle.label | [VarRef] using | +| tst.js:31:1:33:1 | [FunctionDeclStmt] async f ... oo"); } | semmle.label | [FunctionDeclStmt] async f ... oo"); } | +| tst.js:31:1:33:1 | [FunctionDeclStmt] async f ... oo"); } | semmle.order | 5 | +| tst.js:31:16:31:29 | [VarDecl] asyncUsesUsing | semmle.label | [VarDecl] asyncUsesUsing | +| tst.js:31:33:33:1 | [BlockStmt] { a ... oo"); } | semmle.label | [BlockStmt] { a ... oo"); } | +| tst.js:32:5:32:22 | [AwaitExpr] await using("foo") | semmle.label | [AwaitExpr] await using("foo") | +| tst.js:32:5:32:23 | [ExprStmt] await using("foo"); | semmle.label | [ExprStmt] await using("foo"); | +| tst.js:32:11:32:15 | [VarRef] using | semmle.label | [VarRef] using | +| tst.js:32:11:32:22 | [CallExpr] using("foo") | semmle.label | [CallExpr] using("foo") | +| tst.js:32:17:32:21 | [Literal] "foo" | semmle.label | [Literal] "foo" | edges | file://:0:0:0:0 | (Arguments) | tst.js:4:10:4:18 | [AssignExpr] test = 20 | semmle.label | 0 | | file://:0:0:0:0 | (Arguments) | tst.js:4:10:4:18 | [AssignExpr] test = 20 | semmle.order | 0 | @@ -78,6 +88,8 @@ edges | file://:0:0:0:0 | (Arguments) | tst.js:24:11:24:15 | [Literal] "foo" | semmle.order | 0 | | file://:0:0:0:0 | (Arguments) | tst.js:28:11:28:15 | [VarRef] using | semmle.label | 0 | | file://:0:0:0:0 | (Arguments) | tst.js:28:11:28:15 | [VarRef] using | semmle.order | 0 | +| file://:0:0:0:0 | (Arguments) | tst.js:32:17:32:21 | [Literal] "foo" | semmle.label | 0 | +| file://:0:0:0:0 | (Arguments) | tst.js:32:17:32:21 | [Literal] "foo" | semmle.order | 0 | | file://:0:0:0:0 | (Parameters) | tst.js:25:20:25:22 | [SimpleParameter] foo | semmle.label | 0 | | file://:0:0:0:0 | (Parameters) | tst.js:25:20:25:22 | [SimpleParameter] foo | semmle.order | 0 | | tst.html:1:5:1:9 | [ExprStmt] using | tst.html:1:5:1:9 | [VarRef] using | semmle.label | 1 | @@ -194,5 +206,19 @@ edges | tst.js:28:5:28:16 | [CallExpr] using(using) | tst.js:28:5:28:9 | [VarRef] using | semmle.order | 0 | | tst.js:28:5:28:17 | [ExprStmt] using(using); | tst.js:28:5:28:16 | [CallExpr] using(using) | semmle.label | 1 | | tst.js:28:5:28:17 | [ExprStmt] using(using); | tst.js:28:5:28:16 | [CallExpr] using(using) | semmle.order | 1 | +| tst.js:31:1:33:1 | [FunctionDeclStmt] async f ... oo"); } | tst.js:31:16:31:29 | [VarDecl] asyncUsesUsing | semmle.label | 0 | +| tst.js:31:1:33:1 | [FunctionDeclStmt] async f ... oo"); } | tst.js:31:16:31:29 | [VarDecl] asyncUsesUsing | semmle.order | 0 | +| tst.js:31:1:33:1 | [FunctionDeclStmt] async f ... oo"); } | tst.js:31:33:33:1 | [BlockStmt] { a ... oo"); } | semmle.label | 5 | +| tst.js:31:1:33:1 | [FunctionDeclStmt] async f ... oo"); } | tst.js:31:33:33:1 | [BlockStmt] { a ... oo"); } | semmle.order | 5 | +| tst.js:31:33:33:1 | [BlockStmt] { a ... oo"); } | tst.js:32:5:32:23 | [ExprStmt] await using("foo"); | semmle.label | 1 | +| tst.js:31:33:33:1 | [BlockStmt] { a ... oo"); } | tst.js:32:5:32:23 | [ExprStmt] await using("foo"); | semmle.order | 1 | +| tst.js:32:5:32:22 | [AwaitExpr] await using("foo") | tst.js:32:11:32:22 | [CallExpr] using("foo") | semmle.label | 1 | +| tst.js:32:5:32:22 | [AwaitExpr] await using("foo") | tst.js:32:11:32:22 | [CallExpr] using("foo") | semmle.order | 1 | +| tst.js:32:5:32:23 | [ExprStmt] await using("foo"); | tst.js:32:5:32:22 | [AwaitExpr] await using("foo") | semmle.label | 1 | +| tst.js:32:5:32:23 | [ExprStmt] await using("foo"); | tst.js:32:5:32:22 | [AwaitExpr] await using("foo") | semmle.order | 1 | +| tst.js:32:11:32:22 | [CallExpr] using("foo") | file://:0:0:0:0 | (Arguments) | semmle.label | 1 | +| tst.js:32:11:32:22 | [CallExpr] using("foo") | file://:0:0:0:0 | (Arguments) | semmle.order | 1 | +| tst.js:32:11:32:22 | [CallExpr] using("foo") | tst.js:32:11:32:15 | [VarRef] using | semmle.label | 0 | +| tst.js:32:11:32:22 | [CallExpr] using("foo") | tst.js:32:11:32:15 | [VarRef] using | semmle.order | 0 | graphProperties | semmle.graphKind | tree | diff --git a/javascript/ql/test/library-tests/AST/ExplicitResource/tst.js b/javascript/ql/test/library-tests/AST/ExplicitResource/tst.js index ef5c7f0e6792..9dcd80bd3d07 100644 --- a/javascript/ql/test/library-tests/AST/ExplicitResource/tst.js +++ b/javascript/ql/test/library-tests/AST/ExplicitResource/tst.js @@ -26,4 +26,8 @@ function usesUsing() { // ... } using(using); +} + +async function asyncUsesUsing() { + await using("foo"); } \ No newline at end of file