Skip to content

Commit b5e232f

Browse files
authored
Add support for CollectionExpression in async generator (#963)
1 parent 18366ff commit b5e232f

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- uses: Brightspace/third-party-actions@actions/setup-dotnet
1818
with:
1919
dotnet-version: |
20-
7.0.x
20+
9.0.x
2121
2222
- name: Build
2323
run: dotnet build -c Release

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<Deterministic>true</Deterministic>
4-
<LangVersion>10.0</LangVersion>
4+
<LangVersion>12.0</LangVersion>
55
<ImplicitUsings>true</ImplicitUsings>
66
<InvariantGlobalization>true</InvariantGlobalization>
77
<Nullable>enable</Nullable>

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "7.0.300",
3+
"version": "9.0.101",
44
"rollForward": "latestFeature"
55
}
66
}

src/D2L.CodeStyle.Analyzers/Async/Generator/AsyncToSyncMethodTransformer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ private ExpressionSyntax Transform( ExpressionSyntax expr )
258258
.WithLeft( Transform( binExpr.Left ) )
259259
.WithRight( Transform( binExpr.Right ) ),
260260

261+
CollectionExpressionSyntax collectionExpr =>
262+
collectionExpr.WithElements( Transform( collectionExpr.Elements ) ),
263+
261264
ConditionalExpressionSyntax condExpr => condExpr
262265
.WithCondition( Transform( condExpr.Condition ) )
263266
.WithWhenTrue( Transform( condExpr.WhenTrue ) )
@@ -352,6 +355,17 @@ private VariableDesignationSyntax Transform( VariableDesignationSyntax des )
352355
_ => UnhandledSyntax( des )
353356
};
354357

358+
private SeparatedSyntaxList<CollectionElementSyntax> Transform(
359+
SeparatedSyntaxList<CollectionElementSyntax> original
360+
) => SyntaxFactory.SeparatedList(
361+
original.Select( elem => elem switch {
362+
ExpressionElementSyntax ee => ee.WithExpression( Transform( ee.Expression ) ),
363+
SpreadElementSyntax sp => sp.WithExpression( Transform( sp.Expression ) ),
364+
_ => UnhandledSyntax( elem )
365+
} ),
366+
original.GetSeparators()
367+
);
368+
355369
private StatementSyntax Transform( ExpressionStatementSyntax exprStmt ) {
356370
var result = Transform( exprStmt.Expression );
357371

tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/AsyncToSyncMethodTransformerTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,41 @@ public void ImplicitObjectCreation() {
633633
Assert.AreEqual( "[Blocking] T Hello() { return new( Foo() ); }", actual.Value.ToFullString() );
634634
}
635635

636+
[Test]
637+
public void CollectionSyntax() {
638+
var actual = Transform(
639+
"""
640+
[GenerateSync]
641+
async Task<IEnumerable<int>> HelloAsync()
642+
=> [
643+
await FooAsync(),
644+
3,
645+
..(await BarAsync()),
646+
4,
647+
await XAsync() + await YAsync()
648+
];
649+
"""
650+
);
651+
652+
653+
Assert.IsTrue( actual.Success );
654+
Assert.IsEmpty( actual.Diagnostics );
655+
Assert.AreEqual(
656+
"""
657+
[Blocking]
658+
IEnumerable<int> Hello()
659+
=> [
660+
Foo(),
661+
3,
662+
..(Bar()),
663+
4,
664+
X() + Y()
665+
];
666+
""",
667+
actual.Value.ToFullString()
668+
);
669+
670+
}
636671

637672
// loosly assert that the right sorts of diagnostics came out
638673
private static void AssertDiagnostics( IEnumerable<Diagnostic> actual, params DiagnosticDescriptor[] expected ) {

0 commit comments

Comments
 (0)