luacheck icon indicating copy to clipboard operation
luacheck copied to clipboard

Add include path while linting

Open LuxoftAKutsan opened this issue 9 years ago • 2 comments

Would be very useful to add some path to avoid undefined variables in scrips that analyzed. For example I have such files:

╭─akutsan@LuxoftAKutsan  /tmp/luacheck/luacheck-0.16.3  
╰─$ tree test 
test
├── check.lua
└── subdir
    └── nested.lua

1 directory, 2 files
╭─akutsan@LuxoftAKutsan  /tmp/luacheck/luacheck-0.16.3  
╰─$ cat test/subdir/nested.lua 
function hi_from_nested()
        print("Hi from nested")
end
╭─akutsan@LuxoftAKutsan  /tmp/luacheck/luacheck-0.16.3  
╰─$ cat test/check.lua 
require "nested"
hi_from_nested()

╭─akutsan@LuxoftAKutsan  /tmp/luacheck/luacheck-0.16.3  
╰─$ lua -e  'package.path="test/subdir/?.lua"' test/check.lua                                                                                           
Hi from nested

And I wand to lint check.lua with luacheck, but I don't want to lint subdir/nested.lua

Currently luacheck will notify about unknown name:

$ lua -e 'package.preload.lanes=error;package.path="./src/?.lua;./src/?/init.lua;"..package.path' bin/luacheck.lua  ./test/check.lua                    
Checking test/check.lua                           1 warning

    test/check.lua:2:1: accessing undefined variable hi_from_nested

Total: 1 warning / 0 errors in 1 file

Even if I add path to nested in package config:

$ lua -e 'package.preload.lanes=error;package.path="./test/subdir/?.lua;./src/?.lua;./src/?/init.lua;"..package.path' bin/luacheck.lua  ./test/check.lua                                 1 ↵
Checking test/check.lua                           1 warning

    test/check.lua:2:1: accessing undefined variable hi_from_nested

Total: 1 warning / 0 errors in 1 file

Is there existing way to add some include path? I was unable to find anything related to it in http://luacheck.readthedocs.io/en/stable/

LuxoftAKutsan avatar Nov 08 '16 08:11 LuxoftAKutsan

Luacheck doesn't use package.path or any include paths, it checks only files that are passed to it. By default it does not tolerate any custom globals, even if they are set somewhere else. So, if you want to silence the warnings, you need to either whitelist your custom globals using globals setting, or lint all your files together and use --allow-defined.

mpeterv avatar Nov 08 '16 10:11 mpeterv

@LuxoftAKutsan You should preferably adopt a coding style that does away with such globals. With 5.2 the module function was deprecated, and code units should avoid to set global variables.

The recommended way to handle this situation is to set up (and return) a "module table" from the files you require. For example, your nested.lua might look like this:

local M = {} -- "module" table

function M.hi_from_nested()
        print("Hi from nested")
end

function M.something_else()
        print("And now for something completely different")
end

return M

and then you could change check.lua to:

local nested = require "nested"
nested.hi_from_nested()
nested.something_else()

This will keep Luacheck happy, even if it doesn't know anything about the contents of nested.lua.

If you really just declare a single function, you may also return and store it directly:

-- nested lua
local function hi_from_nested()
        print("Hi from nested")
end
return hi_from_nested
-- check.lua
local say_hello = require("nested")
say_hello()

Regards, NiteHawk

n1tehawk avatar Nov 17 '16 16:11 n1tehawk