Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
refactor(sources): warn and prevent registration if unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Alvarez committed Nov 12, 2021
1 parent 8e5d909 commit a3406ba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lua/null-ls/sources.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local methods = require("null-ls.methods")
local u = require("null-ls.utils")

local validate = vim.validate

Expand Down Expand Up @@ -153,6 +154,11 @@ M.validate_and_transform = function(source)
method_map[method] = true
end

if method_map[methods.internal.DIAGNOSTICS_ON_SAVE] and not u.has_version("0.6.0") then
u.echo("WarningMsg", string.format("source %s is not supported on nvim versions < 0.6.0", name))
return
end

return {
name = name,
generator = generator,
Expand Down
16 changes: 16 additions & 0 deletions test/spec/sources_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local stub = require("luassert.stub")
local mock = require("luassert.mock")

local methods = require("null-ls.methods")
local u = mock(require("null-ls.utils"), true)

describe("sources", function()
local sources = require("null-ls.sources")
Expand Down Expand Up @@ -270,6 +272,7 @@ describe("sources", function()
describe("validate_and_transform", function()
local mock_source
before_each(function()
u.has_version.returns(true)
mock_source = {
generator = { fn = function() end, opts = {}, async = false },
name = "mock generator",
Expand All @@ -278,6 +281,10 @@ describe("sources", function()
}
end)

after_each(function()
u.has_version:clear()
end)

it("should validate and return transformed source", function()
local validated = sources.validate_and_transform(mock_source)

Expand Down Expand Up @@ -344,6 +351,15 @@ describe("sources", function()
assert.falsy(validated)
end)

it("should return nil if nvim version does not support method", function()
mock_source.method = methods.internal.DIAGNOSTICS_ON_SAVE
u.has_version.returns(false)

local validated = sources.validate_and_transform(mock_source)

assert.falsy(validated)
end)

it("should throw if generator is invalid", function()
mock_source.generator = nil

Expand Down

0 comments on commit a3406ba

Please sign in to comment.