Ruby: upgrade/downgrade scripts

This commit is contained in:
Arthur Baars 2022-08-24 17:52:04 +02:00
Родитель 59773eb743
Коммит ed005077fa
28 изменённых файлов: 6284 добавлений и 0 удалений

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,28 @@
class AstNode extends @ruby_ast_node_parent {
string toString() { none() }
}
class Location extends @location {
string toString() { none() }
}
class Wrapper = @ruby_body_statement or @ruby_block_body;
predicate astNodeInfo(AstNode child, AstNode parent, int primaryIndex, int secondaryIndex) {
not parent instanceof Wrapper and
exists(AstNode node, Location l |
ruby_ast_node_info(node, parent, primaryIndex, _) and
(
not node instanceof Wrapper and secondaryIndex = 0 and child = node
or
node instanceof Wrapper and ruby_ast_node_info(child, node, secondaryIndex, _)
)
)
}
from AstNode node, AstNode parent, int parent_index, Location loc
where
node =
rank[parent_index + 1](AstNode n, int i, int j | astNodeInfo(n, parent, i, j) | n order by i, j) and
ruby_ast_node_info(node, _, _, loc)
select node, parent, parent_index, loc

Просмотреть файл

@ -0,0 +1,7 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_block, AstNode body, int index, AstNode child
where ruby_block_body(ruby_block, body) and ruby_block_body_child(body, index, child)
select ruby_block, index, child

Просмотреть файл

@ -0,0 +1,7 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_class, AstNode body, int index, AstNode child
where ruby_class_body(ruby_class, body) and ruby_body_statement_child(body, index, child)
select ruby_class, index, child

Просмотреть файл

@ -0,0 +1,8 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_do_block, AstNode body, int index, AstNode child
where ruby_do_block_body(ruby_do_block, body) and ruby_body_statement_child(body, index, child)
select ruby_do_block, index, child

Просмотреть файл

@ -0,0 +1,14 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_method, int index, AstNode child
where
exists(AstNode body |
ruby_method_body(ruby_method, body) and ruby_body_statement_child(body, index, child)
)
or
ruby_method_body(ruby_method, child) and
not child instanceof @ruby_body_statement and
index = 0
select ruby_method, index, child

Просмотреть файл

@ -0,0 +1,8 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_module, AstNode body, int index, AstNode child
where ruby_module_body(ruby_module, body) and ruby_body_statement_child(body, index, child)
select ruby_module, index, child

Просмотреть файл

@ -0,0 +1,8 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_singleton_class, AstNode body, int index, AstNode child
where ruby_singleton_class_body(ruby_singleton_class, body) and ruby_body_statement_child(body, index, child)
select ruby_singleton_class, index, child

Просмотреть файл

@ -0,0 +1,15 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_singleton_method, int index, AstNode child
where
exists(AstNode body |
ruby_singleton_method_body(ruby_singleton_method, body) and
ruby_body_statement_child(body, index, child)
)
or
ruby_singleton_method_body(ruby_singleton_method, child) and
not child instanceof @ruby_body_statement and
index = 0
select ruby_singleton_method, index, child

Просмотреть файл

@ -0,0 +1,30 @@
description: Wrap class, module, method, and block bodies in a named node
compatibility: full
ruby_block_body.rel: delete
ruby_block_body_def.rel: delete
ruby_block_body_child.rel: delete
ruby_block_child.rel: run ruby_block_child.qlo
ruby_body_statement_child.rel: delete
ruby_body_statement_def.rel: delete
ruby_class_body.rel: delete
ruby_class_child.rel: run ruby_class_child.qlo
ruby_do_block_body.rel: delete
ruby_do_block_child.rel: run ruby_do_block_child.qlo
ruby_method_body.rel: delete
ruby_method_child.rel: run ruby_method_child.qlo
ruby_module_body.rel: delete
ruby_module_child.rel: run ruby_module_child.qlo
ruby_singleton_class_body.rel: delete
ruby_singleton_class_child.rel: run ruby_singleton_class_child.qlo
ruby_singleton_method_body.rel: delete
ruby_singleton_method_child.rel: run ruby_singleton_method_child.qlo
ruby_ast_node_info.rel: run ruby_ast_node_info.qlo

Просмотреть файл

