rrubocop 0.8.15

10x faster RuboCop drop-in. Same output format and configs, without maintaining RuboCop gems.
use tree_sitter::Node;

use crate::cop::shared::node_text;
use crate::cop::{Cop, CopConfig};
use crate::diagnostic::{Diagnostic, Severity};
use crate::parse::source::SourceFile;

/// Lint/MixedCaseRange — 'A'..'z' style ranges.
pub struct MixedCaseRange;

fn single_char(s: &str) -> Option<char> {
    let mut chars = s.chars();
    match (chars.next(), chars.next()) {
        (Some(c), None) => Some(c),
        _ => None,
    }
}

fn char_content(source: &SourceFile, node: Node<'_>) -> Option<char> {
    single_char(
        node_text(source, node)
            .trim_matches(|c| c == '\'' || c == '"'),
    )
}

fn mixed_case(a: char, b: char) -> bool {
    (a.is_ascii_uppercase() && b.is_ascii_lowercase())
        || (a.is_ascii_lowercase() && b.is_ascii_uppercase())
}

fn range_chars(source: &SourceFile, node: Node<'_>) -> Option<(char, char)> {
    let begin = node.child_by_field_name("begin")?;
    let end = node.child_by_field_name("end")?;
    if begin.kind() != "string" || end.kind() != "string" {
        return None;
    }
    Some((char_content(source, begin)?, char_content(source, end)?))
}

impl Cop for MixedCaseRange {
    fn name(&self) -> &'static str {
        "Lint/MixedCaseRange"
    }

    fn redundant_disable_audit(&self) -> bool {
        false
    }

    fn default_severity(&self) -> Severity {
        Severity::Warning
    }

    fn interested_node_kinds(&self) -> &'static [&'static str] {
        &["range"]
    }

    fn check_node(
        &self,
        source: &SourceFile,
        node: Node<'_>,
        _config: &CopConfig,
        diagnostics: &mut Vec<Diagnostic>,
        _corrections: Option<&mut Vec<crate::correction::Correction>>,
    ) {
        let Some((a, b)) = range_chars(source, node) else {
            return;
        };
        if !mixed_case(a, b) {
            return;
        }
        let (line, col) = source.offset_to_line_col(node.start_byte());
        diagnostics.push(self.diagnostic(
            source,
            line,
            col,
            "Ranges from upper to lower case ASCII letters may include unintended characters. Prefer explicit ranges like ('A'..'Z') and ('a'..'z').".to_string(),
        ));
    }
}