Skip to content

Commit 59773eb

Browse files
committed
Ruby: update tree-sitter grammar
1 parent 20ac15d commit 59773eb

File tree

9 files changed

+1186
-1070
lines changed

9 files changed

+1186
-1070
lines changed

ruby/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby/extractor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ flate2 = "1.0"
1111
node-types = { path = "../node-types" }
1212
tree-sitter = "0.19"
1313
tree-sitter-embedded-template = { git = "https://github.com/tree-sitter/tree-sitter-embedded-template.git", rev = "1a538da253d73f896b9f6c0c7d79cda58791ac5c" }
14-
tree-sitter-ruby = { git = "https://github.com/tree-sitter/tree-sitter-ruby.git", rev = "e75d04404c9dd71ad68850d5c672b226d5e694f3" }
14+
tree-sitter-ruby = { git = "https://github.com/tree-sitter/tree-sitter-ruby.git", rev = "ad1043283b1f9daf4aad381b6a81f18a5a27fe7e" }
1515
clap = "3.0"
1616
tracing = "0.1"
1717
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }

ruby/generator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ node-types = { path = "../node-types" }
1212
tracing = "0.1"
1313
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
1414
tree-sitter-embedded-template = { git = "https://github.com/tree-sitter/tree-sitter-embedded-template.git", rev = "1a538da253d73f896b9f6c0c7d79cda58791ac5c" }
15-
tree-sitter-ruby = { git = "https://github.com/tree-sitter/tree-sitter-ruby.git", rev = "e75d04404c9dd71ad68850d5c672b226d5e694f3" }
15+
tree-sitter-ruby = { git = "https://github.com/tree-sitter/tree-sitter-ruby.git", rev = "ad1043283b1f9daf4aad381b6a81f18a5a27fe7e" }

ruby/ql/lib/codeql/ruby/ast/internal/Expr.qll

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,35 @@ class Ensure extends StmtSequence, TEnsure {
5151

5252
// Not defined by dispatch, as it should not be exposed
5353
Ruby::AstNode getBodyStmtChild(TBodyStmt b, int i) {
54-
result = any(Ruby::Method g | b = TMethod(g)).getChild(i)
54+
exists(Ruby::Method g, Ruby::AstNode body | b = TMethod(g) and body = g.getBody() |
55+
result = body.(Ruby::BodyStatement).getChild(i)
56+
or
57+
i = 0 and result = body and not body instanceof Ruby::BodyStatement
58+
)
5559
or
56-
result = any(Ruby::SingletonMethod g | b = TSingletonMethod(g)).getChild(i)
60+
exists(Ruby::SingletonMethod g, Ruby::AstNode body |
61+
b = TSingletonMethod(g) and body = g.getBody()
62+
|
63+
result = body.(Ruby::BodyStatement).getChild(i)
64+
or
65+
i = 0 and result = body and not body instanceof Ruby::BodyStatement
66+
)
5767
or
5868
exists(Ruby::Lambda g | b = TLambda(g) |
59-
result = g.getBody().(Ruby::DoBlock).getChild(i) or
60-
result = g.getBody().(Ruby::Block).getChild(i)
69+
result = g.getBody().(Ruby::DoBlock).getBody().getChild(i) or
70+
result = g.getBody().(Ruby::Block).getBody().getChild(i)
6171
)
6272
or
63-
result = any(Ruby::DoBlock g | b = TDoBlock(g)).getChild(i)
73+
result = any(Ruby::DoBlock g | b = TDoBlock(g)).getBody().getChild(i)
6474
or
6575
result = any(Ruby::Program g | b = TToplevel(g)).getChild(i) and
6676
not result instanceof Ruby::BeginBlock
6777
or
68-
result = any(Ruby::Class g | b = TClassDeclaration(g)).getChild(i)
78+
result = any(Ruby::Class g | b = TClassDeclaration(g)).getBody().getChild(i)
6979
or
70-
result = any(Ruby::SingletonClass g | b = TSingletonClass(g)).getChild(i)
80+
result = any(Ruby::SingletonClass g | b = TSingletonClass(g)).getBody().getChild(i)
7181
or
72-
result = any(Ruby::Module g | b = TModuleDeclaration(g)).getChild(i)
82+
result = any(Ruby::Module g | b = TModuleDeclaration(g)).getBody().getChild(i)
7383
or
7484
result = any(Ruby::Begin g | b = TBeginExpr(g)).getChild(i)
7585
}

ruby/ql/lib/codeql/ruby/ast/internal/Method.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BraceBlockReal extends BraceBlock, TBraceBlockReal {
1515
toGenerated(result) = g.getParameters().getChild(n)
1616
}
1717

18-
final override Stmt getStmt(int i) { toGenerated(result) = g.getChild(i) }
18+
final override Stmt getStmt(int i) { toGenerated(result) = g.getBody().getChild(i) }
1919
}
2020

