darklua 0.18.0

Transform Lua scripts
Documentation
use crate::nodes::{Block, FunctionStatement};
use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
use crate::rules::{
    Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleMetadata, RuleProperties,
};

use super::verify_no_rule_properties;

struct FunctionMutator;

impl NodeProcessor for FunctionMutator {
    fn process_function_statement(&mut self, function: &mut FunctionStatement) {
        function.remove_method();
    }
}

pub const REMOVE_METHOD_DEFINITION_RULE_NAME: &str = "remove_method_definition";

/// Change method functions into regular functions.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct RemoveMethodDefinition {
    metadata: RuleMetadata,
}

impl FlawlessRule for RemoveMethodDefinition {
    fn flawless_process(&self, block: &mut Block, _: &Context) {
        let mut processor = FunctionMutator;
        DefaultVisitor::visit_block(block, &mut processor);
    }
}

impl RuleConfiguration for RemoveMethodDefinition {
    fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
        verify_no_rule_properties(&properties)?;

        Ok(())
    }

    fn get_name(&self) -> &'static str {
        REMOVE_METHOD_DEFINITION_RULE_NAME
    }

    fn serialize_to_properties(&self) -> RuleProperties {
        RuleProperties::new()
    }

    fn set_metadata(&mut self, metadata: RuleMetadata) {
        self.metadata = metadata;
    }

    fn metadata(&self) -> &RuleMetadata {
        &self.metadata
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::rules::Rule;

    use insta::assert_json_snapshot;

    fn new_rule() -> RemoveMethodDefinition {
        RemoveMethodDefinition::default()
    }

    fn wrap(rule: RemoveMethodDefinition) -> Box<dyn Rule> {
        Box::new(rule)
    }

    #[test]
    fn serialize_default_rule() {
        assert_json_snapshot!(wrap(new_rule()), @r###""remove_method_definition""###);
    }

    #[test]
    fn configure_with_extra_field_error() {
        let result = json5::from_str::<Box<dyn Rule>>(
            r#"{
            rule: 'remove_method_definition',
            prop: "something",
        }"#,
        );
        insta::assert_snapshot!(result.unwrap_err().to_string(), @"unexpected field 'prop' at line 1 column 1");
    }
}