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
use super::*;
use std::convert::Infallible;

use full_moon::{
    ast::{self, Ast},
    tokenizer,
    visitors::Visitor,
};
use regex::Regex;

lazy_static::lazy_static! {
    static ref STRING_ESCAPE_REGEX: Regex = Regex::new(r"\\(u\{|.)([\da-fA-F]*)(\}?)").unwrap();
}

enum ReasonWhy {
    CodepointTooHigh,
    DecimalTooHigh,
    DoubleInSingle,
    Invalid,
    Malformed,
    SingleInDouble,
}

pub struct BadStringEscapeLint;

impl Lint for BadStringEscapeLint {
    type Config = ();
    type Error = Infallible;

    const SEVERITY: Severity = Severity::Warning;
    const LINT_TYPE: LintType = LintType::Correctness;

    fn new(_: Self::Config) -> Result<Self, Self::Error> {
        Ok(BadStringEscapeLint)
    }

    fn pass(&self, ast: &Ast, context: &Context, _: &AstContext) -> Vec<Diagnostic> {
        let mut visitor = BadStringEscapeVisitor {
            sequences: Vec::new(),
            roblox: context.is_roblox(),
        };

        visitor.visit_ast(ast);

        visitor
            .sequences
            .iter()
            .map(|sequence| match sequence.issue {
                ReasonWhy::Invalid => Diagnostic::new(
                    "bad_string_escape",
                    "string escape sequence doesn't exist".to_owned(),
                    Label::new(sequence.range.to_owned()),
                ),
                ReasonWhy::Malformed => Diagnostic::new(
                    "bad_string_escape",
                    "string escape sequence is malformed".to_owned(),
                    Label::new(sequence.range.to_owned()),
                ),
                ReasonWhy::DecimalTooHigh => Diagnostic::new_complete(
                    "bad_string_escape",
                    "decimal escape is too high".to_owned(),
                    Label::new(sequence.range.to_owned()),
                    vec![
                        "help: the maximum codepoint allowed in decimal escapes is `255`"
                            .to_owned(),
                    ],
                    Vec::new(),
                ),
                ReasonWhy::CodepointTooHigh => Diagnostic::new_complete(
                    "bad_string_escape",
                    "unicode codepoint is too high for this escape sequence".to_owned(),
                    Label::new(sequence.range.to_owned()),
                    vec![
                        "help: the maximum codepoint allowed in unicode escapes is `10ffff`"
                            .to_owned(),
                    ],
                    Vec::new(),
                ),
                ReasonWhy::DoubleInSingle => Diagnostic::new(
                    "bad_string_escape",
                    "double quotes do not have to be escaped when inside single quoted strings"
                        .to_owned(),
                    Label::new(sequence.range.to_owned()),
                ),
                ReasonWhy::SingleInDouble => Diagnostic::new(
                    "bad_string_escape",
                    "single quotes do not have to be escaped when inside double quoted strings"
                        .to_owned(),
                    Label::new(sequence.range.to_owned()),
                ),
            })
            .collect()
    }
}

struct BadStringEscapeVisitor {
    sequences: Vec<StringEscapeSequence>,
    roblox: bool,
}

struct StringEscapeSequence {
    range: (usize, usize),
    issue: ReasonWhy,
}

impl Visitor for BadStringEscapeVisitor {
    fn visit_expression(&mut self, node: &ast::Expression) {
        if_chain::if_chain! {
            if let ast::Expression::String(token) = node;
            if let tokenizer::TokenType::StringLiteral { literal, multi_line, quote_type } = token.token_type();
            if multi_line.is_none();
            then {
                let quote_type = *quote_type;
                let value_start = node.range().unwrap().0.bytes();

                for captures in STRING_ESCAPE_REGEX.captures_iter(literal) {
                    let start = value_start + captures.get(1).unwrap().start();

                    match &captures[1] {
                        "a" | "b" | "f" | "n" | "r" | "t" | "v" | "\\" => {},
                        "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" => {
                            if captures[2].len() > 1 {
                                let hundreds = captures[1].parse::<u16>().unwrap_or(0) * 100;
                                let tens = captures[2][1..2].parse::<u16>().unwrap_or(0);
                                if hundreds + tens > 0xff {
                                    self.sequences.push(
                                        StringEscapeSequence{
                                            range: (start, start + 4),
                                            issue: ReasonWhy::DecimalTooHigh,
                                        }
                                    );
                                }
                            }
                        },
                        "\"" => {
                            if quote_type == tokenizer::StringLiteralQuoteType::Single {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + 2),
                                        issue: ReasonWhy::DoubleInSingle,
                                    }
                                );
                            }
                        },
                        "'" => {
                            if quote_type == tokenizer::StringLiteralQuoteType::Double {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + 2),
                                        issue: ReasonWhy::SingleInDouble,
                                    }
                                );
                            }
                        },
                        "z" => {
                            if !self.roblox {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + 2),
                                        issue: ReasonWhy::Invalid,
                                    }
                                );
                            }
                        },
                        "x" => {
                            if !self.roblox {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + 2),
                                        issue: ReasonWhy::Invalid,
                                    }
                                );
                                continue;
                            }
                            let second_capture_len = captures[2].len();
                            if second_capture_len != 2 {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + second_capture_len + 2),
                                        issue: ReasonWhy::Malformed
                                    }
                                );
                            }
                        },
                        "u{" => {
                            if !self.roblox {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + 2),
                                        issue: ReasonWhy::Invalid,
                                    }
                                );
                                continue;
                            }
                            let second_capture_len = captures[2].len();
                            if captures[3].is_empty() {
                                self.sequences.push(
                                    StringEscapeSequence{
                                        range: (start, start + second_capture_len + 3),
                                        issue: ReasonWhy::Malformed,
                                    }
                                );
                                continue;
                            }
                            let codepoint = u32::from_str_radix(&captures[2], 16).unwrap_or(0x0011_0000);
                            if codepoint > 0x0010_ffff {
                                self.sequences.push(
                                    StringEscapeSequence {
                                        range: (start, start + second_capture_len + 4),
                                        issue: ReasonWhy::CodepointTooHigh,
                                    }
                                );
                            }
                        },
                        _ => {
                            self.sequences.push(
                                StringEscapeSequence{
                                    range: (start, start + 2),
                                    issue: ReasonWhy::Invalid,
                                }
                            );
                        },
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{super::test_util::test_lint, *};

    #[test]
    fn test_bad_string_escape() {
        test_lint(
            BadStringEscapeLint::new(()).unwrap(),
            "bad_string_escape",
            "lua51_string_escapes",
        );
    }

    #[test]
    #[cfg(feature = "roblox")]
    fn test_bad_string_escape_roblox() {
        test_lint(
            BadStringEscapeLint::new(()).unwrap(),
            "bad_string_escape",
            "roblox_string_escapes",
        );
    }
}