-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule to remove method definitions
- Loading branch information
1 parent
86fb53d
commit c0f6416
Showing
9 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::nodes::{Block, FunctionStatement}; | ||
use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor}; | ||
use crate::rules::{Rule, RuleConfigurationError, RuleProperties}; | ||
|
||
#[derive(Default)] | ||
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: &'static str = "remove_method_definition"; | ||
|
||
/// Change method functions into regular functions. | ||
#[derive(Debug, Default, PartialEq, Eq)] | ||
pub struct RemoveMethodDefinition {} | ||
|
||
impl Rule for RemoveMethodDefinition { | ||
fn process(&self, block: &mut Block) { | ||
let mut processor = FunctionMutator::default(); | ||
DefaultVisitor::visit_block(block, &mut processor); | ||
} | ||
|
||
fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> { | ||
for (key, _value) in properties { | ||
return Err(RuleConfigurationError::UnexpectedProperty(key)) | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_name(&self) -> &'static str { | ||
REMOVE_METHOD_DEFINITION_RULE_NAME | ||
} | ||
|
||
fn serialize_to_properties(&self) -> RuleProperties { | ||
RuleProperties::new() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
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!("default_remove_method_definition", wrap(new_rule())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/rules/snapshots/test__default_remove_method_definition.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
source: src/rules/method_def.rs | ||
expression: new_rule() | ||
--- | ||
"remove_method_definition" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ expression: rules | |
--- | ||
[ | ||
"remove_empty_do", | ||
"remove_method_definition", | ||
"rename_variables" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ macro_rules! test_rule { | |
} | ||
|
||
mod remove_empty_do; | ||
mod remove_method_definition; | ||
mod rename_variables; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use darklua_core::rules::RemoveMethodDefinition; | ||
|
||
test_rule!( | ||
RemoveMethodDefinition::default(), | ||
name_without_method("function foo() end") => "function foo() end", | ||
name_with_method("function foo:bar() end") => "function foo.bar(self) end", | ||
name_with_field_and_method("function foo.bar:baz() end") => "function foo.bar.baz(self) end", | ||
with_arguments("function foo:bar(a, b, c) end") => "function foo.bar(self, a, b, c) end", | ||
variadic_function("function foo:bar(...) end") => "function foo.bar(self, ...) end", | ||
variadic_with_arguments("function foo:bar(a, b, c, ...) end") => "function foo.bar(self, a, b, c, ...) end" | ||
); |