1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
531
532
533
534
535
536
537
538
539
540
541
local options = require "luacheck.options"
local builtin_standards = require "luacheck.builtin_standards"
local fs = require "luacheck.fs"
local globbing = require "luacheck.globbing"
local standards = require "luacheck.standards"
local utils = require "luacheck.utils"
 
local config = {}
 
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
 
-- A single config is represented by a table with fields:
-- * `options`: table with all config scope options, including `stds` and `files`.
-- * `config_path`: optional path to file from which config was loaded, used only in error messages.
-- * `anchor_dir`: absolute path to directory relative to which config was loaded,
--   or nil if the config is not anchored. Paths within a config are adjusted to be absolute
--   relative to anchor directory, or current directory if it's not anchored.
--   As current directory can change between config usages, this adjustment happens on demand.
 
-- Returns config path and optional anchor directory or nil and optional error message.
local function locate_config(path, global_path)
   if path == false then
      return
   end
 
   local is_default_path = not path
   path = path or config.default_path
 
   if fs.is_absolute(path) then
      return path
   end
 
   local current_dir = fs.get_current_dir()
   local anchor_dir, rel_dir = fs.find_file(current_dir, path)
 
   if anchor_dir then
      return fs.join(rel_dir, path), anchor_dir
   end
 
   if not is_default_path then
      return nil, ("Couldn't find configuration file %s"):format(path)
   end
 
   if global_path == false then
      return
   end
 
   global_path = global_path or config.get_default_global_path()
 
   if global_path and fs.is_file(global_path) then
      return global_path, (fs.split_base(global_path))
   end
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(anchor_dir)
   if not anchor_dir then
      return
   end
 
   local function loader(modname)
      local modpath = fs.join(anchor_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)
   if not loader then
      return
   end
 
   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
 
-- Requires module from config anchor directory.
-- Returns success flag and module or error message.
function config.relative_require(anchor_dir, modname)
   local loader = add_relative_loader(anchor_dir)
   local ok, mod_or_err = pcall(require, modname)
   remove_relative_loader(loader)
   return ok, mod_or_err
end
 
-- 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 function set_default_std(files, pattern, std)
   -- Avoid mutating option tables, they may be shared between different patterns.
   local pattern_opts = {std = std}
 
   if files[pattern] then
      pattern_opts = utils.update(pattern_opts, files[pattern])
   end
 
   files[pattern] = pattern_opts
end
 
local function add_default_path_options(opts)
   local files = {}
 
   if opts.files then
      files = utils.update(files, opts.files)
   end
 
   opts.files = files
   set_default_std(files, "**/spec/**/*_spec.lua", "+busted")
   set_default_std(files, "**/test/**/*_spec.lua", "+busted")
   set_default_std(files, "**/tests/**/*_spec.lua", "+busted")
   set_default_std(files, "**/*.rockspec", "+rockspec")
   set_default_std(files, "**/*.luacheckrc", "+luacheckrc")
end
 
local fallback_config = {options = {}, anchor_dir = ""}
add_default_path_options(fallback_config.options)
 
-- Loads config from a file, if possible.
-- `path` and `global_path` can be nil (will use default), false (will disable loading), or a string.
-- Doesn't validate the config.
-- Returns a table or nil and an error message.
function config.load_config(path, global_path)
   local config_path, anchor_dir = locate_config(path, global_path)
 
   if not config_path then
      if anchor_dir then
         return nil, anchor_dir
      else
         return fallback_config
      end
   end
 
   local env, special_values = make_config_env()
   local loader = add_relative_loader(anchor_dir)
   local load_ok, ret, load_err = utils.load_config(config_path, env)
   remove_relative_loader(loader)
 
   if not load_ok then
      return nil, ("Couldn't load configuration from %s: %s error (%s)"):format(config_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)
   add_default_path_options(env)
   return {options = env, config_path = config_path, anchor_dir = anchor_dir}
end
 
function config.table_to_config(opts)
   return {options = opts}
end
 
-- Validates custom stds within a config table and adds them to stds map.
-- Returns true on success or nil and an error message on error.
local function add_stds_from_config(conf, stds)
   if conf.options.stds ~= nil then
      if type(conf.options.stds) ~= "table" then
         return nil, ("invalid option 'stds': table expected, got %s"):format(type(conf.options.stds))
      end
 
      -- Validate stds in sorted order for deterministic output when more than one std is invalid.
      local std_names = {}
 
      for std_name in pairs(conf.options.stds) do
         if type(std_name) == "string" then
            table.insert(std_names, std_name)
         end
      end
 
      table.sort(std_names)
 
      for _, std_name in ipairs(std_names) do
         local std = conf.options.stds[std_name]
 
         if type(std) ~= "table" then
            return nil, ("invalid custom std '%s': table expected, got %s"):format(std_name, type(std))
         end
 
         local ok, err = standards.validate_std_table(std)
 
         if not ok then
            return nil, ("invalid custom std '%s': %s"):format(std_name, err)
         end
 
         stds[std_name] = std
      end
   end
 
   return true
end
 
local function error_prefix(conf)
   if conf.config_path then
      return ("in config loaded from %s: "):format(conf.config_path)
   else
      return ""
   end
end
 
local function quiet_validator(x)
   if type(x) == "number" then
      if math.floor(x) == x and x >= 0 and x <= 3 then
         return true
      else
         return false, ("integer in range 0..3 expected, got %.20g"):format(x)
      end
   else
      return false, ("integer in range 0..3 expected, got %s"):format(type(x))
   end
end
 
