arity-formatter 0.7.1

Deterministic, rule-based formatter for the R language
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Opt-in formatter verification.
//!
//! The ordinary formatting entry points stay single-pass. These counterparts
//! parse and render twice, comparing a layout-free projection between passes so
//! callers can pay for stronger guarantees when correctness matters more than
//! throughput.

use std::collections::HashSet;

use rowan::{NodeOrToken, TextRange};

use super::core::{FormatError, format_node};
use super::style::FormatStyle;
use crate::ast::{AstNode, Expr, ForExpr, FunctionExpr, IfExpr, RepeatExpr, WhileExpr};
use crate::parser::{ParseOptions, parse_with_options};
use crate::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};

/// Why verified formatting could not establish its guarantees.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FormatVerificationError {
    /// The original input could not be formatted.
    Format(FormatError),
    /// The first pass emitted output that could not be parsed or reformatted.
    OutputInvalid(FormatError),
    /// The normalized syntax changed during formatting.
    SyntaxChanged { detail: String },
    /// An ordinary comment was changed, lost, duplicated, or reordered.
    CommentsChanged { detail: String },
    /// The second formatting pass did not reproduce the first pass byte-for-byte.
    NonIdempotent,
}

impl std::fmt::Display for FormatVerificationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Format(err) => err.fmt(f),
            Self::OutputInvalid(err) => {
                write!(f, "formatted output failed verification: {err}")
            }
            Self::SyntaxChanged { detail } => write!(
                f,
                "formatter verification failed (formatted output changed R syntax): {detail}"
            ),
            Self::CommentsChanged { detail } => write!(
                f,
                "formatter verification failed (formatted output changed ordinary comments): {detail}"
            ),
            Self::NonIdempotent => {
                f.write_str("formatter verification failed (non-idempotent output)")
            }
        }
    }
}

impl std::error::Error for FormatVerificationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Format(err) | Self::OutputInvalid(err) => Some(err),
            _ => None,
        }
    }
}

impl From<FormatError> for FormatVerificationError {
    fn from(value: FormatError) -> Self {
        Self::Format(value)
    }
}

/// Format with the default style, then verify syntax preservation, ordinary
/// comment preservation, and idempotence.
pub fn format_verified(input: &str) -> Result<String, FormatVerificationError> {
    format_verified_with_style(input, FormatStyle::default())
}

/// [`format_verified`] with an explicit style.
pub fn format_verified_with_style(
    input: &str,
    style: FormatStyle,
) -> Result<String, FormatVerificationError> {
    format_verified_with_options(input, style, &ParseOptions::default())
}

