Skip to content

Commit

Permalink
Allow using globs as keys for path overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Feb 25, 2016
1 parent 4102e90 commit f89d76c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion spec/configs/multioverride_config.luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ files["spec/samples/"] = {
ignore = {"213"}
}

files["spec/samples/unused_code.lua"] = {
files["spec/samples/*_code.lua"] = {
enable = {"213"}
}
33 changes: 17 additions & 16 deletions src/luacheck/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local options = require "luacheck.options"
local stds = require "luacheck.stds"
local fs = require "luacheck.fs"
local globbing = require "luacheck.globbing"
local utils = require "luacheck.utils"

local config = {}
Expand Down Expand Up @@ -104,30 +105,30 @@ local function validate_config(conf)
end
end

-- Returns table with field `paths` containing sorted normalize paths
-- used in overrides and `options` mapping these paths to options.
-- 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 = {paths = {}, options = {}}
local overrides = {globs = {}, options = {}}

local orig_paths = {}
local orig_globs = {}

for path in pairs(files) do
table.insert(orig_paths, path)
for glob in pairs(files) do
table.insert(orig_globs, glob)
end

table.sort(orig_paths)
table.sort(orig_globs)

for _, orig_path in ipairs(orig_paths) do
local path = fs.normalize(fs.join(abs_conf_dir, orig_path))
for _, orig_glob in ipairs(orig_globs) do
local glob = fs.normalize(fs.join(abs_conf_dir, orig_glob))

if not overrides.options[path] then
table.insert(overrides.paths, path)
if not overrides.options[glob] then
table.insert(overrides.globs, glob)
end

overrides.options[path] = files[orig_path]
overrides.options[glob] = files[orig_glob]
end

table.sort(overrides.paths)
table.sort(overrides.globs, globbing.compare)
return overrides
end

Expand Down Expand Up @@ -268,9 +269,9 @@ function config.get_options(conf, file)

local path = fs.normalize(fs.join(conf.cur_dir, file))

for _, override_path in ipairs(conf.overrides.paths) do
if fs.is_subpath(override_path, path) then
table.insert(res, conf.overrides.options[override_path])
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

Expand Down
36 changes: 36 additions & 0 deletions src/luacheck/globbing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,40 @@ function globbing.match(glob, path)
return parts_match(glob_parts, 1, path_parts, 1)
end

-- Checks if glob1 is less specific than glob2 and should be applied
-- first in overrides.
function globbing.compare(glob1, glob2)
local base1, base2
base1, glob1 = fs.split_base(glob1)
base2, glob2 = fs.split_base(glob2)

if base1 ~= base2 then
return base1 < base2
end

local parts1 = get_parts(glob1)
local parts2 = get_parts(glob2)

for i = 1, math.max(#parts1, #parts2) do
if not parts1[i] then
return true
elseif not parts2[i] then
return false
end

if (parts1[i] == "**" or parts2[i] == "**") and parts1[i] ~= parts2[i] then
return parts1[i] == "**"
end

local _, specials1 = parts1[i]:gsub("[%*%?%[]", {})
local _, specials2 = parts2[i]:gsub("[%*%?%[]", {})

if specials1 ~= specials2 then
return specials1 > specials2
end
end

return glob1 < glob2
end

return globbing

0 comments on commit f89d76c

Please sign in to comment.