Skip to content

Commit 2643ab3

Browse files
committed
using is not a keyword
1 parent 5e11fe7 commit 2643ab3

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

javascript/extractor/src/com/semmle/jcorn/ESNextParser.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@
5151
public class ESNextParser extends JSXParser {
5252
public ESNextParser(Options options, String input, int startPos) {
5353
super(options.allowImportExportEverywhere(true), input, startPos);
54-
55-
// recognise `using` as a keyword. See https://github.com/tc39/proposal-explicit-resource-management
56-
this.keywords.add("using");
5754
}
5855

5956
/*

javascript/extractor/src/com/semmle/jcorn/Parser.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,19 @@ && inputSubstring(next, next + len).equals(keyword)
26992699
|| !Identifiers.isIdentifierChar(this.input.codePointAt(next + len), false));
27002700
}
27012701

2702+
// matches "using [identifier]"
2703+
boolean isUsingDecl() {
2704+
if (this.type != TokenType.name
2705+
|| this.options.ecmaVersion() < 8
2706+
|| !this.value.equals("using")) return false;
2707+
2708+
Matcher m = Whitespace.skipWhiteSpace.matcher(this.input);
2709+
m.find(this.pos);
2710+
int next = m.end();
2711+
return !Whitespace.lineBreakG.matcher(inputSubstring(this.pos, next)).matches()
2712+
&& Identifiers.isIdentifierChar(this.input.codePointAt(next + 1), false);
2713+
}
2714+
27022715
/**
27032716
* Parse a single statement.
27042717
*
@@ -2749,7 +2762,7 @@ protected Statement parseStatement(boolean declaration, boolean topLevel, Set<St
27492762
return this.parseThrowStatement(startLoc);
27502763
} else if (starttype == TokenType._try) {
27512764
return this.parseTryStatement(startLoc);
2752-
} else if (starttype == TokenType._const || starttype == TokenType._var || starttype == TokenType._using) {
2765+
} else if (starttype == TokenType._const || starttype == TokenType._var || this.isUsingDecl()) {
27532766
if (kind == null) kind = String.valueOf(this.value);
27542767
if (!declaration && !kind.equals("var")) this.unexpected();
27552768
return this.parseVarStatement(startLoc, kind);
@@ -2859,7 +2872,7 @@ protected Statement parseForStatement(Position startLoc) {
28592872
if (this.isAwaitUsing() && this.inAsync) {
28602873
this.next(); // just skip the await and treat it as a `using` statement
28612874
}
2862-
if (this.type == TokenType._var || this.type == TokenType._const || isLet || this.type == TokenType._using) {
2875+
if (this.type == TokenType._var || this.type == TokenType._const || isLet || (this.type == TokenType.name && this.value.equals("using"))) {
28632876
Position initStartLoc = this.startLoc;
28642877
String kind = isLet ? "let" : String.valueOf(this.value);
28652878
this.next();

javascript/extractor/src/com/semmle/jcorn/TokenType.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ public void updateContext(Parser parser, TokenType prevType) {
180180
_try = new TokenType(kw("try")),
181181
_var = new TokenType(kw("var")),
182182
_const = new TokenType(kw("const")),
183-
_using = new TokenType(kw("using")),
184183
_while = new TokenType(kw("while").isLoop()),
185184
_with = new TokenType(kw("with")),
186185
_new = new TokenType(kw("new").beforeExpr().startsExpr()),

javascript/ql/test/library-tests/AST/ExplicitResource/printAst.expected

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
nodes
22
| file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) |
33
| file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) |
4+
| file://:0:0:0:0 | (Arguments) | semmle.label | (Arguments) |
5+
| file://:0:0:0:0 | (Parameters) | semmle.label | (Parameters) |
46
| tst.js:1:1:10:1 | [FunctionDeclStmt] functio ... } } | semmle.label | [FunctionDeclStmt] functio ... } } |
57
| tst.js:1:1:10:1 | [FunctionDeclStmt] functio ... } } | semmle.order | 1 |
68
| tst.js:1:10:1:10 | [VarDecl] g | semmle.label | [VarDecl] g |
@@ -47,11 +49,27 @@ nodes
4749
| tst.js:20:5:20:23 | [ExprStmt] console.log("end"); | semmle.label | [ExprStmt] console.log("end"); |
4850
| tst.js:20:13:20:15 | [Label] log | semmle.label | [Label] log |
4951
| tst.js:20:17:20:21 | [Literal] "end" | semmle.label | [Literal] "end" |
52+
| tst.js:23:1:28:1 | [FunctionDeclStmt] functio ... } } | semmle.label | [FunctionDeclStmt] functio ... } } |
53+
| tst.js:23:1:28:1 | [FunctionDeclStmt] functio ... } } | semmle.order | 3 |
54+
| tst.js:23:10:23:18 | [VarDecl] usesUsing | semmle.label | [VarDecl] usesUsing |
55+
| tst.js:23:22:28:1 | [BlockStmt] { u ... } } | semmle.label | [BlockStmt] { u ... } } |
56+
| tst.js:24:5:24:9 | [VarRef] using | semmle.label | [VarRef] using |
57+
| tst.js:24:5:24:16 | [CallExpr] using("foo") | semmle.label | [CallExpr] using("foo") |
58+
| tst.js:24:5:24:17 | [ExprStmt] using("foo"); | semmle.label | [ExprStmt] using("foo"); |
59+
| tst.js:24:11:24:15 | [Literal] "foo" | semmle.label | [Literal] "foo" |
60+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | semmle.label | [FunctionDeclStmt] functio ... . } |
61+
| tst.js:25:14:25:18 | [VarDecl] using | semmle.label | [VarDecl] using |
62+
| tst.js:25:20:25:22 | [SimpleParameter] foo | semmle.label | [SimpleParameter] foo |
63+
| tst.js:25:25:27:5 | [BlockStmt] { ... . } | semmle.label | [BlockStmt] { ... . } |
5064
edges
5165
| file://:0:0:0:0 | (Arguments) | tst.js:4:10:4:18 | [AssignExpr] test = 20 | semmle.label | 0 |
5266
| file://:0:0:0:0 | (Arguments) | tst.js:4:10:4:18 | [AssignExpr] test = 20 | semmle.order | 0 |
5367
| file://:0:0:0:0 | (Arguments) | tst.js:20:17:20:21 | [Literal] "end" | semmle.label | 0 |
5468
| file://:0:0:0:0 | (Arguments) | tst.js:20:17:20:21 | [Literal] "end" | semmle.order | 0 |
69+
| file://:0:0:0:0 | (Arguments) | tst.js:24:11:24:15 | [Literal] "foo" | semmle.label | 0 |
70+
| file://:0:0:0:0 | (Arguments) | tst.js:24:11:24:15 | [Literal] "foo" | semmle.order | 0 |
71+
| file://:0:0:0:0 | (Parameters) | tst.js:25:20:25:22 | [SimpleParameter] foo | semmle.label | 0 |
72+
| file://:0:0:0:0 | (Parameters) | tst.js:25:20:25:22 | [SimpleParameter] foo | semmle.order | 0 |
5573
| tst.js:1:1:10:1 | [FunctionDeclStmt] functio ... } } | tst.js:1:10:1:10 | [VarDecl] g | semmle.label | 0 |
5674
| tst.js:1:1:10:1 | [FunctionDeclStmt] functio ... } } | tst.js:1:10:1:10 | [VarDecl] g | semmle.order | 0 |
5775
| tst.js:1:1:10:1 | [FunctionDeclStmt] functio ... } } | tst.js:1:14:10:1 | [BlockStmt] { u ... } } | semmle.label | 5 |
@@ -136,5 +154,25 @@ edges
136154
| tst.js:20:5:20:22 | [MethodCallExpr] console.log("end") | tst.js:20:5:20:15 | [DotExpr] console.log | semmle.order | 0 |
137155
| tst.js:20:5:20:23 | [ExprStmt] console.log("end"); | tst.js:20:5:20:22 | [MethodCallExpr] console.log("end") | semmle.label | 1 |
138156
| tst.js:20:5:20:23 | [ExprStmt] console.log("end"); | tst.js:20:5:20:22 | [MethodCallExpr] console.log("end") | semmle.order | 1 |
157+
| tst.js:23:1:28:1 | [FunctionDeclStmt] functio ... } } | tst.js:23:10:23:18 | [VarDecl] usesUsing | semmle.label | 0 |
158+
| tst.js:23:1:28:1 | [FunctionDeclStmt] functio ... } } | tst.js:23:10:23:18 | [VarDecl] usesUsing | semmle.order | 0 |
159+
| tst.js:23:1:28:1 | [FunctionDeclStmt] functio ... } } | tst.js:23:22:28:1 | [BlockStmt] { u ... } } | semmle.label | 5 |
160+
| tst.js:23:1:28:1 | [FunctionDeclStmt] functio ... } } | tst.js:23:22:28:1 | [BlockStmt] { u ... } } | semmle.order | 5 |
161+
| tst.js:23:22:28:1 | [BlockStmt] { u ... } } | tst.js:24:5:24:17 | [ExprStmt] using("foo"); | semmle.label | 1 |
162+
| tst.js:23:22:28:1 | [BlockStmt] { u ... } } | tst.js:24:5:24:17 | [ExprStmt] using("foo"); | semmle.order | 1 |
163+
| tst.js:23:22:28:1 | [BlockStmt] { u ... } } | tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | semmle.label | 2 |
164+
| tst.js:23:22:28:1 | [BlockStmt] { u ... } } | tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | semmle.order | 2 |
165+
| tst.js:24:5:24:16 | [CallExpr] using("foo") | file://:0:0:0:0 | (Arguments) | semmle.label | 1 |
166+
| tst.js:24:5:24:16 | [CallExpr] using("foo") | file://:0:0:0:0 | (Arguments) | semmle.order | 1 |
167+
| tst.js:24:5:24:16 | [CallExpr] using("foo") | tst.js:24:5:24:9 | [VarRef] using | semmle.label | 0 |
168+
| tst.js:24:5:24:16 | [CallExpr] using("foo") | tst.js:24:5:24:9 | [VarRef] using | semmle.order | 0 |
169+
| tst.js:24:5:24:17 | [ExprStmt] using("foo"); | tst.js:24:5:24:16 | [CallExpr] using("foo") | semmle.label | 1 |
170+
| tst.js:24:5:24:17 | [ExprStmt] using("foo"); | tst.js:24:5:24:16 | [CallExpr] using("foo") | semmle.order | 1 |
171+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | file://:0:0:0:0 | (Parameters) | semmle.label | 1 |
172+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | file://:0:0:0:0 | (Parameters) | semmle.order | 1 |
173+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | tst.js:25:14:25:18 | [VarDecl] using | semmle.label | 0 |
174+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | tst.js:25:14:25:18 | [VarDecl] using | semmle.order | 0 |
175+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | tst.js:25:25:27:5 | [BlockStmt] { ... . } | semmle.label | 5 |
176+
| tst.js:25:5:27:5 | [FunctionDeclStmt] functio ... . } | tst.js:25:25:27:5 | [BlockStmt] { ... . } | semmle.order | 5 |
139177
graphProperties
140178
| semmle.graphKind | tree |

javascript/ql/test/library-tests/AST/ExplicitResource/tst.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ async function h() {
1818
}
1919

2020
console.log("end");
21+
}
22+
23+
function usesUsing() {
24+
using("foo");
25+
function using(foo) {
26+
// ...
27+
}
2128
}

0 commit comments

Comments
 (0)