#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
use super::*;
use crate::lang_helpers::ruby::{YardScope, ruby_yard_reads};
fn ruby_documented_core<'a, 'c>(
node: &Node<'a>,
ancestors: Ancestors<'a, 'c>,
) -> Option<(Node<'a>, Option<ChildAncestors<'a, 'c>>)> {
let mut node = *node;
let mut chain: Option<ChildAncestors<'a, 'c>> = None;
loop {
let inner = match node.kind_id().into() {
Ruby::IfModifier | Ruby::UnlessModifier => node.child_by_field_name("body")?,
Ruby::BodyStatement | Ruby::BodyStatement2 | Ruby::BlockBody => node.child(0)?,
_ => return Some((node, chain)),
};
match &mut chain {
Some(chain) => chain.descend(&node),
None => chain = Some(ancestors.of_children(&node)),
}
node = inner;
}
}
fn ruby_then_first_statement<'a>(node: &Node<'a>) -> Option<Node<'a>> {
(node.kind_id() == Ruby::Then)
.then(|| node.children().find(Node::is_named))
.flatten()
}
impl Checker for RubyCode {
impl_comment_from_rows!(ruby);
fn is_doc_target<'a>(node: &Node<'a>, code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
let (statement, chain) = match ruby_then_first_statement(node) {
Some(first) => (first, Some(ancestors.of_children(node))),
None => (*node, None),
};
let statement_chain = chain.as_ref().map_or(ancestors, ChildAncestors::get);
ruby_documented_core(&statement, statement_chain).is_some_and(|(core, core_chain)| {
let core_chain = core_chain
.as_ref()
.map_or(statement_chain, ChildAncestors::get);
crate::roles::ruby::doc_target::owns(&core, code, core_chain)
&& ruby_yard_reads(YardScope::of(&core), node, ancestors)
})
}
fn doc_target_start_row(node: &Node) -> usize {
ruby_then_first_statement(node).unwrap_or(*node).start_row()
}
impl_doc_run_directive_from_row!(ruby);
fn is_doc_run_opener<'a>(
first: &Node<'a>,
_next: Option<&Node<'a>>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
crate::roles::ruby::doc_run_opener::owns(first, code, ancestors)
}
impl_byte_less_func_space!(ruby, opener_gated);
fn is_func_space_with_code<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
crate::roles::ruby::opens_space(node, code, ancestors) != SpaceKind::Unknown
}
fn is_func_with_code<'a>(node: &Node<'a>, code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
crate::roles::ruby::func_opener::is_callable(node, code, ancestors)
}
fn is_closure<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
crate::roles::ruby::closure::owns(node, &[], ancestors)
}
impl_row_predicates!(ruby, call, is_call, is_call_with_code);
impl_row_predicates!(ruby, string_lit, is_string, is_string_with_code);
fn is_non_arg(node: &Node) -> bool {
matches!(
node.kind_id().into(),
Ruby::LPAREN
| Ruby::LPAREN2
| Ruby::RPAREN
| Ruby::RPAREN2
| Ruby::COMMA
| Ruby::SEMI
| Ruby::PIPE
)
}
impl_else_if_from_row!(ruby);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::for_each_node_with_chain;
#[cfg(feature = "ruby")]
#[test]
fn core_is_asked_with_its_own_chain() {
let source = b"class C\n# doc\nX = 1 unless defined?(X)\nif c then\ndef m; end if d\nend\nfoo { |a|\n# doc\nb = 1\n}\nend\n";
let mut wrapped = Vec::new();
let mut unwrapped = 0;
for_each_node_with_chain::<RubyCode>(source, |node, chain| {
let ancestors = Ancestors::known(chain);
let (statement, then_chain) = match ruby_then_first_statement(node) {
Some(first) => (first, Some(ancestors.of_children(node))),
None => (*node, None),
};
let statement_chain = then_chain.as_ref().map_or(ancestors, ChildAncestors::get);
let Some((core, core_chain)) = ruby_documented_core(&statement, statement_chain) else {
return;
};
let asked = core_chain
.as_ref()
.map_or(statement_chain, ChildAncestors::get);
assert_eq!(
asked.parent(&core).map(|p| p.id()),
core.parent().map(|p| p.id()),
"{}: {} asked with a chain not its own",
node.kind(),
core.kind()
);
assert_eq!(
asked.depth(),
Some(std::iter::successors(core.parent(), Node::parent).count()),
"{}: {} asked at the wrong depth",
node.kind(),
core.kind()
);
if core.id() == node.id() {
assert!(
core_chain.is_none(),
"{}: derived a chain for an unwrapped core",
node.kind()
);
unwrapped += 1;
} else {
wrapped.push((node.kind(), core.kind()));
}
});
assert_eq!(
wrapped,
[
("body_statement", "assignment"),
("unless_modifier", "assignment"),
("then", "method"),
("if_modifier", "method"),
("block_body", "assignment"),
]
);
assert!(unwrapped > 20, "{unwrapped}");
}
}