lintspec 0.12.2

A blazingly fast linter for NatSpec comments in Solidity code
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! `NatSpec` Comment Parser
use std::ops::Range;

use derive_more::IsVariant;
use winnow::{
    LocatingSlice,
    ascii::{line_ending, space0, space1, till_line_ending},
    combinator::{alt, cut_err, delimited, not, opt, repeat, separated},
    error::{StrContext, StrContextValue},
    seq,
    token::{rest, take_till, take_until},
};
pub use winnow::{ModalResult, Parser};

use crate::{
    definitions::Identifier,
    textindex::{TextIndex, TextRange},
};

/// A collection of `NatSpec` items corresponding to a source item (function, struct, etc.)
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NatSpec {
    pub items: Vec<NatSpecItem>,
}

impl NatSpec {
    /// Append the items of another [`NatSpec`] to this one's items
    pub fn append(&mut self, other: &mut Self) {
        self.items.append(&mut other.items);
    }

    /// Populate the return `NatSpec` items with their identifiers (which could be named `None` for unnamed returns)
    #[must_use]
    pub fn populate_returns(mut self, returns: &[Identifier]) -> Self {
        for i in &mut self.items {
            i.populate_return(returns);
        }
        self
    }

    /// Count the number of `NatSpec` items corresponding to a given param identifier
    #[must_use]
    pub fn count_param(&self, ident: &Identifier) -> usize {
        let Some(ident_name) = &ident.name else {
            return 0;
        };
        self.items
            .iter()
            .filter(|n| match &n.kind {
                NatSpecKind::Param { name } => name == ident_name,
                _ => false,
            })
            .count()
    }

    /// Count the number of `NatSpec` items corresponding to a given return identifier
    #[must_use]
    pub fn count_return(&self, ident: &Identifier) -> usize {
        let Some(ident_name) = &ident.name else {
            return 0;
        };
        self.items
            .iter()
            .filter(|n| match &n.kind {
                NatSpecKind::Return { name: Some(name) } => name == ident_name,
                _ => false,
            })
            .count()
    }

    /// Count the number of `NatSpec` items corresponding to an unnamed return
    #[must_use]
    pub fn count_unnamed_returns(&self) -> usize {
        self.items
            .iter()
            .filter(|n| matches!(&n.kind, NatSpecKind::Return { name: None }))
            .count()
    }

    /// Count all the return `NatSpec` entries for this source item
    #[must_use]
    pub fn count_all_returns(&self) -> usize {
        self.items.iter().filter(|n| n.kind.is_return()).count()
    }

    #[must_use]
    pub fn has_param(&self) -> bool {
        self.items.iter().any(|n| n.kind.is_param())
    }

    #[must_use]
    pub fn has_return(&self) -> bool {
        self.items.iter().any(|n| n.kind.is_return())
    }

    #[must_use]
    pub fn has_notice(&self) -> bool {
        self.items.iter().any(|n| n.kind.is_notice())
    }

    #[must_use]
    pub fn has_dev(&self) -> bool {
        self.items.iter().any(|n| n.kind.is_dev())
    }

    #[must_use]
    pub fn has_title(&self) -> bool {
        self.items.iter().any(|n| n.kind.is_title())
    }

    #[must_use]
    pub fn has_author(&self) -> bool {
        self.items.iter().any(|n| n.kind.is_author())
    }
}

/// A single `NatSpec` item
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[non_exhaustive]
#[builder(on(String, into))]
pub struct NatSpecItem {
    /// The kind of `NatSpec` (notice, dev, param, etc.)
    pub kind: NatSpecKind,

    /// The comment associated with this `NatSpec` item
    pub comment: String,

    /// The span of this item, relative to the start offset of the string passed to [`parse_comment`]
    pub span: TextRange,
}

impl NatSpecItem {
    /// Populate a return `NatSpec` item with its name if available
    ///
    /// For non-return items, this function has no effect.
    pub fn populate_return(&mut self, returns: &[Identifier]) {
        if !matches!(self.kind, NatSpecKind::Return { name: _ }) {
            return;
        }
        let name = self
            .comment
            .split_whitespace()
            .next()
            .filter(|first_word| {
                returns.iter().any(|r| match &r.name {
                    Some(name) => first_word == name,
                    None => false,
                })
            })
            .map(ToOwned::to_owned);
        if let Some(name) = &name
            && let Some(comment) = self.comment.strip_prefix(name)
        {
            self.comment = comment.trim_start().to_string();
        }
        self.kind = NatSpecKind::Return { name }
    }

    /// Check if the item is empty (type is `@notice` - the default - and comment is empty)
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.kind == NatSpecKind::Notice && self.comment.is_empty()
    }
}