/// [`format_verified_with_style`] with parser options such as the package-wide
/// roxygen markdown default.
pub fn format_verified_with_options(
    input: &str,
    style: FormatStyle,
    options: &ParseOptions,
) -> Result<String, FormatVerificationError> {
    let before = parse_with_options(input, options);
    if !before.diagnostics.is_empty() {
        return Err(FormatVerificationError::Format(FormatError::ParseErrors {
            count: before.diagnostics.len(),
        }));
    }

    let formatted = format_node(&before.cst, style, input)?;
    let after = parse_with_options(&formatted, options);
    if !after.diagnostics.is_empty() {
        return Err(FormatVerificationError::OutputInvalid(
            FormatError::ParseErrors {
                count: after.diagnostics.len(),
            },
        ));
    }

    verify_preservation(&before.cst, &after.cst).map_err(|err| match err {
        PreservationError::SyntaxChanged { detail } => {
            FormatVerificationError::SyntaxChanged { detail }
        }
        PreservationError::CommentsChanged { detail } => {
            FormatVerificationError::CommentsChanged { detail }
        }
    })?;

    let reformatted = format_node(&after.cst, style, &formatted)
        .map_err(FormatVerificationError::OutputInvalid)?;
    if reformatted != formatted {
        return Err(FormatVerificationError::NonIdempotent);
    }
    Ok(formatted)
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum PreservationError {
    SyntaxChanged { detail: String },
    CommentsChanged { detail: String },
}

impl std::fmt::Display for PreservationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SyntaxChanged { detail } => write!(f, "syntax changed: {detail}"),
            Self::CommentsChanged { detail } => write!(f, "comments changed: {detail}"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum CanonicalEvent {
    Enter(SyntaxKind),
    Token(SyntaxKind, String),
    Leave(SyntaxKind),
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct LocatedEvent {
    event: CanonicalEvent,
    range: TextRange,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct ElementKey {
    range: TextRange,
    kind: SyntaxKind,
    node: bool,
}

impl ElementKey {
    fn new(element: &SyntaxElement) -> Self {
        Self {
            range: element.text_range(),
            kind: element.kind(),
            node: element.as_node().is_some(),
        }
    }
}

fn verify_preservation(before: &SyntaxNode, after: &SyntaxNode) -> Result<(), PreservationError> {
    let before_syntax = canonical_syntax(before);
    let after_syntax = canonical_syntax(after);
    let syntax_equal = before_syntax.len() == after_syntax.len()
        && before_syntax
            .iter()
            .zip(&after_syntax)
            .all(|(left, right)| left.event == right.event);
    if !syntax_equal {
        return Err(PreservationError::SyntaxChanged {
            detail: first_event_difference(&before_syntax, &after_syntax),
        });
    }

    let before_comments = ordinary_comments(before);
    let after_comments = ordinary_comments(after);
    if before_comments != after_comments {
        let index = before_comments
            .iter()
            .zip(&after_comments)
            .position(|(left, right)| left != right)
            .unwrap_or_else(|| before_comments.len().min(after_comments.len()));
        return Err(PreservationError::CommentsChanged {
            detail: format!(
                "comment {index}: input {:?}, formatted {:?}",
                before_comments.get(index),
                after_comments.get(index)
            ),
        });
    }
    Ok(())
}

fn first_event_difference(before: &[LocatedEvent], after: &[LocatedEvent]) -> String {
    let index = before
        .iter()
        .zip(after)
        .position(|(left, right)| left.event != right.event)
        .unwrap_or_else(|| before.len().min(after.len()));
    format!(
        "event {index}: input {}, formatted {}",
        describe_event(before.get(index)),
        describe_event(after.get(index))
    )
}

fn describe_event(event: Option<&LocatedEvent>) -> String {
    match event {
        Some(event) => format!("{:?} at {:?}", event.event, event.range),
        None => "<end of tree>".to_string(),
    }
}

fn canonical_syntax(root: &SyntaxNode) -> Vec<LocatedEvent> {
    let body_elements = body_elements(root);
    let mut events = Vec::new();
    emit_node(root, &body_elements, &mut events);
    events
}

fn body_elements(root: &SyntaxNode) -> HashSet<ElementKey> {
    let mut bodies = HashSet::new();
    for node in root.descendants() {
        match node.kind() {
            SyntaxKind::FUNCTION_EXPR => {
                if let Some(body) = FunctionExpr::cast(node).and_then(|expr| expr.body()) {
                    bodies.insert(ElementKey::new(&body));
                }
            }
            SyntaxKind::IF_EXPR => {
                if let Some(expr) = IfExpr::cast(node) {
                    if let Some(body) = expr.then_elements().and_then(sole_expression) {
                        bodies.insert(ElementKey::new(&body));
                    }
                    if let Some(body) = expr.else_elements().and_then(sole_expression) {
                        bodies.insert(ElementKey::new(&body));
                    }
                }
            }
            SyntaxKind::FOR_EXPR => {
                if let Some(body) = ForExpr::cast(node).and_then(|expr| expr.body_element()) {
                    bodies.insert(ElementKey::new(&body));
                }
            }
            SyntaxKind::WHILE_EXPR => {
                if let Some(body) = WhileExpr::cast(node).and_then(|expr| expr.body_element()) {
                    bodies.insert(ElementKey::new(&body));
                }
            }
            SyntaxKind::REPEAT_EXPR => {
                if let Some(body) = RepeatExpr::cast(node).and_then(|expr| expr.body()) {
                    bodies.insert(ElementKey::new(&body));
                }
            }
            _ => {}
        }
    }
    bodies
}

fn sole_expression(elements: Vec<SyntaxElement>) -> Option<SyntaxElement> {
    let mut expression = None;
    for element in elements {
        if is_ignored_token(element.kind()) || element.kind() == SyntaxKind::ROXYGEN_BLOCK {
            continue;
        }
        Expr::cast(element.clone())?;
        if expression.replace(element).is_some() {
            return None;
        }
    }
    expression
}

fn emit_node(node: &SyntaxNode, bodies: &HashSet<ElementKey>, events: &mut Vec<LocatedEvent>) {
    if node.kind() == SyntaxKind::ROXYGEN_BLOCK {
        return;
    }
    // `ARG` is parser scaffolding around comma-delimited content. In
    // particular, a comment-only call line becomes an empty `ARG` after
    // comments are projected out; commas still preserve real argument holes.
    let transparent = node.kind() == SyntaxKind::ARG;
    if !transparent {
        events.push(LocatedEvent {
            event: CanonicalEvent::Enter(node.kind()),
            range: node.text_range(),
        });
    }
    for child in node.children_with_tokens() {
        emit_element(child, bodies, events);
    }
    if !transparent {
        events.push(LocatedEvent {
            event: CanonicalEvent::Leave(node.kind()),
            range: node.text_range(),
        });
    }
}

fn emit_element(
    element: SyntaxElement,
    bodies: &HashSet<ElementKey>,
    events: &mut Vec<LocatedEvent>,
) {
    if bodies.contains(&ElementKey::new(&element))
        && let Some(node) = element.as_node()
        && let Some(expression) = single_block_expression(node)
    {
        emit_element(expression, bodies, events);
        return;
    }

    match element {
        NodeOrToken::Node(node) => emit_node(&node, bodies, events),
        NodeOrToken::Token(token) if is_ignored_token(token.kind()) => {}
        NodeOrToken::Token(token) => events.push(LocatedEvent {
            event: CanonicalEvent::Token(token.kind(), token.text().to_string()),
            range: token.text_range(),
        }),
    }
}

fn single_block_expression(node: &SyntaxNode) -> Option<SyntaxElement> {
    if node.kind() != SyntaxKind::BLOCK_EXPR {
        return None;
    }
    let mut expression = None;
    for element in node.children_with_tokens() {
        if is_ignored_token(element.kind())
            || element.kind() == SyntaxKind::ROXYGEN_BLOCK
            || matches!(element.kind(), SyntaxKind::LBRACE | SyntaxKind::RBRACE)
        {
            continue;
        }
        Expr::cast(element.clone())?;
        if expression.replace(element).is_some() {
            return None;
        }
    }
    expression
}

fn is_ignored_token(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE | SyntaxKind::SEMICOLON | SyntaxKind::COMMENT
    )
}

fn ordinary_comments(root: &SyntaxNode) -> Vec<String> {
    let mut comments = Vec::new();
    collect_comments(root, false, &mut comments);
    comments
}

fn collect_comments(node: &SyntaxNode, in_roxygen: bool, comments: &mut Vec<String>) {
    let in_roxygen = in_roxygen || node.kind() == SyntaxKind::ROXYGEN_BLOCK;
    for child in node.children_with_tokens() {
        match child {
            NodeOrToken::Node(node) => collect_comments(&node, in_roxygen, comments),
            NodeOrToken::Token(token) if !in_roxygen && token.kind() == SyntaxKind::COMMENT => {
                comments.push(token.text().trim_end().to_string());
            }
            NodeOrToken::Token(_) => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{PreservationError, verify_preservation};
    use crate::parser::parse;

    fn verify(before: &str, after: &str) -> Result<(), PreservationError> {
        verify_preservation(&parse(before).cst, &parse(after).cst)
    }

    #[test]
    fn ignores_layout_comments_and_statement_separators() {
        verify("x <- 1; # first\ny <- 2\n", "# first\nx<-1\ny<-2\n").unwrap();
    }

    #[test]
    fn ignores_trailing_whitespace_in_ordinary_comments() {
        verify("x # comment \n", "x # comment\n").unwrap();
    }

    #[test]
    fn normalizes_single_expression_body_blocks() {
        for (before, after) in [
            ("function(x) x", "function(x) { x }"),
            ("if (x) y else z", "if (x) { y } else { z }"),
            ("for (x in xs) f(x)", "for (x in xs) { f(x) }"),
            ("while (x) f()", "while (x) { f() }"),
            ("repeat f()", "repeat { f() }"),
        ] {
            verify(before, after).unwrap_or_else(|err| panic!("{before:?} != {after:?}: {err}"));
            verify(after, before).unwrap_or_else(|err| panic!("{after:?} != {before:?}: {err}"));
        }
    }

    #[test]
    fn retains_blocks_outside_body_positions_and_nested_blocks() {
        assert!(matches!(
            verify("x <- { y }", "x <- y"),
            Err(PreservationError::SyntaxChanged { .. })
        ));
        assert!(matches!(
            verify("function() {{ x }}", "function() x"),
            Err(PreservationError::SyntaxChanged { .. })
        ));
        assert!(matches!(
            verify("function() { x; y }", "function() x"),
            Err(PreservationError::SyntaxChanged { .. })
        ));
    }

    #[test]
    fn detects_tree_token_and_comment_changes() {
        for (before, after) in [
            ("x + y", "x * y"),
            ("(x + y) * z", "x + y * z"),
            ("f(x, , y)", "f(x, y)"),
            ("x <- { y }", "x <- y"),
        ] {
            assert!(matches!(
                verify(before, after),
                Err(PreservationError::SyntaxChanged { .. })
            ));
        }

        assert!(matches!(
            verify("x # one\n", "x # two\n"),
            Err(PreservationError::CommentsChanged { .. })
        ));
        assert!(matches!(
            verify("# one\n# two\nx\n", "# two\n# one\nx\n"),
            Err(PreservationError::CommentsChanged { .. })
        ));
    }

    #[test]
    fn comment_only_arguments_do_not_change_syntax() {
        verify("f(\n  # comment\n)\n", "# comment\nf()\n").unwrap();
    }

    #[test]
    fn ignores_roxygen_content_but_not_executable_code() {
        verify("#' one\nf()\n", "#' completely different\nf()\n").unwrap();
        assert!(matches!(
            verify("#' one\nf()\n", "one\nf()\n"),
            Err(PreservationError::SyntaxChanged { .. })
        ));
    }
}