https://github.com/emik03/emik.analyzers.matches
Analyzer for compile-time parameter validation with the power of regex.
https://github.com/emik03/emik.analyzers.matches
Last synced: 5 months ago
JSON representation
Analyzer for compile-time parameter validation with the power of regex.
- Host: GitHub
- URL: https://github.com/emik03/emik.analyzers.matches
- Owner: Emik03
- License: mpl-2.0
- Created: 2023-04-20T16:42:38.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T21:44:21.000Z (almost 2 years ago)
- Last Synced: 2025-01-13T02:08:25.735Z (over 1 year ago)
- Language: C#
- Size: 102 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Emik.Analyzers.Matches
[](https://www.nuget.org/packages/Emik.Analyzers.Matches)
[](https://github.com/Emik03/Emik.Analyzers.Matches/blob/main/LICENSE)
Analyzer for compile-time parameter validation with the power of regex.
This project has a dependency to [Emik.Morsels](https://github.com/Emik03/Emik.Morsels), if you are building this
project, refer to its [README](https://github.com/Emik03/Emik.Morsels/blob/main/README.md) first.
---
- [Example](#example)
- [Lints](#lints)
- [Contribute](#contribute)
- [License](#license)
---
## Example
```csharp
[StringSyntax(StringSyntaxAttribute.Regex)]
const string ValidatorQuery = @"^\d{2}$";
[GeneratedRegex(ValidatorQuery)]
partial Regex Validator();
byte? Test([Emik.Match(ValidatorQuery)] string x)
{
if (!Validator().IsMatch(out var capture)) // OK
return null;
if (!Validator().IsMatch(out _, out var captureThatDoesNotExist)) // Error
return null;
if (!new Regex(x).IsMatch(out _)) // Warning: Not a constant
return null;
return byte.Parse(capture);
}
string TestTyped(X x) => x.Value;
record X(string Value)
{
public static implicit operator X([Match(@"^\w{3}$")] string value) => new(value);
}
Test("12"); // OK
Test("34"); // OK
TestTyped("foo"); // OK
TestTyped("bar"); // OK
Test("1"); // Error
Test("foobar"); // Error
TestTyped("12"); // Error
TestTyped("food"); // Error
Test(bool.FalseString); // Warning: Not a constant
TestTyped(bool.TrueString); // Warning: Not a constant
```
## Lints
| Id | Title |
|------------------------------------------------------------------------------------------------|------------------------------------------------------------|
| [EAM001](https://github.com/Emik03/Emik.Analyzers.Matches/tree/master/Documentation/EAM001.md) | Argument fails regex test |
| [EAM002](https://github.com/Emik03/Emik.Analyzers.Matches/tree/master/Documentation/EAM002.md) | Non-constant argument may fail regex test |
| [EAM003](https://github.com/Emik03/Emik.Analyzers.Matches/tree/master/Documentation/EAM003.md) | Argument regex test took too long |
| [EAM004](https://github.com/Emik03/Emik.Analyzers.Matches/tree/master/Documentation/EAM004.md) | Capture group doesn't exist |
| [EAM005](https://github.com/Emik03/Emik.Analyzers.Matches/tree/master/Documentation/EAM005.md) | Missing out declaration for capture group |
| [EAM006](https://github.com/Emik03/Emik.Analyzers.Matches/tree/master/Documentation/EAM006.md) | Non-constant Regex may have wrong number of capture groups |
## Contribute
Issues and pull requests are welcome to help this repository be the best it can be.
## License
This repository falls under the [MPL-2 license](https://www.mozilla.org/en-US/MPL/2.0/).