diff options
author | Akshay <[email protected]> | 2022-01-30 07:00:51 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2022-01-30 07:00:51 +0000 |
commit | 7457498ea64c4542b501d4c03ec2ff617763b070 (patch) | |
tree | 6e8af042bf948d25dff858a61d8016226dc2c834 | |
parent | adacdb07ca7dca0e6dfaaf2379688524f690c8b8 (diff) |
new lint: deprecated_to_path
-rw-r--r-- | bin/tests/data/deprecated_to_path.nix | 6 | ||||
-rw-r--r-- | bin/tests/main.rs | 3 | ||||
-rw-r--r-- | bin/tests/snapshots/main__deprecated_to_path.snap | 34 | ||||
-rw-r--r-- | lib/src/lints.rs | 1 | ||||
-rw-r--r-- | lib/src/lints/deprecated_to_path.rs | 59 |
5 files changed, 102 insertions, 1 deletions
diff --git a/bin/tests/data/deprecated_to_path.nix b/bin/tests/data/deprecated_to_path.nix new file mode 100644 index 0000000..cf8e021 --- /dev/null +++ b/bin/tests/data/deprecated_to_path.nix | |||
@@ -0,0 +1,6 @@ | |||
1 | [ | ||
2 | (builtins.toPath x) | ||
3 | (toPath x) | ||
4 | (toPath "/abc/def") | ||
5 | (builtins.toPath "/some/path") | ||
6 | ] | ||
diff --git a/bin/tests/main.rs b/bin/tests/main.rs index 2c4b521..bec64d2 100644 --- a/bin/tests/main.rs +++ b/bin/tests/main.rs | |||
@@ -62,5 +62,6 @@ test_lint! { | |||
62 | deprecated_is_null, | 62 | deprecated_is_null, |
63 | empty_inherit, | 63 | empty_inherit, |
64 | faster_groupby => session_info!("2.5"), | 64 | faster_groupby => session_info!("2.5"), |
65 | faster_zipattrswith => session_info!("2.6") | 65 | faster_zipattrswith => session_info!("2.6"), |
66 | deprecated_to_path => session_info!("2.4") | ||
66 | } | 67 | } |
diff --git a/bin/tests/snapshots/main__deprecated_to_path.snap b/bin/tests/snapshots/main__deprecated_to_path.snap new file mode 100644 index 0000000..08e0dbe --- /dev/null +++ b/bin/tests/snapshots/main__deprecated_to_path.snap | |||
@@ -0,0 +1,34 @@ | |||
1 | --- | ||
2 | source: bin/tests/main.rs | ||
3 | expression: "&out" | ||
4 | |||
5 | --- | ||
6 | [W17] Warning: Found usage of deprecated builtin toPath | ||
7 | ╭─[data/deprecated_to_path.nix:2:4] | ||
8 | │ | ||
9 | 2 │ (builtins.toPath x) | ||
10 | · ────────┬──────── | ||
11 | · ╰────────── builtins.toPath is deprecated, see :doc builtins.toPath within the REPL for more | ||
12 | ───╯ | ||
13 | [W17] Warning: Found usage of deprecated builtin toPath | ||
14 | ╭─[data/deprecated_to_path.nix:3:4] | ||
15 | │ | ||
16 | 3 │ (toPath x) | ||
17 | · ────┬─── | ||
18 | · ╰───── toPath is deprecated, see :doc builtins.toPath within the REPL for more | ||
19 | ───╯ | ||
20 | [W17] Warning: Found usage of deprecated builtin toPath | ||
21 | ╭─[data/deprecated_to_path.nix:4:4] | ||
22 | │ | ||
23 | 4 │ (toPath "/abc/def") | ||
24 | · ────────┬──────── | ||
25 | · ╰────────── toPath is deprecated, see :doc builtins.toPath within the REPL for more | ||
26 | ───╯ | ||
27 | [W17] Warning: Found usage of deprecated builtin toPath | ||
28 | ╭─[data/deprecated_to_path.nix:5:4] | ||
29 | │ | ||
30 | 5 │ (builtins.toPath "/some/path") | ||
31 | · ──────────────┬───────────── | ||
32 | · ╰─────────────── builtins.toPath is deprecated, see :doc builtins.toPath within the REPL for more | ||
33 | ───╯ | ||
34 | |||
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index d31a754..439fd8f 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -17,4 +17,5 @@ lints! { | |||
17 | empty_inherit, | 17 | empty_inherit, |
18 | faster_groupby, | 18 | faster_groupby, |
19 | faster_zipattrswith, | 19 | faster_zipattrswith, |
20 | deprecated_to_path, | ||
20 | } | 21 | } |
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 | } | ||