darklua_core/rules/
shift_token_line.rs

1use crate::nodes::*;
2use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
3use crate::rules::{
4    Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleProperties,
5};
6
7use super::verify_no_rule_properties;
8
9#[derive(Debug)]
10struct Processor {
11    shift_amount: usize,
12}
13
14impl Processor {
15    fn new(shift_amount: usize) -> Self {
16        Self { shift_amount }
17    }
18}
19
20impl NodeProcessor for Processor {
21    fn process_block(&mut self, block: &mut Block) {
22        block.shift_token_line(self.shift_amount);
23    }
24
25    fn process_function_call(&mut self, call: &mut FunctionCall) {
26        call.shift_token_line(self.shift_amount);
27        call.mutate_arguments().shift_token_line(self.shift_amount);
28    }
29
30    fn process_assign_statement(&mut self, assign: &mut AssignStatement) {
31        assign.shift_token_line(self.shift_amount);
32    }
33
34    fn process_compound_assign_statement(&mut self, assign: &mut CompoundAssignStatement) {
35        assign.shift_token_line(self.shift_amount);
36    }
37
38    fn process_do_statement(&mut self, statement: &mut DoStatement) {
39        statement.shift_token_line(self.shift_amount);
40    }
41
42    fn process_function_statement(&mut self, function: &mut FunctionStatement) {
43        function.shift_token_line(self.shift_amount);
44    }
45
46    fn process_generic_for_statement(&mut self, generic_for: &mut GenericForStatement) {
47        generic_for.shift_token_line(self.shift_amount);
48    }
49
50    fn process_if_statement(&mut self, if_statement: &mut IfStatement) {
51        if_statement.shift_token_line(self.shift_amount);
52    }
53
54    fn process_last_statement(&mut self, statement: &mut LastStatement) {
55        match statement {
56            LastStatement::Break(token) | LastStatement::Continue(token) => {
57                if let Some(token) = token {
58                    token.shift_token_line(self.shift_amount);
59                }
60            }
61            LastStatement::Return(statement) => statement.shift_token_line(self.shift_amount),
62        }
63    }
64
65    fn process_local_assign_statement(&mut self, assign: &mut LocalAssignStatement) {
66        assign.shift_token_line(self.shift_amount);
67    }
68
69    fn process_local_function_statement(&mut self, function: &mut LocalFunctionStatement) {
70        function.shift_token_line(self.shift_amount);
71    }
72
73    fn process_numeric_for_statement(&mut self, numeric_for: &mut NumericForStatement) {
74        numeric_for.shift_token_line(self.shift_amount);
75    }
76
77    fn process_repeat_statement(&mut self, repeat: &mut RepeatStatement) {
78        repeat.shift_token_line(self.shift_amount);
79    }
80
81    fn process_while_statement(&mut self, statement: &mut WhileStatement) {
82        statement.shift_token_line(self.shift_amount);
83    }
84
85    fn process_type_declaration(&mut self, type_declaration: &mut TypeDeclarationStatement) {
86        type_declaration.shift_token_line(self.shift_amount);
87    }
88
89    fn process_expression(&mut self, expression: &mut Expression) {
90        match expression {
91            Expression::False(token)
92            | Expression::Nil(token)
93            | Expression::True(token)
94            | Expression::VariableArguments(token) => {
95                if let Some(token) = token {
96                    token.shift_token_line(self.shift_amount)
97                }
98            }
99            Expression::Binary(_)
100            | Expression::Call(_)
101            | Expression::Field(_)
102            | Expression::Function(_)
103            | Expression::Identifier(_)
104            | Expression::If(_)
105            | Expression::Index(_)
106            | Expression::Number(_)
107            | Expression::Parenthese(_)
108            | Expression::String(_)
109            | Expression::InterpolatedString(_)
110            | Expression::Table(_)
111            | Expression::Unary(_)
112            | Expression::TypeCast(_) => {}
113        }
114    }
115
116    fn process_binary_expression(&mut self, binary: &mut BinaryExpression) {
117        binary.shift_token_line(self.shift_amount);
118    }
119
120    fn process_field_expression(&mut self, field: &mut FieldExpression) {
121        field.shift_token_line(self.shift_amount);
122    }
123
124    fn process_function_expression(&mut self, function: &mut FunctionExpression) {
125        function.shift_token_line(self.shift_amount);
126    }
127
128    fn process_if_expression(&mut self, if_expression: &mut IfExpression) {
129        if_expression.shift_token_line(self.shift_amount);
130    }
131
132    fn process_variable_expression(&mut self, identifier: &mut Identifier) {
133        identifier.shift_token_line(self.shift_amount);
134    }
135
136    fn process_index_expression(&mut self, index: &mut IndexExpression) {
137        index.shift_token_line(self.shift_amount);
138    }
139
140    fn process_number_expression(&mut self, number: &mut NumberExpression) {
141        number.shift_token_line(self.shift_amount);
142    }
143
144    fn process_parenthese_expression(&mut self, expression: &mut ParentheseExpression) {
145        expression.shift_token_line(self.shift_amount);
146    }
147
148    fn process_string_expression(&mut self, string: &mut StringExpression) {
149        string.shift_token_line(self.shift_amount);
150    }
151
152    fn process_interpolated_string_expression(
153        &mut self,
154        string: &mut InterpolatedStringExpression,
155    ) {
156        string.shift_token_line(self.shift_amount);
157    }
158
159    fn process_table_expression(&mut self, table: &mut TableExpression) {
160        table.shift_token_line(self.shift_amount);
161    }
162
163    fn process_unary_expression(&mut self, unary: &mut UnaryExpression) {
164        unary.shift_token_line(self.shift_amount);
165    }
166
167    fn process_type_cast_expression(&mut self, type_cast: &mut TypeCastExpression) {
168        type_cast.shift_token_line(self.shift_amount);
169    }
170
171    fn process_prefix_expression(&mut self, _: &mut Prefix) {}
172
173    fn process_type(&mut self, r#type: &mut Type) {
174        match r#type {
175            Type::True(token) | Type::False(token) | Type::Nil(token) => {
176                if let Some(token) = token {
177                    token.shift_token_line(self.shift_amount);
178                }
179            }
180            _ => {}
181        }
182    }
183
184    fn process_type_name(&mut self, type_name: &mut TypeName) {
185        type_name.shift_token_line(self.shift_amount);
186    }
187
188    fn process_type_field(&mut self, type_field: &mut TypeField) {
189        type_field.shift_token_line(self.shift_amount);
190    }
191
192    fn process_string_type(&mut self, string_type: &mut StringType) {
193        string_type.shift_token_line(self.shift_amount);
194    }
195
196    fn process_array_type(&mut self, array: &mut ArrayType) {
197        array.shift_token_line(self.shift_amount);
198    }
199
200    fn process_table_type(&mut self, table: &mut TableType) {
201        table.shift_token_line(self.shift_amount);
202    }
203
204    fn process_expression_type(&mut self, expression_type: &mut ExpressionType) {
205        expression_type.shift_token_line(self.shift_amount);
206    }
207
208    fn process_parenthese_type(&mut self, parenthese_type: &mut ParentheseType) {
209        parenthese_type.shift_token_line(self.shift_amount);
210    }
211
212    fn process_function_type(&mut self, function_type: &mut FunctionType) {
213        function_type.shift_token_line(self.shift_amount);
214    }
215
216    fn process_optional_type(&mut self, optional: &mut OptionalType) {
217        optional.shift_token_line(self.shift_amount);
218    }
219
220    fn process_intersection_type(&mut self, intersection: &mut IntersectionType) {
221        intersection.shift_token_line(self.shift_amount);
222    }
223
224    fn process_union_type(&mut self, union: &mut UnionType) {
225        union.shift_token_line(self.shift_amount);
226    }
227
228    fn process_type_pack(&mut self, type_pack: &mut TypePack) {
229        type_pack.shift_token_line(self.shift_amount);
230    }
231
232    fn process_generic_type_pack(&mut self, generic_type_pack: &mut GenericTypePack) {
233        generic_type_pack.shift_token_line(self.shift_amount);
234    }
235
236    fn process_variadic_type_pack(&mut self, variadic_type_pack: &mut VariadicTypePack) {
237        variadic_type_pack.shift_token_line(self.shift_amount);
238    }
239}
240
241pub const SHIFT_TOKEN_LINE: &str = "shift_token_line";
242
243#[derive(Debug, PartialEq, Eq)]
244pub(crate) struct ShiftTokenLine {
245    shift_amount: usize,
246}
247
248impl ShiftTokenLine {
249    pub(crate) fn new(shift_amount: usize) -> Self {
250        Self { shift_amount }
251    }
252}
253
254impl FlawlessRule for ShiftTokenLine {
255    fn flawless_process(&self, block: &mut Block, _context: &Context) {
256        if self.shift_amount != 0 {
257            let mut processor = Processor::new(self.shift_amount);
258            DefaultVisitor::visit_block(block, &mut processor);
259        }
260    }
261}
262
263impl RuleConfiguration for ShiftTokenLine {
264    fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
265        verify_no_rule_properties(&properties)?;
266        Ok(())
267    }
268
269    fn get_name(&self) -> &'static str {
270        SHIFT_TOKEN_LINE
271    }
272
273    fn serialize_to_properties(&self) -> RuleProperties {
274        RuleProperties::new()
275    }
276}
277
278#[cfg(test)]
279mod test {
280    use super::*;
281    use crate::rules::Rule;
282
283    use insta::assert_json_snapshot;
284
285    fn new_rule() -> ShiftTokenLine {
286        ShiftTokenLine::new(1)
287    }
288
289    #[test]
290    fn serialize_default_rule() {
291        let rule: Box<dyn Rule> = Box::new(new_rule());
292
293        assert_json_snapshot!("default_replace_referenced_tokens", rule);
294    }
295}