diff --git a/src/IDisposableGenerator/ClassItems.cs b/src/IDisposableGenerator/ClassItems.cs index 777bc95..a21c35f 100644 --- a/src/IDisposableGenerator/ClassItems.cs +++ b/src/IDisposableGenerator/ClassItems.cs @@ -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 Owns { get; } = []; public List Fields { get; } = []; public List SetNull { get; } = []; @@ -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}") diff --git a/src/IDisposableGenerator/DisposableCodeWriter.cs b/src/IDisposableGenerator/DisposableCodeWriter.cs index 4bacadf..1615f1c 100644 --- a/src/IDisposableGenerator/DisposableCodeWriter.cs +++ b/src/IDisposableGenerator/DisposableCodeWriter.cs @@ -106,7 +106,7 @@ End Sub """); - if (classItem.ThrowIfDisposed) + if (!classItem.WithoutThrowIfDisposed) { _ = sourceBuilder.Append($$""" @@ -233,7 +233,7 @@ namespace {workItem.Namespace}; """); - if (classItem.ThrowIfDisposed) + if (!classItem.WithoutThrowIfDisposed) { _ = sourceBuilder.Append($$""" @@ -364,7 +364,7 @@ namespace {workItem.Namespace} """); - if (classItem.ThrowIfDisposed) + if (!classItem.WithoutThrowIfDisposed) { _ = sourceBuilder.Append($$""" diff --git a/src/IDisposableGenerator/Properties/Resources.resx b/src/IDisposableGenerator/Properties/Resources.resx index 284c1f7..7c16a63 100644 --- a/src/IDisposableGenerator/Properties/Resources.resx +++ b/src/IDisposableGenerator/Properties/Resources.resx @@ -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 +#pragma warning restore SA1636, 8618 + ' <autogenerated/> @@ -207,11 +208,12 @@ Namespace IDisposableGenerator ' used only by a source generator to generate Dispose() and Dispose(bool). <AttributeUsage(AttributeTargets.Class, Inherited:=False, AllowMultiple:=False)> - Friend Class GenerateThrowIfDisposedAttribute + Friend Class WithoutThrowIfDisposedAttribute Inherits Attribute End Class End Namespace -#Enable Warning SA1636 +#Enable Warning SA1636 + \ No newline at end of file diff --git a/src/IDisposableGenerator/WorkItemCollection.cs b/src/IDisposableGenerator/WorkItemCollection.cs index bb94ee9..bcf0d52 100644 --- a/src/IDisposableGenerator/WorkItemCollection.cs +++ b/src/IDisposableGenerator/WorkItemCollection.cs @@ -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 diff --git a/tests/IDisposableGeneratorTests.CSharp10.cs b/tests/IDisposableGeneratorTests.CSharp10.cs index 5ab6039..3145737 100644 --- a/tests/IDisposableGeneratorTests.CSharp10.cs +++ b/tests/IDisposableGeneratorTests.CSharp10.cs @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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", @"// @@ -382,6 +446,14 @@ private void Dispose(bool disposing) this.isDisposed = true; } } + + internal void ThrowIfDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(AnotherDisposable)); + } + } } "} }; @@ -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; @@ -422,9 +502,8 @@ internal partial class TestDisposable ", LanguageVersion.CSharp10, testSources, generatedSources); } - [Fact] - public async Task TestGenerateThrowIfDisposedCSharp10() + public async Task TestWithoutThrowIfDisposedCSharp10() { const string generatedSource = """ // @@ -447,14 +526,6 @@ private void Dispose(bool disposing) this.isDisposed = true; } } - - internal void ThrowIfDisposed() - { - if (this.isDisposed) - { - throw new ObjectDisposedException(nameof(TestDisposable)); - } - } } """; @@ -467,7 +538,7 @@ internal void ThrowIfDisposed() namespace MyApp; [GenerateDispose(false)] - [GenerateThrowIfDisposed] + [WithoutThrowIfDisposed] internal partial class TestDisposable { [NullOnDispose] diff --git a/tests/IDisposableGeneratorTests.CSharp9.cs b/tests/IDisposableGeneratorTests.CSharp9.cs index 2e633b7..5633342 100644 --- a/tests/IDisposableGeneratorTests.CSharp9.cs +++ b/tests/IDisposableGeneratorTests.CSharp9.cs @@ -28,6 +28,14 @@ private void Dispose(bool disposing) this.isDisposed = true; } } + + internal void ThrowIfDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(TestDisposable)); + } + } } } ", @"namespace MyApp @@ -73,6 +81,14 @@ private void Dispose(bool disposing) this.isDisposed = true; } } + + internal void ThrowIfDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(TestDisposable)); + } + } } } ", @"namespace MyApp @@ -123,6 +139,14 @@ private void Dispose(bool disposing) this.isDisposed = true; } } + + internal void ThrowIfDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(TestDisposable)); + } + } } } ", @"namespace MyApp @@ -167,6 +191,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)); + } + } } } ", @"namespace MyApp @@ -228,6 +260,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)); + } + } } } ", @"namespace MyApp @@ -286,6 +326,14 @@ private void Dispose(bool disposing) this.isDisposed = true; } } + + internal void ThrowIfDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(TestDisposable)); + } + } } } ", @"namespace MyApp @@ -336,6 +384,14 @@ private void Dispose(bool disposing) this.isDisposed = true; } } + + internal void ThrowIfDisposed() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(TestDisposable)); + } + } } } ", @"namespace MyApp @@ -355,7 +411,7 @@ internal partial class TestDisposable "); [Fact] - public async Task TestGenerateThrowIfDisposedCSharp9() + public async Task TestWithoutThrowIfDisposedCSharp9() { const string generatedSource = """ // @@ -380,14 +436,6 @@ private void Dispose(bool disposing) this.isDisposed = true; } } - - internal void ThrowIfDisposed() - { - if (this.isDisposed) - { - throw new ObjectDisposedException(nameof(TestDisposable)); - } - } } } @@ -401,7 +449,7 @@ namespace MyApp using IDisposableGenerator; [GenerateDispose(false)] - [GenerateThrowIfDisposed] + [WithoutThrowIfDisposed] internal partial class TestDisposable { [NullOnDispose] diff --git a/tests/IDisposableGeneratorTests.VisualBasic.cs b/tests/IDisposableGeneratorTests.VisualBasic.cs index 2cc67ca..c9b9a4a 100644 --- a/tests/IDisposableGeneratorTests.VisualBasic.cs +++ b/tests/IDisposableGeneratorTests.VisualBasic.cs @@ -29,6 +29,12 @@ If Not Me.isDisposed AndAlso disposing Then Me.isDisposed = True End If End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -74,6 +80,12 @@ If Not Me.isDisposed AndAlso disposing Then Me.isDisposed = True End If End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -123,6 +135,12 @@ End If Me.isDisposed = True End If End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -163,6 +181,12 @@ End If ' On Streams call MyBase.Dispose(disposing)!!! MyBase.Dispose(disposing) End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -254,6 +278,12 @@ End If ' On Streams call MyBase.Dispose(disposing)!!! MyBase.Dispose(disposing) End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -347,6 +377,12 @@ If Not Me.isDisposed AndAlso disposing Then Me.isDisposed = True End If End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -395,6 +431,12 @@ If Not Me.isDisposed AndAlso disposing Then Me.isDisposed = True End If End Sub + + Friend Sub ThrowIfDisposed() + If Me.isDisposed Then + Throw New ObjectDisposedException(NameOf(TestDisposable)) + End If + End Sub End Class End Namespace ", @"Imports System @@ -413,7 +455,7 @@ End Namespace ", null); [Fact] - public async Task TestGenerateThrowIfDisposedVisualBasic() + public async Task TestWithoutThrowIfDisposedVisualBasic() { const string generatedSource = """ ' @@ -439,12 +481,6 @@ If Not Me.isDisposed AndAlso disposing Then Me.isDisposed = True End If End Sub - - Friend Sub ThrowIfDisposed() - If Me.isDisposed Then - Throw New ObjectDisposedException(NameOf(TestDisposable)) - End If - End Sub End Class End Namespace @@ -457,7 +493,7 @@ Imports IDisposableGenerator Namespace MyApp - + Friend Partial Class TestDisposable