supa_mdx_lint/
context.rs

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
use std::array;

use anyhow::Result;
use bon::bon;
use log::debug;

use crate::{
    comments::{ConfigurationCommentCollection, LintDisables, LintTimeRuleConfigs},
    geometry::AdjustedOffset,
    parser::ParseResult,
    rope::Rope,
    rules::RuleFilter,
};

#[derive(Clone, Hash, PartialEq, Eq)]
pub(crate) struct ContextId([char; 12]);

impl ContextId {
    pub fn new() -> Self {
        Self(array::from_fn(|_| fastrand::alphanumeric()))
    }
}

impl std::fmt::Debug for ContextId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ContextId({})", String::from_iter(self.0))
    }
}

pub(crate) struct Context<'ctx> {
    /// Key for caching purposes, so individual rules can cache file-level
    /// calculations.
    pub(crate) key: ContextId,
    pub(crate) parse_result: &'ctx ParseResult,
    pub(crate) check_only_rules: RuleFilter<'ctx>,
    pub(crate) disables: LintDisables<'ctx>,
    pub(crate) lint_time_rule_configs: LintTimeRuleConfigs<'ctx>,
}

#[bon]
impl<'ctx> Context<'ctx> {
    #[builder]
    pub(crate) fn new(
        parse_result: &'ctx ParseResult,
        check_only_rules: Option<&'ctx [&'ctx str]>,
    ) -> Result<Self> {
        let (lint_time_rule_configs, disables) =
            ConfigurationCommentCollection::from_parse_result(parse_result)
                .into_parts()
                .unwrap();
        debug!("Lint time rule configs: {:?}", lint_time_rule_configs);
        debug!("Disables: {:?}", disables);

        Ok(Self {
            key: ContextId::new(),
            parse_result,
            check_only_rules,
            disables,
            lint_time_rule_configs,
        })
    }

    pub(crate) fn rope(&self) -> &Rope {
        self.parse_result.rope()
    }

    pub fn content_start_offset(&self) -> AdjustedOffset {
        self.parse_result.content_start_offset()
    }
}