From f7a14a58fd14580e3bdf3b475b8c4c8b1a80c1da Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Wed, 15 Feb 2023 20:08:41 +0000 Subject: [PATCH] AP_Scripting: luacheck: check globals --- libraries/AP_Scripting/docs/docs.lua | 2 + libraries/AP_Scripting/generator/src/main.c | 2 + libraries/AP_Scripting/tests/luacheck.lua | 55 +++++++++++++++++++-- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index 83cb58dfd6..1db70846ed 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -2,6 +2,8 @@ -- This file should be auto generated and then manual edited -- generate with --scripting-docs, eg ./waf copter --scripting-docs -- see: https://github.com/sumneko/lua-language-server/wiki/EmmyLua-Annotations +-- luacheck: ignore 121 (Setting a read-only global variable) +-- luacheck: ignore 122 (Setting a read-only field of a global variable) -- luacheck: ignore 212 (Unused argument) -- luacheck: ignore 241 (Local variable is mutated but never accessed) diff --git a/libraries/AP_Scripting/generator/src/main.c b/libraries/AP_Scripting/generator/src/main.c index 4d1eb6d6a8..401518febf 100644 --- a/libraries/AP_Scripting/generator/src/main.c +++ b/libraries/AP_Scripting/generator/src/main.c @@ -2899,6 +2899,8 @@ int main(int argc, char **argv) { fprintf(docs, "-- This file should be auto generated and then manual edited\n"); fprintf(docs, "-- generate with --scripting-docs, eg ./waf copter --scripting-docs\n"); fprintf(docs, "-- see: https://github.com/sumneko/lua-language-server/wiki/EmmyLua-Annotations\n"); + fprintf(docs, "-- luacheck: ignore 121 (Setting a read-only global variable)\n"); + fprintf(docs, "-- luacheck: ignore 122 (Setting a read-only field of a global variable)\n"); fprintf(docs, "-- luacheck: ignore 212 (Unused argument)\n"); fprintf(docs, "-- luacheck: ignore 241 (Local variable is mutated but never accessed)\n\n"); diff --git a/libraries/AP_Scripting/tests/luacheck.lua b/libraries/AP_Scripting/tests/luacheck.lua index 8202bae0f7..2ae950e89f 100644 --- a/libraries/AP_Scripting/tests/luacheck.lua +++ b/libraries/AP_Scripting/tests/luacheck.lua @@ -1,12 +1,57 @@ --- Don't check globals yet. This requires us to add a list of all the AP bindings -global = false - -- https://luacheck.readthedocs.io/en/stable/warnings.html -ignore = {"631", -- Line is too long +ignore = {"111", -- Setting an undefined global variable. + "113", -- Accessing an undefined global variable. + "631", -- Line is too long. "611", -- A line consists of nothing but whitespace. "612", -- A line contains trailing whitespace. "614"} -- Trailing whitespace in a comment. -- These lua scripts are not for running on AP -exclude_files = {"Tools/CHDK-Scripts/*", "modules/*"} +exclude_files = {"Tools/CHDK-Scripts/*", "modules/*", "libraries/AP_Scripting/tests/luacheck.lua"} + +-- Grab AP globals from docs file +stds.ArduPilot = {} +stds.ArduPilot.read_globals = {} + +local env = setmetatable({}, {__index = _G}) +assert(pcall(setfenv(assert(loadfile("libraries/AP_Scripting/docs/docs.lua")), env))) + +for key, value in pairs(env) do + local singleton = { other_fields = false } + + -- add sub-functions + if type(value) == 'table' then + singleton['fields'] = {} + for s_key, _ in pairs(value) do + singleton['fields'][s_key] = { other_fields = false } + end + end + + stds.ArduPilot.read_globals[key] = singleton +end + +-- We cannot add enums to the docs without giving a value, we don't know the value until compile time +-- There are only a few, so I have added them manually here. +local function add_enum(singleton, enum) + stds.ArduPilot.read_globals[singleton].fields[enum] = { other_fields = false } +end + +add_enum('mission', 'MISSION_COMPLETE') +add_enum('mission', 'MISSION_RUNNING') +add_enum('mission', 'MISSION_STOPPED') + +add_enum('terrain', 'TerrainStatusOK') +add_enum('terrain', 'TerrainStatusUnhealthy') +add_enum('terrain', 'TerrainStatusDisabled') + +add_enum('gps', 'GPS_OK_FIX_3D_RTK_FIXED') +add_enum('gps', 'GPS_OK_FIX_3D_RTK_FLOAT') +add_enum('gps', 'GPS_OK_FIX_3D_DGPS') +add_enum('gps', 'GPS_OK_FIX_3D') +add_enum('gps', 'GPS_OK_FIX_2D') +add_enum('gps', 'NO_FIX') +add_enum('gps', 'NO_GPS') + +std = "lua53+ArduPilot" +