use super::{line_of, node_text, push_pattern_finding, snippet_of};
use crate::findings::types::{Finding, Severity};
use crate::scan::facts::FileFacts;
use std::path::Path;
use tree_sitter::Node;
#[derive(Debug, Clone, Copy)]
pub(super) enum JsRiskPattern {
ProcessExit,
}
impl JsRiskPattern {
pub(super) const ALL: &'static [Self] = &[Self::ProcessExit];
pub(super) fn matches(self, trimmed: &str, _path: &Path) -> bool {
match self {
Self::ProcessExit => trimmed.contains("process.exit("),
}
}
pub(super) fn rule_id(self) -> &'static str {
"language.javascript.runtime-exit-risk"
}
pub(super) fn signal(self) -> &'static str {
match self {
Self::ProcessExit => "js.process-exit",
}
}
pub(super) fn title(self) -> &'static str {
match self {
Self::ProcessExit => "JavaScript process.exit usage",
}
}
pub(super) fn context_label(self) -> &'static str {
match self {
Self::ProcessExit => "JavaScript process exit call",
}
}
pub(super) fn recommendation(self) -> &'static str {
match self {
Self::ProcessExit => {
"Keep process exits at a CLI boundary and return errors from reusable modules."
}
}
}
pub(super) fn base_severity(self) -> Severity {
match self {
Self::ProcessExit => Severity::High,
}
}
}
pub(super) fn emit_js_node(
node: Node<'_>,
content: &str,
path: &Path,
file: &FileFacts,
findings: &mut Vec<Finding>,
) {
let pattern = match node.kind() {
"call_expression" if is_process_exit_call(node, content) => {
Some(JsRiskPattern::ProcessExit)
}
_ => None,
};
if let Some(pattern) = pattern {
push_pattern_finding(
&pattern,
path,
line_of(node),
&snippet_of(node, content),
file,
findings,
);
}
}
fn is_process_exit_call(node: Node<'_>, content: &str) -> bool {
let Some(function) = node.child_by_field_name("function") else {
return false;
};
if function.kind() != "member_expression" {
return false;
}
let object = function
.child_by_field_name("object")
.and_then(|n| node_text(n, content));
let property = function
.child_by_field_name("property")
.and_then(|n| node_text(n, content));
object == Some("process") && property == Some("exit")
}