mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
Merge pull request #51 from idbrii/lint
Add luacheck and fix bugs it found
This commit is contained in:
commit
4b0e0c7ec7
26
.github/workflows/luacheck.yml
vendored
Normal file
26
.github/workflows/luacheck.yml
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
name: Linting
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
luacheck:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
# Subdirectory to avoid linting luarocks code. Use same dir as testy.
|
||||
path: batteries
|
||||
- name: Setup Lua
|
||||
uses: leafo/gh-actions-lua@v8
|
||||
with:
|
||||
luaVersion: 5.4
|
||||
- name: Setup Lua Rocks
|
||||
uses: leafo/gh-actions-luarocks@v4
|
||||
- name: Setup luacheck
|
||||
run: luarocks install luacheck
|
||||
- name: Run Code Linter
|
||||
run: |
|
||||
cd batteries
|
||||
luacheck .
|
33
.luacheckrc
Normal file
33
.luacheckrc
Normal file
@ -0,0 +1,33 @@
|
||||
return {
|
||||
std = "lua51+love",
|
||||
ignore = {
|
||||
"211", -- Unused local variable.
|
||||
"212/self", -- Unused argument self.
|
||||
"213", -- Unused loop variable.
|
||||
"631", -- Line is too long.
|
||||
},
|
||||
files = {
|
||||
["tests.lua"] = {
|
||||
ignore = {
|
||||
"211", -- Unused local variable. (testy will find these local functions)
|
||||
},
|
||||
},
|
||||
["assert.lua"] = {
|
||||
ignore = {
|
||||
"121", -- Setting a read-only global variable. (we clobber assert)
|
||||
},
|
||||
},
|
||||
["init.lua"] = {
|
||||
ignore = {
|
||||
"111", -- Setting an undefined global variable. (batteries and ripairs)
|
||||
"121", -- Setting a read-only global variable. (we clobber assert)
|
||||
"143", -- Accessing an undefined field of a global variable. (we use tablex as table)
|
||||
},
|
||||
},
|
||||
["sort.lua"] = {
|
||||
ignore = {
|
||||
"142", -- Setting an undefined field of a global variable. (inside export)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
@ -75,7 +75,7 @@ end
|
||||
|
||||
--replace everything in assert with nop functions that just return their second argument, for near-zero overhead on release
|
||||
function assert:nop()
|
||||
local nop = function(self, a)
|
||||
local nop = function(_, a)
|
||||
return a
|
||||
end
|
||||
setmetatable(self, {
|
||||
|
@ -136,7 +136,7 @@ function colour.rgb_to_hsl(r, g, b)
|
||||
local l, d = max + min, max - min
|
||||
local s = d / (l > 1 and (2 - l) or l)
|
||||
l = l / 2
|
||||
local h = nil --depends on below
|
||||
local h --depends on below
|
||||
if max == r then
|
||||
h = (g - b) / d
|
||||
if g < b then h = h + 6 end
|
||||
@ -145,6 +145,7 @@ function colour.rgb_to_hsl(r, g, b)
|
||||
else
|
||||
h = (r - g) / d + 4
|
||||
end
|
||||
assert(h)
|
||||
return h / 6, s, l
|
||||
end
|
||||
|
||||
|
@ -209,7 +209,7 @@ end
|
||||
function functional.stitch(t, f)
|
||||
local result = {}
|
||||
for i, v in ipairs(t) do
|
||||
local v = f(v, i)
|
||||
v = f(v, i)
|
||||
if v ~= nil then
|
||||
if type(v) == "table" then
|
||||
for _, e in ipairs(v) do
|
||||
|
@ -186,7 +186,7 @@ function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_ra
|
||||
local numerb = dx1 * dyab - dy1 * dxab
|
||||
|
||||
--check coincident lines
|
||||
local intersected = "none"
|
||||
local intersected
|
||||
if
|
||||
math.abs(numera) < COLLIDE_EPS and
|
||||
math.abs(numerb) < COLLIDE_EPS and
|
||||
@ -215,6 +215,7 @@ function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_ra
|
||||
end
|
||||
end
|
||||
end
|
||||
assert(intersected)
|
||||
|
||||
if intersected == "both" then
|
||||
--simply displace along A normal
|
||||
@ -454,11 +455,11 @@ function intersect.point_aabb_collide(a, b_pos, b_hs, into)
|
||||
end
|
||||
|
||||
function intersect.circle_aabb_overlap(a, a_rad, b_pos, b_hs)
|
||||
return intersect.aabb_circle_overlap(b_pos, b_pos, a, a_rad)
|
||||
return intersect.aabb_circle_overlap(b_pos, b_hs, a, a_rad)
|
||||
end
|
||||
|
||||
function intersect.circle_aabb_collide(a, a_rad, b_pos, b_hs, into)
|
||||
return intersect.reverse_msv(intersect.aabb_circle_collide(b_pos, b_pos, a, a_rad, into))
|
||||
return intersect.reverse_msv(intersect.aabb_circle_collide(b_pos, b_hs, a, a_rad, into))
|
||||
end
|
||||
|
||||
function intersect.circle_line_collide(a, a_rad, b_start, b_end, b_rad, into)
|
||||
|
@ -153,7 +153,6 @@ function pretty._process(input, config, processing_state)
|
||||
end
|
||||
if break_next then
|
||||
table.insert(line_chunks, table.remove(chunks, 1))
|
||||
break_next = false
|
||||
end
|
||||
end
|
||||
chunks = line_chunks
|
||||
|
3
set.lua
3
set.lua
@ -1,5 +1,8 @@
|
||||
--[[
|
||||
set type with appropriate operations
|
||||
|
||||
NOTE: This is actually a unique list (ordered set). So it's more than just
|
||||
a table with keys for values.
|
||||
]]
|
||||
|
||||
local path = (...):gsub("set", "")
|
||||
|
14
sort.lua
14
sort.lua
@ -56,8 +56,8 @@ function sort._merge(array, workspace, low, middle, high, less)
|
||||
local i, j, k
|
||||
i = 1
|
||||
-- copy first half of array to auxiliary array
|
||||
for j = low, middle do
|
||||
workspace[i] = array[j]
|
||||
for w = low, middle do
|
||||
workspace[i] = array[w]
|
||||
i = i + 1
|
||||
end
|
||||
-- sieve through
|
||||
@ -78,8 +78,8 @@ function sort._merge(array, workspace, low, middle, high, less)
|
||||
k = k + 1
|
||||
end
|
||||
-- copy back any remaining elements of first half
|
||||
for k = k, j - 1 do
|
||||
array[k] = workspace[i]
|
||||
for w = k, j - 1 do
|
||||
array[w] = workspace[i]
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
@ -121,7 +121,8 @@ end
|
||||
|
||||
function sort.stable_sort(array, less)
|
||||
--setup
|
||||
local trivial, n, less = sort._sort_setup(array, less)
|
||||
local trivial, n
|
||||
trivial, n, less = sort._sort_setup(array, less)
|
||||
if not trivial then
|
||||
--temp storage; allocate ahead of time
|
||||
local workspace = {}
|
||||
@ -135,7 +136,8 @@ end
|
||||
|
||||
function sort.insertion_sort(array, less)
|
||||
--setup
|
||||
local trivial, n, less = sort._sort_setup(array, less)
|
||||
local trivial, n
|
||||
trivial, n, less = sort._sort_setup(array, less)
|
||||
if not trivial then
|
||||
sort._insertion_sort_impl(array, 1, n, less)
|
||||
end
|
||||
|
@ -171,9 +171,7 @@ function stringx.deindent(s, keep_trailing_empty)
|
||||
--split along newlines
|
||||
local lines = stringx.split(s, newline)
|
||||
--detect and strip any leading blank lines
|
||||
local leading_newline = false
|
||||
while lines[1] == "" do
|
||||
leading_newline = true
|
||||
table.remove(lines, 1)
|
||||
end
|
||||
|
||||
@ -249,6 +247,7 @@ end
|
||||
|
||||
--check if a given string starts with another
|
||||
--(without garbage)
|
||||
--Using loops is actually faster than string.find!
|
||||
function stringx.starts_with(s, prefix)
|
||||
for i = 1, #prefix do
|
||||
if s:byte(i) ~= prefix:byte(i) then
|
||||
|
@ -282,7 +282,7 @@ function tablex.append_inplace(t1, t2, ...)
|
||||
table.insert(t1, v)
|
||||
end
|
||||
if ... then
|
||||
return table.append_inplace(t1, ...)
|
||||
return tablex.append_inplace(t1, ...)
|
||||
end
|
||||
return t1
|
||||
end
|
||||
@ -398,8 +398,8 @@ function tablex.collapse(t)
|
||||
local r = {}
|
||||
for _, v in ipairs(t) do
|
||||
if type(v) == "table" then
|
||||
for _, v in ipairs(v) do
|
||||
table.insert(r, v)
|
||||
for _, w in ipairs(v) do
|
||||
table.insert(r, w)
|
||||
end
|
||||
else
|
||||
table.insert(r, v)
|
||||
|
Loading…
Reference in New Issue
Block a user