Skip to content

Commit

Permalink
Invert ThrowIfDisposed attribute role
Browse files Browse the repository at this point in the history
Signed-off-by: Rekkonnect <[email protected]>
  • Loading branch information
Rekkonnect authored and AraHaan committed May 7, 2024
1 parent 7f41625 commit 64bd04a
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/IDisposableGenerator/ClassItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal class ClassItems
public string? Name { get; set; }
public Accessibility Accessibility { get; set; }
public bool Stream { get; set; }
public bool ThrowIfDisposed { get; set; }
public bool WithoutThrowIfDisposed { get; set; }
public List<string> Owns { get; } = [];
public List<string> Fields { get; } = [];
public List<string> SetNull { get; } = [];
Expand Down Expand Up @@ -47,7 +47,7 @@ public override string ToString()
_ = result.Append($"Class: Name {this.Name}")
.Append($", Accessibility: {this.Accessibility}")
.Append($", Stream: {this.Stream}")
.Append($", ThrowIfDisposed: {this.ThrowIfDisposed}")
.Append($", Without ThrowIfDisposed: {this.WithoutThrowIfDisposed}")
.Append($", Owns Count: {this.Owns.Count}")
.Append($", Fields Count: {this.Fields.Count}")
.Append($", SetNull Count: {this.SetNull.Count}")
Expand Down
6 changes: 3 additions & 3 deletions src/IDisposableGenerator/DisposableCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ End Sub
""");

if (classItem.ThrowIfDisposed)
if (!classItem.WithoutThrowIfDisposed)
{
_ = sourceBuilder.Append($$"""
Expand Down Expand Up @@ -233,7 +233,7 @@ namespace {workItem.Namespace};
""");

if (classItem.ThrowIfDisposed)
if (!classItem.WithoutThrowIfDisposed)
{
_ = sourceBuilder.Append($$"""
Expand Down Expand Up @@ -364,7 +364,7 @@ namespace {workItem.Namespace}
""");

if (classItem.ThrowIfDisposed)
if (!classItem.WithoutThrowIfDisposed)
{
_ = sourceBuilder.Append($$"""
Expand Down
10 changes: 6 additions & 4 deletions src/IDisposableGenerator/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,12 @@ namespace IDisposableGenerator

// used only by a source generator to generate Dispose() and Dispose(bool).
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal class GenerateThrowIfDisposedAttribute : Attribute
internal class WithoutThrowIfDisposedAttribute : Attribute
{
}
}
#pragma warning restore SA1636, 8618</value>
#pragma warning restore SA1636, 8618
</value>
</data>
<data name="AttributeCodeVisualBasic" xml:space="preserve">
<value>' &lt;autogenerated/&gt;
Expand Down Expand Up @@ -207,11 +208,12 @@ Namespace IDisposableGenerator

' used only by a source generator to generate Dispose() and Dispose(bool).
&lt;AttributeUsage(AttributeTargets.Class, Inherited:=False, AllowMultiple:=False)&gt;
Friend Class GenerateThrowIfDisposedAttribute
Friend Class WithoutThrowIfDisposedAttribute
Inherits Attribute
End Class

End Namespace
#Enable Warning SA1636</value>
#Enable Warning SA1636
</value>
</data>
</root>
4 changes: 2 additions & 2 deletions src/IDisposableGenerator/WorkItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public int IndexOf(WorkItem item)
result.Accessibility = testClass.DeclaredAccessibility;
result.Stream = (bool)attr.ConstructorArguments[0].Value!;
break;
case "GenerateThrowIfDisposedAttribute":
result.ThrowIfDisposed = true;
case "WithoutThrowIfDisposedAttribute":
result.WithoutThrowIfDisposed = true;
break;
}
#pragma warning restore IDE0010 // Add missing cases
Expand Down
93 changes: 82 additions & 11 deletions tests/IDisposableGeneratorTests.CSharp10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using IDisposableGenerator;
Expand Down Expand Up @@ -67,6 +75,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using IDisposableGenerator;
Expand Down Expand Up @@ -113,6 +129,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using IDisposableGenerator;
Expand Down Expand Up @@ -153,6 +177,14 @@ protected override void Dispose(bool disposing)
// On Streams call base.Dispose(disposing)!!!
base.Dispose(disposing);
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using System.IO;
Expand Down Expand Up @@ -210,6 +242,14 @@ protected override void Dispose(bool disposing)
// On Streams call base.Dispose(disposing)!!!
base.Dispose(disposing);
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using System.IO;
Expand Down Expand Up @@ -264,6 +304,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using IDisposableGenerator;
Expand Down Expand Up @@ -310,6 +358,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using System.ComponentModel.DataAnnotations;
Expand Down Expand Up @@ -361,6 +417,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
"},
{"Disposables.1.g.cs", @"// <autogenerated/>
Expand All @@ -382,6 +446,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(AnotherDisposable));
}
}
}
"}
};
Expand All @@ -406,6 +478,14 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
", @"global using System;
global using System.ComponentModel.DataAnnotations;
Expand All @@ -422,9 +502,8 @@ internal partial class TestDisposable
", LanguageVersion.CSharp10, testSources, generatedSources);
}


[Fact]
public async Task TestGenerateThrowIfDisposedCSharp10()
public async Task TestWithoutThrowIfDisposedCSharp10()
{
const string generatedSource = """
// <autogenerated/>
Expand All @@ -447,14 +526,6 @@ private void Dispose(bool disposing)
this.isDisposed = true;
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(TestDisposable));
}
}
}
""";
Expand All @@ -467,7 +538,7 @@ internal void ThrowIfDisposed()
namespace MyApp;
[GenerateDispose(false)]
[GenerateThrowIfDisposed]
[WithoutThrowIfDisposed]
internal partial class TestDisposable
{
[NullOnDispose]
Expand Down
Loading

0 comments on commit 64bd04a

Please sign in to comment.