Instagram / LibCST

A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
https://libcst.readthedocs.io/
Other
1.52k stars 183 forks source link

Codemod to convert assignments to augmented assignments #541

Open elfring opened 2 years ago

elfring commented 2 years ago

:eyes: Some source code analysis tools can help to find opportunities for improving software components. :thought_balloon: I propose to increase the usage of augmented assignment statements accordingly.

Would you like to integrate anything from a transformation result which can be generated by a command like the following? (:point_right: Please check also for questionable change suggestions because of an evolving search pattern.)

lokal> perl -p -i.orig -0777 -e 's/^(?<indentation>\s+)(?<target>\S+)\s*=\s*\k<target>[ \t]*(?<operator>[+\-%&|^@]|\*\*?|\/\/?|<<|>>)/$+{indentation}$+{target} $+{operator}=/gm' $(find ~/Aktivitäten/LibCST/lokal -name '*.py')

How do you think about to improve nine source code places? :thinking:


:crystal_ball: How would a corresponding transformation (a “codemod”) be achieved by the means of the software “LibCST”?

zsol commented 2 years ago

If I understand this correctly, you're proposing a codemod that replaces foo = foo + bar style assignments with foo += bar? That's totally doable with LibCST and if you want to write a PR I'm happy to review

elfring commented 2 years ago

Some programming languages support operators with a special property:

Thus corresponding change possibilities can occasionally be reconsidered, can't they? :thinking: