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
//! Tool configuration parsing and validation
use std::path::PathBuf;

use derive_more::IsVariant;
use figment::{
    Figment, Metadata, Profile, Provider,
    providers::{Env, Format as _, Toml},
    value::{Dict, Map},
};
use serde::{Deserialize, Serialize};

/// The requirement for a specific tag in the natspec comment
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, IsVariant)]
#[serde(rename_all = "lowercase")]
pub enum Req {
    /// The tag is ignored, can be present or not
    #[default]
    Ignored,

    /// The tag is required, must be present
    Required,

    /// The tag is forbidden, must not be present
    Forbidden,
}

impl Req {
    /// Helper function to check if the tag is required or ignored (not forbidden)
    #[must_use]
    pub fn is_required_or_ignored(&self) -> bool {
        match self {
            Req::Required | Req::Ignored => true,
            Req::Forbidden => false,
        }
    }

    /// Helper function to check if the tag is forbidden or ignored (not required)
    #[must_use]
    pub fn is_forbidden_or_ignored(&self) -> bool {
        match self {
            Req::Forbidden | Req::Ignored => true,
            Req::Required => false,
        }
    }
}

/// Validation rules for a function natspec comment
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct FunctionRules {
    /// Requirement for the `@notice` tag
    #[builder(default = Req::Required)]
    pub notice: Req,

    /// Requirement for the `@dev` tag
    #[builder(default)]
    pub dev: Req,

    /// Requirement for the `@param` tags
    #[builder(default = Req::Required)]
    pub param: Req,

    /// Requirement for the `@return` tags
    #[serde(rename = "return")]
    #[builder(default = Req::Required)]
    pub returns: Req,
}

impl Default for FunctionRules {
    fn default() -> Self {
        Self {
            notice: Req::Required,
            dev: Req::default(),
            param: Req::Required,
            returns: Req::Required,
        }
    }
}

/// Validation rules for each function visibility (private, internal, public, external)
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct FunctionConfig {
    /// Rules for private functions
    #[builder(default)]
    pub private: FunctionRules,

    /// Rules for internal functions
    #[builder(default)]
    pub internal: FunctionRules,

    /// Rules for public functions
    #[builder(default)]
    pub public: FunctionRules,

    /// Rules for external functions
    #[builder(default)]
    pub external: FunctionRules,
}

/// Validation rules for items which have return values but no params (public state variables)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct WithReturnsRules {
    /// Requirement for the `@notice` tag
    #[builder(default = Req::Required)]
    pub notice: Req,

    /// Requirement for the `@dev` tag
    #[builder(default)]
    pub dev: Req,

    /// Requirement for the `@param` tags
    #[serde(rename = "return")]
    #[builder(default = Req::Required)]
    pub returns: Req,
}

impl Default for WithReturnsRules {
    fn default() -> Self {
        Self {
            notice: Req::Required,
            dev: Req::default(),
            returns: Req::Required,
        }
    }
}

/// Validation rules for items which have no return values and no params (private and internal state variables)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct NoticeDevRules {
    #[builder(default = Req::Required)]
    pub notice: Req,
    #[builder(default)]
    pub dev: Req,
}

impl Default for NoticeDevRules {
    fn default() -> Self {
        Self {
            notice: Req::Required,
            dev: Req::default(),
        }
    }
}

/// Validation rules for contracts, interfaces and libraries
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct ContractRules {
    #[builder(default)]
    pub title: Req,
    #[builder(default)]
    pub author: Req,
    #[builder(default)]
    pub notice: Req,
    #[builder(default)]
    pub dev: Req,
}

/// Validation rules for each state variable visibility (private, internal, public)
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct VariableConfig {
    #[builder(default)]
    pub private: NoticeDevRules,
    #[builder(default)]
    pub internal: NoticeDevRules,
    #[builder(default)]
    pub public: WithReturnsRules,
}

/// Validation rules for items which have params but no returns (constructor, enum, error, event, modifier, struct)
///
/// The default value does not enforce that `@param` is present, because it's not part of the official spec for enums
/// and structs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct WithParamsRules {
    #[builder(default = Req::Required)]
    pub notice: Req,
    #[builder(default)]
    pub dev: Req,
    #[builder(default)]
    pub param: Req,
}

impl Default for WithParamsRules {
    fn default() -> Self {
        Self {
            notice: Req::Required,
            dev: Req::default(),
            param: Req::default(),
        }
    }
}

impl WithParamsRules {
    /// Helper function to get a set of rules which enforces params (default for error, event, modifier)
    #[must_use]
    pub fn required() -> Self {
        Self {
            param: Req::Required,
            ..Default::default()
        }
    }

    /// Helper function to get a default set of rules for constructors (`@notice` is not enforced, but `@param` is)
    #[must_use]
    pub fn default_constructor() -> Self {
        Self {
            notice: Req::Ignored,
            param: Req::Required,
            ..Default::default()
        }
    }
}

/// General config for the tool
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
#[expect(clippy::struct_excessive_bools)]
pub struct BaseConfig {
    /// Paths to files and folders to analyze
    #[builder(default)]
    pub paths: Vec<PathBuf>,

    /// Paths to files and folders to exclude
    #[builder(default)]
    pub exclude: Vec<PathBuf>,

    /// Enforce that all public and external items have `@inheritdoc`
    #[builder(default = true)]
    pub inheritdoc: bool,

    /// Enforce that internal functions and modifiers which are `override` have `@inheritdoc`
    #[builder(default = false)]
    pub inheritdoc_override: bool,

