uber-go / gopatch

Refactoring and code transformation tool for Go.
MIT License
832 stars 33 forks source link

support patches with comments #165

Open ccoVeille opened 5 days ago

ccoVeille commented 5 days ago

I'm trying to use patch to perform something like this

@@
var f identifier
@@
-func f(t *testing.T) {
+func f(t *testing.T) {
+   t.Helper() // my comment
+
+   t.Helper()
    ...
}

This code is part of a bigger patch and code reorganization. So this is a code to replicate the issue/question I want to talk about.

My issues right now are that:

I would like to know if I'm not missing something obvious

ccoVeille commented 5 days ago

Please note that

@@
var f identifier
@@
-func f(t *testing.T) {
+func f(t *testing.T) {
+   t.Helper()
+
+   t.Helper()
    ...
}

will rewrite this

func myTestHelper(t *testing) {
   require.Fail(t)
}

to

func myTestHelper(t *testing) {
   t.Helper()
   t.Helper()
   require.Fail(t)
}

So no line feed

Once again, please note it's pseudo code

abhinav commented 4 days ago

Hey @ccoVeille. gopatch is limited in that aspect: it only transforms significant AST nodes. It could probably do something smarter about them, but the original design left out comments and newlines with more focus on just transforming code.

ccoVeille commented 4 days ago

I was thinking about using the tool to add //nolint somelinter // reason on some pattern I have in my code base.

I would love if comment could be supported.

I would prefer if it was possible, but I can leave without new lines.

Comment is a bit more painful