Skip to content

Commit 9ea308b

Browse files
committed
Rename PublicApiAnalyzer -> InternalForTestingAnalyzer
1 parent d7a6df3 commit 9ea308b

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

tracer/src/Datadog.Trace.Tools.Analyzers/PublicApiAnalyzer/PublicApiAnalyzer.cs renamed to tracer/src/Datadog.Trace.Tools.Analyzers/InternalForTestingAnalyzer/InternalForTestingAnalyzer.cs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="PublicApiAnalyzer.cs" company="Datadog">
1+
// <copyright file="InternalForTestingAnalyzer.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -14,37 +14,38 @@
1414
using Microsoft.CodeAnalysis.Diagnostics;
1515
using Microsoft.CodeAnalysis.Operations;
1616

17-
namespace Datadog.Trace.Tools.Analyzers.PublicApiAnalyzer
17+
namespace Datadog.Trace.Tools.Analyzers.InternalForTestingAnalyzer
1818
{
1919
/// <summary>
20-
/// DD002: Incorrect usage of public API
20+
/// DD002: Incorrect usage of internal API
2121
///
22-
/// Finds internal usages of APIs specifically marked with the [PublicApi] flag.
23-
/// These methods should not be called directly by our library code, only users should invoke them.
24-
/// The analyzer enforces that. requirement
22+
/// Finds internal usages of APIs specifically marked with the [InternalForTesting] flag.
23+
/// These methods should not be called directly by our library code, only from test code.
24+
/// The analyzer enforces that requirement
2525
///
2626
/// </summary>
2727
[DiagnosticAnalyzer(LanguageNames.CSharp)]
28-
public sealed class PublicApiAnalyzer : DiagnosticAnalyzer
28+
public sealed class InternalForTestingAnalyzer : DiagnosticAnalyzer
2929
{
3030
/// <summary>
3131
/// The diagnostic ID displayed in error messages
3232
/// </summary>
3333
public const string DiagnosticId = "DD0002";
3434

35-
private const string PublicApiAttribute = nameof(PublicApiAttribute);
35+
private const string InternalForTestingAttribute = nameof(InternalForTestingAttribute);
3636

37-
private static readonly ImmutableArray<string> PublicApiAttributeNames = ImmutableArray.Create(PublicApiAttribute);
37+
private static readonly ImmutableArray<string> InternalForTestingAttributeNames
38+
= ImmutableArray.Create(InternalForTestingAttribute);
3839

3940
#pragma warning disable RS2008 // Enable analyzer release tracking for the analyzer project
4041
private static readonly DiagnosticDescriptor Rule = new(
4142
DiagnosticId,
42-
title: "Incorrect usage of public API",
43-
messageFormat: "This API is only for public usage and should not be called internally",
43+
title: "Incorrect usage of internal API",
44+
messageFormat: "This API is only for use in tests and should not be called internally",
4445
category: "CodeQuality",
4546
defaultSeverity: DiagnosticSeverity.Error,
4647
isEnabledByDefault: true,
47-
description: "This API is only for public usage and should not be called internally. Use an alternative method.");
48+
description: "This API is only for internal testing and should not be called internally. Use an alternative method.");
4849
#pragma warning restore RS2008
4950

5051
/// <inheritdoc />
@@ -58,23 +59,23 @@ public override void Initialize(AnalysisContext context)
5859

5960
context.RegisterCompilationStartAction(context =>
6061
{
61-
var publicApiMembers = new ConcurrentDictionary<ISymbol, PublicApiStatus>(SymbolEqualityComparer.Default);
62+
var internalApiMembers = new ConcurrentDictionary<ISymbol, InternalApiStatus>(SymbolEqualityComparer.Default);
6263

6364
context.RegisterOperationBlockStartAction(
64-
context => AnalyzeOperationBlock(context, publicApiMembers));
65+
ctx => AnalyzeOperationBlock(ctx, internalApiMembers));
6566
});
6667
}
6768

