Skip to content

Commit cb17696

Browse files
authored
Tweaks to ForceInternal docs (#237)
* Fix readmes * Tweak method arguments
1 parent 5dae3f6 commit cb17696

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,43 @@ You can set the default metadata source to use for a whole project by setting th
282282
</PropertyGroup>
283283
```
284284

285-
You can override the name of the extension class by setting `ExtensionClassName` in the attribute and/or the namespace of the class by setting `ExtensionClassNamespace`. By default, the class will be public if the enum is public, otherwise it will be internal.
285+
You can override the name of the extension class by setting `ExtensionClassName` in the attribute and/or the namespace of the class by setting `ExtensionClassNamespace`.
286+
287+
### Controlling extension accessibility
288+
289+
By default, the generated extension class is `public` if the enum is `public`, and `internal` if the enum is `internal`. You can control the accessibility of the generated extension classes in two ways:
290+
291+
**Global Configuration (MSBuild Property):**
292+
293+
Set the `EnumGenerator_ForceInternal` property in your project file to force all generated extension classes to be `internal`:
294+
295+
```xml
296+
<PropertyGroup>
297+
<!-- Make all generated extension classes internal -->
298+
<EnumGenerator_ForceInternal>true</EnumGenerator_ForceInternal>
299+
</PropertyGroup>
300+
```
301+
302+
Valid values are:
303+
- `false` (default): Generated extensions follow the enum's accessibility (`public` for `public` enums, `internal` for `internal` enums)
304+
- `true`: Forces all generated extensions to be `internal`, even for `public` enums
305+
306+
**Per-Enum Configuration:**
307+
308+
You can also set the `IsInternal` property on individual enums using the `[EnumExtensions]` attribute:
309+
310+
```csharp
311+
// Force this enum's extensions to be internal
312+
[EnumExtensions(IsInternal = true)]
313+
public enum MyEnum { ... }
314+
```
315+
316+
or for external enums:
317+
318+
```csharp
319+
// Force ExternalEnum's extensions to be internal
320+
[EnumExtensions<ExternalEnum>(IsInternal = true)]
321+
```
286322

287323
## Usage Analyzers
288324

src/NetEscapades.EnumGenerators.Generators/EnumGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,14 @@ static void Execute(
9696
{
9797
var useExtensionMembers = compilationDetails.LanguageVersion is not LanguageVersion.Preview and >= (LanguageVersion)1400; // C#14
9898
var useCollectionExpressions = compilationDetails.LanguageVersion is not LanguageVersion.Preview and >= (LanguageVersion)1200; // C#12
99-
var shouldBeInternal = !enumToGenerate.IsPublic || enumToGenerate.ForceInternal ||
100-
defaultValues.ForceInternalAccessModifier;
99+
var forceInternal = enumToGenerate.ForceInternal || defaultValues.ForceInternalAccessModifier;
101100
var (result, filename) = SourceGenerationHelper.GenerateExtensionClass(
102101
enumToGenerate: in enumToGenerate,
103102
useExtensionMembers: useExtensionMembers || defaultValues.ForceExtensionMembers,
104103
useCollectionExpressions: useCollectionExpressions,
105104
defaultMetadataSource: defaultValues.MetadataSource,
106105
hasRuntimeDependencies: compilationDetails.HasRuntimeDeps,
107-
isInternal: shouldBeInternal);
106+
forceInternal: forceInternal);
108107
context.AddSource(filename, SourceText.From(result, Encoding.UTF8));
109108
}
110109

src/NetEscapades.EnumGenerators.Generators/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,12 @@ You can also set the `IsInternal` property on individual enums using the `[EnumE
331331
[EnumExtensions(IsInternal = true)]
332332
public enum MyEnum { ... }
333333
```
334+
or for external enums:
334335

335-
**Important Rules:**
336-
- Internal enums always generate internal extensions
337-
- If the global `EnumGenerator_ForceInternal` MSBuild property is set to `true`, all extensions will be internal regardless of the `IsInternal` attribute setting
338-
- External enums (referenced via `[EnumExtensions<T>]`) also support the `IsInternal` property
336+
```csharp
337+
// Force ExternalEnum's extensions to be internal
338+
[EnumExtensions<ExternalEnum>(IsInternal = true)]
339+
```
339340

340341
## Usage Analyzers
341342

src/NetEscapades.EnumGenerators.Generators/SourceGenerationHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static (string Content, string HintName) GenerateExtensionClass(in EnumTo
2525
bool useCollectionExpressions,
2626
MetadataSource defaultMetadataSource,
2727
bool hasRuntimeDependencies,
28-
bool isInternal = false)
28+
bool forceInternal)
2929
{
3030
var metadataSource = enumToGenerate.MetadataSource ?? defaultMetadataSource;
3131
var isMetadataSourcesEnabled = metadataSource != MetadataSource.None;
@@ -83,7 +83,7 @@ namespace
8383
"""
8484
")]
8585
86-
""").Append(isInternal ? "internal" : "public").Append(" static partial class ")
86+
""").Append((enumToGenerate.IsPublic && !forceInternal) ? "public" : "internal").Append(" static partial class ")
8787
.Append(enumToGenerate.Name).Append(
8888
"""
8989

src/NetEscapades.EnumGenerators/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,12 @@ You can also set the `IsInternal` property on individual enums using the `[EnumE
334334
public enum MyEnum { ... }
335335
```
336336

337-
**Important Rules:**
338-
- Internal enums always generate internal extensions
339-
- If the global `EnumGenerator_ForceInternal` MSBuild property is set to `true`, all extensions will be internal regardless of the `IsInternal` attribute setting
340-
- External enums (referenced via `[EnumExtensions<T>]`) also support the `IsInternal` property
337+
or for external enums:
338+
339+
```csharp
340+
// Force ExternalEnum's extensions to be internal
341+
[EnumExtensions<ExternalEnum>(IsInternal = true)]
342+
```
341343

342344
## Usage Analyzers
343345

tests/NetEscapades.EnumGenerators.Tests/SourceGenerationHelperSnapshotTests.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public Task GeneratesEnumCorrectly(
3939
csharp14IsSupported,
4040
useCollectionExpressions: useCollectionExpressions,
4141
defaultSource,
42-
hasRuntimeDeps).Content;
42+
hasRuntimeDeps,
43+
forceInternal: false).Content;
4344

4445
return Verifier.Verify(result)
4546
.ScrubExpectedChanges()
@@ -66,8 +67,13 @@ public Task GeneratesEnumWithRepeatedValuesCorrectly(bool csharp14IsSupported)
6667
hasFlags: false,
6768
null);
6869

69-
var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported,
70-
useCollectionExpressions: false, DefaultMetadataSource, hasRuntimeDependencies: true).Content;
70+
var result = SourceGenerationHelper.GenerateExtensionClass(
71+
value,
72+
csharp14IsSupported,
73+
useCollectionExpressions: false,
74+
DefaultMetadataSource,
75+
hasRuntimeDependencies: true,
76+
forceInternal: false).Content;
7177

7278
return Verifier.Verify(result)
7379
.ScrubExpectedChanges()
@@ -93,8 +99,13 @@ public Task GeneratesFlagsEnumCorrectly(bool csharp14IsSupported)
9399
hasFlags: true,
94100
metadataSource: null);
95101

96-
var result = SourceGenerationHelper.GenerateExtensionClass(value, csharp14IsSupported,
97-
useCollectionExpressions: false, DefaultMetadataSource, hasRuntimeDependencies: true).Content;
102+
var result = SourceGenerationHelper.GenerateExtensionClass(
103+
value,
104+
csharp14IsSupported,
105+
useCollectionExpressions: false,
106+
DefaultMetadataSource,
107+
hasRuntimeDependencies: true,
108+
forceInternal: false).Content;
98109

99110
return Verifier.Verify(result)
100111
.ScrubExpectedChanges()
@@ -125,7 +136,7 @@ public Task GeneratesForcedInternalEnumCorrectly()
125136
useCollectionExpressions: true,
126137
MetadataSource.None,
127138
false,
128-
isInternal: true).Content;
139+
forceInternal: true).Content;
129140

130141
return Verifier.Verify(result)
131142
.ScrubExpectedChanges()

0 commit comments

Comments
 (0)