|
| 1 | +use crate::nodes::{Arguments, Block, Expression, FunctionCall, StringExpression, TableExpression}; |
| 2 | +use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor}; |
| 3 | +use crate::rules::{Rule, RuleConfigurationError, RuleProperties}; |
| 4 | + |
| 5 | +use std::mem; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Default)] |
| 8 | +struct Processor {} |
| 9 | + |
| 10 | +impl NodeProcessor for Processor { |
| 11 | + fn process_function_call(&mut self, call: &mut FunctionCall) { |
| 12 | + let new_arguments = match call.mutate_arguments() { |
| 13 | + Arguments::Tuple(expressions) if expressions.len() == 1 => { |
| 14 | + let expression = expressions.iter_mut().next().unwrap(); |
| 15 | + |
| 16 | + match expression { |
| 17 | + Expression::String(string) => { |
| 18 | + let mut steal_string = StringExpression::empty(); |
| 19 | + mem::swap(string, &mut steal_string); |
| 20 | + Some(Arguments::String(steal_string)) |
| 21 | + } |
| 22 | + Expression::Table(table) => { |
| 23 | + let mut steal_table = TableExpression::default(); |
| 24 | + mem::swap(table, &mut steal_table); |
| 25 | + Some(Arguments::Table(steal_table)) |
| 26 | + } |
| 27 | + _ => None, |
| 28 | + } |
| 29 | + } |
| 30 | + _ => None, |
| 31 | + }; |
| 32 | + |
| 33 | + if let Some(new_arguments) = new_arguments { |
| 34 | + mem::replace(call.mutate_arguments(), new_arguments); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +pub const REMOVE_FUNCTION_CALL_PARENS: &'static str = "remove_function_call_parens"; |
| 40 | + |
| 41 | +/// A rule that removes parentheses when calling functions with a string or a table. |
| 42 | +#[derive(Debug, Default, PartialEq, Eq)] |
| 43 | +pub struct RemoveFunctionCallParens {} |
| 44 | + |
| 45 | +impl Rule for RemoveFunctionCallParens { |
| 46 | + fn process(&self, block: &mut Block) { |
| 47 | + let mut processor = Processor::default(); |
| 48 | + DefaultVisitor::visit_block(block, &mut processor); |
| 49 | + } |
| 50 | + |
| 51 | + fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> { |
| 52 | + for (key, _value) in properties { |
| 53 | + return Err(RuleConfigurationError::UnexpectedProperty(key)) |
| 54 | + } |
| 55 | + |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | + |
| 59 | + fn get_name(&self) -> &'static str { |
| 60 | + REMOVE_FUNCTION_CALL_PARENS |
| 61 | + } |
| 62 | + |
| 63 | + fn serialize_to_properties(&self) -> RuleProperties { |
| 64 | + RuleProperties::new() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod test { |
| 70 | + use super::*; |
| 71 | + |
| 72 | + use insta::assert_json_snapshot; |
| 73 | + |
| 74 | + fn new_rule() -> RemoveFunctionCallParens { |
| 75 | + RemoveFunctionCallParens::default() |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn serialize_default_rule() { |
| 80 | + let rule: Box<dyn Rule> = Box::new(new_rule()); |
| 81 | + |
| 82 | + assert_json_snapshot!("default_remove_function_call_parens", rule); |
| 83 | + } |
| 84 | +} |
0 commit comments