1use crate::nodes::*;
2use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
3use crate::rules::{
4 Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleMetadata, RuleProperties,
5};
6
7use super::verify_no_rule_properties;
8
9#[derive(Debug, Default)]
10pub(crate) struct RemoveWhitespacesProcessor {}
11
12impl NodeProcessor for RemoveWhitespacesProcessor {
13 fn process_block(&mut self, block: &mut Block) {
14 block.clear_whitespaces();
15 }
16
17 fn process_function_call(&mut self, call: &mut FunctionCall) {
18 call.clear_whitespaces();
19 call.mutate_arguments().clear_whitespaces();
20 }
21
22 fn process_assign_statement(&mut self, assign: &mut AssignStatement) {
23 assign.clear_whitespaces();
24 }
25
26 fn process_compound_assign_statement(&mut self, assign: &mut CompoundAssignStatement) {
27 assign.clear_whitespaces();
28 }
29
30 fn process_do_statement(&mut self, statement: &mut DoStatement) {
31 statement.clear_whitespaces();
32 }
33
34 fn process_function_statement(&mut self, function: &mut FunctionStatement) {
35 function.clear_whitespaces();
36 }
37
38 fn process_generic_for_statement(&mut self, generic_for: &mut GenericForStatement) {
39 generic_for.clear_whitespaces();
40 }
41
42 fn process_if_statement(&mut self, if_statement: &mut IfStatement) {
43 if_statement.clear_whitespaces();
44 }
45
46 fn process_last_statement(&mut self, statement: &mut LastStatement) {
47 match statement {
48 LastStatement::Break(token) | LastStatement::Continue(token) => {
49 if let Some(token) = token {
50 token.clear_whitespaces();
51 }
52 }
53 LastStatement::Return(statement) => statement.clear_whitespaces(),
54 }
55 }
56
57 fn process_local_assign_statement(&mut self, assign: &mut VariableAssignment) {
58 assign.clear_whitespaces();
59 }
60
61 fn process_local_function_statement(&mut self, function: &mut FunctionAssignment) {
62 function.clear_whitespaces();
63 }
64
65 fn process_numeric_for_statement(&mut self, numeric_for: &mut NumericForStatement) {
66 numeric_for.clear_whitespaces();
67 }
68
69 fn process_repeat_statement(&mut self, repeat: &mut RepeatStatement) {
70 repeat.clear_whitespaces();
71 }
72
73 fn process_while_statement(&mut self, statement: &mut WhileStatement) {
74 statement.clear_whitespaces();
75 }
76
77 fn process_type_declaration(&mut self, type_declaration: &mut TypeDeclarationStatement) {
78 type_declaration.clear_whitespaces();
79 }
80
81 fn process_type_function(&mut self, type_function: &mut TypeFunctionStatement) {
82 type_function.clear_whitespaces();
83 }
84
85 fn process_attributes(&mut self, attributes: &mut Attributes) {
86 attributes.clear_whitespaces();
87 }
88
89 fn process_literal_expression(&mut self, expression: &mut LiteralExpression) {
90 match expression {
91 LiteralExpression::True(token)
92 | LiteralExpression::False(token)
93 | LiteralExpression::Nil(token) => {
94 if let Some(token) = token {
95 token.clear_whitespaces()
96 }
97 }
98 LiteralExpression::Number(_)
99 | LiteralExpression::String(_)
100 | LiteralExpression::Table(_) => {}
101 }
102 }
103
104 fn process_literal_table(&mut self, table: &mut LiteralTable) {
105 table.clear_whitespaces();
106 }
107
108 fn process_expression(&mut self, expression: &mut Expression) {
109 match expression {
110 Expression::False(token)
111 | Expression::Nil(token)
112 | Expression::True(token)
113 | Expression::VariableArguments(token) => {
114 if let Some(token) = token {
115 token.clear_whitespaces()
116 }
117 }
118 Expression::Binary(_)
119 | Expression::Call(_)
120 | Expression::Field(_)
121 | Expression::Function(_)
122 | Expression::Identifier(_)
123 | Expression::If(_)
124 | Expression::Index(_)
125 | Expression::Number(_)
126 | Expression::Parenthese(_)
127 | Expression::String(_)
128 | Expression::InterpolatedString(_)
129 | Expression::Table(_)
130 | Expression::Unary(_)
131 | Expression::TypeCast(_)
132 | Expression::TypeInstantiation(_) => {}
133 }
134 }
135
136 fn process_binary_expression(&mut self, binary: &mut BinaryExpression) {
137 binary.clear_whitespaces();
138 }
139
140 fn process_field_expression(&mut self, field: &mut FieldExpression) {
141 field.clear_whitespaces();
142 }
143
144 fn process_function_expression(&mut self, function: &mut FunctionExpression) {
145 function.clear_whitespaces();
146 }
147
148 fn process_if_expression(&mut self, if_expression: &mut IfExpression) {
149 if_expression.clear_whitespaces();
150 }
151
152 fn process_variable_expression(&mut self, identifier: &mut Identifier) {
153 identifier.clear_whitespaces();
154 }
155
156 fn process_index_expression(&mut self, index: &mut IndexExpression) {
157 index.clear_whitespaces();
158 }
159
160 fn process_number_expression(&mut self, number: &mut NumberExpression) {
161 number.clear_whitespaces();
162 }
163
164 fn process_parenthese_expression(&mut self, expression: &mut ParentheseExpression) {
165 expression.clear_whitespaces();
166 }
167
168 fn process_string_expression(&mut self, string: &mut StringExpression) {
169 string.clear_whitespaces();
170 }
171
172 fn process_table_expression(&mut self, table: &mut TableExpression) {
173 table.clear_whitespaces();
174 }
175
176 fn process_unary_expression(&mut self, unary: &mut UnaryExpression) {
177 unary.clear_whitespaces();
178 }
179
180 fn process_interpolated_string_expression(
181 &mut self,
182 string: &mut InterpolatedStringExpression,
183 ) {
184 string.clear_whitespaces();
185 }
186
187 fn process_type_cast_expression(&mut self, type_cast: &mut TypeCastExpression) {
188 type_cast.clear_whitespaces();
189 }
190
191 fn process_type_instantiation(&mut self, type_instantiation: &mut TypeInstantiationExpression) {
192 type_instantiation.clear_whitespaces();
193 }
194
195 fn process_prefix_expression(&mut self, _: &mut Prefix) {}
196
197 fn process_type(&mut self, r#type: &mut Type) {
198 match r#type {
199 Type::True(token) | Type::False(token) | Type::Nil(token) => {
200 if let Some(token) = token {
201 token.clear_whitespaces();
202 }
203 }
204 _ => {}
205 }
206 }
207
208 fn process_type_name(&mut self, type_name: &mut TypeName) {
209 type_name.clear_whitespaces();
210 }
211
212 fn process_type_field(&mut self, type_field: &mut TypeField) {
213 type_field.clear_whitespaces();
214 }
215
216 fn process_string_type(&mut self, string_type: &mut StringType) {
217 string_type.clear_whitespaces();
218 }
219
220 fn process_array_type(&mut self, array: &mut ArrayType) {
221 array.clear_whitespaces();
222 }
223
224 fn process_table_type(&mut self, table: &mut TableType) {
225 table.clear_whitespaces();
226 }
227
228 fn process_expression_type(&mut self, expression_type: &mut ExpressionType) {
229 expression_type.clear_whitespaces();
230 }
231
232 fn process_parenthese_type(&mut self, parenthese_type: &mut ParentheseType) {
233 parenthese_type.clear_whitespaces();
234 }
235
236 fn process_function_type(&mut self, function_type: &mut FunctionType) {
237 function_type.clear_whitespaces();
238 }
239
240 fn process_optional_type(&mut self, optional: &mut OptionalType) {
241 optional.clear_whitespaces();
242 }
243
244 fn process_intersection_type(&mut self, intersection: &mut IntersectionType) {
245 intersection.clear_whitespaces();
246 }
247
248 fn process_union_type(&mut self, union: &mut UnionType) {
249 union.clear_whitespaces();
250 }
251
252 fn process_type_pack(&mut self, type_pack: &mut TypePack) {
253 type_pack.clear_whitespaces();
254 }
255
256 fn process_generic_type_pack(&mut self, generic_type_pack: &mut GenericTypePack) {
257 generic_type_pack.clear_whitespaces();
258 }
259
260 fn process_variadic_type_pack(&mut self, variadic_type_pack: &mut VariadicTypePack) {
261 variadic_type_pack.clear_whitespaces();
262 }
263}
264
265pub const REMOVE_SPACES_RULE_NAME: &str = "remove_spaces";
266
267#[derive(Debug, Default, PartialEq, Eq)]
269pub struct RemoveSpaces {
270 metadata: RuleMetadata,
271}
272
273impl FlawlessRule for RemoveSpaces {
274 fn flawless_process(&self, block: &mut Block, _: &Context) {
275 let mut processor = RemoveWhitespacesProcessor::default();
276 DefaultVisitor::visit_block(block, &mut processor);
277 }
278}
279
280impl RuleConfiguration for RemoveSpaces {
281 fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
282 verify_no_rule_properties(&properties)?;
283 Ok(())
284 }
285
286 fn get_name(&self) -> &'static str {
287 REMOVE_SPACES_RULE_NAME
288 }
289
290 fn serialize_to_properties(&self) -> RuleProperties {
291 RuleProperties::new()
292 }
293
294 fn set_metadata(&mut self, metadata: RuleMetadata) {
295 self.metadata = metadata;
296 }
297
298 fn metadata(&self) -> &RuleMetadata {
299 &self.metadata
300 }
301}
302
303#[cfg(test)]
304mod test {
305 use super::*;
306 use crate::{
307 generator::{LuaGenerator, TokenBasedLuaGenerator},
308 rules::{ContextBuilder, Rule},
309 Parser, Resources,
310 };
311
312 use insta::assert_json_snapshot;
313
314 fn new_rule() -> RemoveSpaces {
315 RemoveSpaces::default()
316 }
317
318 #[test]
319 fn serialize_default_rule() {
320 let rule: Box<dyn Rule> = Box::new(new_rule());
321
322 assert_json_snapshot!(rule, @r###""remove_spaces""###);
323 }
324
325 #[test]
326 fn configure_with_extra_field_error() {
327 let result = json5::from_str::<Box<dyn Rule>>(
328 r#"{
329 rule: 'remove_spaces',
330 prop: "something",
331 }"#,
332 );
333 insta::assert_snapshot!(result.unwrap_err().to_string(), @"unexpected field 'prop' at line 1 column 1");
334 }
335
336 #[test]
337 fn remove_spaces_in_code() {
338 let code = include_str!("../../tests/test_cases/spaces_and_comments.lua");
339
340 let parser = Parser::default().preserve_tokens();
341
342 let mut block = parser.parse(code).expect("unable to parse code");
343
344 RemoveSpaces::default().flawless_process(
345 &mut block,
346 &ContextBuilder::new(".", &Resources::from_memory(), code).build(),
347 );
348
349 let mut generator = TokenBasedLuaGenerator::new(code);
350
351 generator.write_block(&block);
352
353 let code_output = &generator.into_string();
354
355 insta::assert_snapshot!("remove_spaces_in_code", code_output);
356 }
357}