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
use biome_diagnostics::{Category, Diagnostic};
use biome_rowan::{TextRange, TextSize};

/// Single instance of a suppression comment, with the following syntax:
///
/// `// rome-ignore { <category> { (<value>) }? }+: <reason>`
///
/// The category broadly describes what feature is being suppressed (formatting,
/// linting, ...) with the value being and optional, category-specific name of
/// a specific element to disable (for instance a specific lint name). A single
/// suppression may specify one or more categories + values, for instance to
/// disable multiple lints at once
///
/// A suppression must specify a reason: this part has no semantic meaning but
/// is required to document why a particular feature is being disable for this
/// line (lint false-positive, specific formatting requirements, ...)
#[derive(Debug, PartialEq, Eq)]
pub struct Suppression<'a> {
    /// List of categories for this suppression
    ///
    /// Categories are pair of the category name +
    /// an optional category value
    pub categories: Vec<(&'a Category, Option<&'a str>)>,
    /// Reason for this suppression comment to exist
    pub reason: &'a str,
}

pub fn parse_suppression_comment(
    base: &str,
) -> impl Iterator<Item = Result<Suppression, SuppressionDiagnostic>> {
    let (head, mut comment) = base.split_at(2);
    let is_block_comment = match head {
        "//" => false,
        "/*" => {
            comment = comment
                .strip_suffix("*/")
                .or_else(|| comment.strip_suffix(&['*', '/']))
                .unwrap_or(comment);
            true
        }
        token => panic!("comment with unknown opening token {token:?}, from {comment}"),
    };

    comment.lines().filter_map(move |line| {
        // Eat start of line whitespace
        let mut line = line.trim_start();

        // If we're in a block comment eat stars, then whitespace again
        if is_block_comment {
            line = line.trim_start_matches('*').trim_start()
        }

        const PATTERNS: [[char; 2]; 11] = [
            ['r', 'R'],
            ['o', 'O'],
            ['m', 'M'],
            ['e', 'E'],
            ['-', '_'],
            ['i', 'I'],
            ['g', 'G'],
            ['n', 'N'],
            ['o', 'O'],
            ['r', 'R'],
            ['e', 'E'],
        ];

        // Checks for `/rome[-_]ignore/i` without a regex, or skip the line
        // entirely if it doesn't match
        for pattern in PATTERNS {
            line = line.strip_prefix(pattern)?;
        }

        let line = line.trim_start();
        Some(
            parse_suppression_line(line).map_err(|err| SuppressionDiagnostic {
                message: err.message,
                // Adjust the position of the diagnostic in the whole comment
                span: err.span + offset_from(base, line),
            }),
        )
    })
}

#[derive(Clone, Debug, PartialEq, Eq, Diagnostic)]
#[diagnostic(category = "suppressions/parse")]
pub struct SuppressionDiagnostic {
    #[message]
    #[description]
    message: SuppressionDiagnosticKind,
    #[location(span)]
    span: TextRange,
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum SuppressionDiagnosticKind {
    MissingColon,
    ParseCategory(String),
    MissingCategory,
    MissingParen,
}

impl std::fmt::Display for SuppressionDiagnosticKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SuppressionDiagnosticKind::MissingColon => write!(
                f,
                "unexpected token, expected one of ':', '(' or whitespace"
            ),
            SuppressionDiagnosticKind::ParseCategory(category) => {
                write!(f, "failed to parse category {category:?}")
            }
            SuppressionDiagnosticKind::MissingCategory => {
                write!(f, "unexpected token, expected one of ':' or whitespace")
            }
            SuppressionDiagnosticKind::MissingParen => write!(f, "unexpected token, expected ')'"),
        }
    }
}

impl biome_console::fmt::Display for SuppressionDiagnosticKind {
    fn fmt(&self, fmt: &mut biome_console::fmt::Formatter) -> std::io::Result<()> {
        match self {
            SuppressionDiagnosticKind::MissingColon => write!(
                fmt,
                "unexpected token, expected one of ':', '(' or whitespace"
            ),
            SuppressionDiagnosticKind::ParseCategory(category) => {
                write!(fmt, "failed to parse category {category:?}")
            }
            SuppressionDiagnosticKind::MissingCategory => {
                write!(fmt, "unexpected token, expected one of ':' or whitespace")
            }
            SuppressionDiagnosticKind::MissingParen => {
                write!(fmt, "unexpected token, expected ')'")
            }
        }
    }
}

