From a94b9ac906c29e08ce7385c207caeb4cea3731c9 Mon Sep 17 00:00:00 2001 From: Christopher Chang <51393127+chriscerie@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:07:09 -0700 Subject: [PATCH] Add unnecessary string escape fixer --- selene-lib/src/lints/bad_string_escape.rs | 8 ++-- .../lua51_string_escapes.fixed.diff | 41 +++++++++++++++++++ .../lua51_string_escapes.stderr | 4 ++ .../roblox_string_escapes.fixed.diff | 41 +++++++++++++++++++ .../roblox_string_escapes.stderr | 4 ++ 5 files changed, 94 insertions(+), 4 deletions(-) diff --git a/selene-lib/src/lints/bad_string_escape.rs b/selene-lib/src/lints/bad_string_escape.rs index 55c8cae5..f36f6ba8 100644 --- a/selene-lib/src/lints/bad_string_escape.rs +++ b/selene-lib/src/lints/bad_string_escape.rs @@ -89,16 +89,16 @@ impl Lint for BadStringEscapeLint { "double quotes do not have to be escaped when inside single quoted strings" .to_owned(), Label::new(sequence.range.to_owned()), - None, - Applicability::Unspecified, + Some("\"".to_string()), + Applicability::MachineApplicable, ), 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()), - None, - Applicability::Unspecified, + Some("'".to_string()), + Applicability::MachineApplicable, ), }) .collect() diff --git a/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.fixed.diff b/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.fixed.diff index e69de29b..8f44ac33 100644 --- a/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.fixed.diff +++ b/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.fixed.diff @@ -0,0 +1,41 @@ + local x = "\a" + local x = "\b" + local x = "\f" + local x = "\n" + local x = "\r" + local x = "\t" + local x = "\v" + local x = "\"" + local x = "\\" + +-[MA] local x = "\'\"" ++[MA] local x = "'\"" + +-[MA] local x = '\"\'' ++[MA] local x = '"\'' + + local x = "\97" + + -- local x = "\ -- Full-moon doesn't like this style of string + -- " + + local bad = "\z" + + local bad2 = "\x1\x10" + + local bad3 = "\u{1337}\u{1234\u{1337}" + + local bad4 = "\u{10ffff}\u{110000}" + + local bad5 = "\m" + + local bad6 = "\999" + + local bad7 = "\u{ffffffffff}" + + local good = [[\z\x1\u{1234\u{110000}\m\"\'\999]] + + -- See: Issue #292 + local decimal_decimal_hex = "\01B" + local decimal_hex = "\1B" + local all_hex = "\aaa" \ No newline at end of file diff --git a/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr b/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr index dedd73f2..12508462 100644 --- a/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr +++ b/selene-lib/tests/lints/bad_string_escape/lua51_string_escapes.stderr @@ -3,12 +3,16 @@ error[bad_string_escape]: single quotes do not have to be escaped when inside do │ 11 │ local x = "\'\"" │ ^^ + │ + = try: `'` error[bad_string_escape]: double quotes do not have to be escaped when inside single quoted strings ┌─ lua51_string_escapes.lua:13:12 │ 13 │ local x = '\"\'' │ ^^ + │ + = try: `"` error[bad_string_escape]: string escape sequence doesn't exist ┌─ lua51_string_escapes.lua:20:14 diff --git a/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.fixed.diff b/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.fixed.diff index e69de29b..8f44ac33 100644 --- a/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.fixed.diff +++ b/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.fixed.diff @@ -0,0 +1,41 @@ + local x = "\a" + local x = "\b" + local x = "\f" + local x = "\n" + local x = "\r" + local x = "\t" + local x = "\v" + local x = "\"" + local x = "\\" + +-[MA] local x = "\'\"" ++[MA] local x = "'\"" + +-[MA] local x = '\"\'' ++[MA] local x = '"\'' + + local x = "\97" + + -- local x = "\ -- Full-moon doesn't like this style of string + -- " + + local bad = "\z" + + local bad2 = "\x1\x10" + + local bad3 = "\u{1337}\u{1234\u{1337}" + + local bad4 = "\u{10ffff}\u{110000}" + + local bad5 = "\m" + + local bad6 = "\999" + + local bad7 = "\u{ffffffffff}" + + local good = [[\z\x1\u{1234\u{110000}\m\"\'\999]] + + -- See: Issue #292 + local decimal_decimal_hex = "\01B" + local decimal_hex = "\1B" + local all_hex = "\aaa" \ No newline at end of file diff --git a/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr b/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr index a9ac189d..3c4bc975 100644 --- a/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr +++ b/selene-lib/tests/lints/bad_string_escape/roblox_string_escapes.stderr @@ -3,12 +3,16 @@ error[bad_string_escape]: single quotes do not have to be escaped when inside do │ 11 │ local x = "\'\"" │ ^^ + │ + = try: `'` error[bad_string_escape]: double quotes do not have to be escaped when inside single quoted strings ┌─ roblox_string_escapes.lua:13:12 │ 13 │ local x = '\"\'' │ ^^ + │ + = try: `"` error[bad_string_escape]: string escape sequence is malformed ┌─ roblox_string_escapes.lua:22:15