Skip to content

Commit

Permalink
Add machine applicability to each diff line
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 15, 2023
1 parent 441679f commit d2bc397
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 385 deletions.
49 changes: 42 additions & 7 deletions selene-lib/src/lints/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use std::{
};

use codespan_reporting::{
diagnostic::Severity as CodespanSeverity, term::Config as CodespanConfig,
diagnostic::{self, Severity as CodespanSeverity},
term::Config as CodespanConfig,
};

use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -93,23 +94,53 @@ fn apply_diagnostics_fixes(code: &str, diagnostics: &[&Diagnostic]) -> String {
}

/// Returns empty string if there are no diffs
fn generate_diff(source1: &str, source2: &str) -> String {
fn generate_diff(source1: &str, source2: &str, diagnostics: &[&Diagnostic]) -> String {
let mut result = String::new();
let mut has_changes = false;
let mut byte_offset = 0;
let mut prev_non_insert_applicability_prefix = " ";

for change in TextDiff::from_lines(source1, source2).iter_all_changes() {
let change_length = change.value().len() as u32;

let change_end_byte = byte_offset + change_length as u32;

let mut applicability_prefix = " ";
for diagnostic in diagnostics {
let (start, end) = diagnostic.primary_label.range;
if start < change_end_byte && end > byte_offset {
if diagnostic.applicability == Applicability::MachineApplicable {
applicability_prefix = "MA";
break;
} else if diagnostic.applicability == Applicability::MaybeIncorrect {
applicability_prefix = "MI";
break;
}
}
}

if change.tag() == ChangeTag::Insert {
applicability_prefix = prev_non_insert_applicability_prefix;
}

let sign = match change.tag() {
ChangeTag::Delete => {
has_changes = true;
"-"
format!("-{}", applicability_prefix)
}
ChangeTag::Insert => {
has_changes = true;
"+"
format!("+{}", applicability_prefix)
}
ChangeTag::Equal => " ",
ChangeTag::Equal => " ".to_string(),
};
result.push_str(&format!("{}{}", sign, change.value()));

result.push_str(&format!("{} {}", sign, change.value()));

if change.tag() != ChangeTag::Insert {
byte_offset = change_end_byte;
prev_non_insert_applicability_prefix = applicability_prefix;
}
}

if has_changes {
Expand Down Expand Up @@ -195,7 +226,11 @@ pub fn test_lint_config_with_output<
fixed_diagnostics.sort_by_key(|diagnostic| diagnostic.start_position());
}

let fixed_diff = generate_diff(&lua_source, &fixed_code);
let fixed_diff = generate_diff(
&lua_source,
&fixed_code,
&diagnostics.iter().collect::<Vec<_>>(),
);
let diff_output_path = path_base.with_extension("fixed.diff");

if let Ok(expected) = fs::read_to_string(&diff_output_path) {
Expand Down
68 changes: 34 additions & 34 deletions selene-lib/tests/lints/compare_nan/compare_nan_if.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
-if x == 0/0 then
+if x ~= x then
end

-if x ~= 0/0 then
+if x == x then
end

if x ~= x then
end

if x >= 0/0 then
end

if 1 == 0/0 then
end

if 1 ~= 0/0 then
end

if 1 + 0/0 then
end

if y.foo() == 0/0 then
end

if y.bar() ~= 0/0 then
end

if x == 1 then
end

if 1 == 0 then
end
-MI if x == 0/0 then
+MI if x ~= x then
end
-MI if x ~= 0/0 then
+MI if x == x then
end
if x ~= x then
end
if x >= 0/0 then
end
if 1 == 0/0 then
end
if 1 ~= 0/0 then
end
if 1 + 0/0 then
end
if y.foo() == 0/0 then
end
if y.bar() ~= 0/0 then
end
if x == 1 then
end
if 1 == 0 then
end
30 changes: 15 additions & 15 deletions selene-lib/tests/lints/compare_nan/compare_nan_variables.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local _ = 0/0
local _ = 1 ~= 0/0
local _ = 2 == 0/0
local _ = 3 + 0/0
local _ = 4 >= 0/0
-local _ = x ~= 0/0
-local _ = x == 0/0
+local _ = x == x
+local _ = x ~= x
local _ = x >= 0/0
local _ = y.foo() == 0/0
local _ = y.bar() ~= 0/0

local _ = x == 1
local _ = 1 == 0
local _ = 0/0
local _ = 1 ~= 0/0
local _ = 2 == 0/0
local _ = 3 + 0/0
local _ = 4 >= 0/0
-MI local _ = x ~= 0/0
-MI local _ = x == 0/0
+MI local _ = x == x
+MI local _ = x ~= x
local _ = x >= 0/0
local _ = y.foo() == 0/0
local _ = y.bar() ~= 0/0
local _ = x == 1
local _ = 1 == 0
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
print(x == { "a", "b", "c" })
print({ "a", "b", "c" } == x)

-print(x == {})
-print({} == x)
-print(x ~= {})
-print({} ~= x)
+print(next(x) == nil)
+print(next(x) == nil)
+print(next(x) ~= nil)
+print(next(x) ~= nil)

print({ "a", "b", "c" } == { "a", "b", "c" })
print({ "a", "b", "c" } == {})
print({} == {})

print( -- my cool table
- t == {}
+ next(t) == nil
)
print(x == { "a", "b", "c" })
print({ "a", "b", "c" } == x)
-MI print(x == {})
-MI print({} == x)
-MI print(x ~= {})
-MI print({} ~= x)
+MI print(next(x) == nil)
+MI print(next(x) == nil)
+MI print(next(x) ~= nil)
+MI print(next(x) ~= nil)
print({ "a", "b", "c" } == { "a", "b", "c" })
print({ "a", "b", "c" } == {})
print({} == {})
print( -- my cool table
-MI t == {}
+MI next(t) == nil
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
table.foreach({}, function(k, v) end)
-print(table.getn(x))
+print(#x)

table.foreach({}, 3)
table.foreach({}, function(k, v) end)
-MA print(table.getn(x))
+MA print(#x)
table.foreach({}, 3)
146 changes: 73 additions & 73 deletions selene-lib/tests/lints/empty_loop/empty_loop.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
for _ = 1, 10 do
return "Should not warn"
end

for _ = 1, 10 do
print("Should not warn")
end

-for _ = 1, 10 do
- -- Should warn
-end

-for _ = 1, 10 do
- --[[
- Should warn
- ]]
-end

-for _ = 1, 10 do


-
-end

+
for _ in pairs({}) do
return "Should not warn"
end

-for _ in pairs({}) do
-end

+
for _ in ipairs({}) do
return "Should not warn"
end

-for _ in ipairs({}) do
-end

+
for _ in {} do
return "Should not warn"
end

-for _ in {} do
-end

+
for _ in a() do
return "Should not warn"
end

-for _ in a() do
-end

+
while true do
return "Should not warn"
end

-while true do
-end

+
repeat
return "Should not warn"
until true

-repeat
-until true
+
-- comment here shouldn't break anything
for _ = 1, 10 do
return "Should not warn"
end
for _ = 1, 10 do
print("Should not warn")
end
-MI for _ = 1, 10 do
-MI -- Should warn
-MI end
-MI for _ = 1, 10 do
-MI --[[
-MI Should warn
-MI ]]
-MI end
-MI for _ = 1, 10 do
-MI
-MI end
+
for _ in pairs({}) do
return "Should not warn"
end
-MI for _ in pairs({}) do
-MI end
+
for _ in ipairs({}) do
return "Should not warn"
end
-MI for _ in ipairs({}) do
-MI end
+
for _ in {} do
return "Should not warn"
end
-MI for _ in {} do
-MI end
+
for _ in a() do
return "Should not warn"
end
-MI for _ in a() do
-MI end
+
while true do
return "Should not warn"
end
-MI while true do
-MI end
+
repeat
return "Should not warn"
until true
-MI repeat
-MI until true
+MI
-- comment here shouldn't break anything
Loading

0 comments on commit d2bc397

Please sign in to comment.