Skip to content

Commit

Permalink
Add Luacheck (#782)
Browse files Browse the repository at this point in the history
* Add luacheck configuration

* luacheck cleanup

* Add workflow action
  • Loading branch information
charliefoxtwo authored Jul 2, 2024
1 parent c931552 commit 2f85be2
Show file tree
Hide file tree
Showing 30 changed files with 399 additions and 322 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/luacheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Luacheck

on:
push:
pull_request:
merge_group:

jobs:
build:
runs-on: ubuntu-latest

name: Run Luacheck
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Luacheck linter
uses: lunarmodules/luacheck@v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
luacheck.exe
66 changes: 66 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
return {
std = "lua51",
max_line_length = false, -- handled by stylua
exclude_files = { "**/test/**/*.lua", "**/*API_defs.lua", "**/ext/*.lua" },
globals = { "LuaExportStart", "LuaExportStop", "LuaExportBeforeNextFrame", "LuaExportAfterNextFrame" }, -- provided by dcs environment
read_globals = {
"lfs", -- module provided by dcs
"GetDevice", -- all below functions are dcs globals
"LoGetAircraftDrawArgumentValue",
"LoGetSelfData",
"LoGetModelTime",
"LoGetMissionStartTime",
"LoGetControlPanel_HSI",
"LoGetPayloadInfo",
"LoGetMagneticYaw",
"LoGetMCPState",
"LoGetTWSInfo",
"LoGetBasicAtmospherePressure",
"LoGetNavigationInfo",
"LoIsOwnshipExportAllowed",
"LoGetPilotName",
"LoGetIndicatedAirSpeed",
"LoGetRoute",
"LoGetWingInfo",
"LoGetVectorWindVelocity",
"LoGetAngularVelocity",
"LoGetFMData",
"LoGetRadioBeaconsStatus",
"LoGetVectorVelocity",
"LoSetCommand",
"LoSetCommand",
"LoGetAngleOfSideSlip",
"LoGetRadarAltimeter",
"LoIsObjectExportAllowed",
"LoIsSensorExportAllowed",
"LoGetObjectById",
"LoGetWorldObjects",
"LoGetTargetInformation",
"LoGetLockedTargetInformation",
"LoGetF15_TWS_Contacts",
"LoGetSightingSystemInfo",
"LoGetWingTargets",
"LoGetPlayerPlaneId",
"LoGetAltitude",
"LoGetNameByType",
"LoGeoCoordinatesToLoCoordinates",
"LoCoordinatesToGeoCoordinates",
"LoGetAccelerationUnits",
"LoGetADIPitchBankYaw",
"LoGetSnares",
"list_indication",
"list_cockpit_params",
"ValueConvert",
"LoGetAltitudeAboveSeaLevel",
"LoGetAltitudeAboveGroundLevel",
"LoGetVerticalVelocity",
"LoGetTrueAirSpeed",
"LoGetMachNumber",
"LoGetAngleOfAttack",
"LoGetGlideDeviation",
"LoGetSideDeviation",
"LoGetSlipBallPosition",
"LoGetEngineInfo",
"LoGetMechInfo",
},
}
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"johnnymorganz.stylua",
"tomblind.local-lua-debugger-vscode",
"sumneko.lua"
"sumneko.lua",
"rog2.luacheck"
]
}
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
".vscode",
"/Scripts/DCS-BIOS/lib/archive/"
],
"Lua.diagnostics.globals": [
"lfs"
],
"asciidoc.antora.enableAntoraSupport": true
}
12 changes: 7 additions & 5 deletions Scripts/DCS-BIOS/lib/BIOSStateMachine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ end
--- @param module Module
--- @param dev0 CockpitDevice?
function BIOSStateMachine:queue_module_data(module, dev0)
for _, hook in ipairs(module.exportHooks) do
hook(dev0)
if dev0 ~= nil then
for _, hook in ipairs(module.exportHooks) do
hook(dev0)
end
end

-- legacy behavior - for some reason, we seem to typically call this twice. Is this because modules are getting too big?
Expand Down Expand Up @@ -107,7 +109,7 @@ function BIOSStateMachine:step()
local self_data = LoGetSelfData()
local current_aircraft_name = self_data and self_data["Name"] or "NONE"

self.metadata_start:setAircraftName(current_aircraft_name)
self.metadata_start.setAircraftName(current_aircraft_name)

self.active_modules = self.modules_by_name[current_aircraft_name] or {}
if self.active_aircraft_name ~= current_aircraft_name then
Expand All @@ -123,15 +125,15 @@ function BIOSStateMachine:step()
end

self.update_counter = (self.update_counter + 1) % 256
self.metadata_end:setUpdateCounter(self.update_counter)
self.metadata_end.setUpdateCounter(self.update_counter)

-- if the last frame update has not been completely transmitted, skip a frame
if self.bytes_in_transit > 0 then
-- TODO: increase a frame skip counter for logging purposes
self.update_skip_counter = (self.update_skip_counter + 1) % 256
return
end
self.metadata_end:setUpdateSkipCounter(self.update_skip_counter)
self.metadata_end.setUpdateSkipCounter(self.update_skip_counter)
self.next_step_time = curTime + 0.033

