bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// formatter.rs - Bash script formatter
// Following ruchy design patterns for code formatting
use crate::bash_parser::ast::{BashAst, BashExpr, BashStmt};
use crate::bash_quality::formatter_config::FormatterConfig;
use anyhow::Result;

pub struct Formatter {
    pub(crate) config: FormatterConfig,
    source: Option<String>,
}

impl Formatter {
    /// Create a new formatter with default configuration
    pub fn new() -> Self {
        Self::with_config(FormatterConfig::default())
    }

    /// Create a new formatter with custom configuration
    pub fn with_config(config: FormatterConfig) -> Self {
        Self {
            config,
            source: None,
        }
    }

    /// Set the original source text for ignore directives
    pub fn set_source(&mut self, source: impl Into<String>) {
        self.source = Some(source.into());
    }

    /// Format a bash AST
    pub fn format(&self, ast: &BashAst) -> Result<String> {
        let mut result = String::new();

        for (i, stmt) in ast.statements.iter().enumerate() {
            if i > 0 {
                result.push('\n');
            }
            result.push_str(&self.format_stmt(stmt, 0));
        }

        Ok(result)
    }

    /// Format a bash script from source text
    pub fn format_source(&mut self, source: &str) -> Result<String> {
        use crate::bash_parser::BashParser;

        self.set_source(source);
        let mut parser = BashParser::new(source)?;
        let ast = parser
            .parse()
            .map_err(|e| anyhow::anyhow!("Parse error: {}", e))?;

        self.format(&ast)
    }

    /// Format a statement (thin dispatcher)
    pub(crate) fn format_stmt(&self, stmt: &BashStmt, indent: usize) -> String {
        let indent_str = self.make_indent(indent);

        match stmt {
            BashStmt::Comment { text, .. } => {
                format!("{}#{}", indent_str, text)
            }

            BashStmt::Assignment {
                name,
                value,
                exported,
                ..
            } => {
                let export = if *exported { "export " } else { "" };
                format!(
                    "{}{}{}={}",
                    indent_str,
                    export,
                    name,
                    self.format_expr(value)
                )
            }

            BashStmt::Command { name, args, .. } => {
                let mut result = format!("{}{}", indent_str, name);
                for arg in args {
                    result.push(' ');
                    result.push_str(&self.format_expr(arg));
                }
                result
            }

            BashStmt::Return { code, .. } => {
                if let Some(expr) = code {
                    format!("{}return {}", indent_str, self.format_expr(expr))
                } else {
                    format!("{}return", indent_str)
                }
            }

            // Control flow: if/for/while/until/case/select
            BashStmt::If { .. }
            | BashStmt::While { .. }
            | BashStmt::Until { .. }
            | BashStmt::For { .. }
            | BashStmt::ForCStyle { .. }
            | BashStmt::Case { .. }
            | BashStmt::Select { .. } => self.format_control_flow_stmt(stmt, indent, &indent_str),

            // Compound: function/pipeline/and/or/brace/coproc/negated
            BashStmt::Function { .. }
            | BashStmt::Pipeline { .. }
            | BashStmt::AndList { .. }
            | BashStmt::OrList { .. }
            | BashStmt::BraceGroup { .. }
            | BashStmt::Coproc { .. }
            | BashStmt::Negated { .. } => self.format_compound_stmt(stmt, indent, &indent_str),
        }
    }

    /// Format control flow statements: if/for/while/until/case/select
    fn format_control_flow_stmt(&self, stmt: &BashStmt, indent: usize, indent_str: &str) -> String {
        match stmt {
            BashStmt::If {
                condition,
                then_block,
                elif_blocks,
                else_block,
                ..
            } => self.format_if_stmt(
                condition,
                then_block,
                elif_blocks,
                else_block,
                indent,
                indent_str,
            ),

            BashStmt::While {
                condition, body, ..
            } => self.format_loop_stmt("while", condition, body, indent, indent_str),

            BashStmt::Until {
                condition, body, ..
            } => self.format_loop_stmt("until", condition, body, indent, indent_str),

            BashStmt::For {
                variable,
                items,
                body,
                ..
            } => self.format_for_stmt(variable, items, body, indent, indent_str),

            BashStmt::ForCStyle {
                init,
                condition,
                increment,
                body,
                ..
            } => self.format_for_c_style_stmt(init, condition, increment, body, indent, indent_str),

            BashStmt::Case { word, arms, .. } => {
                self.format_case_stmt(word, arms, indent, indent_str)
            }

            BashStmt::Select {
                variable,
                items,
                body,
                ..
            } => self.format_select_stmt(variable, items, body, indent, indent_str),

            // Unreachable: caller only passes control flow variants
            _ => unreachable!(),
        }
    }

