Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add luacheck config #1741

Merged
merged 2 commits into from Jun 18, 2019
Merged

Add luacheck config #1741

merged 2 commits into from Jun 18, 2019

Conversation

ghost
Copy link

@ghost ghost commented Jun 9, 2019

In the mumble developer meeting the lua linter luacheck was suggested for the CI pipeline.

As i played around with it, and already have some .luacheckrc snippets for Gluon i want to add the config file and try to fix warnings found by luacheck.

In the check_site.lua files the following issues where fixed

  • line is too long (137 > 120) -> what is the maximum line length that should be used in Gluon? 120 is the default by luacheck
  • unused loop variable k, v -> replaced by _
  • setting non-standard global variable from, to -> add local
  • line contains trailing whitespace -> removed

@ghost
Copy link
Author

ghost commented Jun 9, 2019

For check_site.lua one issue is left atm

$ luacheck scripts/check_site.lua 
Checking scripts/check_site.lua                   OK

Total: 0 warnings / 0 errors in 1 file
$ luacheck package/**/check_site.lua
Checking package/gluon-authorized-keys/check_site.lua OK
Checking package/gluon-autoupdater/check_site.lua OK
Checking package/gluon-client-bridge/check_site.lua OK
Checking package/gluon-config-mode-contact-info/check_site.lua OK
Checking package/gluon-config-mode-domain-select/check_site.lua OK
Checking package/gluon-config-mode-geo-location/check_site.lua OK
Checking package/gluon-config-mode-geo-location-osm/check_site.lua OK
Checking package/gluon-config-mode-hostname/check_site.lua OK
Checking package/gluon-core/check_site.lua        OK
Checking package/gluon-ebtables/check_site.lua    OK
Checking package/gluon-ebtables-source-filter/check_site.lua OK
Checking package/gluon-mesh-babel/check_site.lua  OK
Checking package/gluon-mesh-batman-adv/check_site.lua OK
Checking package/gluon-mesh-vpn-core/check_site.lua OK
Checking package/gluon-mesh-vpn-fastd/check_site.lua OK
Checking package/gluon-mesh-vpn-tunneldigger/check_site.lua OK
Checking package/gluon-node-info/check_site.lua   OK
Checking package/gluon-radv-filterd/check_site.lua OK
Checking package/gluon-scheduled-domain-switch/check_site.lua 1 warning

    package/gluon-scheduled-domain-switch/check_site.lua:1:45: (W113) accessing undefined variable check_domain_switch

Checking package/gluon-setup-mode/check_site.lua  OK
Checking package/gluon-web-admin/check_site.lua   OK
Checking package/gluon-web-mesh-vpn-fastd/check_site.lua OK
Checking package/gluon-web-node-role/check_site.lua OK

Total: 1 warning / 0 errors in 23 files

@rubo77
Copy link
Contributor

rubo77 commented Jun 9, 2019

Already noticed here:
#1684 (comment)

@mweinelt
Copy link
Contributor

mweinelt commented Jun 9, 2019

Good catch, it's the subcheck that should be handed to need_table, but it does not exist, which need_table checks for, before executing it, hence no breakage.

@ghost
Copy link
Author

ghost commented Jun 9, 2019

  • Defined several globals per file/path (very simple, there is a more complex way to define read_globals and so on).
  • Included Lua-Scripts whiteout lua filename extension (maybe there are still some left)
  • Fixed several minor issues (set local, ..)

@mweinelt
Copy link
Contributor

mweinelt commented Jun 9, 2019

Included Lua-Scripts whiteout lua filename extension (maybe there are still some left)

How about adding lua extensions where possible?

cc @NeoRaider

@mweinelt
Copy link
Contributor

mweinelt commented Jun 9, 2019

line is too long (137 > 120) -> what is the maximum line length that should be used in Gluon? 120 is the default by luacheck

I think 120 characters is mighty fine these days.

@ghost
Copy link
Author

ghost commented Jun 15, 2019

As wished on IRC this PR should not mix fixes for warnings and the lua config file. I removed the "fix" part, and might open different PR for that, atm the opinions of the Gluon developers are missing how specific luacheck warnings should be handled (ignored, ..)

The config contains atm global definitions (global functions, variables, and so on), mostly.

@rotanid rotanid requested a review from neocturne June 15, 2019 15:11
@rotanid rotanid added the 0. type: enhancement The changeset is an enhancement label Jun 15, 2019
@neocturne
Copy link
Member

How about instead of listing all those globals in the config, we update our modules to define them in an explicit module table? This will be necessary anyways to become compatible with Lua 5.3.

@ghost
Copy link
Author

ghost commented Jun 16, 2019

@NeoRaider could you point to an example? Checking http://lua-users.org/wiki/ModulesTutorial it could be something like:

local mymodule = {}

function mymodule.foo()
    print("Hello World!")
end

return mymodule

and including the module

local mymodule = require "mymodule"
mymodule.foo()

Or do you have something different in mind?

@neocturne
Copy link
Member

@NeoRaider could you point to an example? Checking http://lua-users.org/wiki/ModulesTutorial it could be something like:

local mymodule = {}

function mymodule.foo()
    print("Hello World!")
end

return mymodule

Yes, like this.

  • For including the module, nothing changes
  • Let's always call the module table M
  • This change will also get rid of module(...) calls at the top of Lua files (in fact, module() does not exist in newer Lua versions anymore)
  • We can also remove the annoying definitions for pulling global symbols into the local scope (like local io = io) before module(...), which was only a workaround to avoid using package.seeall

@rubo77
Copy link
Contributor