68-
private void AnalyzeOperationBlock(
69+
private static void AnalyzeOperationBlock(
6970
OperationBlockStartAnalysisContext context,
70-
ConcurrentDictionary<ISymbol, PublicApiStatus> publicApiMembers)
71+
ConcurrentDictionary<ISymbol, InternalApiStatus> internalApiMembers)
7172
{
72-
var publicApiOperations = PooledConcurrentDictionary<KeyValuePair<IOperation, ISymbol>, bool>.GetInstance();
73+
var internalApiOperations = PooledConcurrentDictionary<KeyValuePair<IOperation, ISymbol>, bool>.GetInstance();
7374

7475
context.RegisterOperationAction(
75-
context =>
76+
ctx =>
7677
{
77-
Helpers.AnalyzeOperation(context.Operation, publicApiOperations, publicApiMembers);
78+
Helpers.AnalyzeOperation(ctx.Operation, internalApiOperations, internalApiMembers);
7879
},
7980
OperationKind.MethodReference,
8081
OperationKind.EventReference,
@@ -83,23 +84,23 @@ private void AnalyzeOperationBlock(
8384
OperationKind.ObjectCreation,
8485
OperationKind.PropertyReference);
8586

86-
context.RegisterOperationBlockEndAction(context =>
87+
context.RegisterOperationBlockEndAction(ctx =>
8788
{
8889
try
8990
{
90-
if (publicApiOperations.IsEmpty)
91+
if (internalApiOperations.IsEmpty)
9192
{
9293
return;
9394
}
9495

95-
foreach (var kvp in publicApiOperations)
96+
foreach (var kvp in internalApiOperations)
9697
{
97-
context.ReportDiagnostic(Diagnostic.Create(Rule, kvp.Key.Key.Syntax.GetLocation()));
98+
ctx.ReportDiagnostic(Diagnostic.Create(Rule, kvp.Key.Key.Syntax.GetLocation()));
9899
}
99100
}
100101
finally
101102
{
102-
publicApiOperations.Free(context.CancellationToken);
103+
internalApiOperations.Free(ctx.CancellationToken);
103104
}
104105
});
105106
}
@@ -108,8 +109,8 @@ private static class Helpers
108109
{
109110
internal static void AnalyzeOperation(
110111
IOperation operation,
111-
PooledConcurrentDictionary<KeyValuePair<IOperation, ISymbol>, bool> publicApiOperations,
112-
ConcurrentDictionary<ISymbol, PublicApiStatus> publicApiMembers)
112+
PooledConcurrentDictionary<KeyValuePair<IOperation, ISymbol>, bool> internalApiOperations,
113+
ConcurrentDictionary<ISymbol, InternalApiStatus> internalApiMembers)
113114
{
114115
var symbol = GetOperationSymbol(operation);
115116

@@ -184,9 +185,9 @@ void CheckTypeArgumentsCore(ImmutableArray<ITypeSymbol> typeArguments, PooledHas
184185

185186
void CheckOperationAttributes(ISymbol symbol, bool checkParents)
186187
{
187-
if (TryGetOrCreatePublicApiAttributes(symbol, checkParents, publicApiMembers, out _))
188+
if (TryGetOrCreatePublicApiAttributes(symbol, checkParents, internalApiMembers, out _))
188189
{
189-
publicApiOperations.TryAdd(new KeyValuePair<IOperation, ISymbol>(operation, symbol), true);
190+
internalApiOperations.TryAdd(new KeyValuePair<IOperation, ISymbol>(operation, symbol), true);
190191
}
191192
}
192193
}
@@ -238,7 +239,7 @@ void CheckOperationAttributes(ISymbol symbol, bool checkParents)
238239
return iEvent;
239240
}
240241

241-
private static PublicApiStatus CopyAttributes(PublicApiStatus copyAttributes) =>
242+
private static InternalApiStatus CopyAttributes(InternalApiStatus copyAttributes) =>
242243
new()
243244
{
244245
IsAssemblyAttribute = copyAttributes.IsAssemblyAttribute,
@@ -260,8 +261,8 @@ or BinaryOperatorKind.LessThanOrEqual
260261
private static bool TryGetOrCreatePublicApiAttributes(
261262
ISymbol symbol,
262263
bool checkParents,
263-
ConcurrentDictionary<ISymbol, PublicApiStatus> publicApiMembers,
264-
out PublicApiStatus attributes)
264+
ConcurrentDictionary<ISymbol, InternalApiStatus> publicApiMembers,
265+
out InternalApiStatus attributes)
265266
{
266267
if (!publicApiMembers.TryGetValue(symbol, out attributes))
267268
{
@@ -281,7 +282,7 @@ private static bool TryGetOrCreatePublicApiAttributes(
281282
}
282283
}
283284

284-
attributes ??= new PublicApiStatus() { IsAssemblyAttribute = symbol is IAssemblySymbol };
285+
attributes ??= new InternalApiStatus() { IsAssemblyAttribute = symbol is IAssemblySymbol };
285286
MergePlatformAttributes(symbol.GetAttributes(), ref attributes);
286287
attributes = publicApiMembers.GetOrAdd(symbol, attributes);
287288
}
@@ -290,11 +291,11 @@ private static bool TryGetOrCreatePublicApiAttributes(
290291

291292
static void MergePlatformAttributes(
292293
ImmutableArray<AttributeData> immediateAttributes,
293-
ref PublicApiStatus parentAttributes)
294+
ref InternalApiStatus parentAttributes)
294295
{
295296
foreach (AttributeData attribute in immediateAttributes)
296297
{
297-
if (PublicApiAttributeNames.Contains(attribute.AttributeClass!.Name))
298+
if (InternalForTestingAttributeNames.Contains(attribute.AttributeClass!.Name))
298299
{
299300
parentAttributes.IsPublicApi = true;
300301
return;
@@ -304,7 +305,7 @@ static void MergePlatformAttributes(
304305
}
305306
}
306307

307-
private sealed class PublicApiStatus
308+
private sealed class InternalApiStatus
308309
{
309310
public bool IsAssemblyAttribute { get; set; }
310311

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="PublicApiAttribute.cs" company="Datadog">
1+
// <copyright file="InternalForTestingAttribute.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -8,15 +8,15 @@
88
namespace Datadog.Trace.SourceGenerators;
99

1010
/// <summary>
11-
/// A marker attribute added to a public API to indicate it should only be
12-
/// called by consumers. Used by analyzers to confirm we're not calling a public API method.
11+
/// A marker attribute added to APIs to indicate they should only be
12+
/// used for testing. Used by analyzers to confirm we're not calling testing convenience methods.
1313
/// </summary>
1414
[System.Diagnostics.Conditional("DEBUG")]
1515
[System.AttributeUsage(
1616
System.AttributeTargets.Field
1717
| System.AttributeTargets.Property
1818
| System.AttributeTargets.Method
1919
| System.AttributeTargets.Constructor)]
20-
internal sealed class PublicApiAttribute : System.Attribute
20+
internal sealed class InternalForTestingAttribute : System.Attribute
2121
{
2222
}

tracer/test/Datadog.Trace.Tools.Analyzers.Tests/PublicApiAnalyzerTests.cs renamed to tracer/test/Datadog.Trace.Tools.Analyzers.Tests/InternalForTestingAnalyzerTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="PublicApiAnalyzerTests.cs" company="Datadog">
1+
// <copyright file="InternalForTestingAnalyzerTests.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -12,18 +12,18 @@
1212
using Microsoft.CodeAnalysis.Testing;
1313
using Xunit;
1414
using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier<
15-
Datadog.Trace.Tools.Analyzers.PublicApiAnalyzer.PublicApiAnalyzer,
15+
Datadog.Trace.Tools.Analyzers.InternalForTestingAnalyzer.InternalForTestingAnalyzer,
1616
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
1717

1818
namespace Datadog.Trace.Tools.Analyzers.Tests
1919
{
20-
public class PublicApiAnalyzerTests
20+
public class InternalForTestingAnalyzerTests
2121
{
22-
private const string DiagnosticId = PublicApiAnalyzer.PublicApiAnalyzer.DiagnosticId;
22+
private const string DiagnosticId = InternalForTestingAnalyzer.InternalForTestingAnalyzer.DiagnosticId;
2323

24-
public static string[] GetPublicApiAttributes { get; } = { "PublicApi", "PublicApiAttribute" };
24+
public static string[] GetInternalForTestingAttributes { get; } = { "InternalForTesting", "InternalForTestingAttribute" };
2525

26-
public static string[] NonPublicApiAccesses { get; } =
26+
public static string[] NonInternalForTestingAccesses { get; } =
2727
{
2828
"var x = _nonPublicField;",
2929
"var x = NonPublicProperty;",
@@ -40,7 +40,7 @@ public class PublicApiAnalyzerTests
4040
"var x = new NonPublicClass();",
4141
};
4242

43-
public static string[] PublicApiAccesses { get; } =
43+
public static string[] InternalForTestingAccesses { get; } =
4444
{
4545
"var x = {|#0:_publicField|};",
4646
"var x = {|#0:PublicProperty|};",
@@ -66,21 +66,21 @@ public class PublicApiAnalyzerTests
6666
};
6767

6868
public static IEnumerable<object[]> NonPublicCombination { get; } =
69-
from attrs in GetPublicApiAttributes
69+
from attrs in GetInternalForTestingAttributes
7070
from includeNamespace in new[] { true, false }
71-
from api in NonPublicApiAccesses
71+
from api in NonInternalForTestingAccesses
7272
from conditional in new[] { true, false }
7373
select new object[] { attrs, includeNamespace, api, conditional };
7474

7575
public static IEnumerable<object[]> PublicCombination { get; } =
76-
from attrs in GetPublicApiAttributes
76+
from attrs in GetInternalForTestingAttributes
7777
from includeNamespace in new[] { true, false }
78-
from api in PublicApiAccesses
78+
from api in InternalForTestingAccesses
7979
from conditional in new[] { true, false }
8080
select new object[] { attrs, includeNamespace, api, conditional };
8181

8282
public static IEnumerable<object[]> NotSupportedCombination { get; } =
83-
from attrs in GetPublicApiAttributes
83+
from attrs in GetInternalForTestingAttributes
8484
from includeNamespace in new[] { true, false }
8585
from api in ShouldThrowButDoesnt
8686
from conditional in new[] { true, false }
@@ -97,7 +97,7 @@ public async Task EmptySourceShouldNotHaveDiagnostics()
9797

9898
[Theory]
9999
[MemberData(nameof(NonPublicCombination))]
100-
public async Task ShouldNotFlagUsageOfNonPublicApi(string publicAttribute, bool includeNamespace, string testFragment, bool attributeIsConditional)
100+
public async Task ShouldNotFlagUsageOfNonInternalForTesting(string publicAttribute, bool includeNamespace, string testFragment, bool attributeIsConditional)
101101
{
102102
var code = GetSampleCode(publicAttribute, includeNamespace, testFragment, attributeIsConditional);
103103

@@ -106,7 +106,7 @@ public async Task ShouldNotFlagUsageOfNonPublicApi(string publicAttribute, bool
106106

107107
[Theory]
108108
[MemberData(nameof(PublicCombination))]
109-
public async Task ShouldFlagUsageOfPublicApi(string publicAttribute, bool includeNamespace, string testFragment, bool attributeIsConditional)
109+
public async Task ShouldFlagUsageOfInternalForTesting(string publicAttribute, bool includeNamespace, string testFragment, bool attributeIsConditional)
110110
{
111111
var code = GetSampleCode(publicAttribute, includeNamespace, testFragment, attributeIsConditional);
112112

0 commit comments

Comments
 (0)