Skip to content

Commit

Permalink
Add rule to remove method definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeparlefrancais committed Mar 7, 2020
1 parent 86fb53d commit c0f6416
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "darklua"
version = "0.2.1"
version = "0.2.2"
authors = ["jeparlefrancais <jeparlefrancais21@gmail.com>"]
edition = "2018"
readme = "README.md"
Expand Down
33 changes: 33 additions & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
You can find the available rules and their properties here. The default rule stack is:

- [Remove empty do statements](#remove-empty-do-statements)
- [Remove method definitions](#remove-method-definitions)
- [Rename variables](#rename-variables)

---
Expand All @@ -21,6 +22,38 @@ This rule does not have any properties.

---

## Remove method definitions
```remove_method_definition```

Functions defined using the method syntax (with a `:`) will be replaced with their field like syntax. So the following Lua code:

```lua
local Car = {}

function Car:move(distance)
self.position = self.position + distance
end
```

Will produce the following code:

```lua
local Car = {}

function Car.move(self, distance)
self.position = self.position + distance
end
```

### Examples
```json5
{
rule: 'remove_method_definition',
}
```

---

## Rename variables
```rename_variables```

Expand Down
25 changes: 22 additions & 3 deletions src/nodes/statements/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl FunctionName {
self
}

pub fn with_method(mut self, method: String) -> Self {
self.method.replace(method);
pub fn with_method<S: Into<String>>(mut self, method: S) -> Self {
self.method.replace(method.into());
self
}

Expand Down Expand Up @@ -168,10 +168,29 @@ mod test {
use super::*;

#[test]
fn generate_() {
fn generate_empty_function() {
let output = FunctionStatement::from_name("foo", Block::default())
.to_lua_string();

assert_eq!(output, "function foo()end");
}

#[test]
fn generate_empty_function_with_field() {
let function_name = FunctionName::from_name("foo")
.with_fields(vec!["bar".to_owned()]);
let output = FunctionStatement::new(function_name, Block::default(), Vec::new(), false)
.to_lua_string();

assert_eq!(output, "function foo.bar()end");
}

#[test]
fn generate_empty_function_with_method() {
let function_name = FunctionName::from_name("foo").with_method("bar");
let output = FunctionStatement::new(function_name, Block::default(), Vec::new(), false)
.to_lua_string();

assert_eq!(output, "function foo:bar()end");
}
}
61 changes: 61 additions & 0 deletions src/rules/method_def.rs
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()));
}
}
4 changes: 4 additions & 0 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! A module that contains the different rules that mutates a Lua block.

mod empty_do;
mod method_def;
mod rename_variables;

pub use empty_do::*;
pub use method_def::*;
pub use rename_variables::*;

use crate::nodes::Block;
Expand Down Expand Up @@ -75,6 +77,7 @@ pub trait Rule {
pub fn get_default_rules() -> Vec<Box<dyn Rule>> {
vec![
Box::new(RemoveEmptyDo::default()),
Box::new(RemoveMethodDefinition::default()),
Box::new(RenameVariables::default()),
]
}
Expand All @@ -85,6 +88,7 @@ impl FromStr for Box<dyn Rule> {
fn from_str(string: &str) -> Result<Self, Self::Err> {
let rule: Box<dyn Rule> = match string {
REMOVE_EMPTY_DO_RULE_NAME => Box::new(RemoveEmptyDo::default()),
REMOVE_METHOD_DEFINITION_RULE_NAME => Box::new(RemoveMethodDefinition::default()),
RENAME_VARIABLES_RULE_NAME => Box::new(RenameVariables::default()),
_ => return Err(format!("invalid rule name: {}", string)),
};
Expand Down
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"
1 change: 1 addition & 0 deletions src/rules/snapshots/test__default_rules.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ expression: rules
---
[
"remove_empty_do",
"remove_method_definition",
"rename_variables"
]
1 change: 1 addition & 0 deletions tests/rule_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ macro_rules! test_rule {
}

mod remove_empty_do;
mod remove_method_definition;
mod rename_variables;
11 changes: 11 additions & 0 deletions tests/rule_tests/remove_method_definition.rs
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"
);

0 comments on commit c0f6416

Please sign in to comment.