Skip to content

Commit 2ba52a0

Browse files
committed
Reflect the feedbacks and suggestions from reviews (the rule name has changed to convert_luau_numbers and only converts binary to decimal and removes underscores from token)
1 parent 1d747c0 commit 2ba52a0

9 files changed

+143
-126
lines changed

src/rules/convert_luau_numbers.rs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use crate::{
2+
nodes::{Block, DecimalNumber, NumberExpression},
3+
process::{DefaultVisitor, NodeProcessor, NodeVisitor},
4+
rules::{Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleProperties},
5+
};
6+
7+
use super::verify_no_rule_properties;
8+
9+
#[derive(Default)]
10+
struct Processor<'a> {
11+
code: &'a str,
12+
}
13+
14+
impl NodeProcessor for Processor<'_> {
15+
fn process_number_expression(&mut self, num_exp: &mut NumberExpression) {
16+
if let NumberExpression::Binary(binary) = num_exp {
17+
let value = binary.compute_value();
18+
*num_exp = DecimalNumber::new(value).into();
19+
return;
20+
}
21+
if let Some(token) = num_exp.get_token() {
22+
let content = token.read(self.code);
23+
let mut underscore_removed = String::with_capacity(content.len());
24+
let mut changed = false;
25+
26+
for c in content.chars() {
27+
if c != '_' {
28+
underscore_removed.push(c);
29+
} else {
30+
changed = true;
31+
}
32+
}
33+
34+
if changed {
35+
let mut new_token = token.clone();
36+
new_token.replace_with_content(underscore_removed);
37+
num_exp.set_token(new_token);
38+
}
39+
}
40+
}
41+
}
42+
43+
impl<'a> Processor<'a> {
44+
fn new(code: &'a str) -> Self {
45+
Self { code }
46+
}
47+
}
48+
49+
pub const CONVERT_LUAU_NUMBERS_RULE_NAME: &str = "convert_luau_numbers";
50+
51+
/// A rule that converts Luau number literals to decimal numbers.
52+
#[derive(Default, Debug, PartialEq, Eq)]
53+
pub struct ConvertLuauNumbers {}
54+
55+
impl FlawlessRule for ConvertLuauNumbers {
56+
fn flawless_process(&self, block: &mut Block, context: &Context) {
57+
let mut processor = Processor::new(context.original_code());
58+
DefaultVisitor::visit_block(block, &mut processor);
59+
}
60+
}
61+
62+
impl RuleConfiguration for ConvertLuauNumbers {
63+
fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
64+
verify_no_rule_properties(&properties)?;
65+
66+
Ok(())
67+
}
68+
69+
fn get_name(&self) -> &'static str {
70+
CONVERT_LUAU_NUMBERS_RULE_NAME
71+
}
72+
73+
fn serialize_to_properties(&self) -> RuleProperties {
74+
RuleProperties::new()
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod test {
80+
use super::*;
81+
use crate::rules::Rule;
82+
83+
use insta::assert_json_snapshot;
84+
85+
fn new_rule() -> ConvertLuauNumbers {
86+
ConvertLuauNumbers::default()
87+
}
88+
89+
#[test]
90+
fn serialize_default_rule() {
91+
let rule: Box<dyn Rule> = Box::new(new_rule());
92+
93+
assert_json_snapshot!("default_convert_luau_numbers", rule);
94+
}
95+
96+
#[test]
97+
fn configure_with_extra_field_error() {
98+
let result = json5::from_str::<Box<dyn Rule>>(
99+
r#"{
100+
rule: 'convert_luau_numbers',
101+
prop: "something",
102+
}"#,
103+
);
104+
pretty_assertions::assert_eq!(result.unwrap_err().to_string(), "unexpected field 'prop'");
105+
}
106+
}

src/rules/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod call_parens;
66
mod compute_expression;
77
mod configuration_error;
88
mod convert_index_to_field;
9+
mod convert_luau_numbers;
910
mod convert_require;
1011
mod empty_do;
1112
mod filter_early_return;
@@ -23,7 +24,6 @@ mod remove_floor_division;
2324
mod remove_if_expression;
2425
mod remove_interpolated_string;
2526
mod remove_nil_declarations;
26-
mod remove_number_literals;
2727
mod remove_spaces;
2828
mod remove_types;
2929
mod remove_unused_variable;
@@ -40,6 +40,7 @@ pub use call_parens::*;
4040
pub use compute_expression::*;
4141
pub use configuration_error::RuleConfigurationError;
4242
pub use convert_index_to_field::*;
43+
pub use convert_luau_numbers::*;
4344
pub use convert_require::*;
4445
pub use empty_do::*;
4546
pub use filter_early_return::*;
@@ -56,7 +57,6 @@ pub use remove_floor_division::*;
5657
pub use remove_if_expression::*;
5758
pub use remove_interpolated_string::*;
5859
pub use remove_nil_declarations::*;
59-
pub use remove_number_literals::*;
6060
pub use remove_spaces::*;
6161
pub use remove_types::*;
6262
pub use remove_unused_variable::*;
@@ -237,7 +237,6 @@ pub fn get_default_rules() -> Vec<Box<dyn Rule>> {
237237
Box::<RemoveNilDeclaration>::default(),
238238
Box::<RenameVariables>::default(),
239239
Box::<RemoveFunctionCallParens>::default(),
240-
Box::<RemoveNumberLiterals>::default(),
241240
]
242241
}
243242

