-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve regex integration with DisableSyntax #907
Changes from 7 commits
0a78e24
10ba343
3ac05ef
b562db2
8ad537d
64e39aa
ff24832
fb0198b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,40 @@ println( | |
scalafix.website.rule("DisableSyntax", DisableSyntaxConfig.default) | ||
) | ||
``` | ||
|
||
## Regex | ||
|
||
Regex patterns have 3 available ways to be configured. The example below shows 1 of each way. | ||
|
||
```hocon | ||
DisableSyntax.regex = [ | ||
{ | ||
id = offensive | ||
pattern = "[Pp]imp" | ||
message = "Please consider a less offensive word than ${0} such as Extension" | ||
} | ||
"Await\\.result" | ||
{ | ||
id = magicNumbers | ||
regex = { | ||
pattern = "(?:(?:[,(]\\s)|(?:^\\s*))+(\\d+(\\.\\d+)?)" | ||
captureGroup = 1 | ||
} | ||
message = "Numbers ({$1} in this instance) should always have a named parameter attached, or be assigned to a val." | ||
} | ||
] | ||
``` | ||
|
||
1. The first way has a simple object providing an `id`, `pattern`, and `message`. | ||
2. The second way is just the pattern. When this is used, the `id` is set equal | ||
to the pattern, and a generic message is provided for you. | ||
3. The third way allows you to specify what capture-group the problematic thing | ||
is in, in case your regex is complicated. | ||
|
||
### Error Messages | ||
|
||
Error messages have access to the capture groups of the regex. Simply use `{$n}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
where `n` is the index of the capture group you wish to appear in that part of | ||
the message. | ||
|
||
You can see this used in the 3rd example. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package scalafix.config | ||
|
||
import metaconfig.Conf | ||
import metaconfig.ConfDecoder | ||
import java.util.regex.Pattern | ||
import scalafix.internal.config.ScalafixMetaconfigReaders._ | ||
|
||
class Regex( | ||
val pattern: Pattern, | ||
val captureGroup: Option[Int] | ||
) | ||
|
||
object Regex { | ||
implicit val regexDecoder: ConfDecoder[Regex] = | ||
ConfDecoder.instance[Regex] { | ||
case obj: Conf.Obj => | ||
(obj.get[Pattern]("pattern") |@| obj.getOption[Int]("captureGroup")) | ||
.map { | ||
case (pattern, groupIndex) => new Regex(pattern, groupIndex) | ||
} | ||
} | ||
|
||
implicit val customMessageRegexDecoder: ConfDecoder[CustomMessage[Regex]] = | ||
CustomMessage.decoder[Regex](field = "regex") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,17 @@ DisableSyntax.noFinalize = true | |
DisableSyntax.regex = [ | ||
{ | ||
id = offensive | ||
pattern = "[P|p]imp" | ||
pattern = "[Pp]imp" | ||
message = "Please consider a less offensive word such as Extension" | ||
} | ||
{ | ||
id = magicNumbers | ||
regex = { | ||
pattern = "(?:(?:[,(]\\s)|(?:^\\s*))+(\\d+(\\.\\d+)?)" | ||
captureGroup = 1 | ||
} | ||
message = "Numbers ({$1} in this instance) should always have a named parameter attached, or be assigned to a val." | ||
} | ||
"Await\\.result" | ||
] | ||
*/ | ||
|
@@ -52,8 +60,12 @@ case object DisableSyntaxBase { | |
def -(other: String): String = s"$value - $other" | ||
} | ||
|
||
5 // assert: DisableSyntax.magicNumbers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we assert the contents of the message here? 5 /* assert: DisableSyntax.magicNumbers
^
Numbers (5 in this instance) should always ...
*/ See end of this section https://scalacenter.github.io/scalafix/docs/developers/tutorial.html#use-diagnostic-to-report-linter-errors |
||
|
||
val fortyTwo = 42 | ||
val someDays = 75.days | ||
// actually 7.5 million years | ||
Await.result(Future(42), 75.days) // assert: DisableSyntax.Await\.result | ||
Await.result(Future(fortyTwo), someDays) // assert: DisableSyntax.Await\.result | ||
|
||
override def finalize(): Unit = println("exit") // assert: DisableSyntax.noFinalize | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/simple//