codeint-lang-shell 0.10.4

Built-in Shell Language plugin for codeint
Documentation
#![forbid(unsafe_code)]

mod commands;
mod dialect;
mod flagged;
mod functions;
mod hazards;
mod lexer;
mod source_set;

use codeint_model::language::{FileModel, LowerError, Token, TokenKind};
use codeint_model::scan::CoreReferenceRanges;
use codeint_model::{path_file_name, token_text_hash};

use crate::lexer::{ShellToken, ShellTokenKind};

pub use dialect::{ShellDialect, detect_dialect};
pub use flagged::{SHELL_FLAGGED_RULES, flagged_line_rules};
pub use hazards::{SHELL_HAZARD_RULES, hazard_line_rules};

const SHELL_KEYWORDS_AND_BUILTINS: &[&str] = &[
    "!", "[", "[[", "]", "]]", "{", "}", "break", "case", "cd", "continue", "coproc", "declare",
    "do", "done", "echo", "elif", "else", "esac", "eval", "exec", "exit", "export", "fi", "for",
    "function", "if", "in", "local", "read", "readonly", "return", "select", "set", "setopt",
    "shift", "source", "test", "then", "time", "trap", "typeset", "unset", "until", "while",
];

pub struct Shell;

const SHELL_RANGES: CoreReferenceRanges = CoreReferenceRanges::new(
    (0.364, 0.930),
    (0.218, 0.828),
    (0.000, 0.031),
    (0.003, 0.713),
    None,
);

fn lower_shell(
    _plugin: &Shell,
    path: &codeint_model::RepoPath,
    text: &str,
    _dialect: Option<&str>,
) -> Result<FileModel, codeint_model::LowerError> {
    lower_file(path.as_str(), text).map_err(codeint_model::LowerError::from)
}

codeint_model::impl_builtin_language_plugin!(
    Shell,
    ["sh", "bash", "zsh"],
    lower_shell,
    SHELL_RANGES.build(&codeint_model::scan::calibrated_source(
        "100 Shell repositories",
    )),
    flagged_lines(flagged_line_rules()),
    imports(none),
    frameworks(none),
    hazards(SHELL_HAZARD_RULES),
);

fn lower_file(path: &str, text: &str) -> Result<FileModel, LowerError> {
    if !is_supported_path(path) {
        return Err(LowerError::UnsupportedFile(format!(
            "{path} is not a Shell source file"
        )));
    }
    let dialect = detect_dialect(path, text);
    let lexed = lexer::scan(text, dialect)?;
    let source_lines = u32::try_from(lexed.code_lines.len()).map_err(|_| LowerError::TooLarge)?;
    let extracted = functions::extract(&lexed.tokens, &lexed.code_lines, dialect);
    let flagged_lines = flagged::evaluate(&lexed.tokens, &lexed.code_lines);
    let hazard_lines =
        hazards::evaluate(&lexed.tokens, &lexed.code_lines, &extracted, dialect, text);
    let mut tokens = Vec::new();
    append_duplication_tokens(&lexed.tokens, &mut tokens);
    let source_set = source_set::classify(path, &lexed.header_comments);
    Ok(FileModel {
        flagged_lines,
        hazard_lines,
        tokens,
        functions: extracted.functions,
        source_lines,
        ..codeint_model::empty_file_model(source_set)
    })
}

fn is_supported_path(path: &str) -> bool {
    let file_name = path_file_name(path);
    if source_set::is_generated_filename(file_name) {
        return true;
    }
    matches!(
        file_name.rsplit_once('.'),
        Some((stem, "sh" | "bash" | "zsh")) if !stem.is_empty()
    )
}

fn append_duplication_tokens(raw: &[ShellToken], out: &mut Vec<Token>) {
    for tok in raw {
        if tok.is_newline() {
            continue;
        }
        let kind = classify_duplication_kind(tok);
        out.push(Token {
            kind,
            text_hash: token_text_hash(&tok.text),
            line: tok.line,
        });
        for sub in &tok.sub_tokens {
            append_duplication_tokens(sub, out);
        }
    }
}

fn classify_duplication_kind(tok: &ShellToken) -> TokenKind {
    if matches!(
        tok.kind,
        ShellTokenKind::Operator | ShellTokenKind::ArithCmd
    ) {
        return TokenKind::Other;
    }
    if tok.is_fully_single_quoted || is_numeric_literal(&tok.text) {
        return TokenKind::Literal;
    }
    if tok.has_quotes && !tok.has_var_expansion {
        return TokenKind::Literal;
    }
    if !tok.has_quotes
        && (tok.text.starts_with('-') || SHELL_KEYWORDS_AND_BUILTINS.contains(&tok.text.as_str()))
    {
        return TokenKind::Other;
    }
    TokenKind::Identifier
}

fn is_numeric_literal(text: &str) -> bool {
    !text.is_empty() && text.bytes().all(|b| b.is_ascii_digit())
}