sketch-hq / nicene

Additional Credo checks
MIT License
39 stars 7 forks source link

Parsing error while running ConsistentFunctionDefinitions with macros #16

Open halostatue opened 4 years ago

halostatue commented 4 years ago

I’m checking out Nicene for some of our code and I’m seeing some errors. The first error is preventing Credo from completing:

** (config) Error while running Elixir.Nicene.ConsistentFunctionDefinitions on test/support/fixtures/map_attribute.ex
08:14:08.208 [error] Task #PID<0.1043.0> started from #PID<0.93.0> terminating
** (Protocol.UndefinedError) protocol String.Chars not implemented for {:unquote, [line: 18, column: 11], [{:json_attrs, [line: 18, column: 19], nil}]} of type Tuple
    (elixir 1.10.4) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir 1.10.4) lib/string/chars.ex:22: String.Chars.to_string/1
    lib/consistent_function_definitions.ex:42: Nicene.ConsistentFunctionDefinitions.process_line/3
    (elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
    lib/consistent_function_definitions.ex:19: Nicene.ConsistentFunctionDefinitions.run/2
    lib/credo/check/runner.ex:168: Credo.Check.Runner.do_run_check_on_single_source_file/3
    lib/credo/check/runner.ex:161: Credo.Check.Runner.run_check_on_single_source_file/3
    (elixir 1.10.4) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
Function: #Function<9.9572610/0 in Credo.Check.Runner.run/2>
    Args: []

I modified this to print out some details of the line:

      IO.inspect({
        Process.get(:source_file),
        line_no,
        line,
        Process.get(:position),
        funs[line_no]
      })

This is what I see:

{%SourceFile<test/support/fixtures/map_attribute.ex>, 18,
 "      def unquote(json_attrs)(merge \\\\ %{}) do", 1,
 {:unquote, [line: 18, column: 11],
  [{:json_attrs, [line: 18, column: 19], nil}]}}
{%SourceFile<lib/lowes_loyalty/models/tokens/macros.ex>, 191,
 "      def unquote(names.find)(%User{id: id}), do: unquote(names.find)(%{user_id: id})",
 1,
 {:unquote, [line: 191, column: 11],
  [
    {{:., [line: 191, column: 24],
      [{:names, [line: 191, column: 19], nil}, :find]},
     [no_parens: true, line: 191, column: 24], []}
  ]}}

So the problem here is in funs[line_no] not producing something that can be interpolated into the regex at line 40.

I’ll try to work a unit test that reproduces this, but it will take some time. (I’m seeing a similar, but different, error in FileTopToBottom. I will file a different report on that when I am clearer on what the exact issue is with that test.)

halostatue commented 4 years ago

Note that this is related to the use of unquote(value) in a macro quote block as the two failing unit tests on #18 demonstrate.