darklua_core/rules/
method_def.rs

1use crate::nodes::{Block, FunctionStatement};
2use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
3use crate::rules::{
4    Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleProperties,
5};
6
7use super::verify_no_rule_properties;
8
9struct FunctionMutator;
10
11impl NodeProcessor for FunctionMutator {
12    fn process_function_statement(&mut self, function: &mut FunctionStatement) {
13        function.remove_method();
14    }
15}
16
17pub const REMOVE_METHOD_DEFINITION_RULE_NAME: &str = "remove_method_definition";
18
19/// Change method functions into regular functions.
20#[derive(Debug, Default, PartialEq, Eq)]
21pub struct RemoveMethodDefinition {}
22
23impl FlawlessRule for RemoveMethodDefinition {
24    fn flawless_process(&self, block: &mut Block, _: &Context) {
25        let mut processor = FunctionMutator;
26        DefaultVisitor::visit_block(block, &mut processor);
27    }
28}
29
30impl RuleConfiguration for RemoveMethodDefinition {
31    fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
32        verify_no_rule_properties(&properties)?;
33
34        Ok(())
35    }
36
37    fn get_name(&self) -> &'static str {
38        REMOVE_METHOD_DEFINITION_RULE_NAME
39    }
40
41    fn serialize_to_properties(&self) -> RuleProperties {
42        RuleProperties::new()
43    }
44}
45
46#[cfg(test)]
47mod test {
48    use super::*;
49    use crate::rules::Rule;
50
51    use insta::assert_json_snapshot;
52
53    fn new_rule() -> RemoveMethodDefinition {
54        RemoveMethodDefinition::default()
55    }
56
57    fn wrap(rule: RemoveMethodDefinition) -> Box<dyn Rule> {
58        Box::new(rule)
59    }
60
61    #[test]
62    fn serialize_default_rule() {
63        assert_json_snapshot!("default_remove_method_definition", wrap(new_rule()));
64    }
65
66    #[test]
67    fn configure_with_extra_field_error() {
68        let result = json5::from_str::<Box<dyn Rule>>(
69            r#"{
70            rule: 'remove_method_definition',
71            prop: "something",
72        }"#,
73        );
74        pretty_assertions::assert_eq!(result.unwrap_err().to_string(), "unexpected field 'prop'");
75    }
76}