diff options
Diffstat (limited to 'lib/src/lints/deprecated_to_path.rs')
-rw-r--r-- | lib/src/lints/deprecated_to_path.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/src/lints/deprecated_to_path.rs b/lib/src/lints/deprecated_to_path.rs new file mode 100644 index 0000000..36cbe35 --- /dev/null +++ b/lib/src/lints/deprecated_to_path.rs | |||
@@ -0,0 +1,59 @@ | |||
1 | use crate::{session::SessionInfo, Metadata, Report, Rule}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{Apply, TypedNode}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | /// ## What it does | ||
11 | /// Checks for usage of the `toPath` function. | ||
12 | /// | ||
13 | /// ## Why is this bad? | ||
14 | /// `toPath` is deprecated. | ||
15 | /// | ||
16 | /// ## Example | ||
17 | /// | ||
18 | /// ```nix | ||
19 | /// builtins.toPath "/path" | ||
20 | /// ``` | ||
21 | /// | ||
22 | /// Try these instead: | ||
23 | /// | ||
24 | /// ```nix | ||
25 | /// # to convert the string to an absolute path: | ||
26 | /// /. + "/path" | ||
27 | /// # => /abc | ||
28 | /// | ||
29 | /// # to convert the string to a path relative to the current directory: | ||
30 | /// ./. + "/bin" | ||
31 | /// # => /home/np/statix/bin | ||
32 | /// ``` | ||
33 | #[lint( | ||
34 | name = "deprecated_to_path", | ||
35 | note = "Found usage of deprecated builtin toPath", | ||
36 | code = 17, | ||
37 | match_with = SyntaxKind::NODE_APPLY | ||
38 | )] | ||
39 | struct DeprecatedIsNull; | ||
40 | |||
41 | static ALLOWED_PATHS: &[&str; 2] = &["builtins.toPath", "toPath"]; | ||
42 | |||
43 | impl Rule for DeprecatedIsNull { | ||
44 | fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> { | ||
45 | if_chain! { | ||
46 | if let NodeOrToken::Node(node) = node; | ||
47 | if let Some(apply) = Apply::cast(node.clone()); | ||
48 | let lambda_path = apply.lambda()?.to_string(); | ||
49 | if ALLOWED_PATHS.iter().any(|&p| p == lambda_path.as_str()); | ||
50 | then { | ||
51 | let at = node.text_range(); | ||
52 | let message = format!("`{}` is deprecated, see `:doc builtins.toPath` within the REPL for more", lambda_path); | ||
53 | Some(self.report().diagnostic(at, message)) | ||
54 | } else { | ||
55 | None | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | } | ||