aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/unquoted_uri.rs
blob: b111f78e3e1d3ea15edf03911e347f247584e654 (plain)
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
use crate::{make, Metadata, Report, Rule, Suggestion};

use if_chain::if_chain;
use macros::lint;
use rnix::{types::TypedNode, NodeOrToken, SyntaxElement, SyntaxKind};

/// ## What it does
/// Checks for URI expressions that are not quoted.
///
/// ## Why is this bad?
/// The Nix language has a special syntax for URLs even though quoted
/// strings can also be used to represent them. Unlike paths, URLs do
/// not have any special properties in the Nix expression language
/// that would make the difference useful. Moreover, using variable
/// expansion in URLs requires some URLs to be quoted strings anyway.
/// So the most consistent approach is to always use quoted strings to
/// represent URLs. Additionally, a semicolon immediately after the
/// URL can be mistaken for a part of URL by language-agnostic tools
/// such as terminal emulators.
///
/// See RFC 00045 [1] for more.
///
/// [1]: https://github.com/NixOS/rfcs/blob/master/rfcs/0045-deprecate-url-syntax.md
///
/// ## Example
///
/// ```nix
/// inputs = {
///   gitignore.url = github:hercules-ci/gitignore.nix;
/// }
/// ```
///
/// Quote the URI expression:
///
/// ```nix
/// inputs = {
///   gitignore.url = "github:hercules-ci/gitignore.nix";
/// }
/// ```
#[lint(
    name = "unquoted uri",
    note = "Found unquoted URI expression",
    code = 12,
    match_with = SyntaxKind::TOKEN_URI
)]
struct UnquotedUri;

impl Rule for UnquotedUri {
    fn validate(&self, node: &SyntaxElement) -> Option<Report> {
        if_chain! {
            if let NodeOrToken::Token(token) = node;
            then {
                let parent_node = token.parent();
                let at = token.text_range();
                let replacement = make::quote(&parent_node).node().clone();
                let message = "Consider quoting this URI expression";
                Some(self.report().suggest(at, message, Suggestion::new(at, replacement)))
            } else {
                None
            }
        }
    }
}