-
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remove_if_expression
rule
#208
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
06f2333
Add remove-if-expression rule
jiwonz 0ec2c26
Add more test
jiwonz 5b4cb82
Fix indents
jiwonz 8902fab
Fix imports and takes into advantages but this convertion method will…
jiwonz babab98
Implement the new translation method and formated code by 'cargo fmt'
jiwonz 2b0c283
Add more tests
jiwonz bde769e
Fix indent
jiwonz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use crate::nodes::{ | ||
self, BinaryExpression, Block, ElseIfExpressionBranch, Expression, IndexExpression, | ||
ParentheseExpression, TableEntry, TableExpression, | ||
}; | ||
use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor}; | ||
use crate::rules::{ | ||
Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleProperties, | ||
}; | ||
|
||
use super::verify_no_rule_properties; | ||
|
||
#[derive(Default)] | ||
struct Processor {} | ||
|
||
fn process( | ||
condition: &Expression, | ||
result: &Expression, | ||
else_result: &Expression, | ||
branches: &Vec<&ElseIfExpressionBranch>, | ||
) -> Expression { | ||
let result_parenthese = ParentheseExpression::new(result.clone()); | ||
let result_table = TableExpression::new(vec![TableEntry::Value(result_parenthese.into())]); | ||
let if_bin = BinaryExpression::new(nodes::BinaryOperator::And, condition.clone(), result_table); | ||
let bin_right: Expression = if branches.len() > 0 { | ||
let b = branches[0]; | ||
let elseif_result = process( | ||
b.get_condition(), | ||
b.get_result(), | ||
else_result, | ||
&branches[1..].to_vec(), | ||
); | ||
let elseif_result_table = TableExpression::new(vec![TableEntry::Value(elseif_result)]); | ||
elseif_result_table.into() | ||
} else { | ||
let else_result_parenthese = ParentheseExpression::new(else_result.clone()); | ||
let else_result_table = | ||
TableExpression::new(vec![TableEntry::Value(else_result_parenthese.into())]); | ||
else_result_table.into() | ||
}; | ||
let bin = BinaryExpression::new(nodes::BinaryOperator::Or, if_bin, bin_right); | ||
let bin_parenthese = ParentheseExpression::new(Expression::Binary(Box::new(bin))); | ||
IndexExpression::new(bin_parenthese, 1).into() | ||
} | ||
|
||
impl NodeProcessor for Processor { | ||
fn process_expression(&mut self, expression: &mut Expression) { | ||
if let Expression::If(if_exp) = expression { | ||
let translated_exp = process( | ||
if_exp.get_condition(), | ||
if_exp.get_result(), | ||
if_exp.get_else_result(), | ||
&if_exp.iter_branches().collect(), | ||
); | ||
*expression = translated_exp; | ||
} | ||
} | ||
} | ||
|
||
pub const REMOVE_IF_EXPRESSION_RULE_NAME: &str = "remove_if_expression"; | ||
|
||
/// A rule that removes trailing `nil` in local assignments. | ||
#[derive(Debug, Default, PartialEq, Eq)] | ||
pub struct RemoveIfExpression {} | ||
|
||
impl FlawlessRule for RemoveIfExpression { | ||
fn flawless_process(&self, block: &mut Block, _: &Context) { | ||
let mut processor = Processor::default(); | ||
DefaultVisitor::visit_block(block, &mut processor); | ||
} | ||
} | ||
|
||
impl RuleConfiguration for RemoveIfExpression { | ||
fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> { | ||
verify_no_rule_properties(&properties)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_name(&self) -> &'static str { | ||
REMOVE_IF_EXPRESSION_RULE_NAME | ||
} | ||
|
||
fn serialize_to_properties(&self) -> RuleProperties { | ||
RuleProperties::new() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::rules::Rule; | ||
|
||
use insta::assert_json_snapshot; | ||
|
||
fn new_rule() -> RemoveIfExpression { | ||
RemoveIfExpression::default() | ||
} | ||
|
||
#[test] | ||
fn serialize_default_rule() { | ||
let rule: Box<dyn Rule> = Box::new(new_rule()); | ||
|
||
assert_json_snapshot!("default_remove_if_expression", rule); | ||
} | ||
|
||
#[test] | ||
fn configure_with_extra_field_error() { | ||
let result = json5::from_str::<Box<dyn Rule>>( | ||
r#"{ | ||
rule: 'remove_if_expression', | ||
prop: "something", | ||
}"#, | ||
); | ||
pretty_assertions::assert_eq!(result.unwrap_err().to_string(), "unexpected field 'prop'"); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/rules/snapshots/darklua_core__rules__remove_continue__test__default_remove_continue.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/remove_continue.rs | ||
expression: rule | ||
--- | ||
"remove_continue" |
5 changes: 5 additions & 0 deletions
5
...pshots/darklua_core__rules__remove_if_expression__test__default_remove_if_expression.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/remove_if_expression.rs | ||
expression: rule | ||
--- | ||
"remove_if_expression" |
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,25 @@ | ||
use darklua_core::rules::{RemoveIfExpression, Rule}; | ||
|
||
test_rule!( | ||
remove_if_expression, | ||
RemoveIfExpression::default(), | ||
assign_if_expression("local a = if true then 1 else 2") => "local a = (true and {(1)} or {(2)})[1]", | ||
assign_if_expression_with_elseif("local a = if true then 1 elseif false then 2 else 3") => "local a = (true and {(1)} or {(false and {(2)} or {(3)})[1]})[1]", | ||
if_expression_with_varargs("local function f(...: string) return if condition(...) then ... else transform(...) end") => "local function f(...: string) return (condition(...) and {(...)} or {(transform(...))})[1] end", | ||
if_expression_with_varargs_elseif("local function f(...: string) return if condition(...) then ... elseif condition(...) then ... else transform(...) end") => "local function f(...: string) return (condition(...) and {(...)} or {(condition(...) and {(...)} or {(transform(...))})[1]})[1] end" | ||
); | ||
|
||
#[test] | ||
fn deserialize_from_object_notation() { | ||
json5::from_str::<Box<dyn Rule>>( | ||
r#"{ | ||
rule: 'remove_if_expression', | ||
}"#, | ||
) | ||
.unwrap(); | ||
} | ||
|
||
#[test] | ||
fn deserialize_from_string() { | ||
json5::from_str::<Box<dyn Rule>>("'remove_if_expression'").unwrap(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add more tests! For example, we need to treat function calls and
...
in a specific way to make sure we are not returning multiple values. For that we need to wrap them in parentheses.