Skip to content

Commit

Permalink
Remove diff code when there are no changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 15, 2023
1 parent c8d1919 commit 4c10d3e
Show file tree
Hide file tree
Showing 90 changed files with 63 additions and 270 deletions.
18 changes: 15 additions & 3 deletions selene-lib/src/lints/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,31 @@ fn apply_diagnostics_fixes(code: &str, diagnostics: &[&Diagnostic]) -> String {
new_code
}

/// Returns empty string if there are no diffs
fn generate_diff(source1: &str, source2: &str) -> String {
let mut result = String::new();
let mut has_changes = false;

for change in TextDiff::from_lines(source1, source2).iter_all_changes() {
let sign = match change.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Delete => {
has_changes = true;
"-"
}
ChangeTag::Insert => {
has_changes = true;
"+"
}
ChangeTag::Equal => " ",
};
result.push_str(&format!("{}{}", sign, change.value()));
}

result
if has_changes {
result
} else {
"".to_string()
}
}

pub fn test_lint_config_with_output<
Expand Down
16 changes: 0 additions & 16 deletions selene-lib/tests/lints/almost_swapped/almost_swapped.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
x, y = y, x

a = b
b = a

t[1], t[2] = t[2], t[1]

t[1] = t[2]
t[2] = t[1]

foo().a = foo().b
foo().b = foo().a

-- We use a weird hack so this comment might break something, oh no!
a = b
b = a
1 change: 0 additions & 1 deletion selene-lib/tests/lints/almost_swapped/panic.fixed.diff
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
foo = "bar"
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
local _ = this.is_deprecated
local _ = all_of_this.is_deprecated
local _ = all_of_this.even.deeper.is_deprecated
7 changes: 0 additions & 7 deletions selene-lib/tests/lints/deprecated/specific_allow.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
deprecated_allowed()
deprecated_blocked()

more.deprecated_allowed()
wow.extra.deprecated_allowed()

deprecated_allowed.more()
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
-- Tests that the old TOML format is unchanged
print(this_is.deprecated)
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions selene-lib/tests/lints/empty_loop/empty_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ error[empty_loop]: empty loop block
10 │ │ -- Should warn
11 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:13:1
Expand All @@ -15,6 +17,8 @@ error[empty_loop]: empty loop block
16 │ │ ]]
17 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:19:1
Expand All @@ -25,46 +29,60 @@ error[empty_loop]: empty loop block
22 │ │
23 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:29:1
29 │ ╭ for _ in pairs({}) do
30 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:36:1
36 │ ╭ for _ in ipairs({}) do
37 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:43:1
43 │ ╭ for _ in {} do
44 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:50:1
50 │ ╭ for _ in a() do
51 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:57:1
57 │ ╭ while true do
58 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop.lua:64:1
64 │ ╭ repeat
65 │ │ until true
│ ╰──────────^
= try: ``

14 changes: 14 additions & 0 deletions selene-lib/tests/lints/empty_loop/empty_loop_comments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,60 @@ error[empty_loop]: empty loop block
22 │ │
23 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:29:1
29 │ ╭ for _ in pairs({}) do
30 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:36:1
36 │ ╭ for _ in ipairs({}) do
37 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:43:1
43 │ ╭ for _ in {} do
44 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:50:1
50 │ ╭ for _ in a() do
51 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:57:1
57 │ ╭ while true do
58 │ │ end
│ ╰───^
= try: ``

error[empty_loop]: empty loop block
┌─ empty_loop_comments.lua:64:1
64 │ ╭ repeat
65 │ │ until true
│ ╰──────────^
= try: ``

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,40 @@ error[parenthese_conditions]: lua does not require parentheses around conditions
2 │ if (x) then end
│ ^^^
= try: `x`

error[parenthese_conditions]: lua does not require parentheses around conditions
┌─ parenthese_conditions.lua:4:4
4 │ if (x) then
│ ^^^
= try: `x`

error[parenthese_conditions]: lua does not require parentheses around conditions
┌─ parenthese_conditions.lua:6:8
6 │ elseif (z) then
│ ^^^
= try: `z`

