Closed sobolevn closed 4 years ago
I would like to work on this issue
Awesome! @dinolinjob you are on!
I understood that I have to create a violation in https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/violations/refactoring.py.
Should I raise an error as soon as I come across some line consisting x += x + 1
exists?
Does it come under Tokenize violation?
What are the further steps to complete writing this rule?
@dinolinjob it is an ASTViolation
. Then you should create a method inside visitors/ast
to find these cases.
@sobolevn should I write a new visitor?
Try to write a _check_*
method first on an existing visitor to check the logic. Then we can decide on a new visitor.
@sobolevn In which file under visitors/ast/
should I write my _check_*
method?
visitors/ast/statements.py
@sobolevn I am trying to do a TDD, i have written a test in test_statements.py . But when i run it inside the docker image, it takes the installed wemake_python_styleguide. How do one configure the tests to run?
@viveksoundrapandi oh, I must document this. This Dockerfile
is for GithubActions, not the local development.
Ahh! Thanks for the clarification
@sobolevn Can you suggest me some ideas on how to approach while writing this rule. Is there a way to compare variables before and after assignment operators?
Yes, you need to visit ast.AugAssign
and fetch variables from the right part. If the left variable is contained in the right part - then we raise a violation.
That's how ast.AugAssign
looks like for this simple case:
» echo 'x += x + 1' > ex.py
» python scripts/parse.py ex.py
AugAssign(target=Name(id='x', ctx=Store(), lineno=1, col_offset=0), op=Add(), value=BinOp(left=Name(id='x', ctx=Load(), lineno=1, col_offset=5), op=Add(), right=Num(n=1, lineno=1, col_offset=9), lineno=1, col_offset=5), lineno=1, col_offset=0)
Rule request
Thesis
We need to forbid code like:
It is most likely an error. Refactor it to:
x += 1
x *= 2; x += 1
https://rust-lang.github.io/rust-clippy/master/index.html#misrefactored_assign_op Related: #789
Reasoning
This is a refactoring rule.