darklua_core/rules/
call_parens.rs1use crate::nodes::{Arguments, Block, Expression, FunctionCall, StringExpression, TableExpression};
2use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
3use crate::rules::{
4 Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleMetadata, RuleProperties,
5};
6
7use std::mem;
8
9use super::verify_no_rule_properties;
10
11#[derive(Debug, Clone, Default)]
12struct Processor {}
13
14impl NodeProcessor for Processor {
15 fn process_function_call(&mut self, call: &mut FunctionCall) {
16 let new_arguments = match call.mutate_arguments() {
17 Arguments::Tuple(tuple) if tuple.len() == 1 => {
18 let expression = tuple.iter_mut_values().next().unwrap();
19
20 match expression {
21 Expression::String(string) => {
22 let mut steal_string = StringExpression::empty();
23 mem::swap(string, &mut steal_string);
24 Some(Arguments::String(steal_string))
25 }
26 Expression::Table(table) => {
27 let mut steal_table = TableExpression::default();
28 mem::swap(table, &mut steal_table);
29 Some(Arguments::Table(steal_table))
30 }
31 _ => None,
32 }
33 }
34 _ => None,
35 };
36
37 if let Some(new_arguments) = new_arguments {
38 *call.mutate_arguments() = new_arguments;
39 }
40 }
41}
42
43pub const REMOVE_FUNCTION_CALL_PARENS_RULE_NAME: &str = "remove_function_call_parens";
44
45#[derive(Debug, Default, PartialEq, Eq)]
47pub struct RemoveFunctionCallParens {
48 metadata: RuleMetadata,
49}
50
51impl FlawlessRule for RemoveFunctionCallParens {
52 fn flawless_process(&self, block: &mut Block, _: &Context) {
53 let mut processor = Processor::default();
54 DefaultVisitor::visit_block(block, &mut processor);
55 }
56}
57
58impl RuleConfiguration for RemoveFunctionCallParens {
59 fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
60 verify_no_rule_properties(&properties)?;
61
62 Ok(())
63 }
64
65 fn get_name(&self) -> &'static str {
66 REMOVE_FUNCTION_CALL_PARENS_RULE_NAME
67 }
68
69 fn serialize_to_properties(&self) -> RuleProperties {
70 RuleProperties::new()
71 }
72
73 fn set_metadata(&mut self, metadata: RuleMetadata) {
74 self.metadata = metadata;
75 }
76
77 fn metadata(&self) -> &RuleMetadata {
78 &self.metadata
79 }
80}
81
82#[cfg(test)]
83mod test {
84 use super::*;
85 use crate::rules::Rule;
86
87 use insta::assert_json_snapshot;
88
89 fn new_rule() -> RemoveFunctionCallParens {
90 RemoveFunctionCallParens::default()
91 }
92
93 #[test]
94 fn serialize_default_rule() {
95 let rule: Box<dyn Rule> = Box::new(new_rule());
96
97 assert_json_snapshot!(rule, @r###""remove_function_call_parens""###);
98 }
99
100 #[test]
101 fn configure_with_extra_field_error() {
102 let result = json5::from_str::<Box<dyn Rule>>(
103 r#"{
104 rule: 'remove_function_call_parens',
105 prop: "something",
106 }"#,
107 );
108 insta::assert_snapshot!(result.unwrap_err().to_string(), @"unexpected field 'prop' at line 1 column 1");
109 }
110}