darklua_core/rules/
remove_method_call.rs1use crate::nodes::{Block, Expression, FieldExpression, FunctionCall, Prefix};
2use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
3use crate::rules::{
4 Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleMetadata, RuleProperties,
5};
6
7use super::verify_no_rule_properties;
8
9#[derive(Debug, Default)]
10struct Processor {}
11
12impl NodeProcessor for Processor {
13 fn process_expression(&mut self, expression: &mut Expression) {
14 if let Expression::Call(call) = expression {
15 self.process_function_call(call);
16 }
17 }
18
19 fn process_function_call(&mut self, call: &mut FunctionCall) {
20 if !call.has_method() {
21 return;
22 }
23
24 let replace_with: Option<Prefix> = match call.get_prefix() {
25 Prefix::Identifier(identifier) => Some(identifier.clone().into()),
26 Prefix::Parenthese(parenthese) => match *parenthese.inner_expression() {
27 Expression::If(_)
28 | Expression::Index(_)
29 | Expression::Field(_)
30 | Expression::Function(_)
31 | Expression::Binary(_)
32 | Expression::Table(_)
33 | Expression::Unary(_)
34 | Expression::VariableArguments(_)
35 | Expression::TypeCast(_)
36 | Expression::TypeInstantiation(_)
37 | Expression::InterpolatedString(_)
38 | Expression::Parenthese(_)
39 | Expression::Call(_) => None,
40
41 Expression::Nil(_)
42 | Expression::True(_)
43 | Expression::False(_)
44 | Expression::String(_)
45 | Expression::Number(_)
46 | Expression::Identifier(_) => Some(parenthese.inner_expression().clone().into()),
47 },
48 _ => None,
49 };
50
51 if let Some(new_prefix) = replace_with {
52 let method_name = call
53 .take_method()
54 .expect("method name is expected to exist");
55
56 *call.mutate_prefix() = FieldExpression::new(new_prefix.clone(), method_name).into();
57 call.mutate_arguments()
58 .insert(0, Expression::from(new_prefix));
59 }
60 }
61}
62
63pub const REMOVE_METHOD_CALL_RULE_NAME: &str = "remove_method_call";
64
65#[derive(Debug, Default, PartialEq, Eq)]
70pub struct RemoveMethodCall {
71 metadata: RuleMetadata,
72}
73
74impl FlawlessRule for RemoveMethodCall {
75 fn flawless_process(&self, block: &mut Block, _: &Context) {
76 let mut processor = Processor::default();
77 DefaultVisitor::visit_block(block, &mut processor);
78 }
79}
80
81impl RuleConfiguration for RemoveMethodCall {
82 fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
83 verify_no_rule_properties(&properties)?;
84 Ok(())
85 }
86
87 fn get_name(&self) -> &'static str {
88 REMOVE_METHOD_CALL_RULE_NAME
89 }
90
91 fn serialize_to_properties(&self) -> RuleProperties {
92 RuleProperties::new()
93 }
94
95 fn set_metadata(&mut self, metadata: RuleMetadata) {
96 self.metadata = metadata;
97 }
98
99 fn metadata(&self) -> &RuleMetadata {
100 &self.metadata
101 }
102}
103
104#[cfg(test)]
105mod test {
106 use super::*;
107 use crate::rules::Rule;
108
109 use insta::assert_json_snapshot;
110
111 fn new_rule() -> RemoveMethodCall {
112 RemoveMethodCall::default()
113 }
114
115 #[test]
116 fn serialize_default_rule() {
117 let rule: Box<dyn Rule> = Box::new(new_rule());
118
119 assert_json_snapshot!(rule, @r###""remove_method_call""###);
120 }
121
122 #[test]
123 fn configure_with_extra_field_error() {
124 let result = json5::from_str::<Box<dyn Rule>>(
125 r#"{
126 rule: 'remove_method_call',
127 prop: "something",
128 }"#,
129 );
130 insta::assert_snapshot!(result.unwrap_err().to_string(), @"unexpected field 'prop' at line 1 column 1");
131 }
132}