Skip to content

Commit

Permalink
feat(extension,credo)!: configurable cli options and new default (#322)
Browse files Browse the repository at this point in the history
Previously, we hard coded the `--strict --all` cli options, but these
really shouldn't be the default.

You can also now pass in the cli options you'd like to use.
  • Loading branch information
mhanberg committed Nov 2, 2023
1 parent 6fda39e commit 34738f5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
6 changes: 4 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,8 @@ defmodule NextLS do

defmodule InitOpts.Extensions.Credo do
@moduledoc false
defstruct enable: true
defstruct enable: true,
cli_options: []
end

defmodule InitOpts.Extensions do
Expand Down Expand Up @@ -1136,7 +1137,8 @@ defmodule NextLS do
schema(NextLS.InitOpts.Extensions, %{
optional(:credo) =>
schema(NextLS.InitOpts.Extensions.Credo, %{
optional(:enable) => bool()
optional(:enable) => bool(),
optional(:cli_options) => list(str())
}),
optional(:elixir) =>
map(%{
Expand Down
2 changes: 1 addition & 1 deletion lib/next_ls/extensions/credo_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule NextLS.CredoExtension do

task =
Task.Supervisor.async_nolink(state.task_supervisor, fn ->
case Runtime.call(runtime, {:_next_ls_private_credo, :issues, [path]}) do
case Runtime.call(runtime, {:_next_ls_private_credo, :issues, [state.settings.cli_options, path]}) do
{:ok, issues} -> issues
_error -> []
end
Expand Down
5 changes: 3 additions & 2 deletions priv/monkey/_next_ls_private_credo.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule :_next_ls_private_credo do
@moduledoc false

def issues(dir) do
["--strict", "--all", "--working-dir", dir]
def issues(args, dir) do
args
|> Kernel.++(["--working-dir", dir])
|> Credo.run()
|> Credo.Execution.get_issues()
end
Expand Down
56 changes: 47 additions & 9 deletions test/next_ls/extensions/credo_extension_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule NextLS.CredoExtensionTest do
File.write!(foo, """
defmodule Foo do
def run() do
:ok
dbg(:ok)
end
end
""")
Expand All @@ -39,7 +39,7 @@ defmodule NextLS.CredoExtensionTest do
setup :with_lsp

@tag init_options: %{"extensions" => %{"credo" => %{"enable" => false}}}
test "disables Credo", %{client: client, foo: foo} = context do
test "disables Credo", %{client: client} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

assert_is_ready(context, "my_proj")
Expand All @@ -51,6 +51,44 @@ defmodule NextLS.CredoExtensionTest do
}
end

@tag init_options: %{"extensions" => %{"credo" => %{"cli_options" => ["--only", "warning"]}}}
test "configures cli options", %{client: client, foo: foo} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

assert_is_ready(context, "my_proj")
assert_compiled(context, "my_proj")

assert_notification "window/logMessage", %{
"message" => "[NextLS] [extension] Credo initializing with options" <> _,
"type" => 4
}

uri = uri(foo)

assert_notification "textDocument/publishDiagnostics", %{
"uri" => ^uri,
"diagnostics" => [
%{
"code" => "Credo.Check.Warning.Dbg",
"codeDescription" => %{
"href" => "https://hexdocs.pm/credo/Credo.Check.Warning.Dbg.html"
},
"data" => %{
"check" => "Elixir.Credo.Check.Warning.Dbg",
"file" => "lib/foo.ex"
},
"message" => "There should be no calls to dbg.",
"range" => %{
"end" => %{"character" => 999, "line" => 2},
"start" => %{"character" => 4, "line" => 2}
},
"severity" => 2,
"source" => "credo"
}
]
}
end

test "publishes credo diagnostics", %{client: client, foo: foo} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

Expand All @@ -69,20 +107,20 @@ defmodule NextLS.CredoExtensionTest do
"uri" => ^uri,
"diagnostics" => [
%{
"code" => "Credo.Check.Readability.ParenthesesOnZeroArityDefs",
"code" => "Credo.Check.Warning.Dbg",
"codeDescription" => %{
"href" => "https://hexdocs.pm/credo/Credo.Check.Readability.ParenthesesOnZeroArityDefs.html"
"href" => "https://hexdocs.pm/credo/Credo.Check.Warning.Dbg.html"
},
"data" => %{
"check" => "Elixir.Credo.Check.Readability.ParenthesesOnZeroArityDefs",
"check" => "Elixir.Credo.Check.Warning.Dbg",
"file" => "lib/foo.ex"
},
"message" => "Do not use parentheses when defining a function which has no arguments.",
"message" => "There should be no calls to dbg.",
"range" => %{
"end" => %{"character" => 999, "line" => 1},
"start" => %{"character" => 0, "line" => 1}
"end" => %{"character" => 999, "line" => 2},
"start" => %{"character" => 4, "line" => 2}
},
"severity" => 3,
"severity" => 2,
"source" => "credo"
},
%{
Expand Down

0 comments on commit 34738f5

Please sign in to comment.