RegexChecker (Groovy 5.0.0)

Package: groovy.typecheckers

## [Groovy] Class RegexChecker

- groovy.typecheckers.RegexChecker

-     @Incubating
      class RegexChecker
      extends TypeCheckingDSL

  Checks at compile-time for cases of invalid regex usage where the actual regex string can be identified (e.g. inline or defined by a local variable with an initial value or a field with an initial value). A number of errors which would normally surface only at runtime are handled.

  - Invalid pattern definitions when using the Groovy pattern operator or the JDK's `Pattern#compile` method:

             ~/\w{3/               // missing closing repetition quantifier brace
             ~"(.)o(.*"            // missing closing group bracket
             Pattern.compile(/?/)  // dangling meta character '?' (Java longhand)

    These examples illustrate explicitly defined constant strings but local variable or field definitions where a constant string can be determined are also checked.
  - Invalid regex strings in conjunction with Groovy's regex find and regex match expressions, or the JDK's `Pattern#matches` method:

             'foobar'  =~ /f[o]{2/        // missing closing repetition quantifier brace
             'foobar' ==~ /(foo/          // missing closing group bracket
             Pattern.matches(/?/, 'foo')  // dangling meta character '?' (Java longhand)

  Invalid group count terms where the regex string can be determined and the group index can be determined to be a constant.

  -          def m = 'foobar' =~ /(...)(...)/
             assert m[0][1] == 'foo'     // okay
             assert m[0][3]              // type error: only two groups in regex

    And similarly for Java long-hand variants:

             Pattern p = Pattern.compile('(...)(...)')
             Matcher m = p.matcher('foobar')
             assert m.find()
             assert m.group(1) == 'foo'  // okay
             assert m.group(3)           // type error: only two groups in regex

  Also, when using the regex find operator, smarter type inferencing occurs. For an expression like `matcher[0]`, the `getAt` extension method for `Matcher` is called. This may return a String (if no groups occur within the regex) or a list of String values if grouping is used, hence the declared return type of the mentioned `getAt` method is `Object` to account for these two possibilities. When using `RegexChecker`, the inferred type will be either `String` or `List<String>` when a regex string can be identified. Over time, the idea would be to support more cases as per: https://checkerframework.org/manual/#regex-checker https://homes.cs.washington.edu/~mernst/pubs/regex-types-ftfjp2012.pdf https://github.com/typetools/checker-framework/tree/master/checker/src/main/java/org/checkerframework/checker/regex

- - ### Constructor Summary

    Constructors
    Constructor and description
    `RegexChecker()`

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `Object` | `run()`

- - ### Constructor Detail

    - #### RegexChecker()

  - ### Method Detail

    - #### @Override
      Object run()