From d9b2ea890107d10c837052c1077075d82b660972 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Wed, 1 Nov 2023 21:34:44 -0400 Subject: [PATCH] feat!(extension,credo): configurable cli options and new default (#322) 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. --- lib/next_ls.ex | 6 +- lib/next_ls/extensions/credo_extension.ex | 2 +- priv/monkey/_next_ls_private_credo.ex | 5 +- .../extensions/credo_extension_test.exs | 56 ++++++++++++++++--- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 62ddb045..f10756fe 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -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 @@ -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(%{ diff --git a/lib/next_ls/extensions/credo_extension.ex b/lib/next_ls/extensions/credo_extension.ex index aa5e89b1..2d21ac48 100644 --- a/lib/next_ls/extensions/credo_extension.ex +++ b/lib/next_ls/extensions/credo_extension.ex @@ -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 diff --git a/priv/monkey/_next_ls_private_credo.ex b/priv/monkey/_next_ls_private_credo.ex index 19958c32..6f3287b6 100644 --- a/priv/monkey/_next_ls_private_credo.ex +++ b/priv/monkey/_next_ls_private_credo.ex @@ -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 diff --git a/test/next_ls/extensions/credo_extension_test.exs b/test/next_ls/extensions/credo_extension_test.exs index fc302a85..e050d67d 100644 --- a/test/next_ls/extensions/credo_extension_test.exs +++ b/test/next_ls/extensions/credo_extension_test.exs @@ -23,7 +23,7 @@ defmodule NextLS.CredoExtensionTest do File.write!(foo, """ defmodule Foo do def run() do - :ok + dbg(:ok) end end """) @@ -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") @@ -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: %{}}) @@ -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" }, %{