/// Parse the `{ <category> { (<value>) }? }+: <reason>` section of a suppression line
fn parse_suppression_line(base: &str) -> Result<Suppression, SuppressionDiagnostic> {
    let mut line = base;
    let mut categories = Vec::new();

    loop {
        // Find either a colon opening parenthesis or space
        let separator = line
            .find(|c: char| c == ':' || c == '(' || c.is_whitespace())
            .ok_or_else(|| SuppressionDiagnostic {
                message: SuppressionDiagnosticKind::MissingColon,
                span: TextRange::at(offset_from(base, line), TextSize::of(line)),
            })?;

        let (category, rest) = line.split_at(separator);
        let category = category.trim_end();
        let category: Option<&'static Category> = if !category.is_empty() {
            let category = category.parse().map_err(|()| SuppressionDiagnostic {
                message: SuppressionDiagnosticKind::ParseCategory(category.into()),
                span: TextRange::at(offset_from(base, category), TextSize::of(category)),
            })?;
            Some(category)
        } else {
            None
        };

        // Skip over and match the separator
        let (separator, rest) = rest.split_at(1);

        match separator {
            // Colon token: stop parsing categories
            ":" => {
                if let Some(category) = category {
                    categories.push((category, None));
                }

                line = rest.trim_start();
                break;
            }
            // Paren token: parse a category + value
            "(" => {
                let category = category.ok_or_else(|| SuppressionDiagnostic {
                    message: SuppressionDiagnosticKind::MissingCategory,
                    span: TextRange::at(
                        offset_from(base, line),
                        offset_from(line, separator) + TextSize::of(separator),
                    ),
                })?;
                let paren = rest.find(')').ok_or_else(|| SuppressionDiagnostic {
                    message: SuppressionDiagnosticKind::MissingParen,
                    span: TextRange::at(offset_from(base, rest), TextSize::of(rest)),
                })?;

                let (value, rest) = rest.split_at(paren);
                let value = value.trim();

                categories.push((category, Some(value)));

                line = rest.strip_prefix(')').unwrap().trim_start();
            }
            // Whitespace: push a category without value
            _ => {
                if let Some(category) = category {
                    categories.push((category, None));
                }

                line = rest.trim_start();
            }
        }
    }

    let reason = line.trim_end();
    Ok(Suppression { categories, reason })
}

/// Returns the byte offset of `substr` within `base`
///
/// # Safety
///
/// `substr` must be a substring of `base`, or calling this method will result
/// in undefined behavior.
fn offset_from(base: &str, substr: &str) -> TextSize {
    let base_len = base.len();
    assert!(substr.len() <= base_len);

    let base = base.as_ptr();
    let substr = substr.as_ptr();
    let offset = unsafe { substr.offset_from(base) };

    // SAFETY: converting from `isize` to `usize` can only fail if `offset` is
    // negative, meaning `base` is either a substring of `substr` or the two
    // string slices are unrelated
    let offset = usize::try_from(offset).expect("usize underflow");
    assert!(offset <= base_len);

    // SAFETY: the conversion from `usize` to `TextSize` can fail if `offset`
    // is larger than 2^32
    TextSize::try_from(offset).expect("TextSize overflow")
}

#[cfg(test)]
mod tests {
    use biome_diagnostics::category;
    use biome_rowan::{TextRange, TextSize};

    use crate::{offset_from, SuppressionDiagnostic, SuppressionDiagnosticKind};

    use super::{parse_suppression_comment, Suppression};

    #[test]
    fn parse_simple_suppression() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore parse: explanation1").collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("parse"), None)],
                reason: "explanation1"
            })],
        );

        assert_eq!(
            parse_suppression_comment("/** rome-ignore parse: explanation2 */").collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("parse"), None)],
                reason: "explanation2"
            })],
        );

        assert_eq!(
            parse_suppression_comment(
                "/**
                  * rome-ignore parse: explanation3
                  */"
            )
            .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("parse"), None)],
                reason: "explanation3"
            })],
        );

        assert_eq!(
            parse_suppression_comment(
                "/**
                  * hello
                  * rome-ignore parse: explanation4
                  */"
            )
            .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("parse"), None)],
                reason: "explanation4"
            })],
        );
    }
    #[test]
    fn parse_unclosed_block_comment_suppressions() {
        assert_eq!(
            parse_suppression_comment("/* rome-ignore format: explanation").collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("format"), None)],
                reason: "explanation"
            })],
        );

        assert_eq!(
            parse_suppression_comment("/* rome-ignore format: explanation *").collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("format"), None)],
                reason: "explanation"
            })],
        );

        assert_eq!(
            parse_suppression_comment("/* rome-ignore format: explanation /").collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("format"), None)],
                reason: "explanation"
            })],
        );
    }

    #[test]
    fn parse_multiple_suppression() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore parse(foo) parse(dog): explanation")
                .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![
                    (category!("parse"), Some("foo")),
                    (category!("parse"), Some("dog"))
                ],
                reason: "explanation"
            })],
        );

        assert_eq!(
            parse_suppression_comment("/** rome-ignore parse(bar) parse(cat): explanation */")
                .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![
                    (category!("parse"), Some("bar")),
                    (category!("parse"), Some("cat"))
                ],
                reason: "explanation"
            })],
        );

        assert_eq!(
            parse_suppression_comment(
                "/**
                  * rome-ignore parse(yes) parse(frog): explanation
                  */"
            )
            .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![
                    (category!("parse"), Some("yes")),
                    (category!("parse"), Some("frog"))
                ],
                reason: "explanation"
            })],
        );

        assert_eq!(
            parse_suppression_comment(
                "/**
                  * hello
                  * rome-ignore parse(wow) parse(fish): explanation
                  */"
            )
            .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![
                    (category!("parse"), Some("wow")),
                    (category!("parse"), Some("fish"))
                ],
                reason: "explanation"
            })],
        );
    }

    #[test]
    fn parse_multiple_suppression_categories() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore format lint: explanation")
                .collect::<Vec<_>>(),
            vec![Ok(Suppression {
                categories: vec![(category!("format"), None), (category!("lint"), None)],
                reason: "explanation"
            })],
        );
    }

    #[test]
    fn check_offset_from() {
        const BASE: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";

        assert_eq!(offset_from(BASE, BASE), TextSize::from(0));

        let (_, substr) = BASE.split_at(55);
        assert_eq!(offset_from(BASE, substr), TextSize::from(55));

        let (_, substr) = BASE.split_at(BASE.len());
        assert_eq!(offset_from(BASE, substr), TextSize::of(BASE));
    }

    #[test]
    fn diagnostic_missing_colon() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore format explanation").collect::<Vec<_>>(),
            vec![Err(SuppressionDiagnostic {
                message: SuppressionDiagnosticKind::MissingColon,
                span: TextRange::new(TextSize::from(22), TextSize::from(33))
            })],
        );
    }

    #[test]
    fn diagnostic_missing_paren() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore format(:").collect::<Vec<_>>(),
            vec![Err(SuppressionDiagnostic {
                message: SuppressionDiagnosticKind::MissingParen,
                span: TextRange::new(TextSize::from(22), TextSize::from(23))
            })],
        );
    }

    #[test]
    fn diagnostic_missing_category() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore (value): explanation").collect::<Vec<_>>(),
            vec![Err(SuppressionDiagnostic {
                message: SuppressionDiagnosticKind::MissingCategory,
                span: TextRange::new(TextSize::from(15), TextSize::from(16))
            })],
        );
    }

    #[test]
    fn diagnostic_unknown_category() {
        assert_eq!(
            parse_suppression_comment("// rome-ignore unknown: explanation").collect::<Vec<_>>(),
            vec![Err(SuppressionDiagnostic {
                message: SuppressionDiagnosticKind::ParseCategory(String::from("unknown")),
                span: TextRange::new(TextSize::from(15), TextSize::from(22))
            })],
        );
    }
}