Skip to content

Commit 64d1860

Browse files
committed
Fix directive processing
1 parent 046f169 commit 64d1860

6 files changed

+63
-9
lines changed

src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,11 +2161,14 @@ BinaryExpressionSyntax be
21612161
if (trivia.IsKind(SyntaxKind.DisabledTextTrivia) && directiveStack.IsSyncOnly == true)
21622162
{
21632163
var statementsText = trivia.ToString();
2164-
var compilation = ParseCompilationUnit(statementsText);
2164+
var syntaxTree = CSharpSyntaxTree.ParseText(statementsText, options: (CSharpParseOptions)semanticModel.SyntaxTree.Options);
2165+
var compilation = semanticModel.Compilation.AddSyntaxTrees(syntaxTree);
2166+
var model = compilation.GetSemanticModel(syntaxTree);
2167+
var asyncToSyncRewriter = new AsyncToSyncRewriter(model, disableNullable, preserveProgress);
21652168

2166-
foreach (var m in compilation.Members)
2169+
foreach (var m in syntaxTree.GetCompilationUnitRoot().Members)
21672170
{
2168-
process(m);
2171+
process((MemberDeclarationSyntax)asyncToSyncRewriter.Visit(m));
21692172
}
21702173

21712174
continue;
@@ -2195,8 +2198,6 @@ BinaryExpressionSyntax be
21952198
triviaList.Clear();
21962199
}
21972200

2198-
//Cannot to the following because syntax node is not within syntax tree
2199-
//var statement = (StatementSyntax)Visit(gs.Statement)!;
22002201
statements.Add(statement);
22012202
});
22022203

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//HintName: Test.Class.MethodAsync.g.cs
22
if (true)
33
{
4-
throw new InvalidOperationException("Some exception");
4+
throw new global::System.InvalidOperationException("Some exception");
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//HintName: Test.Class.ExecAsync.g.cs
22
public void Exec()
33
{
4-
throw new InvalidOperationException("Some exception");
4+
throw new global::System.InvalidOperationException("Some exception");
55
}

tests/Generator.Tests/Snapshots/SyncOnlyTests.SyncOnlyBeforeMethodBody#MethodWithObsoleteSyncAsync.g.verified.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//HintName: Test.Class.MethodWithObsoleteSyncAsync.g.cs
2-
[global::System.Obsolete]
3-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2+
[global::System.ObsoleteAttribute]
3+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute]
44
public void MethodWithObsoleteSync()
55
{
66
global::System.Threading.Thread.Sleep(1000);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//HintName: Test.DisposeTest.DisposeAsync.g.cs
2+
// <auto-generated/>
3+
#nullable enable
4+
namespace Test;
5+
public partial class DisposeTest
6+
{
7+
public void Dispose(object? obj)
8+
{
9+
10+
if (entity is global::System.IDisposable disposable)
11+
{
12+
disposable.Dispose();
13+
return;
14+
}
15+
16+
if (entity is global::System.IAsyncDisposable)
17+
{
18+
throw new global::System.InvalidOperationException("Cannot dispose an async disposable in sync-only mode.");
19+
}
20+
}
21+
}

tests/Generator.Tests/SyncOnlyTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,36 @@ public Task SyncOnlyArgument() => """
290290
#endif
291291
);
292292
""".Verify(sourceType: SourceType.MethodBody);
293+
294+
[Fact]
295+
public Task SyncOnlyFullNamespace() => """
296+
using System;
297+
namespace Test;
298+
public partial class DisposeTest
299+
{
300+
[CreateSyncVersion]
301+
public async Task DisposeAsync(object? obj)
302+
{
303+
#if !SYNC_ONLY
304+
if (obj is IAsyncDisposable asyncDisposable)
305+
{
306+
await asyncDisposable.DisposeAsync();
307+
return;
308+
}
309+
#endif
310+
311+
if (obj is IDisposable disposable)
312+
{
313+
disposable.Dispose();
314+
return;
315+
}
316+
317+
#if SYNC_ONLY
318+
if (obj is IAsyncDisposable)
319+
{
320+
throw new InvalidOperationException("Cannot dispose an async disposable in sync-only mode.");
321+
}
322+
#endif
323+
}
324+
""".Verify(sourceType: SourceType.Full);
293325
}

0 commit comments

Comments
 (0)