@ -0,0 +1,68 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
class Location extends @location {
string toString() { none() }
}
class File extends @file {
string toString() { none() }
}
private predicate body_statement(AstNode body, int index, Location loc) {
exists(AstNode node, AstNode child | ruby_ast_node_info(child, _, _, loc) |
ruby_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_class_child(node, index, child)
or
ruby_do_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_do_block_child(node, index, child)
or
ruby_method_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_method_child(node, index, child)
or
ruby_module_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_module_child(node, index, child)
or
ruby_singleton_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_class_child(node, index, child)
or
ruby_singleton_method_def(node, _, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_method_child(node, index, child)
or
ruby_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "}") and
ruby_block_child(node, index, child)
)
}
from Location loc, File file, int start_line, int start_column, int end_line, int end_column
where
locations_default(loc, file, start_line, start_column, end_line, end_column) and
not exists(AstNode node | ruby_ast_node_info(node, _, _, loc) and body_statement(node, _, _))
or
exists(AstNode node |
ruby_ast_node_info(node, _, _, loc) and
exists(Location first |
body_statement(node, 0, first) and
locations_default(first, file, start_line, start_column, _, _)
) and
exists(Location last |
last = max(Location l, int i | body_statement(node, i, l) | l order by i) and
locations_default(last, file, _, _, end_line, end_column)
)
)
select loc, file, start_line, start_column, end_line, end_column

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,76 @@
class AstNode extends @ruby_ast_node_parent {
string toString() { none() }
}
class Location extends @location {
string toString() { none() }
}
private predicate body_statement(AstNode body, AstNode firstChild) {
exists(AstNode node |
ruby_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_class_child(node, 0, firstChild)
or
ruby_do_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_do_block_child(node, 0, firstChild)
or
ruby_method_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_method_child(node, 0, firstChild)
or
ruby_module_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_module_child(node, 0, firstChild)
or
ruby_singleton_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_class_child(node, 0, firstChild)
or
ruby_singleton_method_def(node, _, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_method_child(node, 0, firstChild)
or
ruby_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "}") and
ruby_block_child(node, 0, firstChild)
)
}
private predicate body_statement_child(AstNode body, int index, AstNode child) {
exists(AstNode parent, AstNode firstChild, int firstChildIndex |
body_statement(body, firstChild) and
ruby_ast_node_info(firstChild, parent, firstChildIndex, _) and
child =
rank[index + 1](AstNode c, int i |
ruby_ast_node_info(c, parent, i, _) and i >= firstChildIndex and c != body
|
c order by i
)
)
}
private predicate astNodeInfo(AstNode node, AstNode parent, int parent_index, Location loc) {
ruby_ast_node_info(node, parent, parent_index, loc) and
not body_statement(node, _) and
not body_statement_child(_, _, node)
}
from AstNode node, AstNode parent, int parent_index, Location loc
where
astNodeInfo(node, parent, parent_index, loc)
or
body_statement_child(parent, parent_index, node) and ruby_ast_node_info(node, _, _, loc)
or
body_statement(node, _) and
ruby_ast_node_info(node, parent, _, loc) and
parent_index = count(AstNode n | astNodeInfo(n, parent, _, _))
select node, parent, parent_index, loc

Просмотреть файл

@ -0,0 +1,11 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_block, AstNode body
where
ruby_block_def(ruby_block) and
ruby_ast_node_info(body, ruby_block, _, _) and
ruby_tokeninfo(body, _, "}") and
ruby_block_child(ruby_block, _, _)
select ruby_block, body

Просмотреть файл

@ -0,0 +1,11 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode ruby_block, AstNode body, int index, AstNode child
where
ruby_block_def(ruby_block) and
ruby_ast_node_info(body, ruby_block, _, _) and
ruby_tokeninfo(body, _, "}") and
ruby_block_child(ruby_block, index, child)
select body, index, child

Просмотреть файл

@ -0,0 +1,18 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
/*
* It's not possible to generate fresh IDs for the new ruby_block_body nodes,
* therefore we re-purpose the "}"-token that closes the block and use its ID instead.
* As a result the AST will be missing the "}" tokens, but those are unlikely to be used
* for anything.
*/
from AstNode ruby_block, AstNode body
where
ruby_block_def(ruby_block) and
ruby_ast_node_info(body, ruby_block, _, _) and
ruby_tokeninfo(body, _, "}") and
ruby_block_child(ruby_block, _, _)
select body

Просмотреть файл