    /// Do not distinguish between `@notice` and `@dev` when considering "required" validation rules
    #[builder(default)]
    pub notice_or_dev: bool,

    /// Skip the detection of the Solidity version and use the latest version supported by `slang_solidity`
    #[cfg_attr(not(feature = "slang"), serde(skip))]
    #[builder(default)]
    pub skip_version_detection: bool,
}

impl Default for BaseConfig {
    fn default() -> Self {
        Self {
            paths: Vec::default(),
            exclude: Vec::default(),
            inheritdoc: true,
            inheritdoc_override: false,
            notice_or_dev: false,
            skip_version_detection: false,
        }
    }
}

/// Output config for the tool
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct OutputConfig {
    /// Path to a file to write the output to (instead of stderr)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub out: Option<PathBuf>,

    /// Output diagnostics in JSON format
    #[builder(default)]
    pub json: bool,

    /// Compact output (minified JSON or compact text representation)
    #[builder(default)]
    pub compact: bool,

    /// Sort the results by file path
    #[builder(default)]
    pub sort: bool,
}

/// The parsed and validated config for the tool
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[non_exhaustive]
pub struct Config {
    /// General config for the tool
    #[builder(default)]
    pub lintspec: BaseConfig,

    /// Output config for the tool
    #[builder(default)]
    pub output: OutputConfig,

    /// Validation rules for constructors
    #[serde(rename = "constructor")]
    #[builder(default = WithParamsRules::default_constructor())]
    pub constructors: WithParamsRules,

    /// Validation rules for contracts
    #[serde(rename = "contract")]
    #[builder(default)]
    pub contracts: ContractRules,

    /// Validation rules for interfaces
    #[serde(rename = "interface")]
    #[builder(default)]
    pub interfaces: ContractRules,

    /// Validation rules for libraries
    #[serde(rename = "library")]
    #[builder(default)]
    pub libraries: ContractRules,

    /// Validation rules for enums
    #[serde(rename = "enum")]
    #[builder(default)]
    pub enums: WithParamsRules,

    /// Validation rules for errors
    #[serde(rename = "error")]
    #[builder(default = WithParamsRules::required())]
    pub errors: WithParamsRules,

    /// Validation rules for events
    #[serde(rename = "event")]
    #[builder(default = WithParamsRules::required())]
    pub events: WithParamsRules,

    /// Validation rules for functions
    #[serde(rename = "function")]
    #[builder(default)]
    pub functions: FunctionConfig,

    /// Validation rules for modifiers
    #[serde(rename = "modifier")]
    #[builder(default = WithParamsRules::required())]
    pub modifiers: WithParamsRules,

    /// Validation rules for structs
    #[serde(rename = "struct")]
    #[builder(default)]
    pub structs: WithParamsRules,

    /// Validation rules for state variables
    #[serde(rename = "variable")]
    #[builder(default)]
    pub variables: VariableConfig,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            lintspec: BaseConfig::default(),
            output: OutputConfig::default(),
            contracts: ContractRules::default(),
            interfaces: ContractRules::default(),
            libraries: ContractRules::default(),
            constructors: WithParamsRules::default_constructor(),
            enums: WithParamsRules::default(),
            errors: WithParamsRules::required(),
            events: WithParamsRules::required(),
            functions: FunctionConfig::default(),
            modifiers: WithParamsRules::required(),
            structs: WithParamsRules::default(),
            variables: VariableConfig::default(),
        }
    }
}

impl Config {
    pub fn from(provider: impl Provider) -> Result<Config, Box<figment::Error>> {
        Figment::from(provider).extract().map_err(Box::new)
    }

    /// Create a Figment which reads the config from the default file and environment variables
    #[must_use]
    pub fn figment(config_path: Option<PathBuf>) -> Figment {
        Figment::from(Config::default())
            .admerge(Toml::file(config_path.unwrap_or(".lintspec.toml".into())))
            .admerge(Env::prefixed("LS_").split("_").map(|k| {
                // special case for parameters with an underscore in the name
                match k.as_str() {
                    "LINTSPEC.NOTICE.OR.DEV" => "LINTSPEC.NOTICE_OR_DEV".into(),
                    "LINTSPEC.SKIP.VERSION.DETECTION" => "LINTSPEC.SKIP_VERSION_DETECTION".into(),
                    _ => k.into(),
                }
            }))
    }
}

/// Implement [`Provider`] for composability
impl Provider for Config {
    fn metadata(&self) -> figment::Metadata {
        Metadata::named("LintSpec Config")
    }

    fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
        figment::providers::Serialized::defaults(Config::default()).data()
    }
}

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

    use super::*;

    #[test]
    fn test_default_builder() {
        assert_eq!(FunctionRules::default(), FunctionRules::builder().build());
        assert_eq!(FunctionConfig::default(), FunctionConfig::builder().build());
        assert_eq!(
            WithReturnsRules::default(),
            WithReturnsRules::builder().build()
        );
        assert_eq!(NoticeDevRules::default(), NoticeDevRules::builder().build());
        assert_eq!(ContractRules::default(), ContractRules::builder().build());
        assert_eq!(VariableConfig::default(), VariableConfig::builder().build());
        assert_eq!(
            WithParamsRules::default(),
            WithParamsRules::builder().build()
        );
        assert_eq!(BaseConfig::default(), BaseConfig::builder().build());
        assert_eq!(OutputConfig::default(), OutputConfig::builder().build());
        assert_eq!(Config::default(), Config::builder().build());
    }
}