darklua_core/rules/require/
match_require.rs1use std::path::{Path, PathBuf};
2
3use crate::{
4 nodes::{Arguments, Expression, FunctionCall, Prefix},
5 process::IdentifierTracker,
6 utils,
7};
8
9const REQUIRE_FUNCTION_IDENTIFIER: &str = "require";
10
11pub(crate) fn is_require_call(call: &FunctionCall, identifier_tracker: &IdentifierTracker) -> bool {
12 if call.get_method().is_some() {
13 return false;
14 }
15
16 match call.get_prefix() {
17 Prefix::Identifier(identifier) => {
18 identifier.get_name() == REQUIRE_FUNCTION_IDENTIFIER
19 && !identifier_tracker.is_identifier_used(REQUIRE_FUNCTION_IDENTIFIER)
20 }
21 _ => false,
22 }
23}
24
25pub(crate) fn match_path_require_call(call: &FunctionCall) -> Option<PathBuf> {
26 match call.get_arguments() {
27 Arguments::String(string) => Some(string.get_value()),
28 Arguments::Tuple(tuple) if tuple.len() == 1 => {
29 let expression = tuple.iter_values().next().unwrap();
30
31 match expression {
32 Expression::String(string) => Some(string.get_value()),
33 _ => None,
34 }
35 }
36 _ => None,
37 }
38 .map(Path::new)
39 .map(utils::normalize_path_with_current_dir)
40}