darklua_core/rules/
method_def.rs1use crate::nodes::{Block, FunctionStatement};
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
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#[derive(Debug, Default, PartialEq, Eq)]
21pub struct RemoveMethodDefinition {
22 metadata: RuleMetadata,
23}
24
25impl FlawlessRule for RemoveMethodDefinition {
26 fn flawless_process(&self, block: &mut Block, _: &Context) {
27 let mut processor = FunctionMutator;
28 DefaultVisitor::visit_block(block, &mut processor);
29 }
30}
31
32impl RuleConfiguration for RemoveMethodDefinition {
33 fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
34 verify_no_rule_properties(&properties)?;
35
36 Ok(())
37 }
38
39 fn get_name(&self) -> &'static str {
40 REMOVE_METHOD_DEFINITION_RULE_NAME
41 }
42
43 fn serialize_to_properties(&self) -> RuleProperties {
44 RuleProperties::new()
45 }
46
47 fn set_metadata(&mut self, metadata: RuleMetadata) {
48 self.metadata = metadata;
49 }
50
51 fn metadata(&self) -> &RuleMetadata {
52 &self.metadata
53 }
54}
55
56#[cfg(test)]
57mod test {
58 use super::*;
59 use crate::rules::Rule;
60
61 use insta::assert_json_snapshot;
62
63 fn new_rule() -> RemoveMethodDefinition {
64 RemoveMethodDefinition::default()
65 }
66
67 fn wrap(rule: RemoveMethodDefinition) -> Box<dyn Rule> {
68 Box::new(rule)
69 }
70
71 #[test]
72 fn serialize_default_rule() {
73 assert_json_snapshot!(wrap(new_rule()), @r###""remove_method_definition""###);
74 }
75
76 #[test]
77 fn configure_with_extra_field_error() {
78 let result = json5::from_str::<Box<dyn Rule>>(
79 r#"{
80 rule: 'remove_method_definition',
81 prop: "something",
82 }"#,
83 );
84 insta::assert_snapshot!(result.unwrap_err().to_string(), @"unexpected field 'prop' at line 1 column 1");
85 }
86}