-- send frame sync sequence
Expand Down
4 changes: 2 additions & 2 deletions Scripts/DCS-BIOS/lib/Protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ function Protocol.saveAddresses()
end
end

for moduleName, export_module in pairs(Protocol.exportModules) do
for _, export_module in pairs(Protocol.exportModules) do
for _, category in pairs(export_module.documentation) do
for identifier, args in pairs(category) do
for _, args in pairs(category) do
local outputs = args.outputs or {}
for _, output in ipairs(outputs) do
-- we redefine here for a few legacy plane controls using BIOS.util.defineTumb
Expand Down
3 changes: 0 additions & 3 deletions Scripts/DCS-BIOS/lib/common/JSONHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function JSONHelper.encode_to_file(value, filename)
if file then
file:write(json_string)
file:close()
file = nil
end
end

Expand All @@ -41,7 +40,6 @@ function JSONHelper.encode_to_jsonp_file(value, prefix, suffix, filename)
file:write(json_string)
file:write(suffix)
file:close()
file = nil
end
end

Expand All @@ -62,7 +60,6 @@ function JSONHelper.decode_from_file(filename)
--- @type table
json_table = JSON:decode(file:read("*a"))
file:close()
file = nil
end

return json_table or {}
Expand Down
3 changes: 2 additions & 1 deletion Scripts/DCS-BIOS/lib/common/Log.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module("Log", package.seeall)

Logger = require("Scripts.DCS-BIOS.lib.common.Logger")
local Logger = require("Scripts.DCS-BIOS.lib.common.Logger")

--- @type Logger
local Log = Logger:new(lfs.writedir() .. [[Logs/DCS-BIOS.log]])

Expand Down
12 changes: 5 additions & 7 deletions Scripts/DCS-BIOS/lib/common/Logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function Logger:log_table(level, tab, max_depth, max_bytes_to_log)
self.bytes_logged = 0
self.max_bytes_to_log = max_bytes_to_log or default_log_size_limit
max_depth = max_depth or default_max_depth
local return_code, buffer = self:dump_table(tab, max_depth)
local _, buffer = self:dump_table(tab, max_depth)
self:log_simple(level, "\n" .. buffer .. "\n")
end

Expand All @@ -164,9 +164,8 @@ function Logger:dump_table(tab, max_depth)
--- Recursive table dump
--- @param tablex table
--- @param depth number
--- @param max_depth number
--- @return number, string
local function log_tablex(tablex, depth, max_depth)
local function log_tablex(tablex, depth)
local return_value = 0
local return_buffer = ""

Expand All @@ -192,7 +191,7 @@ function Logger:dump_table(tab, max_depth)
self.bytes_logged = self.bytes_logged + string.len(str)
return_buffer = return_buffer .. "\n" .. str
local tmp_buffer
return_value, tmp_buffer = log_tablex(v, depth + 1, max_depth)
return_value, tmp_buffer = log_tablex(v, depth + 1)
if tmp_buffer ~= nil then
return_buffer = return_buffer .. tmp_buffer
end
Expand All @@ -205,7 +204,7 @@ function Logger:dump_table(tab, max_depth)
end
return return_value, return_buffer
end
local result_code, result_buffer = log_tablex(tab, 0, max_depth)
local result_code, result_buffer = log_tablex(tab, 0)
return result_code, result_buffer
end

Expand All @@ -221,7 +220,7 @@ function Logger:log_table_indexes(tab)

local output = "Indexes :\n"

for k, v in pairs(tab) do
for k, _ in pairs(tab) do
if k ~= nil then
if type(k) == "string" or type(k) == "number" then
output = output .. k .. "\n"
Expand All @@ -246,7 +245,6 @@ function Logger:log_array(array)
return
end

local padLength = 20
local output = "Array :\n"
for k, v in pairs(array) do
if k ~= nil then
Expand Down
2 changes: 1 addition & 1 deletion Scripts/DCS-BIOS/lib/io/Connection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end

--- @abstract
--- Initializes the connection
function Connection:init()
function Connection:init() -- luacheck: ignore 212
error("init must be implemented by the Connection subclass", 2)
end

Expand Down
2 changes: 1 addition & 1 deletion Scripts/DCS-BIOS/lib/io/ReadableConnection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
--- @abstract
--- Receives and processes data
--- @return boolean, string? success whether the step was successful, and the error code if not
function ReadableConnection:receive()
function ReadableConnection:receive() -- luacheck: ignore 212
error("receive must be implemented by the ReadableConnection subclass", 2)
end

Expand Down
8 changes: 4 additions & 4 deletions Scripts/DCS-BIOS/lib/io/Server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ end

--- @abstract
--- Initializes any socket connections
function Server:init()
function Server:init() -- luacheck: ignore 212
error("init must be implemented by the Server subclass", 2)
end

--- @abstract
--- Receives incoming data to be processed
function Server:step()
function Server:step() -- luacheck: ignore 212
error("step must be implemented by the Server subclass", 2)
end

