/usr/share/lua/5.1/luacheck/config.lua is in lua-check 0.21.1-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 | local options = require "luacheck.options"
local builtin_standards = require "luacheck.builtin_standards"
local fs = require "luacheck.fs"
local globbing = require "luacheck.globbing"
local utils = require "luacheck.utils"
local config = {}
-- Config must support special metatables for some keys:
-- autovivification for `files`, fallback to built-in stds for `stds`.
local special_mts = {
stds = {__index = builtin_standards},
files = {__index = function(files, key)
files[key] = {}
return files[key]
end}
}
local function make_config_env_mt()
local env_mt = {}
local special_values = {}
for key, mt in pairs(special_mts) do
special_values[key] = setmetatable({}, mt)
end
function env_mt.__index(_, key)
if special_mts[key] then
return special_values[key]
else
return _G[key]
end
end
function env_mt.__newindex(env, key, value)
if special_mts[key] then
if type(value) == "table" then
setmetatable(value, special_mts[key])
end
special_values[key] = value
else
rawset(env, key, value)
end
end
return env_mt, special_values
end
local function make_config_env()
local mt, special_values = make_config_env_mt()
return setmetatable({}, mt), special_values
end
local function remove_env_mt(env, special_values)
setmetatable(env, nil)
utils.update(env, special_values)
end
local top_options = {
color = utils.has_type("boolean"),
codes = utils.has_type("boolean"),
formatter = utils.either(utils.has_type("string"), utils.has_type("function")),
cache = utils.either(utils.has_type("string"), utils.has_type("boolean")),
jobs = function(x) return type(x) == "number" and math.floor(x) == x and x >= 1 end,
files = utils.has_type("table"),
stds = utils.has_type("table"),
exclude_files = utils.array_of("string"),
include_files = utils.array_of("string")
}
utils.update(top_options, options.all_options)
options.add_order(top_options)
-- Returns error or nil if options are valid.
local function validate_options(option_set, opts)
local ok, invalid_field = options.validate(option_set, opts)
if not ok then
if invalid_field then
return ("invalid value of option '%s'"):format(invalid_field)
else
return "validation error"
end
end
end
-- Returns error or nil if config is valid.
local function validate_config(conf)
local top_err = validate_options(top_options, conf)
if top_err then
return top_err
end
for path, opts in pairs(conf.files) do
if type(path) == "string" then
local override_err = validate_options(options.all_options, opts)
if override_err then
return ("%s in options for path '%s'"):format(override_err, path)
end
end
end
end
-- Returns table with field `globs` containing sorted normalized globs
-- used in overrides and `options` mapping these globs to options.
local function normalize_overrides(files, abs_conf_dir)
local overrides = {globs = {}, options = {}}
local orig_globs = {}
for glob in pairs(files) do
table.insert(orig_globs, glob)
end
table.sort(orig_globs)
for _, orig_glob in ipairs(orig_globs) do
local glob = fs.normalize(fs.join(abs_conf_dir, orig_glob))
if not overrides.options[glob] then
table.insert(overrides.globs, glob)
end
overrides.options[glob] = files[orig_glob]
end
table.sort(overrides.globs, globbing.compare)
return overrides
end
local function try_load(path)
local src = utils.read_file(path)
if not src then
return
end
local func, err = utils.load(src, nil, "@"..path)
return err or func
end
local function add_relative_loader(conf)
local function loader(modname)
local modpath = fs.join(conf.rel_dir, (modname:gsub("%.", utils.dir_sep)))
return try_load(modpath..".lua") or try_load(modpath..utils.dir_sep.."init.lua"), modname
end
table.insert(package.loaders or package.searchers, 1, loader) -- luacheck: compat
return loader
end
local function remove_relative_loader(loader)
for i, func in ipairs(package.loaders or package.searchers) do -- luacheck: compat
if func == loader then
table.remove(package.loaders or package.searchers, i) -- luacheck: compat
return
end
end
end
local function get_global_config_dir()
if utils.is_windows then
local local_app_data_dir = os.getenv("LOCALAPPDATA")
if not local_app_data_dir then
local user_profile_dir = os.getenv("USERPROFILE")
if user_profile_dir then
local_app_data_dir = fs.join(user_profile_dir, "Local Settings", "Application Data")
end
end
if local_app_data_dir then
return fs.join(local_app_data_dir, "Luacheck")
end
else
local fh = assert(io.popen("uname -s"))
local system = fh:read("*l")
fh:close()
if system == "Darwin" then
local home_dir = os.getenv("HOME")
if home_dir then
return fs.join(home_dir, "Library", "Application Support", "Luacheck")
end
else
local config_home_dir = os.getenv("XDG_CONFIG_HOME")
if not config_home_dir then
local home_dir = os.getenv("HOME")
if home_dir then
config_home_dir = fs.join(home_dir, ".config")
end
end
if config_home_dir then
return fs.join(config_home_dir, "luacheck")
end
end
end
end
config.default_path = ".luacheckrc"
function config.get_default_global_path()
local global_config_dir = get_global_config_dir()
if global_config_dir then
return fs.join(global_config_dir, config.default_path)
end
end
config.empty_config = {empty = true}
-- Loads config from path, returns config object or nil and error message.
function config.load_config(path, global_path)
local is_default_path = not path
path = path or config.default_path
local current_dir = fs.get_current_dir()
local abs_conf_dir, rel_conf_dir = fs.find_file(current_dir, path)
if not abs_conf_dir then
if is_default_path then
if global_path and fs.is_file(global_path) then
abs_conf_dir = current_dir
rel_conf_dir = ""
path = global_path
else
return config.empty_config
end
else
return nil, "Couldn't find configuration file "..path
end
end
local conf = {
abs_dir = abs_conf_dir,
rel_dir = rel_conf_dir,
cur_dir = current_dir
}
local conf_path = fs.join(rel_conf_dir, path)
local env, special_values = make_config_env()
local loader = add_relative_loader(conf)
local load_ok, ret, load_err = utils.load_config(conf_path, env)
remove_relative_loader(loader)
if not load_ok then
return nil, ("Couldn't load configuration from %s: %s error (%s)"):format(conf_path, ret, load_err)
end
-- Support returning some options from config instead of setting them as globals.
-- This allows easily loading options from another file, for example using require.
if type(ret) == "table" then
utils.update(env, ret)
end
remove_env_mt(env, special_values)
-- Update stds before validating config - std validation relies on that.
if type(env.stds) == "table" then
-- Ideally config shouldn't mutate global builtin standards module,
-- not if `luacheck.config` becomes public interface.
utils.update(builtin_standards, env.stds)
end
local err = validate_config(env)
if err then
return nil, ("Couldn't load configuration from %s: %s"):format(conf_path, err)
end
conf.options = env
conf.overrides = normalize_overrides(env.files, abs_conf_dir)
return conf
end
-- Adjusts path starting from config dir to start from current directory.
function config.relative_path(conf, path)
if conf.empty then
return path
else
return fs.join(conf.rel_dir, path)
end
end
-- Requires module from config directory.
-- Returns success flag and module or error message.
function config.relative_require(conf, modname)
local loader
if not conf.empty then
loader = add_relative_loader(conf)
end
local ok, mod_or_err = pcall(require, modname)
if not conf.empty then
remove_relative_loader(loader)
end
return ok, mod_or_err
end
-- Returns top-level options.
function config.get_top_options(conf)
return conf.empty and {} or conf.options
end
-- Returns array of options for a file.
function config.get_options(conf, file)
if conf.empty then
return {}
end
local res = {conf.options}
if type(file) ~= "string" then
return res
end
local path = fs.normalize(fs.join(conf.cur_dir, file))
for _, override_glob in ipairs(conf.overrides.globs) do
if globbing.match(override_glob, path) then
table.insert(res, conf.overrides.options[override_glob])
end
end
return res
end
return config
|