2121
/**

ruby/ql/lib/codeql/ruby/ast/internal/TreeSitter.qll

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,15 @@ module Ruby {
311311
/** Gets the name of the primary QL class for this element. */
312312
final override string getAPrimaryQlClass() { result = "Block" }
313313

314+
/** Gets the node corresponding to the field `body`. */
315+
final BlockBody getBody() { ruby_block_body(this, result) }
316+
314317
/** Gets the node corresponding to the field `parameters`. */
315318
final BlockParameters getParameters() { ruby_block_parameters(this, result) }
316319

317-
/** Gets the `i`th child of this node. */
318-
final AstNode getChild(int i) { ruby_block_child(this, i, result) }
319-
320320
/** Gets a field or child node of this node. */
321321
final override AstNode getAFieldOrChild() {
322-
ruby_block_parameters(this, result) or ruby_block_child(this, _, result)
322+
ruby_block_body(this, result) or ruby_block_parameters(this, result)
323323
}
324324
}
325325

@@ -335,6 +335,18 @@ module Ruby {
335335
final override AstNode getAFieldOrChild() { ruby_block_argument_child(this, result) }
336336
}
337337

338+
/** A class representing `block_body` nodes. */
339+
class BlockBody extends @ruby_block_body, AstNode {
340+
/** Gets the name of the primary QL class for this element. */
341+
final override string getAPrimaryQlClass() { result = "BlockBody" }
342+
343+
/** Gets the `i`th child of this node. */
344+
final AstNode getChild(int i) { ruby_block_body_child(this, i, result) }
345+
346+
/** Gets a field or child node of this node. */
347+
final override AstNode getAFieldOrChild() { ruby_block_body_child(this, _, result) }
348+
}
349+
338350
/** A class representing `block_parameter` nodes. */
339351
class BlockParameter extends @ruby_block_parameter, AstNode {
340352
/** Gets the name of the primary QL class for this element. */
@@ -364,6 +376,18 @@ module Ruby {
364376
}
365377
}
366378