@@ -268,7 +267,7 @@ pub fn get_all_rule_names() -> Vec<&'static str> {
268267
RENAME_VARIABLES_RULE_NAME,
269268
REMOVE_IF_EXPRESSION_RULE_NAME,
270269
REMOVE_CONTINUE_RULE_NAME,
271-
REMOVE_NUMBER_LITERALS_RULE_NAME,
270+
CONVERT_LUAU_NUMBERS_RULE_NAME,
272271
]
273272
}
274273

@@ -305,7 +304,7 @@ impl FromStr for Box<dyn Rule> {
305304
RENAME_VARIABLES_RULE_NAME => Box::<RenameVariables>::default(),
306305
REMOVE_IF_EXPRESSION_RULE_NAME => Box::<RemoveIfExpression>::default(),
307306
REMOVE_CONTINUE_RULE_NAME => Box::<RemoveContinue>::default(),
308-
REMOVE_NUMBER_LITERALS_RULE_NAME => Box::<RemoveNumberLiterals>::default(),
307+
CONVERT_LUAU_NUMBERS_RULE_NAME => Box::<ConvertLuauNumbers>::default(),
309308
_ => return Err(format!("invalid rule name: {}", string)),
310309
};
311310

src/rules/remove_number_literals.rs

-90
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: src/rules/convert_luau_numbers.rs
3+
expression: rule
4+
---
5+
"convert_luau_numbers"

src/rules/snapshots/darklua_core__rules__test__all_rule_names.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ expression: rule_names
2828
"rename_variables",
2929
"remove_if_expression",
3030
"remove_continue",
31-
"remove_number_literals"
31+
"convert_luau_numbers"
3232
]

src/rules/snapshots/darklua_core__rules__test__default_rules.snap

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ expression: rules
1515
"convert_index_to_field",
1616
"remove_nil_declaration",
1717
"rename_variables",
18-
"remove_function_call_parens",
19-
"remove_number_literals"
18+
"remove_function_call_parens"
2019
]
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use darklua_core::rules::{ConvertLuauNumbers, Rule};
2+
3+
test_rule!(
4+
convert_luau_numbers,
5+
ConvertLuauNumbers::default(),
6+
binary_integer_literals("local a = 0b01010101 local b = 0B01010101 local c = 0b_0101_0101 local d = 0B_0101_0101 local e = 0b__________0101_0101")
7+
=> "local a = 85 local b = 85 local c = 85 local d = 85 local e = 85",
8+
decimal_separators("local a = 1_048_576 local a1 = 1___048__576__ local b = 0xFFFF_FFFF local c = 0b_0101_0101 local d = 0B_0101_0101")
9+
=> "local a = 1048576 local a1 = 1048576 local b = 0xFFFFFFFF local c = 85 local d = 85",
10+
);
11+
12+
#[test]
13+
fn deserialize_from_object_notation() {
14+
json5::from_str::<Box<dyn Rule>>(
15+
r#"{
16+
rule: 'convert_luau_numbers',
17+
}"#,
18+
)
19+
.unwrap();
20+
}
21+
22+
#[test]
23+
fn deserialize_from_string() {
24+
json5::from_str::<Box<dyn Rule>>("'convert_luau_numbers'").unwrap();
25+
}

tests/rule_tests/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ macro_rules! test_rule_snapshot {
418418
mod append_text_comment;
419419
mod compute_expression;
420420
mod convert_index_to_field;
421+
mod convert_luau_numbers;
421422
mod convert_require;
422423
mod filter_early_return;
423424
mod group_local_assignment;
@@ -435,7 +436,6 @@ mod remove_if_expression;
435436
mod remove_interpolated_string;
436437
mod remove_method_definition;
437438
mod remove_nil_declaration;
438-
mod remove_number_literals;
439439
mod remove_types;
440440
mod remove_unused_if_branch;
441441
mod remove_unused_variable;

tests/rule_tests/remove_number_literals.rs

-27
This file was deleted.

0 commit comments

Comments
 (0)