Skip to content

Commit a86ecec

Browse files
doc
1 parent 7882c3d commit a86ecec

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ private readonly record struct ApiLine(string Text, TextSpan Span, AdditionalFil
3333

3434
private readonly record struct ApiName(string Name, string NameWithNullability);
3535

36-
/// <param name="NullableRank">Number for the max line where #nullable enable was found (-1 otherwise)</param>
37-
private sealed record ApiData(ImmutableArray<ApiLine> ApiList, ImmutableArray<RemovedApiLine> RemovedApiList, int NullableRank)
36+
/// <param name="NullableLineNumber">Number for the max line where #nullable enable was found (-1 otherwise)</param>
37+
private sealed record ApiData(ImmutableArray<ApiLine> ApiList, ImmutableArray<RemovedApiLine> RemovedApiList, int NullableLineNumber)
3838
{
39-
public static readonly ApiData Empty = new(ImmutableArray<ApiLine>.Empty, ImmutableArray<RemovedApiLine>.Empty, NullableRank: -1);
39+
public static readonly ApiData Empty = new(ImmutableArray<ApiLine>.Empty, ImmutableArray<RemovedApiLine>.Empty, NullableLineNumber: -1);
4040
}
4141

4242
private sealed class Impl
@@ -61,7 +61,7 @@ private static readonly ImmutableArray<MethodKind> s_ignorableMethodKinds
6161
internal Impl(Compilation compilation, ApiData shippedData, ApiData unshippedData, bool isPublic, AnalyzerOptions analyzerOptions)
6262
{
6363
_compilation = compilation;
64-
_useNullability = shippedData.NullableRank >= 0 || unshippedData.NullableRank >= 0;
64+
_useNullability = shippedData.NullableLineNumber >= 0 || unshippedData.NullableLineNumber >= 0;
6565
_unshippedData = unshippedData;
6666

6767
var publicApiMap = new Dictionary<string, ApiLine>(StringComparer.Ordinal);

src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,25 @@ private static ApiData ReadApiData(string path, SourceText sourceText, bool isSh
160160
{
161161
var apiBuilder = ArrayBuilder<ApiLine>.GetInstance();
162162
var removedBuilder = ArrayBuilder<RemovedApiLine>.GetInstance();
163-
var maxNullableRank = -1;
164-
var rank = -1;
163+
var lastNullableLineNumber = -1;
164+
165+
// current line we're on. Note: we ignore whitespace lines when computin gthis.
166+
var lineNumber = -1;
165167

166168
var additionalFileInfo = new AdditionalFileInfo(path, sourceText, isShippedApi);
167169

168170
foreach (var line in sourceText.Lines)
169171
{
172+
// Skip whitespace.
170173
var text = line.ToString();
171174
if (string.IsNullOrWhiteSpace(text))
172175
continue;
173176

174-
rank++;
177+
lineNumber++;
175178

176179
if (text == NullableEnable)
177180
{
178-
maxNullableRank = rank;
181+
lastNullableLineNumber = lineNumber;
179182
continue;
180183
}
181184

@@ -191,7 +194,7 @@ private static ApiData ReadApiData(string path, SourceText sourceText, bool isSh
191194
}
192195
}
193196

194-
return new ApiData(apiBuilder.ToImmutableAndFree(), removedBuilder.ToImmutableAndFree(), maxNullableRank);
197+
return new ApiData(apiBuilder.ToImmutableAndFree(), removedBuilder.ToImmutableAndFree(), lastNullableLineNumber);
195198
}
196199

197200
private static bool TryGetApiData(AnalyzerOptions analyzerOptions, Compilation compilation, bool isPublic, List<Diagnostic> errors, CancellationToken cancellationToken, [NotNullWhen(true)] out ApiData? shippedData, [NotNullWhen(true)] out ApiData? unshippedData)
@@ -263,7 +266,7 @@ static ApiData Flatten(ArrayBuilder<ApiData> allData)
263266
return new ApiData(
264267
apiBuilder.ToImmutableAndFree(),
265268
removedBuilder.ToImmutableAndFree(),
266-
allData.Max(static d => d.NullableRank));
269+
allData.Max(static d => d.NullableLineNumber));
267270
}
268271
}
269272

@@ -379,13 +382,13 @@ private static bool ValidateApiFiles(ApiData shippedData, ApiData unshippedData,
379382
errors.Add(Diagnostic.Create(descriptor, Location.None, InvalidReasonShippedCantHaveRemoved));
380383
}
381384

382-
if (shippedData.NullableRank > 0)
385+
if (shippedData.NullableLineNumber > 0)
383386
{
384387
// '#nullable enable' must be on the first line
385388
errors.Add(Diagnostic.Create(descriptor, Location.None, InvalidReasonMisplacedNullableEnable));
386389
}
387390

388-
if (unshippedData.NullableRank > 0)
391+
if (unshippedData.NullableLineNumber > 0)
389392
{
390393
// '#nullable enable' must be on the first line
391394
errors.Add(Diagnostic.Create(descriptor, Location.None, InvalidReasonMisplacedNullableEnable));

0 commit comments

Comments
 (0)