379+
/** A class representing `body_statement` nodes. */
380+
class BodyStatement extends @ruby_body_statement, AstNode {
381+
/** Gets the name of the primary QL class for this element. */
382+
final override string getAPrimaryQlClass() { result = "BodyStatement" }
383+
384+
/** Gets the `i`th child of this node. */
385+
final AstNode getChild(int i) { ruby_body_statement_child(this, i, result) }
386+
387+
/** Gets a field or child node of this node. */
388+
final override AstNode getAFieldOrChild() { ruby_body_statement_child(this, _, result) }
389+
}
390+
367391
/** A class representing `break` nodes. */
368392
class Break extends @ruby_break, AstNode {
369393
/** Gets the name of the primary QL class for this element. */
@@ -468,20 +492,20 @@ module Ruby {
468492
/** Gets the name of the primary QL class for this element. */
469493
final override string getAPrimaryQlClass() { result = "Class" }
470494

495+
/** Gets the node corresponding to the field `body`. */
496+
final BodyStatement getBody() { ruby_class_body(this, result) }
497+
471498
/** Gets the node corresponding to the field `name`. */
472499
final AstNode getName() { ruby_class_def(this, result) }
473500

474501
/** Gets the node corresponding to the field `superclass`. */
475502
final Superclass getSuperclass() { ruby_class_superclass(this, result) }
476503

477-
/** Gets the `i`th child of this node. */
478-
final AstNode getChild(int i) { ruby_class_child(this, i, result) }
479-
480504
/** Gets a field or child node of this node. */
481505
final override AstNode getAFieldOrChild() {
506+
ruby_class_body(this, result) or
482507
ruby_class_def(this, result) or
483-
ruby_class_superclass(this, result) or
484-
ruby_class_child(this, _, result)
508+
ruby_class_superclass(this, result)
485509
}
486510
}
487511

@@ -592,15 +616,15 @@ module Ruby {
592616
/** Gets the name of the primary QL class for this element. */
593617
final override string getAPrimaryQlClass() { result = "DoBlock" }
594618

619+
/** Gets the node corresponding to the field `body`. */
620+
final BodyStatement getBody() { ruby_do_block_body(this, result) }
621+
595622
/** Gets the node corresponding to the field `parameters`. */
596623
final BlockParameters getParameters() { ruby_do_block_parameters(this, result) }
597624

598-
/** Gets the `i`th child of this node. */
599-
final AstNode getChild(int i) { ruby_do_block_child(this, i, result) }
600-
601625
/** Gets a field or child node of this node. */
602626
final override AstNode getAFieldOrChild() {
603-
ruby_do_block_parameters(this, result) or ruby_do_block_child(this, _, result)
627+
ruby_do_block_body(this, result) or ruby_do_block_parameters(this, result)
604628
}
605629
}
606630

@@ -1106,20 +1130,20 @@ module Ruby {
11061130
/** Gets the name of the primary QL class for this element. */
11071131
final override string getAPrimaryQlClass() { result = "Method" }
11081132

1133+
/** Gets the node corresponding to the field `body`. */
1134+
final AstNode getBody() { ruby_method_body(this, result) }
1135+
11091136
/** Gets the node corresponding to the field `name`. */
11101137
final UnderscoreMethodName getName() { ruby_method_def(this, result) }
11111138

11121139
/** Gets the node corresponding to the field `parameters`. */
11131140
final MethodParameters getParameters() { ruby_method_parameters(this, result) }
11141141

1115-
/** Gets the `i`th child of this node. */
1116-
final AstNode getChild(int i) { ruby_method_child(this, i, result) }
1117-
11181142
/** Gets a field or child node of this node. */
11191143
final override AstNode getAFieldOrChild() {
1144+
ruby_method_body(this, result) or
11201145
ruby_method_def(this, result) or
1121-
ruby_method_parameters(this, result) or
1122-
ruby_method_child(this, _, result)
1146+
ruby_method_parameters(this, result)
11231147
}
11241148
}
11251149

@@ -1140,15 +1164,15 @@ module Ruby {
11401164
/** Gets the name of the primary QL class for this element. */
11411165
final override string getAPrimaryQlClass() { result = "Module" }
11421166

1167+
/** Gets the node corresponding to the field `body`. */
1168+
final BodyStatement getBody() { ruby_module_body(this, result) }
1169+
11431170
/** Gets the node corresponding to the field `name`. */
11441171
final AstNode getName() { ruby_module_def(this, result) }
11451172

1146-
/** Gets the `i`th child of this node. */
1147-
final AstNode getChild(int i) { ruby_module_child(this, i, result) }
1148-
11491173
/** Gets a field or child node of this node. */
11501174
final override AstNode getAFieldOrChild() {
1151-
ruby_module_def(this, result) or ruby_module_child(this, _, result)
1175+
ruby_module_body(this, result) or ruby_module_def(this, result)
11521176
}
11531177
}
11541178

@@ -1504,15 +1528,15 @@ module Ruby {
15041528
/** Gets the name of the primary QL class for this element. */
15051529
final override string getAPrimaryQlClass() { result = "SingletonClass" }
15061530

1531+
/** Gets the node corresponding to the field `body`. */
1532+
final BodyStatement getBody() { ruby_singleton_class_body(this, result) }
1533+
15071534
/** Gets the node corresponding to the field `value`. */
15081535
final UnderscoreArg getValue() { ruby_singleton_class_def(this, result) }
15091536

1510-
/** Gets the `i`th child of this node. */
1511-
final AstNode getChild(int i) { ruby_singleton_class_child(this, i, result) }
1512-
15131537
/** Gets a field or child node of this node. */
15141538
final override AstNode getAFieldOrChild() {
1515-
ruby_singleton_class_def(this, result) or ruby_singleton_class_child(this, _, result)
1539+
ruby_singleton_class_body(this, result) or ruby_singleton_class_def(this, result)
15161540
}
15171541
}
15181542

@@ -1521,6 +1545,9 @@ module Ruby {
15211545
/** Gets the name of the primary QL class for this element. */
15221546
final override string getAPrimaryQlClass() { result = "SingletonMethod" }
15231547

1548+
/** Gets the node corresponding to the field `body`. */
1549+
final AstNode getBody() { ruby_singleton_method_body(this, result) }
1550+
15241551
/** Gets the node corresponding to the field `name`. */
15251552
final UnderscoreMethodName getName() { ruby_singleton_method_def(this, result, _) }
15261553

@@ -1530,15 +1557,12 @@ module Ruby {
15301557
/** Gets the node corresponding to the field `parameters`. */
15311558
final MethodParameters getParameters() { ruby_singleton_method_parameters(this, result) }
15321559

1533-
/** Gets the `i`th child of this node. */
1534-
final AstNode getChild(int i) { ruby_singleton_method_child(this, i, result) }
1535-
15361560
/** Gets a field or child node of this node. */
15371561
final override AstNode getAFieldOrChild() {
1562+
ruby_singleton_method_body(this, result) or
15381563
ruby_singleton_method_def(this, result, _) or
15391564
ruby_singleton_method_def(this, _, result) or
1540-
ruby_singleton_method_parameters(this, result) or
1541-
ruby_singleton_method_child(this, _, result)
1565+
ruby_singleton_method_parameters(this, result)
15421566
}
15431567
}
15441568

ruby/ql/lib/codeql/ruby/ast/internal/Variable.qll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,18 @@ private module Cached {
200200
or
201201
i = any(Ruby::Binary x).getRight()
202202
or
203-
i = any(Ruby::Block x).getChild(_)
204-
or
205203
i = any(Ruby::BlockArgument x).getChild()
206204
or
205+
i = any(Ruby::BlockBody x).getChild(_)
206+
or
207+
i = any(Ruby::BodyStatement x).getChild(_)
208+
or
207209
i = any(Ruby::Call x).getReceiver()
208210
or
209211
i = any(Ruby::Case x).getValue()
210212
or
211213
i = any(Ruby::CaseMatch x).getValue()
212214
or
213-
i = any(Ruby::Class x).getChild(_)
214-
or
215215
i = any(Ruby::Conditional x).getCondition()
216216
or
217217
i = any(Ruby::Conditional x).getConsequence()
@@ -220,8 +220,6 @@ private module Cached {
220220
or
221221
i = any(Ruby::Do x).getChild(_)
222222
or
223-
i = any(Ruby::DoBlock x).getChild(_)
224-
or
225223
i = any(Ruby::ElementReference x).getChild(_)
226224
or
227225
i = any(Ruby::ElementReference x).getObject()
@@ -250,9 +248,7 @@ private module Cached {
250248
or
251249
i = any(Ruby::KeywordParameter x).getValue()
252250
or
253-
i = any(Ruby::Method x).getChild(_)
254-
or
255-
i = any(Ruby::Module x).getChild(_)
251+
i = any(Ruby::Method x).getBody()
256252
or
257253
i = any(Ruby::OperatorAssignment x).getRight()
258254
or
@@ -282,9 +278,7 @@ private module Cached {
282278
or
283279
i = any(Ruby::SingletonClass x).getValue()
284280
or
285-
i = any(Ruby::SingletonClass x).getChild(_)
286-
or
287-
i = any(Ruby::SingletonMethod x).getChild(_)
281+
i = any(Ruby::SingletonMethod x).getBody()
288282
or
289283
i = any(Ruby::SingletonMethod x).getObject()
290284
or

0 commit comments

Comments
 (0)