Skip to content

Commit 25b3f63

Browse files
committed
add param validation
1 parent 21afd79 commit 25b3f63

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

lua/plenary/path2.lua

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,19 @@ function Path.is_path(x)
537537
return getmetatable(x) == Path
538538
end
539539

540+
---@param x any
541+
---@return boolean
542+
local function is_path_like(x)
543+
return type(x) == "string" or Path.is_path(x)
544+
end
545+
546+
local function is_path_like_opt(x)
547+
if x == nil then
548+
return true
549+
end
550+
return is_path_like(x)
551+
end
552+
540553
---@return boolean
541554
function Path:is_absolute()
542555
if self.root == "" then
@@ -710,6 +723,8 @@ end
710723
---@param to plenary.Path2|string path to compare to
711724
---@return boolean
712725
function Path:is_relative(to)
726+
vim.validate { to = { to, is_path_like } }
727+
713728
if not Path.is_path(to) then
714729
to = Path:new(to)
715730
end
@@ -739,6 +754,11 @@ end
739754
---@param walk_up boolean? walk up to the provided path using '..' (default: `false`)
740755
---@return string
741756
function Path:make_relative(to, walk_up)
757+
vim.validate {
758+
to = { to, is_path_like_opt },
759+
walk_up = { walk_up, "b", true },
760+
}
761+
742762
-- NOTE: could probably take some shortcuts and avoid some `Path:new` calls
743763
-- by allowing _WindowsPath/_PosixPath handle this individually.
744764
-- As always, Windows root complicates things, so generating a new Path often
@@ -800,6 +820,11 @@ end
800820
---@param excludes integer[]?
801821
---@return string
802822
function Path:shorten(len, excludes)
823+
vim.validate {
824+
len = { len, "n", true },
825+
excludes = { excludes, "t", true },
826+
}
827+
803828
len = vim.F.if_nil(len, 1)
804829
excludes = vim.F.if_nil(excludes, { #self.relparts })
805830

@@ -825,6 +850,12 @@ end
825850
---@param opts plenary.Path2.mkdirOpts?
826851
function Path:mkdir(opts)
827852
opts = opts or {}
853+
vim.validate {
854+
mode = { opts.mode, "n", true },
855+
parents = { opts.parents, "b", true },
856+
exists_ok = { opts.exists_ok, "b", true },
857+
}
858+
828859
opts.mode = vim.F.if_nil(opts.mode, 511)
829860
opts.parents = vim.F.if_nil(opts.parents, false)
830861
opts.exists_ok = vim.F.if_nil(opts.exists_ok, false)
@@ -877,6 +908,10 @@ end
877908
---@param opts plenary.Path2.touchOpts?
878909
function Path:touch(opts)
879910
opts = opts or {}
911+
vim.validate {
912+
mode = { opts.mode, "n", true },
913+
parents = { opts.parents, { "n", "b" }, true },
914+
}
880915
opts.mode = vim.F.if_nil(opts.mode, 438)
881916
opts.parents = vim.F.if_nil(opts.parents, false)
882917

@@ -912,6 +947,7 @@ end
912947
---@param opts plenary.Path2.rmOpts?
913948
function Path:rm(opts)
914949
opts = opts or {}
950+
vim.validate { recursive = { opts.recursive, "b", true } }
915951
opts.recursive = vim.F.if_nil(opts.recursive, false)
916952

917953
if not opts.recursive or not self:is_dir() then
@@ -950,6 +986,8 @@ end
950986
---@param opts plenary.Path2.renameOpts
951987
---@return plenary.Path2
952988
function Path:rename(opts)
989+
vim.validate { new_name = { opts.new_name, is_path_like } }
990+
953991
if not opts.new_name or opts.new_name == "" then
954992
error "Please provide the new name!"
955993
end
@@ -979,6 +1017,12 @@ end
9791017
---@param opts plenary.Path2.copyOpts
9801018
---@return {[plenary.Path2]: {success:boolean, err: string?}} # indicating success of copy; nested tables constitute sub dirs
9811019
function Path:copy(opts)
1020+
vim.validate {
1021+
destination = { opts.destination, is_path_like },
1022+
recursive = { opts.recursive, "b", true },
1023+
override = { opts.override, "b", true },
1024+
}
1025+
9821026
opts.recursive = vim.F.if_nil(opts.recursive, false)
9831027
opts.override = vim.F.if_nil(opts.override, true)
9841028

@@ -996,6 +1040,13 @@ function Path:copy(opts)
9961040
error(string.format("Warning: %s was not copied as `recursive=false`", self:absolute()))
9971041
end
9981042

1043+
vim.validate {
1044+
respect_gitignore = { opts.respect_gitignore, "b", true },
1045+
hidden = { opts.hidden, "b", true },
1046+
parents = { opts.parents, "b", true },
1047+
exists_ok = { opts.exists_ok, "b", true },
1048+
}
1049+
9991050
opts.respect_gitignore = vim.F.if_nil(opts.respect_gitignore, false)
10001051
opts.hidden = vim.F.if_nil(opts.hidden, true)
10011052
opts.parents = vim.F.if_nil(opts.parents, false)
@@ -1028,6 +1079,8 @@ end
10281079
---@param callback fun(data: string)? callback to use for async version, nil for default
10291080
---@return string? data
10301081
function Path:read(callback)
1082+
vim.validate { callback = { callback, "f", true } }
1083+
10311084
if not self:is_file() then
10321085
error(string.format("'%s' is not a file", self:absolute()))
10331086
end
@@ -1117,9 +1170,7 @@ end
11171170
---@param lines integer? number of lines to read from the head of the file (default: `10`)
11181171
---@return string data
11191172
function Path:head(lines)
1120-
vim.validate {
1121-
lines = { lines, "n", true },
1122-
}
1173+
vim.validate { lines = { lines, "n", true } }
11231174

11241175
local stat = self:_get_readable_stat()
11251176

@@ -1174,9 +1225,7 @@ end
11741225
---@param lines integer? number of lines to read from the tail of the file (default: `10`)
11751226
---@return string data
11761227
function Path:tail(lines)
1177-
vim.validate {
1178-
lines = { lines, "n", true },
1179-
}
1228+
vim.validate { lines = { lines, "n", true } }
11801229

11811230
local stat = self:_get_readable_stat()
11821231

@@ -1265,6 +1314,7 @@ function Path:write(data, flags, mode)
12651314
return b
12661315
end
12671316

1317+
--- iterate over contents in the current path recursive
12681318
---@param top_down boolean? walk from current path down (default: `true`)
12691319
---@return fun(): plenary.Path2?, string[]?, string[]? # iterator which yields (dirpath, dirnames, filenames)
12701320
function Path:walk(top_down)

0 commit comments

Comments
 (0)