Skip to content

Commit d81fb2b

Browse files
author
Jose Alvarez
committed
perf(helpers): only validate opts on first run
1 parent 0bef68a commit d81fb2b

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

lua/null-ls/helpers.lua

+26-23
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,34 @@ local formats = {
5050
}
5151

5252
M.generator_factory = function(opts)
53+
local command, args, on_output, format, to_stderr, to_stdin, ignore_errors,
54+
check_exit_code = opts.command, opts.args, opts.on_output,
55+
opts.format, opts.to_stderr, opts.to_stdin,
56+
opts.ignore_errors, opts.check_exit_code
57+
58+
local _validated
59+
local validate_opts = function()
60+
validate({
61+
command = {command, "string"},
62+
args = {args, "table", true},
63+
on_output = {on_output, "function"},
64+
format = {
65+
format, function(a)
66+
return not a or vim.tbl_contains(vim.tbl_values(formats), a)
67+
end, "raw, line, or json"
68+
},
69+
to_stderr = {to_stderr, "boolean", true},
70+
to_stdin = {to_stdin, "boolean", true},
71+
ignore_errors = {ignore_errors, "boolean", true},
72+
check_exit_code = {check_exit_code, "function", true}
73+
})
74+
75+
_validated = true
76+
end
77+
5378
return {
5479
fn = function(params, done)
55-
local command, args, on_output, format, to_stderr, to_stdin,
56-
ignore_errors, check_exit_code = opts.command, opts.args,
57-
opts.on_output, opts.format,
58-
opts.to_stderr,
59-
opts.to_stdin,
60-
opts.ignore_errors,
61-
opts.check_exit_code
62-
63-
validate({
64-
command = {command, "string"},
65-
args = {args, "table", true},
66-
on_output = {on_output, "function"},
67-
format = {
68-
format, function(a)
69-
return not a or
70-
vim.tbl_contains(vim.tbl_values(formats), a)
71-
end, "raw, line, or json"
72-
},
73-
to_stderr = {to_stderr, "boolean", true},
74-
to_stdin = {to_stdin, "boolean", true},
75-
ignore_errors = {ignore_errors, "boolean", true},
76-
check_exit_code = {check_exit_code, "function", true}
77-
})
80+
if not _validated then validate_opts() end
7881

7982
local wrapper = function(error_output, output)
8083
if to_stderr then

test/spec/helpers_spec.lua

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ local test_utils = require("test.utils")
55

66
describe("helpers", function()
77
_G._TEST = true
8-
local helpers = require("null-ls.helpers")
8+
9+
stub(vim, "validate")
910

1011
local done = stub.new()
1112
local on_output = stub.new()
1213

1314
after_each(function()
1415
done:clear()
1516
on_output:clear()
17+
vim.validate:clear()
1618
end)
1719

20+
local helpers = require("null-ls.helpers")
1821
describe("json_output_wrapper", function()
1922
it("should throw error if json decode fails", function()
2023
local bad_json = "this is not json"
@@ -88,8 +91,26 @@ describe("helpers", function()
8891
end)
8992

9093
after_each(function()
91-
vim.cmd("bufdo! bwipeout!")
9294
loop.spawn:clear()
95+
96+
vim.cmd("bufdo! bwipeout!")
97+
end)
98+
99+
it("should validate opts on first run", function()
100+
local generator = helpers.generator_factory(generator_args)
101+
102+
generator.fn({})
103+
104+
assert.stub(vim.validate).was_called()
105+
end)
106+
107+
it("should not validate opts on subsequent runs", function()
108+
local generator = helpers.generator_factory(generator_args)
109+
110+
generator.fn({})
111+
generator.fn({})
112+
113+
assert.stub(vim.validate).was_called(1)
93114
end)
94115

95116
it("should set async to true", function()

0 commit comments

Comments
 (0)