use crate::Ruby;
use crate::lang_helpers::doc_comment::node_bytes;
use crate::node::{Ancestors, FoldLane, Node};
#[must_use]
pub(crate) fn is_stabby_lambda_body<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
ancestors
.parent(node)
.is_some_and(|parent| parent.kind_id() == Ruby::Lambda)
}
pub(crate) fn ruby_documented_call(node: &Node, code: &[u8]) -> bool {
let Some(method) = node.child_by_field_name("method") else {
return false;
};
match node_bytes(&method, code) {
b"attr" | b"attr_reader" | b"attr_writer" | b"attr_accessor" | b"alias_method" => true,
b"private"
| b"protected"
| b"public"
| b"module_function"
| b"private_class_method"
| b"public_class_method" => {
node.child_by_field_name("receiver").is_none()
&& node
.child_by_field_name("arguments")
.and_then(|arguments| arguments.children().find(Node::is_named))
.is_some_and(|argument| {
matches!(
argument.kind_id().into(),
Ruby::Method | Ruby::SingletonMethod
)
})
}
_ => false,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum YardScope {
DefBody,
Namespace,
}
impl YardScope {
#[must_use]
pub(crate) fn of(core: &Node) -> Self {
if matches!(core.kind_id().into(), Ruby::Method | Ruby::SingletonMethod) {
Self::DefBody
} else {
Self::Namespace
}
}
}
pub(crate) fn ruby_yard_reads<'a>(
needs: YardScope,
scope: &Node<'a>,
ancestors: Ancestors<'a, '_>,
) -> bool {
const DEF_READS: u8 = 0b01;
const NAMESPACED_READS: u8 = 0b10;
const BOTH: u8 = DEF_READS | NAMESPACED_READS;
let reads = ancestors.fold_down(
scope,
0,
FoldLane::DocScope,
BOTH,
|above, ancestor, _, _| match ancestor.kind_id().into() {
Ruby::Program => BOTH,
Ruby::Class
| Ruby::Module
| Ruby::SingletonClass
| Ruby::If
| Ruby::Unless
| Ruby::Elsif => {
if above & NAMESPACED_READS == 0 {
0
} else {
BOTH
}
}
Ruby::Method | Ruby::SingletonMethod => above & DEF_READS,
Ruby::Case | Ruby::CaseMatch | Ruby::Begin | Ruby::While | Ruby::Until | Ruby::For => 0,
_ => above,
},
);
let need = match needs {
YardScope::DefBody => DEF_READS,
YardScope::Namespace => NAMESPACED_READS,
};
reads & need != 0
}