/usr/share/lua/5.1/luacheck/main.lua is in lua-check 0.13.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | local luacheck = require "luacheck"
local argparse = require "luacheck.argparse"
local config = require "luacheck.config"
local options = require "luacheck.options"
local expand_rockspec = require "luacheck.expand_rockspec"
local multithreading = require "luacheck.multithreading"
local cache = require "luacheck.cache"
local format = require "luacheck.format"
local version = require "luacheck.version"
local fs = require "luacheck.fs"
local globbing = require "luacheck.globbing"
local utils = require "luacheck.utils"
local function critical(msg)
io.stderr:write("Critical error: "..msg.."\n")
os.exit(3)
end
local function global_error_handler(err)
if type(err) == "table" and err.pattern then
critical("Invalid pattern '" .. err.pattern .. "'")
else
critical(debug.traceback(
("Luacheck %s bug (please report at github.com/mpeterv/luacheck/issues):\n%s"):format(luacheck._VERSION, err), 2))
end
end
local function main()
local default_cache_path = ".luacheckcache"
local function get_parser()
local parser = argparse("luacheck", "luacheck " .. luacheck._VERSION .. ", a simple static analyzer for Lua.", [[
Links:
Luacheck on GitHub: https://github.com/mpeterv/luacheck
Luacheck documentation: http://luacheck.readthedocs.org]])
parser:argument "files"
:description (fs.has_lfs and [[List of files, directories and rockspecs to check.
Pass "-" to check stdin.]] or [[List of files and rockspecs to check.
Pass "-" to check stdin.]])
:args "+"
:argname "<file>"
parser:flag("-g --no-global", [[Filter out warnings related to global variables.
Equivalent to --ignore 1.]]):target("global"):action("store_false")
parser:flag("-u --no-unused", [[Filter out warnings related to unused variables
and values. Equivalent to --ignore [23].]]):target("unused"):action("store_false")
parser:flag("-r --no-redefined", [[Filter out warnings related to redefined variables.
Equivalent to --ignore 4.]]):target("redefined"):action("store_false")
parser:flag("-a --no-unused-args", [[Filter out warnings related to unused arguments and
loop variables. Equivalent to --ignore 21[23].]]):target("unused_args"):action("store_false")
parser:flag("-s --no-unused-secondaries", [[Filter out warnings related to unused variables set
together with used ones.]]):target("unused_secondaries"):action("store_false")
parser:flag("--no-self", "Filter out warnings related to implicit self argument."):target("self"):action("store_false")
parser:option("--std", [[Set standard globals. <std> can be one of:
_G (default) - globals of the current Lua
interpreter;
lua51 - globals of Lua 5.1;
lua52 - globals of Lua 5.2;
lua52c - globals of Lua 5.2 with LUA_COMPAT_ALL;
lua53 - globals of Lua 5.3;
lua53c - globals of Lua 5.3 with LUA_COMPAT_5_2;
luajit - globals of LuaJIT 2.0;
ngx_lua - globals of Openresty lua-nginx-module
with LuaJIT 2.0;
min - intersection of globals of Lua 5.1, Lua 5.2,
Lua 5.3 and LuaJIT 2.0;
max - union of globals of Lua 5.1, Lua 5.2, Lua 5.3
and LuaJIT 2.0;
busted - globals added by Busted 2.0;
none - no standard globals.
Sets can be combined using "+".]])
parser:option("--globals", "Add custom globals on top of standard ones.")
:args "*"
:count "*"
:argname "<global>"
:action "concat"
:init(nil)
parser:option("--read-globals", "Add read-only globals.")
:args "*"
:count "*"
:argname "<global>"
:action "concat"
:init(nil)
parser:option("--new-globals", [[Set custom globals. Removes custom globals added
previously.]])
:args "*"
:count "*"
:argname "<global>"
:action "concat"
:init(nil)
parser:option("--new-read-globals", [[Set read-only globals. Removes read-only globals added
previously.]])
:args "*"
:count "*"
:argname "<global>"
:action "concat"
:init(nil)
parser:flag("-c --compat", "Equivalent to --std max.")
parser:flag("-d --allow-defined", "Allow defining globals implicitly by setting them.")
parser:flag("-t --allow-defined-top", [[Allow defining globals implicitly by setting them in
the top level scope.]])
parser:flag("-m --module", [[Limit visibility of implicitly defined globals to
their files.]])
parser:option("--ignore -i", [[Filter out warnings matching these patterns.
If a pattern contains slash, part before slash matches
warning code and part after it matches name of related
variable. Otherwise, if the pattern contains letters
or underscore, it matches name of related variable.
Otherwise, the pattern matches warning code.]])
:args "+"
:count "*"
:argname "<patt>"
:action "concat"
:init(nil)
parser:option("--enable -e", "Do not filter out warnings matching these patterns.")
:args "+"
:count "*"
:argname "<patt>"
:action "concat"
:init(nil)
parser:option("--only -o", "Filter out warnings not matching these patterns.")
:args "+"
:count "*"
:argname "<patt>"
:action "concat"
:init(nil)
parser:flag("--no-inline", "Disable inline options."):target("inline"):action("store_false")
parser:mutex(
parser:option("--config", "Path to configuration file. (default: "..config.default_path..")"),
parser:flag("--no-config", "Do not look up configuration file.")
)
parser:option("--filename", [[Use another filename in output and for selecting
configuration overrides.]])
parser:option("--exclude-files", "Do not check files matching these globbing patterns.")
:args "+"
:count "*"
:argname "<glob>"
parser:option("--include-files", [[Do not check files not matching these globbing
patterns.]])
:args "+"
:count "*"
:argname "<glob>"
if fs.has_lfs then
parser:mutex(
parser:option("--cache", "Path to cache file.", default_cache_path)
:defmode "arg",
parser:flag("--no-cache", "Do not use cache.")
)
end
if multithreading.has_lanes then
parser:option("-j --jobs", "Check <jobs> files in parallel.")
:convert(tonumber)
end
parser:option("--formatter" , [[Use custom formatter.
<formatter> must be a module name or one of:
TAP - Test Anything Protocol formatter;
JUnit - JUnit XML formatter;
plain - simple warning-per-line formatter;
default - standard formatter.]])
parser:flag("-q --quiet", [[Suppress output for files without warnings.
-qq: Suppress output of warnings.
-qqq: Only print total number of warnings and
errors.]])
:count "0-3"
parser:flag("--codes", "Show warning codes.")
parser:flag("--ranges", "Show ranges of columns related to warnings.")
parser:flag("--no-color", "Do not color output.")
parser:flag("-v --version", "Show version info and exit.")
:action(function() print(version.string) os.exit(0) end)
return parser
end
local function match_any(globs, name)
for _, glob in ipairs(globs) do
if globbing.match(glob, name) then
return true
end
end
return false
end
local function is_included(args, name)
return not match_any(args.exclude_files, name) and (#args.include_files == 0 or match_any(args.include_files, name))
end
-- Expands folders, rockspecs, -
-- Returns new array of filenames and table mapping indexes of bad rockspecs to error messages.
-- Removes "./" in the beginnings of file names.
-- Filters filenames using args.exclude_files and args.include_files.
local function expand_files(args)
-- If --include-files is used, do not focus on .lua files in directories.
local dir_pattern = #args.include_files > 0 and "" or "%.lua$"
local res, bad_rockspecs = {}, {}
local function add(file)
if type(file) == "string" then
file = file:gsub("^./", "")
end
local name = args.filename or file
if type(name) == "string" then
if not is_included(args, name) then
return false
end
end
table.insert(res, file)
return true
end
for _, file in ipairs(args.files) do
if file == "-" then
add(io.stdin)
elseif fs.is_dir(file) then
for _, nested_file in ipairs(fs.extract_files(file, dir_pattern)) do
add(nested_file)
end
elseif file:sub(-#".rockspec") == ".rockspec" then
local related_files, err = expand_rockspec(file)
if related_files then
for _, related_file in ipairs(related_files) do
add(related_file)
end
else
if add(file) then
bad_rockspecs[#res] = err
end
end
else
add(file)
end
end
return res, bad_rockspecs
end
local function validate_args(args, parser)
if args.jobs and args.jobs < 1 then
parser:error("<jobs> must be at least 1")
end
if args.std and not options.split_std(args.std) then
parser:error("<std> must name a standard library")
end
end
local function combine_conf_and_args_path_arrays(conf, args, option)
local conf_opts = config.get_top_options(conf)
if conf_opts[option] then
for i, path in ipairs(conf_opts[option]) do
conf_opts[option][i] = config.relative_path(conf, path)
end
table.insert(args[option], conf_opts[option])
end
args[option] = utils.concat_arrays(args[option])
end
-- Applies cli-specific options from config to args.
local function combine_config_and_args(conf, args)
local conf_opts = config.get_top_options(conf)
if args.no_color then
args.color = false
else
args.color = conf_opts.color ~= false
end
args.codes = args.codes or conf_opts.codes
args.formatter = args.formatter or conf_opts.formatter or "default"
if args.no_cache or not fs.has_lfs then
args.cache = false
elseif not args.cache then
if type(conf_opts.cache) == "string" then
args.cache = config.relative_path(conf, conf_opts.cache)
else
args.cache = conf_opts.cache
end
end
if args.cache == true then
args.cache = config.relative_path(conf, default_cache_path)
end
args.jobs = args.jobs or conf_opts.jobs
combine_conf_and_args_path_arrays(conf, args, "exclude_files")
combine_conf_and_args_path_arrays(conf, args, "include_files")
end
-- Returns sparse array of mtimes and map of filenames to cached reports.
local function get_mtimes_and_cached_reports(cache_filename, files, bad_files)
local cache_files = {}
local cache_mtimes = {}
local sparse_mtimes = {}
for i, file in ipairs(files) do
if not bad_files[i] and file ~= io.stdin then
table.insert(cache_files, file)
local mtime = fs.mtime(file)
table.insert(cache_mtimes, mtime)
sparse_mtimes[i] = mtime
end
end
return sparse_mtimes, cache.load(cache_filename, cache_files, cache_mtimes) or critical(
("Couldn't load cache from %s: data corrupted"):format(cache_filename))
end
-- Returns sparse array of sources of files that need to be checked, updates bad_files with files that had I/O issues.
local function get_srcs_to_check(cached_reports, files, bad_files)
local res = {}
for i, file in ipairs(files) do
if not bad_files[i] and not cached_reports[file] then
local src = utils.read_file(file)
if src then
res[i] = src
else
bad_files[i] = "I/O"
end
end
end
return res
end
-- Returns sparse array of new reports.
local function get_new_reports(files, srcs, jobs)
local dense_srcs = {}
local dense_to_sparse = {}
for i in ipairs(files) do
if srcs[i] then
table.insert(dense_srcs, srcs[i])
dense_to_sparse[#dense_srcs] = i
end
end
local map = jobs and multithreading.has_lanes and multithreading.pmap or utils.map
local dense_res = map(luacheck.get_report, dense_srcs, jobs)
local res = {}
for i in ipairs(dense_srcs) do
res[dense_to_sparse[i]] = dense_res[i]
end
return res
end
-- Updates cache with new_reports. Updates bad_files for which mtime is absent.
local function update_cache(cache_filename, files, bad_files, srcs, mtimes, new_reports)
local cache_files = {}
local cache_mtimes = {}
local cache_reports = {}
for i, file in ipairs(files) do
if srcs[i] and file ~= io.stdin then
if not mtimes[i] then
bad_files[i] = "I/O"
else
table.insert(cache_files, file)
table.insert(cache_mtimes, mtimes[i])
table.insert(cache_reports, new_reports[i] or false)
end
end
end
return cache.update(cache_filename, cache_files, cache_mtimes, cache_reports) or critical(
("Couldn't save cache to %s: I/O error"):format(cache_filename))
end
-- Returns array of reports for files.
local function get_reports(cache_filename, files, bad_rockspecs, jobs)
local bad_files = utils.update({}, bad_rockspecs)
local mtimes
local cached_reports
if cache_filename then
mtimes, cached_reports = get_mtimes_and_cached_reports(cache_filename, files, bad_files)
else
cached_reports = {}
end
local srcs = get_srcs_to_check(cached_reports, files, bad_files)
local new_reports = get_new_reports(files, srcs, jobs)
if cache_filename then
update_cache(cache_filename, files, bad_files, srcs, mtimes, new_reports)
end
local res = {}
for i, file in ipairs(files) do
if bad_files[i] then
res[i] = {fatal = bad_files[i]}
else
res[i] = cached_reports[file] or new_reports[i]
end
end
return res
end
local function combine_config_and_options(conf, cli_opts, files)
local res = {}
for i, file in ipairs(files) do
res[i] = config.get_options(conf, file)
table.insert(res[i], cli_opts)
end
return res
end
local function substitute_filename(new_filename, files)
if new_filename then
for i = 1, #files do
files[i] = new_filename
end
end
end
local function normalize_stdin_in_filenames(files)
for i, file in ipairs(files) do
if type(file) ~= "string" then
files[i] = "stdin"
end
end
end
local builtin_formatters = utils.array_to_set({"TAP", "JUnit", "plain", "default"})
local function pformat(report, file_names, conf, args)
if builtin_formatters[args.formatter] then
return format.format(report, file_names, args)
end
local formatter = args.formatter
local ok, output
if type(formatter) == "string" then
ok, formatter = config.relative_require(conf, formatter)
if not ok then
critical(("Couldn't load custom formatter '%s': %s"):format(args.formatter, formatter))
end
end
ok, output = pcall(formatter, report, file_names, args)
if not ok then
critical(("Couldn't run custom formatter '%s': %s"):format(tostring(args.formatter), output))
end
return output
end
local parser = get_parser()
local args = parser:parse()
local conf
if args.no_config then
conf = config.empty_config
else
local err
conf, err = config.load_config(args.config)
if not conf then
critical(err)
end
end
-- Validate args only after loading config so that custom stds are already in place.
validate_args(args, parser)
combine_config_and_args(conf, args)
local files, bad_rockspecs = expand_files(args)
local reports = get_reports(args.cache, files, bad_rockspecs, args.jobs)
substitute_filename(args.filename, files)
local report = luacheck.process_reports(reports, combine_config_and_options(conf, args, files))
normalize_stdin_in_filenames(files)
local output = pformat(report, files, conf, args)
if #output > 0 and output:sub(-1) ~= "\n" then
output = output .. "\n"
end
io.stdout:write(output)
local exit_code
if report.fatals > 0 then
exit_code = 2
elseif report.warnings > 0 or report.errors > 0 then
exit_code = 1
else
exit_code = 0
end
os.exit(exit_code)
end
xpcall(main, global_error_handler)
|