/// The kind of a `NatSpec` item
#[derive(Debug, Clone, PartialEq, Eq, IsVariant)]
pub enum NatSpecKind {
    Title,
    Author,
    Notice,
    Dev,
    Param {
        name: String,
    },
    /// For return items, [`parse_comment`] does not include the return name automatically. The [`NatSpecItem::populate_return`] function must be called to retrieve the name, if any.
    Return {
        name: Option<String>,
    },
    Inheritdoc {
        parent: String,
    },
    Custom {
        tag: String,
    },
}

impl From<NatSpecItem> for NatSpec {
    fn from(value: NatSpecItem) -> Self {
        Self { items: vec![value] }
    }
}

/// Parse a Solidity doc-comment to extract the `NatSpec` information
pub fn parse_comment(input: &mut &str) -> ModalResult<NatSpec> {
    // consume input to avoid errors when used with `Parser::parse`
    let input = rest::<&str, _>.parse_next(input)?;
    // wrap in a `LocatingSlice` to track offsets/spans
    let (mut natspec, spans) = alt((single_line_comment, multiline_comment, empty_multiline))
        .parse_next(&mut LocatingSlice::new(input))?;
    if natspec.items.is_empty() {
        return Ok(natspec);
    }

    // convert byte offsets to `TextIndex`
    let mut current_index = TextIndex::ZERO;
    let mut char_iter = input.chars().peekable();
    for (natspec_item, byte_span) in natspec.items.iter_mut().zip(spans.iter()) {
        if current_index.utf8 == byte_span.start {
            natspec_item.span.start = current_index;
        } else {
            // find start offset
            while let Some(c) = char_iter.next() {
                current_index.advance(c, char_iter.peek());
                if current_index.utf8 == byte_span.start {
                    natspec_item.span.start = current_index;
                    break;
                }
            }
        }
        // find end offset
        while let Some(c) = char_iter.next() {
            current_index.advance(c, char_iter.peek());
            if current_index.utf8 == byte_span.end {
                natspec_item.span.end = current_index;
                break;
            }
        }
    }
    Ok(natspec)
}

/// Parse an identifier (contiguous non-whitespace characters)
fn ident(input: &mut LocatingSlice<&str>) -> ModalResult<String> {
    take_till(1.., |c: char| c.is_whitespace())
        .map(|ident: &str| ident.to_owned())
        .parse_next(input)
}

/// Parse a [`NatSpecKind`] (tag followed by an optional identifier)
///
/// For `@return`, the identifier, if present, is not included in the `NatSpecItem` for now. A post-processing
/// step ([`NatSpecItem::populate_return`]) is needed to extract the name.
fn natspec_kind(input: &mut LocatingSlice<&str>) -> ModalResult<NatSpecKind> {
    alt((
        "@title".map(|_| NatSpecKind::Title),
        "@author".map(|_| NatSpecKind::Author),
        "@notice".map(|_| NatSpecKind::Notice),
        "@dev".map(|_| NatSpecKind::Dev),
        seq! {NatSpecKind::Param {
            _: "@param",
            _: space1,
            name: ident
        }},
        "@return".map(|_| NatSpecKind::Return { name: None }), // we will process the name later since it's optional
        seq! {NatSpecKind::Inheritdoc {
            _: "@inheritdoc",
            _: space1,
            parent: ident
        }},
        seq! {NatSpecKind::Custom {
            _: "@custom:",
            tag: ident
        }},
    ))
    .parse_next(input)
}

/// Parse the end of a multiline comment (one or more `*` followed by `/`)
#[expect(clippy::unnecessary_wraps)]
fn end_of_comment(input: &mut LocatingSlice<&str>) -> ModalResult<()> {
    let _ = (repeat::<_, _, (), (), _>(1.., '*'), '/').parse_next(input);
    Ok(())
}

/// Parse a single `NatSpec` item (line) in a multiline comment
fn one_multiline_natspec(
    input: &mut LocatingSlice<&str>,
) -> ModalResult<(NatSpecItem, Range<usize>)> {
    let _ = space0.parse_next(input)?;
    let () = repeat::<_, _, (), _, _>(0.., '*').parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let (kind, kind_span) = opt(natspec_kind)
        .map(|v| v.unwrap_or(NatSpecKind::Notice))
        .with_span()
        .parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let (comment, comment_span) = take_until(0.., ("\r", "\n", "*/"))
        .parse_to()
        .with_span()
        .parse_next(input)?;
    Ok((
        NatSpecItem {
            kind,
            comment,
            span: TextRange::default(),
        },
        kind_span.start..comment_span.end,
    ))
}

/// Parse a multiline `NatSpec` comment
fn multiline_comment(input: &mut LocatingSlice<&str>) -> ModalResult<(NatSpec, Vec<Range<usize>>)> {
    delimited(
        (
            (
                "/**",
                // three stars is not a valid doc-comment
                // <https://github.com/ethereum/solidity/issues/9139>
                cut_err(not('*'))
                    .context(StrContext::Label("delimiter"))
                    .context(StrContext::Expected(StrContextValue::Description("/**"))),
            ),
            space0,
            opt(line_ending),
        ),
        separated(0.., one_multiline_natspec, line_ending),
        (opt(line_ending), space0, end_of_comment),
    )
    .map(|items: Vec<(NatSpecItem, Range<usize>)>| {
        let (items, spans) = items.into_iter().unzip();
        (NatSpec { items }, spans)
    })
    .parse_next(input)
}

/// Parse an empty multiline comment (without any text in the body)
fn empty_multiline(input: &mut LocatingSlice<&str>) -> ModalResult<(NatSpec, Vec<Range<usize>>)> {
    let _ = ("/**", space1, repeat::<_, _, (), _, _>(1.., '*'), '/').parse_next(input)?;
    Ok((NatSpec::default(), Vec::new()))
}

/// Parse a single line comment `NatSpec` item
fn single_line_natspec(
    input: &mut LocatingSlice<&str>,
) -> ModalResult<(NatSpecItem, Range<usize>)> {
    let _ = space0.parse_next(input)?;
    let (kind, kind_span) = opt(natspec_kind)
        .map(|v| v.unwrap_or(NatSpecKind::Notice))
        .with_span()
        .parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let (comment, comment_span) = till_line_ending.parse_to().with_span().parse_next(input)?;
    Ok((
        NatSpecItem {
            kind,
            comment,
            span: TextRange::default(),
        },
        kind_span.start..comment_span.end,
    ))
}

/// Parse a single line `NatSpec` comment
fn single_line_comment(
    input: &mut LocatingSlice<&str>,
) -> ModalResult<(NatSpec, Vec<Range<usize>>)> {
    let (item, range) = delimited(
        (
            "///",
            // four slashes is not a valid doc-comment
            // <https://github.com/ethereum/solidity/issues/9139>
            cut_err(not('/'))
                .context(StrContext::Label("delimiter"))
                .context(StrContext::Expected(StrContextValue::Description("///"))),
        ),
        single_line_natspec,
        opt(line_ending),
    )
    .parse_next(input)?;
    if item.is_empty() {
        return Ok((NatSpec::default(), Vec::new()));
    }
    Ok((item.into(), vec![range]))
}

#[cfg(test)]
mod tests {
    use similar_asserts::assert_eq;
    use winnow::error::ParseError;

    use super::*;

    #[test]
    fn test_kind() {
        let cases = [
            ("@title", NatSpecKind::Title),
            ("@author", NatSpecKind::Author),
            ("@notice", NatSpecKind::Notice),
            ("@dev", NatSpecKind::Dev),
            (
                "@param  foo",
                NatSpecKind::Param {
                    name: "foo".to_string(),
                },
            ),
            ("@return", NatSpecKind::Return { name: None }),
            (
                "@inheritdoc  ISomething",
                NatSpecKind::Inheritdoc {
                    parent: "ISomething".to_string(),
                },
            ),
            (
                "@custom:foo",
                NatSpecKind::Custom {
                    tag: "foo".to_string(),
                },
            ),
        ];
        for case in cases {
            let res = natspec_kind.parse(LocatingSlice::new(case.0));
            assert!(res.is_ok(), "{res:?}");
            let res = res.unwrap();
            assert_eq!(res, case.1);
        }
    }

    #[test]
    fn test_one_multiline_item() {
        let cases = [
            ("@dev Hello world\n", NatSpecKind::Dev, "Hello world"),
            ("@title The Title\n", NatSpecKind::Title, "The Title"),
            (
                "        * @author McGyver <hi@buildanything.com>\n",
                NatSpecKind::Author,
                "McGyver <hi@buildanything.com>",
            ),
            (
                " @param foo The bar\r\n",
                NatSpecKind::Param {
                    name: "foo".to_string(),
                },
                "The bar",
            ),
            (
                " @return something The return value\n",
                NatSpecKind::Return { name: None },
                "something The return value",
            ),
            (
                "\t* @custom:foo bar\n",
                NatSpecKind::Custom {
                    tag: "foo".to_string(),
                },
                "bar",
            ),
            ("  lorem ipsum\n", NatSpecKind::Notice, "lorem ipsum"),
            ("lorem ipsum\r\n", NatSpecKind::Notice, "lorem ipsum"),
            ("\t*  foobar\n", NatSpecKind::Notice, "foobar"),
            ("    * foobar\n", NatSpecKind::Notice, "foobar"),
        ];
        for case in cases {
            let res = (one_multiline_natspec, line_ending).parse(LocatingSlice::new(case.0));
            assert!(res.is_ok(), "{res:?}");
            let ((res, _), _) = res.unwrap();
            assert_eq!(
                res,
                NatSpecItem {
                    kind: case.1,
                    comment: case.2.to_string(),
                    span: TextRange::default()
                }
            );
        }
    }

    #[test]
    fn test_single_line() {
        let cases = [
            ("/// Foo bar", NatSpecKind::Notice, "Foo bar"),
            ("///  Baz", NatSpecKind::Notice, "Baz"),
            (
                "/// @notice  Hello world",
                NatSpecKind::Notice,
                "Hello world",
            ),
            (
                "/// @param foo This is bar\n",
                NatSpecKind::Param {
                    name: "foo".to_string(),
                },
                "This is bar",
            ),
            (
                "/// @return The return value\r\n",
                NatSpecKind::Return { name: None },
                "The return value",
            ),
            (
                "/// @custom:foo  This is bar\n",
                NatSpecKind::Custom {
                    tag: "foo".to_string(),
                },
                "This is bar",
            ),
        ];
        for case in cases {
            let res = single_line_comment.parse(LocatingSlice::new(case.0));
            assert!(res.is_ok(), "{res:?}");
            let (res, _) = res.unwrap();
            assert_eq!(
                res,
                NatSpecItem {
                    kind: case.1,
                    comment: case.2.to_string(),
                    span: TextRange::default()
                }
                .into()
            );
        }
    }

    #[test]
    fn test_single_line_empty() {
        let res = single_line_comment.parse(LocatingSlice::new("///\n"));
        assert!(res.is_ok(), "{res:?}");
        let (res, _) = res.unwrap();
        assert_eq!(res, NatSpec::default());
    }

    #[test]
    fn test_single_line_weird() {
        let res = single_line_comment.parse(LocatingSlice::new("//// Hello\n"));
        assert!(matches!(res, Err(ParseError { .. })));
    }

    #[test]
    fn test_multiline() {
        let comment = "/**
     * @notice Some notice text.
     */";
        let res = multiline_comment.parse(LocatingSlice::new(comment));
        assert!(res.is_ok(), "{res:?}");
        let (res, _) = res.unwrap();
        assert_eq!(
            res,
            NatSpec {
                items: vec![NatSpecItem {
                    kind: NatSpecKind::Notice,
                    comment: "Some notice text.".to_string(),
                    span: TextRange::default()
                }]
            }
        );
    }

    #[test]
    fn test_multiline2() {
        let comment = "/**
     * @notice Some notice text.
     * @custom:something
     */";
        let res = multiline_comment.parse(LocatingSlice::new(comment));
        assert!(res.is_ok(), "{res:?}");
        let (res, _) = res.unwrap();
        assert_eq!(
            res,
            NatSpec {
                items: vec![
                    NatSpecItem {
                        kind: NatSpecKind::Notice,
                        comment: "Some notice text.".to_string(),
                        span: TextRange::default()
                    },
                    NatSpecItem {
                        kind: NatSpecKind::Custom {
                            tag: "something".to_string()
                        },
                        comment: String::new(),
                        span: TextRange::default()
                    }
                ]
            }
        );
    }

    #[test]
    fn test_multiline3() {
        let comment = "/** @notice Some notice text.
Another notice
        * @param test
     \t** @custom:something */";
        let res = multiline_comment.parse(LocatingSlice::new(comment));
        assert!(res.is_ok(), "{res:?}");
        let (res, _) = res.unwrap();
        assert_eq!(
            res,
            NatSpec {
                items: vec![
                    NatSpecItem {
                        kind: NatSpecKind::Notice,
                        comment: "Some notice text.".to_string(),
                        span: TextRange::default()
                    },
                    NatSpecItem {
                        kind: NatSpecKind::Notice,
                        comment: "Another notice".to_string(),
                        span: TextRange::default()
                    },
                    NatSpecItem {
                        kind: NatSpecKind::Param {
                            name: "test".to_string()
                        },
                        comment: String::new(),
                        span: TextRange::default()
                    },
                    NatSpecItem {
                        kind: NatSpecKind::Custom {
                            tag: "something".to_string()
                        },
                        comment: String::new(),
                        span: TextRange::default()
                    }
                ]
            }
        );
    }

    #[test]
    fn test_multiline_empty() {
        let comment = "/**
        */";
        let res = parse_comment.parse(comment);

        assert!(res.is_ok(), "{res:?}");
        let res = res.unwrap();
        assert_eq!(res, NatSpec::default());

        let comment = "/** */";
        let res = parse_comment.parse(comment);

        assert!(res.is_ok(), "{res:?}");
        let res = res.unwrap();
        assert_eq!(res, NatSpec::default());
    }

    #[test]
    fn test_multiline_weird() {
        let comment = "/**** @notice Some text
    ** */";
        let res = parse_comment.parse(comment);
        assert!(matches!(res, Err(ParseError { .. })));
    }
}