    /// Format an if/elif/else statement
    fn format_if_stmt(
        &self,
        condition: &BashExpr,
        then_block: &[BashStmt],
        elif_blocks: &[(BashExpr, Vec<BashStmt>)],
        else_block: &Option<Vec<BashStmt>>,
        indent: usize,
        indent_str: &str,
    ) -> String {
        let mut result = format!("{}if ", indent_str);
        result.push_str(&self.format_expr(condition));

        if self.config.inline_then {
            result.push_str("; then");
        } else {
            result.push_str("\nthen");
        }
        result.push('\n');

        for stmt in then_block {
            result.push_str(&self.format_stmt(stmt, indent + 1));
            result.push('\n');
        }

        for (cond, block) in elif_blocks {
            result.push_str(&format!("{}elif ", indent_str));
            result.push_str(&self.format_expr(cond));
            if self.config.inline_then {
                result.push_str("; then\n");
            } else {
                result.push_str("\nthen\n");
            }
            for stmt in block {
                result.push_str(&self.format_stmt(stmt, indent + 1));
                result.push('\n');
            }
        }

        if let Some(else_stmts) = else_block {
            result.push_str(&format!("{}else\n", indent_str));
            for stmt in else_stmts {
                result.push_str(&self.format_stmt(stmt, indent + 1));
                result.push('\n');
            }
        }

        result.push_str(&format!("{}fi", indent_str));
        result
    }

    /// Format while/until loop (shared logic)
    fn format_loop_stmt(
        &self,
        keyword: &str,
        condition: &BashExpr,
        body: &[BashStmt],
        indent: usize,
        indent_str: &str,
    ) -> String {
        let mut result = format!("{}{} ", indent_str, keyword);
        result.push_str(&self.format_expr(condition));
        result.push_str("; do\n");

        for stmt in body {
            result.push_str(&self.format_stmt(stmt, indent + 1));
            result.push('\n');
        }

        result.push_str(&format!("{}done", indent_str));
        result
    }

    /// Format a for-in loop
    fn format_for_stmt(
        &self,
        variable: &str,
        items: &BashExpr,
        body: &[BashStmt],
        indent: usize,
        indent_str: &str,
    ) -> String {
        let mut result = format!("{}for {} in ", indent_str, variable);
        result.push_str(&self.format_expr(items));
        result.push_str("; do\n");

        for stmt in body {
            result.push_str(&self.format_stmt(stmt, indent + 1));
            result.push('\n');
        }

        result.push_str(&format!("{}done", indent_str));
        result
    }

    /// Format a C-style for loop
    fn format_for_c_style_stmt(
        &self,
        init: &str,
        condition: &str,
        increment: &str,
        body: &[BashStmt],
        indent: usize,
        indent_str: &str,
    ) -> String {
        let mut result = format!(
            "{}for (({}; {}; {})); do\n",
            indent_str, init, condition, increment
        );

        for stmt in body {
            result.push_str(&self.format_stmt(stmt, indent + 1));
            result.push('\n');
        }

        result.push_str(&format!("{}done", indent_str));
        result
    }

