Skip to content

Fix prefixing unused variables with underscore#778

Closed
sheldak wants to merge 2 commits intoelixir-lsp:masterfrom
Qarma-inspect:fix-prefix-with-underscore
Closed

Fix prefixing unused variables with underscore#778
sheldak wants to merge 2 commits intoelixir-lsp:masterfrom
Qarma-inspect:fix-prefix-with-underscore

Conversation

@sheldak
Copy link
Contributor

@sheldak sheldak commented Nov 30, 2022

Since I noticed some issues with this feature and an attempt to revert it (#775), I would like to propose a fix that hopefully makes it more usable.

Instead of adding _ at the beginning of the line, I would replace the unused variable var with _var using String.replace/4 with the following regex:

"(?<![[:alnum:]._])#{variable}(?![[:alnum:]._])"

I assume the unused variable does not have any of

  • .
  • _
  • letters or numbers

as a prefix or suffix.

Also, to prevent replacing incorrectly in the cases like:

def foo do
  var1 = Enum.map([1, 2], fn var1 -> var1 + 1 end) # we don't want to replace inner `var1` 
  var2 = " var2 " # we don't want to replace the string `"var2"`
end

I'm not creating a quickfix if there is more than one match in the line.

|> SourceFile.lines()
|> Enum.at(start_line)

pattern = Regex.compile!("(?<![[:alnum:]._])#{variable}(?![[:alnum:]._])")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Regex.escape/1 on variable. Elixir variables can have non regex friendly chars !?.

@lukaszsamson
Copy link
Collaborator

Thanks for the PR but this will need to be solved properly by AST manipulation

@sheldak sheldak deleted the fix-prefix-with-underscore branch April 5, 2023 08:22
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.

2 participants