foundry_common/comments/
inline_config.rs

1use solar::{
2    interface::{BytePos, RelativeBytePos, SourceMap, Span},
3    parse::ast::{self, Visit},
4};
5use std::{
6    collections::{HashMap, hash_map::Entry},
7    hash::Hash,
8    ops::ControlFlow,
9};
10
11/// A disabled formatting range.
12#[derive(Debug, Clone, Copy)]
13struct DisabledRange<T = BytePos> {
14    /// Start position, inclusive.
15    lo: T,
16    /// End position, inclusive.
17    hi: T,
18}
19
20impl DisabledRange<BytePos> {
21    fn includes(&self, span: Span) -> bool {
22        span.lo() >= self.lo && span.hi() <= self.hi
23    }
24}
25
26/// An inline config item
27#[derive(Clone, Debug)]
28pub enum InlineConfigItem<I> {
29    /// Disables the next code (AST) item regardless of newlines
30    DisableNextItem(I),
31    /// Disables formatting on the current line
32    DisableLine(I),
33    /// Disables formatting between the next newline and the newline after
34    DisableNextLine(I),
35    /// Disables formatting for any code that follows this and before the next "disable-end"
36    DisableStart(I),
37    /// Disables formatting for any code that precedes this and after the previous "disable-start"
38    DisableEnd(I),
39}
40
41impl InlineConfigItem<Vec<String>> {
42    /// Parse an inline config item from a string. Validates IDs against available IDs.
43    pub fn parse(s: &str, available_ids: &[&str]) -> Result<Self, InvalidInlineConfigItem> {
44        let (disable, relevant) = s.split_once('(').unwrap_or((s, ""));
45        let ids = if relevant.is_empty() || relevant == "all)" {
46            vec!["all".to_string()]
47        } else {
48            match relevant.split_once(')') {
49                Some((id_str, _)) => id_str.split(",").map(|s| s.trim().to_string()).collect(),
50                None => return Err(InvalidInlineConfigItem::Syntax(s.into())),
51            }
52        };
53
54        // Validate IDs
55        let mut invalid_ids = Vec::new();
56        'ids: for id in &ids {
57            if id == "all" {
58                continue;
59            }
60            for available_id in available_ids {
61                if *available_id == id {
62                    continue 'ids;
63                }
64            }
65            invalid_ids.push(id.to_owned());
66        }
67
68        if !invalid_ids.is_empty() {
69            return Err(InvalidInlineConfigItem::Ids(invalid_ids));
70        }
71
72        let res = match disable {
73            "disable-next-item" => Self::DisableNextItem(ids),
74            "disable-line" => Self::DisableLine(ids),
75            "disable-next-line" => Self::DisableNextLine(ids),
76            "disable-start" => Self::DisableStart(ids),
77            "disable-end" => Self::DisableEnd(ids),
78            s => return Err(InvalidInlineConfigItem::Syntax(s.into())),
79        };
80
81        Ok(res)
82    }
83}
84
85impl std::str::FromStr for InlineConfigItem<()> {
86    type Err = InvalidInlineConfigItem;
87    fn from_str(s: &str) -> Result<Self, Self::Err> {
88        Ok(match s {
89            "disable-next-item" => Self::DisableNextItem(()),
90            "disable-line" => Self::DisableLine(()),
91            "disable-next-line" => Self::DisableNextLine(()),
92            "disable-start" => Self::DisableStart(()),
93            "disable-end" => Self::DisableEnd(()),
94            s => return Err(InvalidInlineConfigItem::Syntax(s.into())),
95        })
96    }
97}
98
99#[derive(Debug)]
100pub enum InvalidInlineConfigItem {
101    Syntax(String),
102    Ids(Vec<String>),
103}
104
105impl std::fmt::Display for InvalidInlineConfigItem {
106    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107        match self {
108            Self::Syntax(s) => write!(f, "invalid inline config item: {s}"),
109            Self::Ids(ids) => {
110                write!(f, "unknown id: '{}'", ids.join("', '"))
111            }
112        }
113    }
114}
115
116/// A trait for `InlineConfigItem` types that can be iterated over to produce keys for storage.
117pub trait ItemIdIterator {
118    type Item: Eq + Hash + Clone;
119    fn into_iter(self) -> impl IntoIterator<Item = Self::Item>;
120}
121
122impl ItemIdIterator for () {
123    type Item = ();
124    fn into_iter(self) -> impl IntoIterator<Item = Self::Item> {
125        std::iter::once(())
126    }
127}
128
129impl ItemIdIterator for Vec<String> {
130    type Item = String;
131    fn into_iter(self) -> impl IntoIterator<Item = Self::Item> {
132        self
133    }
134}
135
136#[derive(Debug, Default)]
137pub struct InlineConfig<I: ItemIdIterator> {
138    disabled_ranges: HashMap<I::Item, Vec<DisabledRange>>,
139}
140
141impl<I: ItemIdIterator> InlineConfig<I> {
142    /// Build a new inline config with an iterator of inline config items and their locations in a
143    /// source file.
144    ///
145    /// # Panics
146    ///
147    /// Panics if `items` is not sorted in ascending order of [`Span`]s.
148    pub fn from_ast<'ast>(
149        items: impl IntoIterator<Item = (Span, InlineConfigItem<I>)>,
150        ast: &'ast ast::SourceUnit<'ast>,
151        source_map: &SourceMap,
152    ) -> Self {
153        Self::build(items, source_map, |offset| NextItemFinder::new(offset).find(ast))
154    }
155
156    fn build(
157        items: impl IntoIterator<Item = (Span, InlineConfigItem<I>)>,
158        source_map: &SourceMap,
159        mut find_next_item: impl FnMut(BytePos) -> Option<Span>,
160    ) -> Self {
161        let mut cfg = Self::new();
162        let mut disabled_blocks = HashMap::new();
163
164        let mut prev_sp = Span::DUMMY;
165        for (sp, item) in items {
166            if cfg!(debug_assertions) {
167                assert!(sp >= prev_sp, "InlineConfig::new: unsorted items: {sp:?} < {prev_sp:?}");
168                prev_sp = sp;
169            }
170
171            cfg.disable_item(sp, item, source_map, &mut disabled_blocks, &mut find_next_item);
172        }
173
174        for (id, (_, lo, hi)) in disabled_blocks {
175            cfg.disable(id, DisabledRange { lo, hi });
176        }
177
178        cfg
179    }
180
181    fn new() -> Self {
182        Self { disabled_ranges: HashMap::new() }
183    }
184
185    fn disable_many(&mut self, ids: I, range: DisabledRange) {
186        for id in ids.into_iter() {
187            self.disable(id, range);
188        }
189    }
190
191    fn disable(&mut self, id: I::Item, range: DisabledRange) {
192        self.disabled_ranges.entry(id).or_default().push(range);
193    }
194
195    fn disable_item(
196        &mut self,
197        span: Span,
198        item: InlineConfigItem<I>,
199        source_map: &SourceMap,
200        disabled_blocks: &mut HashMap<I::Item, (usize, BytePos, BytePos)>,
201        find_next_item: &mut dyn FnMut(BytePos) -> Option<Span>,
202    ) {
203        let result = source_map.span_to_source(span).unwrap();
204        let file = result.file;
205        let comment_range = result.data;
206        let src = file.src.as_str();
207
208        match item {
209            InlineConfigItem::DisableNextItem(ids) => {
210                if let Some(next_item) = find_next_item(span.hi()) {
211                    self.disable_many(
212                        ids,
213                        DisabledRange { lo: next_item.lo(), hi: next_item.hi() },
214                    );
215                }
216            }
217            InlineConfigItem::DisableLine(ids) => {
218                let start = src[..comment_range.start].rfind('\n').map_or(0, |i| i);
219                let end = src[comment_range.end..]
220                    .find('\n')
221                    .map_or(src.len(), |i| comment_range.end + i);
222                self.disable_many(
223                    ids,
224                    DisabledRange {
225                        lo: file.absolute_position(RelativeBytePos::from_usize(start)),
226                        hi: file.absolute_position(RelativeBytePos::from_usize(end)),
227                    },
228                );
229            }
230            InlineConfigItem::DisableNextLine(ids) => {
231                if let Some(offset) = src[comment_range.end..].find('\n') {
232                    let next_line = comment_range.end + offset + 1;
233                    if next_line < src.len() {
234                        let end = src[next_line..].find('\n').map_or(src.len(), |i| next_line + i);
235                        self.disable_many(
236                            ids,
237                            DisabledRange {
238                                lo: file.absolute_position(RelativeBytePos::from_usize(
239                                    comment_range.start,
240                                )),
241                                hi: file.absolute_position(RelativeBytePos::from_usize(end)),
242                            },
243                        );
244                    }
245                }
246            }
247
248            InlineConfigItem::DisableStart(ids) => {
249                for id in ids.into_iter() {
250                    disabled_blocks.entry(id).and_modify(|(depth, _, _)| *depth += 1).or_insert((
251                        1,
252                        span.lo(),
253                        // Use file end as fallback for unclosed blocks
254                        file.absolute_position(RelativeBytePos::from_usize(src.len())),
255                    ));
256                }
257            }
258            InlineConfigItem::DisableEnd(ids) => {
259                for id in ids.into_iter() {
260                    if let Entry::Occupied(mut entry) = disabled_blocks.entry(id) {
261                        let (depth, lo, _) = entry.get_mut();
262                        *depth = depth.saturating_sub(1);
263
264                        if *depth == 0 {
265                            let lo = *lo;
266                            let (id, _) = entry.remove_entry();
267
268                            self.disable(id, DisabledRange { lo, hi: span.hi() });
269                        }
270                    }
271                }
272            }
273        }
274    }
275}
276
277impl InlineConfig<()> {
278    /// Checks if a span is disabled (only applicable when inline config doesn't require an id).
279    pub fn is_disabled(&self, span: Span) -> bool {
280        if let Some(ranges) = self.disabled_ranges.get(&()) {
281            return ranges.iter().any(|range| range.includes(span));
282        }
283        false
284    }
285}
286
287impl<I: ItemIdIterator> InlineConfig<I>
288where
289    I::Item: std::borrow::Borrow<str>,
290{
291    /// Checks if a span is disabled for a specific id. Also checks against "all", which disables
292    /// all rules.
293    pub fn is_id_disabled(&self, span: Span, id: &str) -> bool {
294        self.is_id_disabled_inner(span, id)
295            || (id != "all" && self.is_id_disabled_inner(span, "all"))
296    }
297
298    fn is_id_disabled_inner(&self, span: Span, id: &str) -> bool {
299        if let Some(ranges) = self.disabled_ranges.get(id)
300            && ranges.iter().any(|range| range.includes(span))
301        {
302            return true;
303        }
304
305        false
306    }
307}
308
309macro_rules! find_next_item {
310    ($self:expr, $x:expr, $span:expr, $walk:ident) => {{
311        let span = $span;
312        // If the item is *entirely* before the offset, skip traversing it.
313        if span.hi() < $self.offset {
314            return ControlFlow::Continue(());
315        }
316        // Check if this item starts after the offset.
317        if span.lo() > $self.offset {
318            return ControlFlow::Break(span);
319        }
320        // Otherwise, continue traversing inside this item.
321        $self.$walk($x)
322    }};
323}
324
325/// An AST visitor that finds the first `Item` that starts after a given offset.
326#[derive(Debug)]
327struct NextItemFinder {
328    /// The offset to search after.
329    offset: BytePos,
330}
331
332impl NextItemFinder {
333    fn new(offset: BytePos) -> Self {
334        Self { offset }
335    }
336
337    /// Finds the next AST item or statement which a span that begins after the `offset`.
338    fn find<'ast>(&mut self, ast: &'ast ast::SourceUnit<'ast>) -> Option<Span> {
339        match self.visit_source_unit(ast) {
340            ControlFlow::Break(span) => Some(span),
341            ControlFlow::Continue(()) => None,
342        }
343    }
344}
345
346impl<'ast> ast::Visit<'ast> for NextItemFinder {
347    type BreakValue = Span;
348
349    fn visit_item(&mut self, item: &'ast ast::Item<'ast>) -> ControlFlow<Self::BreakValue> {
350        find_next_item!(self, item, item.span, walk_item)
351    }
352
353    fn visit_stmt(&mut self, stmt: &'ast ast::Stmt<'ast>) -> ControlFlow<Self::BreakValue> {
354        find_next_item!(self, stmt, stmt.span, walk_stmt)
355    }
356
357    fn visit_yul_stmt(
358        &mut self,
359        stmt: &'ast ast::yul::Stmt<'ast>,
360    ) -> ControlFlow<Self::BreakValue> {
361        find_next_item!(self, stmt, stmt.span, walk_yul_stmt)
362    }
363}
364
365#[cfg(test)]
366mod tests {
367    use super::*;
368
369    impl DisabledRange<usize> {
370        fn to_byte_pos(self) -> DisabledRange<BytePos> {
371            DisabledRange::<BytePos> {
372                lo: BytePos::from_usize(self.lo),
373                hi: BytePos::from_usize(self.hi),
374            }
375        }
376
377        fn includes(&self, range: std::ops::Range<usize>) -> bool {
378            self.to_byte_pos().includes(Span::new(
379                BytePos::from_usize(range.start),
380                BytePos::from_usize(range.end),
381            ))
382        }
383    }
384
385    #[test]
386    fn test_disabled_range_includes() {
387        let strict = DisabledRange { lo: 10, hi: 20 };
388        assert!(strict.includes(10..20));
389        assert!(strict.includes(12..18));
390        assert!(!strict.includes(5..15)); // Partial overlap fails
391    }
392
393    #[test]
394    fn test_inline_config_item_from_str() {
395        assert!(matches!(
396            "disable-next-item".parse::<InlineConfigItem<()>>().unwrap(),
397            InlineConfigItem::DisableNextItem(())
398        ));
399        assert!(matches!(
400            "disable-line".parse::<InlineConfigItem<()>>().unwrap(),
401            InlineConfigItem::DisableLine(())
402        ));
403        assert!(matches!(
404            "disable-start".parse::<InlineConfigItem<()>>().unwrap(),
405            InlineConfigItem::DisableStart(())
406        ));
407        assert!(matches!(
408            "disable-end".parse::<InlineConfigItem<()>>().unwrap(),
409            InlineConfigItem::DisableEnd(())
410        ));
411        assert!("invalid".parse::<InlineConfigItem<()>>().is_err());
412    }
413
414    #[test]
415    fn test_inline_config_item_parse_with_lints() {
416        let lint_ids = vec!["lint1", "lint2"];
417
418        // No lints = "all"
419        match InlineConfigItem::parse("disable-line", &lint_ids).unwrap() {
420            InlineConfigItem::DisableLine(lints) => assert_eq!(lints, vec!["all"]),
421            _ => panic!("Wrong type"),
422        }
423
424        // Valid single lint
425        match InlineConfigItem::parse("disable-start(lint1)", &lint_ids).unwrap() {
426            InlineConfigItem::DisableStart(lints) => assert_eq!(lints, vec!["lint1"]),
427            _ => panic!("Wrong type"),
428        }
429
430        // Multiple lints with spaces
431        match InlineConfigItem::parse("disable-end(lint1, lint2)", &lint_ids).unwrap() {
432            InlineConfigItem::DisableEnd(lints) => assert_eq!(lints, vec!["lint1", "lint2"]),
433            _ => panic!("Wrong type"),
434        }
435
436        // Invalid lint ID
437        assert!(matches!(
438            InlineConfigItem::parse("disable-line(unknown)", &lint_ids),
439            Err(InvalidInlineConfigItem::Ids(_))
440        ));
441
442        // Malformed syntax
443        assert!(matches!(
444            InlineConfigItem::parse("disable-line(lint1", &lint_ids),
445            Err(InvalidInlineConfigItem::Syntax(_))
446        ));
447    }
448}