--- @abstract
--- Sends a message to the client
--- @param message string the message to send
function Server:send(message)
function Server:send(message) -- luacheck: ignore 212
error("send must be implemented by the Server subclass", 2)
end

--- @abstract
--- Closes any socket connections
function Server:close()
function Server:close() -- luacheck: ignore 212
error("close must be implemented by the Server subclass", 2)
end

Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions Scripts/DCS-BIOS/lib/modules/Module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ function Module:defineSetCommandTumb(identifier, device_id, command, arg_number,
if output_map and strAlloc then
control.outputs[1].suffix = Suffix.int

local description = "possible values: "
local output_description = "possible values: "
for i = 1, #output_map, 1 do
description = description .. '"' .. output_map[i] .. '" '
output_description = output_description .. '"' .. output_map[i] .. '" '
end

control.outputs[2] = StringOutput:new(strAlloc, Suffix.str, description)
control.outputs[2] = StringOutput:new(strAlloc, Suffix.str, output_description)
end

self:addInputProcessor(identifier, function(state)
local value = GetDevice(0):get_argument_value(arg_number)
local n = tonumber(string.format("%.0f", (value - limits[1]) / step))
local new_n = n
local new_n

if state == "INC" then
new_n = Module.cap(n + 1, 0, last_n, cycle)
Expand Down
1 change: 0 additions & 1 deletion Scripts/DCS-BIOS/lib/modules/aircraft_modules/AJS37.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module("AJS37", package.seeall)

local ActionArgument = require("Scripts.DCS-BIOS.lib.modules.documentation.ActionArgument")
local ActionInput = require("Scripts.DCS-BIOS.lib.modules.documentation.ActionInput")
local ApiVariant = require("Scripts.DCS-BIOS.lib.modules.documentation.ApiVariant")
local Control = require("Scripts.DCS-BIOS.lib.modules.documentation.Control")
local ControlType = require("Scripts.DCS-BIOS.lib.modules.documentation.ControlType")
local Functions = require("Scripts.DCS-BIOS.lib.common.Functions")
Expand Down
2 changes: 1 addition & 1 deletion Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-14.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end)

local hsd_ind = {}

F_14:addExportHook(function(dev0)
F_14:addExportHook(function()
hsd_ind = Module.parse_indication(1)
end)

Expand Down
14 changes: 6 additions & 8 deletions Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-15E.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ local function combine_ufc_line(left, center, right, centerStart)
left = Functions.coerce_nil_to_string(left)
right = Functions.coerce_nil_to_string(right)
center = Functions.coerce_nil_to_string(center)
local leftChars, leftSpecial
local rightChars, rightSpecial
local centerChars, centerSpecial
left, leftChars, leftSpecial = prepareCharsAndSpecial(left)
right, rightChars, rightSpecial = prepareCharsAndSpecial(right)
center, centerChars, centerSpecial = prepareCharsAndSpecial(center)

local _, leftChars, leftSpecial = prepareCharsAndSpecial(left)
local _, rightChars, rightSpecial = prepareCharsAndSpecial(right)
local _, centerChars, centerSpecial = prepareCharsAndSpecial(center)

local paddingSpaces = ufc_string_length - string.len(rightChars)

Expand Down Expand Up @@ -232,8 +230,8 @@ end

-- UFC Active Radios
local function determine_active_radios(line5, line6)
local activeUHF1 = ""
local activeUHF2 = ""
local activeUHF1
local activeUHF2

if line6:find("*") == 1 then
activeUHF1 = line6:sub(2, 4)
Expand Down
11 changes: 7 additions & 4 deletions Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-16C_50.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module("F-16C_50", package.seeall)

local Functions = require("Scripts.DCS-BIOS.lib.common.Functions")
local Log = require("Scripts.DCS-BIOS.lib.common.Log")

local Module = require("Scripts.DCS-BIOS.lib.modules.Module")

Expand Down Expand Up @@ -1674,7 +1673,7 @@ DEDLayout_l5["HTS_TOT"] = { 7, 10 }

--DEDLayout_l5[""] = {,}

DEDLayout = { DEDLayout_l1, DEDLayout_l2, DEDLayout_l3, DEDLayout_l4, DEDLayout_l5 }
local DEDLayout = { DEDLayout_l1, DEDLayout_l2, DEDLayout_l3, DEDLayout_l4, DEDLayout_l5 }

---------------------------- DED Display Utility Functions
local function mergeString(original_string, new_data, location)
Expand Down Expand Up @@ -1710,10 +1709,14 @@ local function IntToByteString(intval)
return string.char(retBytes[1], retBytes[2], retBytes[3], retBytes[4])
end

local OR, XOR, AND = 1, 3, 4
local OR = 1
-- local XOR = 3
-- local AND = 4

local function bitoper(a, b, oper)
local r, m, s = 0, 2 ^ 31, 0
local r = 0
local m = 2 ^ 31
local s

repeat
s, a, b = a + b + m, a % m, b % m
Expand Down
Loading

0 comments on commit 2f85be2

Please sign in to comment.