rubo77 commented Jun 16, 2019

how do you apply the file .luacheckrc so it gives you those warnings?

I guess, just install and call like this:

apt install luacheck
luacheck .

How do you activate it in github as auto-check then? Do you want to use Travis?

@ghost
Copy link
Author

ghost commented Jun 16, 2019

What to do about the following issues? (running luacheck without a config file the following warnings show up)

@rubo77
Copy link
Contributor

rubo77 commented Jun 16, 2019

Maybe we should ignore all warnings for now and implement only a check for errors at first.
Later we can add more and more warnings to the output:

ignore = {
	"111", -- setting non-standard global variable xyx
	"112", -- mutating non-standard global variable xyz
	"113", -- accessing undefined variable xyz
	"121", -- setting read-only global variable table
	"122", -- setting read-only field ? of global xyz
	"142", -- setting undefined field optional of global table
	"143", -- accessing undefined field value of global table
	"211", -- unused variable xyz
	"212", -- unused argument xyz
	"213", -- unused loop variable xyz
	"231", -- variable value is never accessed
	"311", -- value assigned to variable xyz is unused
	"321", -- accessing uninitialized variable
	"331", -- value assigned to variable xyz is mutated but never accessed
	"411", -- variable xyz was previously defined
	"412", -- variable xyz was previously defined as an argument
	"421", -- shadowing definition of variable xyz
	"422", -- shadowing definition of argument
	"423", -- shadowing definition of loop variable
	"431", -- shadowing upvalue xyz
	"432", -- shadowing upvalue argument xyz
	"511", -- unreachable code
	"512", -- loop is executed at most once
	"531", -- right side of assignment has more values than left side expects
	"542", -- empty if branch
	"611", -- line contains only whitespace
	"612", -- line contains trailing whitespace
	"614", -- trailing whitespace in a comment
	"621", -- inconsistent indentation (SPACE followed by TAB)
	"631", -- line is too long (> 120)
}

Then we would only get those errors:

    openwrt/feeds/luci/applications/luci-app-ocserv/luasrc/model/cbi/ocserv/users.lua:29:27: (E011) invalid escape sequence '\$'
    openwrt/feeds/luci/applications/luci-app-travelmate/luasrc/model/cbi/travelmate/wifi_edit.lua:32:33: (E011) invalid escape sequence '\+'
    openwrt/feeds/luci/modules/luci-base/luasrc/cbi/datatypes.lua:417:26: (E011) invalid escape sequence '\*'
    openwrt/feeds/luci/modules/luci-base/luasrc/util.lua:208:34: (E011) invalid escape sequence '\-'
    openwrt/package/feeds/luci/luci-app-ocserv/luasrc/model/cbi/ocserv/users.lua:29:27: (E011) invalid escape sequence '\$'
    openwrt/package/feeds/luci/luci-app-travelmate/luasrc/model/cbi/travelmate/wifi_edit.lua:32:33: (E011) invalid escape sequence '\+'
    openwrt/package/feeds/luci/luci-base/luasrc/cbi/datatypes.lua:417:26: (E011) invalid escape sequence '\*'
    openwrt/package/feeds/luci/luci-base/luasrc/util.lua:208:34: (E011) invalid escape sequence '\-'
    packages/luci/applications/luci-app-ocserv/luasrc/model/cbi/ocserv/users.lua:29:27: (E011) invalid escape sequence '\$'
    packages/luci/applications/luci-app-travelmate/luasrc/model/cbi/travelmate/wifi_edit.lua:32:33: (E011) invalid escape sequence '\+'
    packages/luci/modules/luci-base/luasrc/cbi/datatypes.lua:417:26: (E011) invalid escape sequence '\*'
    packages/luci/modules/luci-base/luasrc/util.lua:208:34: (E011) invalid escape sequence '\-'

all no valid errors, because they are a bug in luacheck: mpeterv/luacheck#152


I added this in our Travis check for our site config: Freifunk-Nord/nord-site@a4aeb8a

works fine, thanks ;)

@neocturne
Copy link
Member

neocturne commented Jun 16, 2019

@rubo77 Why are you even checking the luci feed? We don't use any packages from that feed, and there is no use in linting code that is not in our repo anyways.

All warnings in the Gluon packages are easy to fix - and some of them hint at real bugs.

@neocturne
Copy link
Member

@bobcanthelpyou

Included Lua-Scripts whiteout lua filename extension (maybe there are still some left)

How about adding lua extensions where possible?

cc @NeoRaider

Yes, likely a good idea.

@neocturne
Copy link
Member

@bobcanthelpyou Have you already fixed the issues in package/gluon-web/luasrc/usr/lib/lua/gluon/web/http/protocol.lua? I would really like to go through all of Gluon and fix the remaining luacheck issues, but now I can't see which you already took care of...

@ghost
Copy link
Author

ghost commented Jun 16, 2019

Added the fixes in a separate pull request #1748

Removed all globals which are not required anymore.

@neocturne
Copy link
Member

I think most of the globals should actually be read_globals.

@ghost
Copy link
Author

ghost commented Jun 17, 2019

I misused new_read_globals (the docs say: Do not overwrite), switched to read_globals.

Added globals of ./targets and ./scripts

@ghost
Copy link
Author

ghost commented Jun 18, 2019

Squashed and rebased to master

@ghost ghost marked this pull request as ready for review June 18, 2019 07:42
@neocturne neocturne merged commit 025e9c7 into freifunk-gluon:master Jun 18, 2019
@ghost ghost deleted the luacheck branch July 7, 2019 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0. type: enhancement The changeset is an enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants