use crate::lang_helpers::case_arms::ruby_in_clause_counts;
use crate::lang_helpers::doc_comment::{node_bytes, ruby_directive_opener, ruby_magic_comment};
use crate::lang_helpers::ruby::{
YardScope, is_stabby_lambda_body, ruby_documented_call, ruby_yard_reads,
};
use crate::role_table;
use crate::{Ancestors, Node, Ruby};
fn under_binary<'a>(node: &Node<'a>, _code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
ancestors
.parent(node)
.is_some_and(|p| comparison::OWNERS.contains(&p.kind_id()))
}
fn is_decision_in_clause(node: &Node<'_>, code: &[u8]) -> bool {
ruby_in_clause_counts(node, code)
}
fn not_a_lambda_body<'a>(node: &Node<'a>, _code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
!is_stabby_lambda_body(node, ancestors)
}
fn is_assignment_target<'a>(node: &Node<'a>, _code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
ancestors.parent(node).is_some_and(|parent| {
matches!(
parent.kind_id().into(),
Ruby::Assignment | Ruby::Assignment2
) && parent.child(0).is_some_and(|first| first.id() == node.id())
})
}
fn kernel_raise_or_exit(node: &Node<'_>, code: &[u8]) -> bool {
node.child_by_field_name("method").is_some_and(|method| {
method.kind_id() == Ruby::Identifier as u16
&& matches!(method.utf8_text(code), Some("raise" | "exit" | "exit!"))
}) && node.child_by_field_name("receiver").is_none_or(|receiver| {
receiver.kind_id() == Ruby::Constant as u16 && receiver.utf8_text(code) == Some("Kernel")
})
}
fn alias_names_a_method(node: &Node<'_>, _: &[u8]) -> bool {
node.child_by_field_name("name")
.is_none_or(|name| name.kind_id() != Ruby::GlobalVariable as u16)
}
fn singleton_of_a_constant(node: &Node<'_>, _: &[u8]) -> bool {
node.child_by_field_name("value").is_some_and(|value| {
matches!(
value.kind_id().into(),
Ruby::Constant | Ruby::ScopeResolution | Ruby::ScopeResolution2
)
})
}
fn binds_a_constant_or_class_variable(node: &Node<'_>, _: &[u8]) -> bool {
node.child_by_field_name("left").is_some_and(|left| {
matches!(
left.kind_id().into(),
Ruby::Constant | Ruby::ScopeResolution | Ruby::ScopeResolution2 | Ruby::ClassVariable
)
})
}
fn yard_documented_call(node: &Node<'_>, code: &[u8]) -> bool {
ruby_documented_call(node, code)
}
fn opens_a_directive_block_where_yard_reads<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
ruby_directive_opener(node_bytes(node, code))
&& ruby_yard_reads(YardScope::Namespace, node, ancestors)
}
crate::roles::byte_gate!(is_magic_comment, ruby_magic_comment);
role_table! {
lang: Ruby;
Call { owns [Call, Call2, Call3, Call4] }
StringLit {
owns [
String,
ChainedString,
BareString,
Subshell,
Regex,
HeredocBody,
DelimitedSymbol,
SimpleSymbol,
StringArray,
SymbolArray,
Character,
]
wraps [Interpolation, String, BareString, BareSymbol]
}
Comparison {
owns [
Binary(holds_payer),
Binary2(holds_payer),
Binary3(holds_payer),
]
pays [
EQEQ gated_by under_binary, BANGEQ gated_by under_binary,
EQEQEQ gated_by under_binary, LT gated_by under_binary, GT gated_by under_binary,
LTEQ gated_by under_binary, GTEQ gated_by under_binary, LTEQGT gated_by under_binary,
EQTILDE gated_by under_binary, BANGTILDE gated_by under_binary,
]
project "operator"
}
ShortCircuit {
owns [
Binary(holds_payer),
Binary2(holds_payer),
Binary3(holds_payer),
]
pays [AMPAMP, PIPEPIPE, And, Or]
project "operator"
}
EagerLogical {
owns []
excludes [
AMP "a boolean `&` is the operator method's kind, which the grammar does not tell from the integral one",
PIPE "a boolean `|` is the operator method's kind, which the grammar does not tell from the integral one",
CARET "a boolean `^` is the operator method's kind, which the grammar does not tell from the integral one",
]
}
Ternary {
owns [Conditional]
pays [QMARK]
project "operator"
}
NullSafe {
owns [
Call(holds_payer), Call2(holds_payer),
Call3(holds_payer), Call4(holds_payer),
]
pays [AMPDOT]
project "operator"
}
CaseArm {
owns [When, InClause(text_gated_by is_decision_in_clause no_chain)]
excludes [
Else "the fallback arm of a `case`, no decision (lesson 11); an `if`'s `else` is `Branch`'s",
When2 "the arm's keyword, billed as such; the arm pays",
In2 "the pattern arm's keyword, billed as such; the arm pays",
]
}
Guard { owns [Guard, IfGuard, UnlessGuard] }
Branch { owns [If, Unless, IfModifier, UnlessModifier] }
Switch { owns [Case, CaseMatch] }
Loop { owns [While, Until, For, WhileModifier, UntilModifier] }
Catch { owns [Rescue, RescueModifier, RescueModifier2, RescueModifier3] }
ElseIf { owns [Elsif(pays_itself)] pays [] }
Exit {
owns [
Return, Return2,
Call(text_gated_by kernel_raise_or_exit no_chain),
Call2(text_gated_by kernel_raise_or_exit no_chain),
Call3(text_gated_by kernel_raise_or_exit no_chain),
Call4(text_gated_by kernel_raise_or_exit no_chain),
]
}
Modifier { absent "Ruby's `private` / `protected` are method calls read by name in the \
method consumer, not keywords" }
DeclKeyword { owns [Def, Class2, Module2, Alias2, Undef2] }
Primitive { absent "Ruby has no type names" }
FuncOpener {
owns [
Method(callable opens Function),
SingletonMethod(callable opens Function),
Lambda(opens Function),
Block(opens Function gated_by not_a_lambda_body),
DoBlock(opens Function gated_by not_a_lambda_body),
]
}
Closure {
owns [
Lambda,
Block(gated_by not_a_lambda_body),
DoBlock(gated_by not_a_lambda_body),
]
}
Container {
owns [
Program(opens Unit),
Class(opens Class),
SingletonClass(opens Class),
Module(opens Namespace),
]
}
Member {
owns [
InstanceVariable(gated_by is_assignment_target),
ClassVariable(gated_by is_assignment_target),
]
}
Comment { owns [Comment] }
DocComment { absent "YARD reads a comment run by its position above a statement, never by a marker" }
Docstring { absent "Ruby documents with RDoc / YARD comments, not string literals" }
DocTarget {
owns [
Method,
SingletonMethod,
Class,
Module,
Alias(gated_by alias_names_a_method no_chain),
SingletonClass(gated_by singleton_of_a_constant no_chain),
Assignment(gated_by binds_a_constant_or_class_variable no_chain),
Assignment2(gated_by binds_a_constant_or_class_variable no_chain),
Call(text_gated_by yard_documented_call no_chain),
Call2(text_gated_by yard_documented_call no_chain),
Call3(text_gated_by yard_documented_call no_chain),
Call4(text_gated_by yard_documented_call no_chain),
]
}
DocRunOpener { owns [Comment(text_gated_by opens_a_directive_block_where_yard_reads)] }
DocRunDirective { owns [Comment(text_gated_by is_magic_comment no_chain)] }
DocBodyResidual { absent "YARD reads a comment run by scope, which the Checker folds; nothing beyond the `def` withholds one" }
}