LintFix change multiple parts with pattern

311 views
Skip to first unread message

Saman Sattari

unread,
Jan 31, 2022, 8:48:09 AM1/31/22
to lint-dev
I have a code like below:
notif("Hello", 2000).show()
but I can also use it like below:
val n = notif("Hello"2000)
n.show()
So I made another fun that simplifies the first one into this:
showNotif("Hello"2000)
Now I have added a lint rule that detects the first one and warns the user to change it to showNotif. but I don't know how to make a LintFix to that. I tried this:
LintFix.create()
             .name("Use `showNotif()` function")
             .replace()
             .pattern("notif|show")
             .with("showToast\k")
             .shortenNames()
             .reformat(true)
             .robot(true)
             .independent(false)
             .build()
But It gives me this error: Illegal escape: \k
So how can I change multiple parts of a code?

Róbert Papp

unread,
Feb 8, 2022, 4:15:54 AM2/8/22
to lint-dev
Hi, if I understand your use case correctly: from a quick glance at the LintFix class, looks like you can do this:
LintFix.create().group()
     .type(COMPOSITE)
    .add(LintFix.create()....notif...build())
    .add(LintFix.create()....show...build())
    .build()

but it might not do what you want, I think you're better off detecting the two patterns separately and providing different fixes.
This is because notif and show looks like two different methods, i.e. show() is just empty params, so you need to do more AST transform to get to showNotif(...) usage (guess that's what `with` was meant to be).

Reply all
Reply to author
Forward
0 new messages