error[parenthese_conditions]: lua does not require parentheses around conditions
┌─ parenthese_conditions.lua:10:7
10 │ while (true) do end
│ ^^^^^^
= try: `true`

error[parenthese_conditions]: lua does not require parentheses around conditions
┌─ parenthese_conditions.lua:13:14
13 │ repeat until (false)
│ ^^^^^^^
= try: `false`

error[parenthese_conditions]: lua does not require parentheses around conditions
┌─ parenthese_conditions.lua:17:4
Expand All @@ -36,10 +46,16 @@ error[parenthese_conditions]: lua does not require parentheses around conditions
18 │ │ if (x) then end
19 │ │ end)()) then end
│ ╰───────^
= try: `(function()
if (x) then end
end)()`

error[parenthese_conditions]: lua does not require parentheses around conditions
┌─ parenthese_conditions.lua:18:8
18 │ if (x) then end
│ ^^^
= try: `x`

Empty file.
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Roact.createElement("Frame")
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 0 additions & 6 deletions selene-lib/tests/lints/standard_library/any.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
print(foo)
print(foo.x)
print(foo.x.y.z)
print(foo:x())
foo.x = 1
foo.x.y = 2
9 changes: 0 additions & 9 deletions selene-lib/tests/lints/standard_library/assert.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
assert(true, "message")
assert(call())
assert(call(), "this is ok")
assert(...)

assert(true)
assert(true, "message", call())
assert(true, "message", ...)
assert()
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
print()
print("a", 2, "c")
math.sin(3)
math.sin(1 + 1)
math.random()
math.random(1)
print "hello"
--
math.sin("i'd like the sine of three, please")
math.floor(3.5, "remember to make sure it's 3 :)")
math.sin "pi"
--
local variable = 3
math.sin(variable)
math.sin(variable, variable)
--
math.max(2)
math.pi()
string.format(-2, "foo")
string.format(1 + 1)
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
expect.extend({})
expect(1).to.be.ok()
3 changes: 0 additions & 3 deletions selene-lib/tests/lints/standard_library/complex.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
print(require("foo").bar)
print(coroutine.wrap(print)())
getmetatable({}).__index = function() end
9 changes: 0 additions & 9 deletions selene-lib/tests/lints/standard_library/constants.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
collectgarbage("count")
collectgarbage "count"

collectgarbage("doge")
collectgarbage(1)

collectgarbage("coun" .. "t")

collectgarbage()
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
math.floor(if x then 1 else 2)
math.floor(if x then "a" else "b")

-- TODO: Let's try to capture this, right now the entire expression just resolves to no type
math.floor(if x then 1 else "a")
2 changes: 0 additions & 2 deletions selene-lib/tests/lints/standard_library/lua52.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
table.unpack({})
setfenv() -- This would error if it was defined, since setfenv requires an argument
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
usesType(x * 0.5)
usesType(0.5 * x)
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
foo:bar(1)
foo:bar("a")
foo.bar()
foo:baz()
11 changes: 0 additions & 11 deletions selene-lib/tests/lints/standard_library/required.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
-- ipairs requires an argument--this should fail.
ipairs()

-- setmetatable requires a first argument, but not a second one. this should still fail.
setmetatable()

-- This, however, should not.
setmetatable({})

-- nil counts as filling in an unrequired argument.
setmetatable({}, nil)
11 changes: 0 additions & 11 deletions selene-lib/tests/lints/standard_library/shadowing.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
local math = {}
math.dog = 1
math.sin = print

math.sin("hi" + math.dog)

function foo(table)
table.insert()
end

table.insert()
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
collectgarbage(`count`)
collectgarbage(`ohno`)

string.upper(`foo`)
string.upper(`foo{"bar"}`)

math.floor(`oh no`)
3 changes: 0 additions & 3 deletions selene-lib/tests/lints/standard_library/ternary.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
math.floor(r > 1 and 1 or 0)
math.floor(r > 1 + 1)
math.floor(not flag and 1 or 0)
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
print("3.14 = ", math.pie)
print(print.foo)
print(math.huge.big)
math.call()

-- These aren't correct, but they should be covered under a different lint
call()
local _ = bad
print(foo.bar)
Loading

0 comments on commit 4c10d3e

Please sign in to comment.