    /// Format a case statement
    fn format_case_stmt(
        &self,
        word: &BashExpr,
        arms: &[crate::bash_parser::ast::CaseArm],
        indent: usize,
        indent_str: &str,
    ) -> String {
        let mut result = format!("{}case {} in", indent_str, self.format_expr(word));
        result.push('\n');

        for arm in arms {
            // Format pattern(s)
            let pattern_str = arm.patterns.join("|");
            result.push_str(&format!("{}  {})", indent_str, pattern_str));
            result.push('\n');

            // Format body
            for stmt in &arm.body {
                result.push_str(&self.format_stmt(stmt, indent + 2));
                result.push('\n');
            }

            // Add ;;
            result.push_str(&format!("{}    ;;", indent_str));
            result.push('\n');
        }

        result.push_str(&format!("{}esac", indent_str));
        result
    }

    /// Format a select statement
    fn format_select_stmt(
        &self,
        variable: &str,
        items: &BashExpr,
        body: &[BashStmt],
        indent: usize,
        indent_str: &str,
    ) -> String {
        // F017: Format select statement
        let items_str = self.format_expr(items);
        let body_stmts: Vec<String> = body
            .iter()
            .map(|s| self.format_stmt(s, indent + 1))
            .collect();
        format!(
            "{}select {} in {}; do\n{}\n{}done",
            indent_str,
            variable,
            items_str,
            body_stmts.join("\n"),
            indent_str
        )
    }

    /// Format compound statements: function/pipeline/and/or/brace/coproc/negated
    fn format_compound_stmt(&self, stmt: &BashStmt, indent: usize, indent_str: &str) -> String {
        match stmt {
            BashStmt::Function { name, body, .. } => {
                let brace_space = if self.config.space_before_brace {
                    " "
                } else {
                    ""
                };
                let mut result = if self.config.normalize_functions {
                    format!("{}{}(){}{{", indent_str, name, brace_space)
                } else {
                    format!("{}function {}(){}{{", indent_str, name, brace_space)
                };
                result.push('\n');

                for stmt in body {
                    result.push_str(&self.format_stmt(stmt, indent + 1));
                    result.push('\n');
                }

                result.push_str(&format!("{}}}", indent_str));
                result
            }

            BashStmt::Pipeline { commands, .. } => {
                // Format pipeline with proper spacing: cmd1 | cmd2 | cmd3
                let formatted_cmds: Vec<String> = commands
                    .iter()
                    .map(|cmd| self.format_stmt(cmd, 0).trim().to_string())
                    .collect();
                format!("{}{}", indent_str, formatted_cmds.join(" | "))
            }

            BashStmt::AndList { left, right, .. } => {
                // Format AND list: cmd1 && cmd2
                let left_fmt = self.format_stmt(left, 0).trim().to_string();
                let right_fmt = self.format_stmt(right, 0).trim().to_string();
                format!("{}{} && {}", indent_str, left_fmt, right_fmt)
            }

            BashStmt::OrList { left, right, .. } => {
                // Format OR list: cmd1 || cmd2
                let left_fmt = self.format_stmt(left, 0).trim().to_string();
                let right_fmt = self.format_stmt(right, 0).trim().to_string();
                format!("{}{} || {}", indent_str, left_fmt, right_fmt)
            }

            BashStmt::BraceGroup { body, .. } => {
                // Format brace group: { cmd1; cmd2; }
                let stmts: Vec<String> = body
                    .iter()
                    .map(|s| self.format_stmt(s, 0).trim().to_string())
                    .collect();
                format!("{}{{ {}; }}", indent_str, stmts.join("; "))
            }

            BashStmt::Coproc { name, body, .. } => {
                // Format coproc: coproc NAME { cmd; }
                let stmts: Vec<String> = body
                    .iter()
                    .map(|s| self.format_stmt(s, 0).trim().to_string())
                    .collect();
                if let Some(n) = name {
                    format!("{}coproc {} {{ {}; }}", indent_str, n, stmts.join("; "))
                } else {
                    format!("{}coproc {{ {}; }}", indent_str, stmts.join("; "))
                }
            }

            BashStmt::Negated { command, .. } => {
                // Issue #133: Format negated command: ! cmd
                format!("{}! {}", indent_str, self.format_stmt(command, 0).trim())
            }

            // Unreachable: caller only passes compound variants
            _ => unreachable!(),
        }
    }
}
impl Default for Formatter {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
#[path = "formatter_tests_dummy_metada.rs"]
mod tests_ext;