local function jobs_validator(x)
   if type(x) == "number" then
      if math.floor(x) == x and x >= 1 then
         return true
      else
         return false, ("positive integer expected, got %.20g"):format(x)
      end
   else
      return false, ("positive integer expected, got %s"):format(type(x))
   end
end
 
config.format_options = {
   quiet = quiet_validator,
   color = utils.has_type("boolean"),
   codes = utils.has_type("boolean"),
   ranges = utils.has_type("boolean"),
   formatter = utils.has_either_type("string", "function")
}
 
local top_options = {
   cache = utils.has_either_type("string", "boolean"),
   jobs = jobs_validator,
   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, config.format_options)
utils.update(top_options, options.all_options)
 
-- Returns true if config is valid, nil and error message otherwise.
local function validate_config(conf, stds)
   local ok, err = options.validate(top_options, conf.options, stds)
 
   if not ok then
      return nil, err
   end
 
   if conf.options.files then
      for path, opts in pairs(conf.options.files) do
         if type(path) == "string" then
            ok, err = options.validate(options.all_options, opts, stds)
 
            if not ok then
               return nil, ("invalid options for path '%s': %s"):format(path, err)
            end
         end
      end
   end
 
   return true
end
 
local ConfigStack = utils.class()
 
function ConfigStack:__init(configs, stds)
   self._configs = configs
   self._stds = stds
end
 
function ConfigStack:get_stds()
   return self._stds
end
 
-- Accepts an array of config tables, as returned from `load_config` and `table_to_config`.
-- Assumes that configs closer to end of the array override configs closer to beginning.
-- Returns an instance of `ConfigStack`. On validation error returns nil and an error message.
function config.stack_configs(configs)
   -- First, collect and validate stds from all configs, they are required to validate `std` option.
   local stds = utils.update({}, builtin_standards)
 
   for _, conf in ipairs(configs) do
      local ok, err = add_stds_from_config(conf, stds)
 
      if not ok then
         return nil, error_prefix(conf) .. err
      end
   end
 
   for _, conf in ipairs(configs) do
      local ok, err = validate_config(conf, stds)
 
      if not ok then
         return nil, error_prefix(conf) .. err
      end
   end
 
   return ConfigStack(configs, stds)
end
 
-- Returns a table of top-level config options, except `files` and `stds`.
function ConfigStack:get_top_options()
   local res = {
      quiet = 0,
      color = true,
      codes = false,
      ranges = false,
      formatter = "default",
      cache = false,
      jobs = false,
      include_files = {},
      exclude_files = {}
   }
 
   local current_dir = fs.get_current_dir()
   local last_anchor_dir
 
   for _, conf in ipairs(self._configs) do
      for _, option in ipairs({"quiet", "color", "codes", "ranges", "jobs"}) do
         if conf.options[option] ~= nil then
            res[option] = conf.options[option]
         end
      end
 
      -- It's not immediately obvious relatively to which config formatter modules
      -- should be resolved when they are specified in a config without an anchor dir.
      -- For now, use the last anchor directory available, that should result
      -- in reasonable behaviour in the current case of a single anchored config (loaded from file)
      -- + a single not anchored config (loaded from CLI options).
      last_anchor_dir = conf.anchor_dir or last_anchor_dir
 
      if conf.options.formatter ~= nil then
         res.formatter = conf.options.formatter
         res.formatter_anchor_dir = last_anchor_dir
      end
 
      -- Path options, on the other hand, are interpreted relatively to the current directory
      -- when specified in a config without anchor. Behaviour similar to formatter could also
      -- make sense, but this is consistent with pre 0.22.0 behaviou
      local anchor_dir = conf.anchor_dir or current_dir
 
      for _, option in ipairs({"include_files", "exclude_files"}) do
         if conf.options[option] ~= nil then
            for _, glob in ipairs(conf.options[option]) do
               table.insert(res[option], fs.normalize(fs.join(anchor_dir, glob)))
            end
         end
      end
 
      if conf.options.cache ~= nil then
         if conf.options.cache == true then
            if not res.cache then
               res.cache = fs.normalize(fs.join(last_anchor_dir or current_dir, ".luacheckcache"))
            end
         elseif conf.options.cache == false then
            res.cache = false
         else
            res.cache = fs.normalize(fs.join(anchor_dir, conf.options.cache))
         end
      end
   end
 
   return res
end
 
local function add_applying_overrides(option_stack, conf, filename)
   if not filename or not conf.options.files then
      return
   end
 
   local current_dir = fs.get_current_dir()
   local abs_filename = fs.normalize(fs.join(current_dir, filename))
   local anchor_dir
 
   if conf.anchor_dir == "" then
      anchor_dir = fs.split_base(current_dir)
   else
      anchor_dir = conf.anchor_dir or current_dir
   end
 
   local matching_pairs = {}
 
   for glob, opts in pairs(conf.options.files) do
      if type(glob) == "string" then
         local abs_glob = fs.normalize(fs.join(anchor_dir, glob))
 
         if globbing.match(abs_glob, abs_filename) then
            table.insert(matching_pairs, {
               abs_glob = abs_glob,
               opts = opts
            })
         end
      end
   end
 
   table.sort(matching_pairs, function(pair1, pair2)
      return globbing.compare(pair1.abs_glob, pair2.abs_glob)
   end)
 
   for _, pair in ipairs(matching_pairs) do
      table.insert(option_stack, pair.opts)
   end
end
 
-- Returns an option stack applicable to a file with given name, or in general if name is not given.
function ConfigStack:get_options(filename)
   local res = {}
 
   for _, conf in ipairs(self._configs) do
      table.insert(res, conf.options)
      add_applying_overrides(res, conf, filename)
   end
 
   return res
end
 
return config