@ -0,0 +1,36 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body, int index, AstNode child
where
ruby_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_class_child(node, index, child)
or
ruby_do_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_do_block_child(node, index, child)
or
ruby_method_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_method_child(node, index, child)
or
ruby_module_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_module_child(node, index, child)
or
ruby_singleton_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_class_child(node, index, child)
or
ruby_singleton_method_def(node, _, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_method_child(node, index, child)
select body, index, child

Просмотреть файл

@ -0,0 +1,43 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
/*
* It's not possible to generate fresh IDs for the new ruby_body_statement nodes,
* therefore we re-purpose the "end"-token that closes the block and use its ID instead.
* As a result the AST will be missing the "end" tokens, but those are unlikely to be used
* for anything.
*/
from AstNode node, AstNode body
where
ruby_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_class_child(node, _, _)
or
ruby_do_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_do_block_child(node, _, _)
or
ruby_method_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_method_child(node, _, _)
or
ruby_module_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_module_child(node, _, _)
or
ruby_singleton_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_class_child(node, _, _)
or
ruby_singleton_method_def(node, _, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_method_child(node, _, _)
select body

Просмотреть файл

@ -0,0 +1,11 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body
where
ruby_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_class_child(node, _, _)
select node, body

Просмотреть файл

@ -0,0 +1,11 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body
where
ruby_do_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_do_block_child(node, _, _)
select node, body

Просмотреть файл

@ -0,0 +1,20 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body
where
ruby_method_def(node, _) and
(
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_method_child(node, _, _)
or
ruby_method_child(node, 0, body) and
not exists(AstNode n |
ruby_ast_node_info(n, node, _, _) and
ruby_tokeninfo(n, _, "end")
)
)
// TODO : handle end-less methods
select node, body

Просмотреть файл

@ -0,0 +1,11 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body
where
ruby_module_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_module_child(node, _, _)
select node, body

Просмотреть файл

@ -0,0 +1,11 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body
where
ruby_singleton_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_class_child(node, _, _)
select node, body

Просмотреть файл

@ -0,0 +1,17 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
from AstNode node, AstNode body
where
ruby_singleton_method_def(node, _, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_method_child(node, _, _)
or
ruby_singleton_method_child(node, 0, body) and
not exists(AstNode n |
ruby_ast_node_info(n, node, _, _) and
ruby_tokeninfo(n, _, "end")
)
select node, body

Просмотреть файл

@ -0,0 +1,46 @@
class AstNode extends @ruby_ast_node {
string toString() { none() }
}
private predicate body_statement(AstNode body) {
exists(AstNode node |
ruby_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_class_child(node, _, _)
or
ruby_do_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_do_block_child(node, _, _)
or
ruby_method_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_method_child(node, _, _)
or
ruby_module_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_module_child(node, _, _)
or
ruby_singleton_class_def(node, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_class_child(node, _, _)
or
ruby_singleton_method_def(node, _, _) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "end") and
ruby_singleton_method_child(node, _, _)
or
ruby_block_def(node) and
ruby_ast_node_info(body, node, _, _) and
ruby_tokeninfo(body, _, "}") and
ruby_block_child(node, _, _)
)
}
from AstNode token, int kind, string value
where ruby_tokeninfo(token, kind, value) and not body_statement(token)
select token, kind, value

Просмотреть файл

@ -0,0 +1,33 @@
description: Wrap class, module, method, and block bodies in a named node
compatibility: partial
ruby_block_body.rel: run ruby_block_body.qlo
ruby_block_body_def.rel: run ruby_block_body_def.qlo
ruby_block_body_child.rel: run ruby_block_body_child.qlo
ruby_block_child.rel: delete
ruby_body_statement_child.rel: run ruby_body_statement_child.qlo
ruby_body_statement_def.rel: run ruby_body_statement_def.qlo
ruby_class_body.rel: run ruby_class_body.qlo
ruby_class_child.rel: delete
ruby_do_block_body.rel: run ruby_do_block_body.qlo
ruby_do_block_child.rel: delete
ruby_method_body.rel: run ruby_method_body.qlo
ruby_method_child.rel: delete
ruby_module_body.rel: run ruby_module_body.qlo
ruby_module_child.rel: delete
ruby_singleton_class_body.rel: run ruby_singleton_class_body.qlo
ruby_singleton_class_child.rel: delete
ruby_singleton_method_body.rel: run ruby_singleton_method_body.qlo
ruby_singleton_method_child.rel: delete
ruby_ast_node_info.rel: run ruby_ast_node_info.qlo
ruby_tokeninfo.rel: run ruby_tokeninfo.qlo
locations_default.rel: run locations_default.qlo