-
-
Notifications
You must be signed in to change notification settings - Fork 19
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 convert_luau_numbers
rule
#257
Open
jiwonz
wants to merge
2
commits into
seaofvoices:main
Choose a base branch
from
CavefulGames:remove-number-literals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,90 @@ | ||||||
use crate::{ | ||||||
nodes::{Block, DecimalNumber, Expression, NumberExpression}, | ||||||
process::{DefaultVisitor, NodeProcessor, NodeVisitor}, | ||||||
rules::{Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleProperties}, | ||||||
}; | ||||||
|
||||||
use super::verify_no_rule_properties; | ||||||
|
||||||
#[derive(Default)] | ||||||
struct Processor {} | ||||||
|
||||||
impl NodeProcessor for Processor { | ||||||
fn process_expression(&mut self, exp: &mut Expression) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a function for number expressions you can use directly:
Suggested change
|
||||||
if let Expression::Number(num_exp) = exp { | ||||||
match num_exp { | ||||||
NumberExpression::Binary(binary) => { | ||||||
let value = binary.compute_value(); | ||||||
*exp = DecimalNumber::new(value).into(); | ||||||
} | ||||||
NumberExpression::Decimal(decimal) => { | ||||||
let value = decimal.compute_value(); | ||||||
*exp = DecimalNumber::new(value).into(); | ||||||
} | ||||||
NumberExpression::Hex(hex) => { | ||||||
let value = hex.compute_value(); | ||||||
*exp = DecimalNumber::new(value).into(); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
pub const REMOVE_NUMBER_LITERALS_RULE_NAME: &str = "remove_number_literals"; | ||||||
|
||||||
/// A rule that removes number literals. | ||||||
#[derive(Default, Debug, PartialEq, Eq)] | ||||||
pub struct RemoveNumberLiterals {} | ||||||
|
||||||
impl FlawlessRule for RemoveNumberLiterals { | ||||||
fn flawless_process(&self, block: &mut Block, _: &Context) { | ||||||
let mut processor = Processor {}; | ||||||
DefaultVisitor::visit_block(block, &mut processor); | ||||||
} | ||||||
} | ||||||
|
||||||
impl RuleConfiguration for RemoveNumberLiterals { | ||||||
fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> { | ||||||
verify_no_rule_properties(&properties)?; | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
|
||||||
fn get_name(&self) -> &'static str { | ||||||
REMOVE_NUMBER_LITERALS_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() -> RemoveNumberLiterals { | ||||||
RemoveNumberLiterals::default() | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn serialize_default_rule() { | ||||||
let rule: Box<dyn Rule> = Box::new(new_rule()); | ||||||
|
||||||
assert_json_snapshot!("default_remove_number_literals", rule); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn configure_with_extra_field_error() { | ||||||
let result = json5::from_str::<Box<dyn Rule>>( | ||||||
r#"{ | ||||||
rule: 'remove_number_literals', | ||||||
prop: "something", | ||||||
}"#, | ||||||
); | ||||||
pretty_assertions::assert_eq!(result.unwrap_err().to_string(), "unexpected field 'prop'"); | ||||||
} | ||||||
} |
5 changes: 5 additions & 0 deletions
5
...ts/darklua_core__rules__remove_number_literals__test__default_remove_number_literals.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_number_literals.rs | ||
expression: rule | ||
--- | ||
"remove_number_literals" |
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,27 @@ | ||
use darklua_core::rules::{RemoveNumberLiterals, Rule}; | ||
|
||
test_rule!( | ||
remove_number_literals, | ||
RemoveNumberLiterals::default(), | ||
hexadecimal_integer_literals("local a = 0xABC local b = 0XABC") | ||
=> "local a = 2748 local b = 2748", | ||
binary_integer_literals("local a = 0b01010101 local b = 0B01010101") | ||
=> "local a = 85 local b = 85", | ||
decimal_separators("local a = 1_048_576 local b = 0xFFFF_FFFF local c = 0b_0101_0101") | ||
=> "local a = 1048576 local b = 4294967295 local c = 85", | ||
); | ||
|
||
#[test] | ||
fn deserialize_from_object_notation() { | ||
json5::from_str::<Box<dyn Rule>>( | ||
r#"{ | ||
rule: 'remove_number_literals', | ||
}"#, | ||
) | ||
.unwrap(); | ||
} | ||
|
||
#[test] | ||
fn deserialize_from_string() { | ||
json5::from_str::<Box<dyn Rule>>("'remove_number_literals'").unwrap(); | ||
} |
Oops, something went wrong.
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.
I think I would not put this rule in the default rules, at least for now