Skip to content

Fix crash on extract variable in function reference capture - #36

Open
katafrakt wants to merge 1 commit into
gp-pereira:mainfrom
katafrakt:fix-extract-variable-capture-arity
Open

katafrakt wants to merge 1 commit into
gp-pereira:mainfrom
katafrakt:fix-extract-variable-capture-arity

Conversation

@katafrakt

Copy link
Copy Markdown

When ExtractVariable refactor is invoked on this code:

defmodule Foo do
  def all_atoms?(list) do
    Enum.all?(list, &is_atom/1)
  end
end

with either "is_atom" or "1" selected, Refactorex crashes with exception

14:37:07.906 [error] Task #PID<0.4879.0> started from #PID<0.4855.0> terminating
** (FunctionClauseError) no function clause matching in XPSourceror.Zipper.replace/2
    (xp_sourceror 1.10.1) lib/sourceror/zipper.ex:340: XPSourceror.Zipper.replace(nil, {:extracted_variable, [], nil})
    (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:146: XPRefactorex.Refactor.Variable.ExtractVariable.replace_selection_by_variable/3
    (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:104: XPRefactorex.Refactor.Variable.ExtractVariable.extract_and_assign/4
    (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:41: XPRefactorex.Refactor.Variable.ExtractVariable.refactor/2
    (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:2: XPRefactorex.Refactor.Variable.ExtractVariable.visit/4
    (xp_sourceror 1.10.1) lib/sourceror/zipper.ex:612: XPSourceror.Zipper.do_traverse_while/3
    (xp_sourceror 1.10.1) lib/sourceror/zipper.ex:607: XPSourceror.Zipper.traverse_while/3
    (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:2: XPRefactorex.Refactor.Variable.ExtractVariable.execute/2
Function: &:erlang.apply/2
    Args: [#Function<0.114616880/1 in XPRefactorex.Refactor.available_refactorings/4>, [XPRefactorex.Refactor.Variable.ExtractVariable]]

or

15:05:10.958 instance_id=19E641EF850 [error] an exception was raised:
    ** (FunctionClauseError) no function clause matching in XPRefactorex.Refactor.Variable.ExtractVariable.refactor/2
        (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:37: XPRefactorex.Refactor.Variable.ExtractVariable.refactor(nil, {:defmodule, [trailing_comments: [], leading_comments: [], end_of_expression: [newlines: 1, line: 5, column: 4], do: [line: 1, column: 15], end: [line: 5, column: 1], line: 1, column: 1], [{:__aliases__, [trailing_comments: [], leading_comments: [], last: [line: 1, column: 11], line: 1, column: 11], [:Foo]}, [{{:__block__, [trailing_comments: [], leading_comments: [], line: 1, column: 15], [:do]}, {:def, [trailing_comments: [], leading_comments: [], end_of_expression: [newlines: 1, line: 4, column: 6], do: [line: 2, column: 24], end: [line: 4, column: 3], line: 2, column: 3], [{:all_atoms?, [trailing_comments: [], leading_comments: [], closing: [line: 2, column: 22], line: 2, column: 7], [{:list, [trailing_comments: [], leading_comments: [], line: 2, column: 18], nil}]}, [{{:__block__, [trailing_comments: [], leading_comments: [], line: 2, column: 24], [:do]}, {{:., [trailing_comments: [], line: 3, column: 9], [{:__aliases__, [trailing_comments: [], ...], ...}, ...]}, ...}}]]}}]]})
        (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:2: XPRefactorex.Refactor.Variable.ExtractVariable.visit/4
        (xp_sourceror 1.10.1) lib/sourceror/zipper.ex:612: XPSourceror.Zipper.do_traverse_while/3
        (xp_sourceror 1.10.1) lib/sourceror/zipper.ex:607: XPSourceror.Zipper.traverse_while/3
        (xp_refactorex 0.1.52) lib/refactorex/refactor/variable/extract_variable.ex:2: XPRefactorex.Refactor.Variable.ExtractVariable.execute/2
        (xp_refactorex 0.1.52) lib/refactorex/refactor.ex:115: anonymous fn/4 in XPRefactorex.Refactor.available_refactorings/4
        (elixir 1.19.4) lib/task/supervised.ex:105: Task.Supervised.invoke_mfa/2
        (elixir 1.19.4) lib/task/supervised.ex:40: Task.Supervised.reply/4

This PR fixes these failures by checking if we are in such a function reference capture.

doorgan added a commit to expert-lsp/expert that referenced this pull request Jul 1, 2026
Close #678 
Fix #612 
Fix #481

Ports Refactorex code into expert, under the Forge application, so we
can fix issues here faster and improve expert-specific integration. This
changes several pieces of code from Refactorex where we already have
code for it, like diffs.

It also ports the following unmerged PRs from refactorex:
- gp-pereira/refactorex#37
- gp-pereira/refactorex#36
- gp-pereira/refactorex#35
- gp-pereira/refactorex#32
- gp-pereira/refactorex#34

cc @gp-pereira to review proper license attribution, there are lots of
files so I followed the VSCode approach of having a THIRD_PARTY_NOTICES
file and a small header on each ported file

PS: apologies to reviewers for the thousand lines PRs I've been pumping
lately

---------

Co-authored-by: Paweł Świątkowski <katafrakt@vivaldi.net>
doorgan added a commit to expert-lsp/expert that referenced this pull request Jul 1, 2026
Close #678
Fix #612
Fix #481

Ports Refactorex code into expert, under the Forge application, so we
can fix issues here faster and improve expert-specific integration. This
changes several pieces of code from Refactorex where we already have
code for it, like diffs.

It also ports the following unmerged PRs from refactorex:
- gp-pereira/refactorex#37
- gp-pereira/refactorex#36
- gp-pereira/refactorex#35
- gp-pereira/refactorex#32
- gp-pereira/refactorex#34

cc @gp-pereira to review proper license attribution, there are lots of
files so I followed the VSCode approach of having a THIRD_PARTY_NOTICES
file and a small header on each ported file

PS: apologies to reviewers for the thousand lines PRs I've been pumping
lately

(cherry picked from commit c27bf61)

Co-authored-by: Paweł Świątkowski <katafrakt@vivaldi.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant