-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from earloc/feature/typed-parameters
typed parameters
- Loading branch information
Showing
16 changed files
with
428 additions
and
117 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...010_TargetProjectRootDirectoryNotFound.md → ...001_TargetProjectRootDirectoryNotFound.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../TYPEALIZR001010_AmbigiousRessourceKey.md → ...reference/TR0002_AmbigiousRessourceKey.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...YPEALIZR001011_UnnamedGenericParameter.md → ...ference/TR0003_UnnamedGenericParameter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# TR0004: UnrecognizedParameterType | ||
|
||
## Cause | ||
The processed Resx-file contains a key that uses a parameter with an unrecognized type-annotation | ||
|
||
## Rule description | ||
|
||
When extracting parameters for a method-signature, the source-generator tries to infer a better suitable parameter-type to be use when constructing the method-signature. | ||
This is done by inspecting the parameter-token after the colon `:`, if present. | ||
|
||
> e.g. `xyz` as ín `hello {world:xyz}` | ||
If no parameter-annotation was found, the generator just assumes to use `object`. | ||
|
||
When a value is found, it is matched against the following built-in data-types, in order to generate a stricter typed method-signature: | ||
|
||
|alias | type | example usage (alias) | example usage (type) | | ||
|-------|-----------------|-----------------------|-----------------------| | ||
| i | int | {count:i} | {count:int} | | ||
| s | string | {name:s} | {name:string} | | ||
| dt | DateTime | {date:dt} | {date:DateTime} | | ||
| dto | DateTimeOffset | {date:dto} | {date:DateTimeOffset} | | ||
| d | DateOnly | {date:d} | {date:DateOnly} | | ||
| t | TimeOnly | {time:t} | {time:TimeOnly} | | ||
|
||
However, when the provided parameter-annotation could not be matched, the generator still falls back to using `object` as the parameter-type within the generated method-signature and emits this warning, additionaly. | ||
|
||
## How to fix violations | ||
Remove the type-annotation or use a valid one as shown in above table. | ||
|
||
## When to suppress warnings | ||
If you don´t care that your code-base has an easy-to-fix issue. | ||
|
||
## Example of a violation | ||
|
||
### Description | ||
The following content of a Resx-file demonstrate, what keys produce this warning. | ||
### Code | ||
|
||
```xml | ||
<root> | ||
<data name="Hello {name:xyz}." xml:space="preserve"> | ||
<value>Hello, {0}.</value> | ||
</data> | ||
</root> | ||
``` | ||
|
||
## Example of how to fix | ||
|
||
### Description | ||
In order to fix this particular situation, just remove the type-annotation, or use one of the built-in supported ones. | ||
### Code | ||
|
||
```xml | ||
<root> | ||
<data name="Hello {name}." xml:space="preserve"> | ||
<value>Hello, {0}.</value> | ||
</data> | ||
</root> | ||
``` | ||
|
||
or | ||
|
||
```xml | ||
<root> | ||
<data name="Hello {name:s}." xml:space="preserve"> | ||
<value>Hello, {0}.</value> | ||
</data> | ||
</root> | ||
``` | ||
|
||
or | ||
|
||
```xml | ||
<root> | ||
<data name="Hello {name:int}." xml:space="preserve"> | ||
<value>Hello, {0}.</value> | ||
</data> | ||
</root> | ||
``` | ||
|
||
or similar ;) | ||
|
||
## Related rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/TypealizR.SourceGenerators.Tests/ExtensionMethodParameterInfo.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using TypealizR.SourceGenerators.StringLocalizer; | ||
|
||
namespace TypealizR.SourceGenerators.Tests; | ||
|
||
public class ExtensionMethodParameterInfo_Tests | ||
{ | ||
[Theory] | ||
[InlineData("{0}", "object", false)] | ||
[InlineData("{1}", "object", false)] | ||
[InlineData("{userName}", "object", false)] | ||
[InlineData("{count:int}", "int", false)] | ||
[InlineData("{count:i}", "int", false)] | ||
[InlineData("{userName:string}", "string", false)] | ||
[InlineData("{userName:s}", "string", false)] | ||
[InlineData("{now:DateTime}", "DateTime", false)] | ||
[InlineData("{now:dt}", "DateTime", false)] | ||
[InlineData("{now:DateTimeOffset}", "DateTimeOffset", false)] | ||
[InlineData("{now:dto}", "DateTimeOffset", false)] | ||
[InlineData("{today:DateOnly}", "DateOnly", false)] | ||
[InlineData("{today:d}", "DateOnly", false)] | ||
[InlineData("{now:TimeOnly}", "TimeOnly", false)] | ||
[InlineData("{now:t}", "TimeOnly", false)] | ||
[InlineData("{now:wtf}", "object", true)] | ||
public void Parameter_Gets_Typed_As(string token, string expected, bool expectInvalidTypeExpression) | ||
{ | ||
var match = StringLocalizerExtensionMethodBuilder.parameterExpression.Match(token); | ||
var name = match.Groups["name"].Value; | ||
var expression = match.Groups["expression"].Value; | ||
|
||
var sut = new ExtensionMethodParameterInfo(token, name, expression); | ||
|
||
var actual = sut.Type; | ||
|
||
actual.Should().Be(expected); | ||
|
||
if (expectInvalidTypeExpression) | ||
{ | ||
sut.InvalidTypeAnnotation.Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.