-
Notifications
You must be signed in to change notification settings - Fork 175
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 #148 from terrajobst/episode26
Episode 26: Miscellaneous
- Loading branch information
Showing
20 changed files
with
147 additions
and
66 deletions.
There are no files selected for viewing
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,64 @@ | ||
# Episode 26 | ||
|
||
[Video](https://www.youtube.com/watch?v=Y2Gn6qr_twA&list=PLRAdsfhKI4OWNOSfS7EUu5GRAVmze1t2y&index=26) | | ||
[Pull Request](https://github.com/terrajobst/minsk/pull/148) | | ||
[Previous](episode-26.md) | | ||
[Next](episode-27.md) | ||
|
||
## Completed items | ||
|
||
* Enabled nullable in Minsk.Tests & Minsk.Generators | ||
* Honored nullable annotations in source generator | ||
* Filed and fixed various TODOs | ||
|
||
## Interesting aspects | ||
|
||
### Leveraging nullable annotations inside the source generator | ||
|
||
We're now honoring nullable annotations when generating the | ||
`SyntaxNode.GetChildren()` methods. | ||
|
||
For example, consider `ReturnStatementSyntax`. The `Expression` property is | ||
marked as being nullable (because `return` can be used without an expression). | ||
|
||
```C# | ||
partial class ReturnStatementSyntax : StatementSyntax | ||
{ | ||
public SyntaxToken ReturnKeyword { get; } | ||
public ExpressionSyntax? Expression { get; } | ||
} | ||
``` | ||
|
||
Our generator now uses the null annotation for the `Expression` property and | ||
emits a `null` check: | ||
|
||
```C# | ||
partial class ReturnStatementSyntax | ||
{ | ||
public override IEnumerable<SyntaxNode> GetChildren() | ||
{ | ||
yield return ReturnKeyword; | ||
if (Expression != null) | ||
yield return Expression; | ||
} | ||
} | ||
``` | ||
|
||
This is simply done by [using Roslyn's `NullableAnnotation` | ||
property][null-annotations]: | ||
|
||
```C# | ||
var canBeNull = property.NullableAnnotation == NullableAnnotation.Annotated; | ||
if (canBeNull) | ||
{ | ||
writer.WriteLine($"if ({property.Name} != null)"); | ||
writer.Indent++; | ||
} | ||
|
||
writer.WriteLine($"yield return {property.Name};"); | ||
|
||
if (canBeNull) | ||
writer.Indent--; | ||
``` | ||
|
||
[null-annotations]: https://github.com/terrajobst/minsk/blob/877fefa36e184da125fd62942b5797328df79896/src/Minsk.Generators/SyntaxNodeGetChildrenGenerator.cs#L59-L69 |
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
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
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
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
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
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.