Skip to content

Commit 53ae8c2

Browse files
committed
Remove unnecessary null checks
1 parent d6237d6 commit 53ae8c2

File tree

2 files changed

+6
-36
lines changed

2 files changed

+6
-36
lines changed

src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorDocumentMappingBenchmark.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ private bool TryMapToHostDocumentPosition(RazorCSharpDocument csharpDocument, in
129129
throw new ArgumentNullException(nameof(csharpDocument));
130130
}
131131

132-
if (csharpDocument.CodeDocument is not { } codeDocument)
133-
{
134-
throw new InvalidOperationException("Cannot use document mapping service on a generated document that has a null CodeDocument.");
135-
}
136-
137132
foreach (var mapping in csharpDocument.SourceMappings)
138133
{
139134
var generatedSpan = mapping.GeneratedSpan;
@@ -148,7 +143,7 @@ private bool TryMapToHostDocumentPosition(RazorCSharpDocument csharpDocument, in
148143
// Found the generated span that contains the generated absolute index
149144

150145
hostDocumentIndex = mapping.OriginalSpan.AbsoluteIndex + distanceIntoGeneratedSpan;
151-
hostDocumentPosition = codeDocument.Source.Text.GetLinePosition(hostDocumentIndex);
146+
hostDocumentPosition = csharpDocument.CodeDocument.Source.Text.GetLinePosition(hostDocumentIndex);
152147
return true;
153148
}
154149
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/AbstractDocumentMappingService.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ public bool TryMapToRazorDocumentRange(RazorCSharpDocument csharpDocument, LineP
178178

179179
public bool TryMapToCSharpDocumentRange(RazorCSharpDocument csharpDocument, LinePositionSpan razorRange, out LinePositionSpan csharpRange)
180180
{
181-
if (csharpDocument.CodeDocument is not { } codeDocument)
182-
{
183-
throw new InvalidOperationException("Cannot use document mapping service on a generated document that has a null CodeDocument.");
184-
}
185-
186181
csharpRange = default;
187182

188183
if (razorRange.End.Line < razorRange.Start.Line ||
@@ -194,7 +189,7 @@ public bool TryMapToCSharpDocumentRange(RazorCSharpDocument csharpDocument, Line
194189
return false;
195190
}
196191

197-
var sourceText = codeDocument.Source.Text;
192+
var sourceText = csharpDocument.CodeDocument.Source.Text;
198193
var range = razorRange;
199194
if (!IsRangeWithinDocument(range, sourceText))
200195
{
@@ -231,11 +226,6 @@ public bool TryMapToCSharpDocumentRange(RazorCSharpDocument csharpDocument, Line
231226

232227
public bool TryMapToRazorDocumentPosition(RazorCSharpDocument csharpDocument, int csharpIndex, out LinePosition razorPosition, out int razorIndex)
233228
{
234-
if (csharpDocument.CodeDocument is not { } codeDocument)
235-
{
236-
throw new InvalidOperationException("Cannot use document mapping service on a generated document that has a null CodeDocument.");
237-
}
238-
239229
var sourceMappings = csharpDocument.SourceMappings;
240230

241231
// We expect source mappings to be ordered by their generated document absolute index, because that is how the compiler creates them: As it
@@ -268,7 +258,7 @@ public bool TryMapToRazorDocumentPosition(RazorCSharpDocument csharpDocument, in
268258
var distanceIntoGeneratedSpan = csharpIndex - generatedAbsoluteIndex;
269259

270260
razorIndex = mapping.OriginalSpan.AbsoluteIndex + distanceIntoGeneratedSpan;
271-
razorPosition = codeDocument.Source.Text.GetLinePosition(razorIndex);
261+
razorPosition = csharpDocument.CodeDocument.Source.Text.GetLinePosition(razorIndex);
272262
return true;
273263
}
274264

@@ -285,11 +275,6 @@ public bool TryMapToCSharpDocumentPosition(RazorCSharpDocument csharpDocument, i
285275

286276
private static bool TryMapToCSharpDocumentPositionInternal(RazorCSharpDocument csharpDocument, int razorIndex, bool nextCSharpPositionOnFailure, out LinePosition csharpPosition, out int csharpIndex)
287277
{
288-
if (csharpDocument.CodeDocument is not { } codeDocument)
289-
{
290-
throw new InvalidOperationException("Cannot use document mapping service on a generated document that has a null CodeDocument.");
291-
}
292-
293278
foreach (var mapping in csharpDocument.SourceMappings)
294279
{
295280
var originalSpan = mapping.OriginalSpan;
@@ -310,7 +295,7 @@ private static bool TryMapToCSharpDocumentPositionInternal(RazorCSharpDocument c
310295
{
311296
// The "next" C# location is only valid if it is on the same line in the source document
312297
// as the requested position.
313-
var hostDocumentLinePosition = codeDocument.Source.Text.GetLinePosition(razorIndex);
298+
var hostDocumentLinePosition = csharpDocument.CodeDocument.Source.Text.GetLinePosition(razorIndex);
314299

315300
if (mapping.OriginalSpan.LineIndex == hostDocumentLinePosition.Line)
316301
{
@@ -364,11 +349,6 @@ private bool TryMapToRazorDocumentRangeStrict(RazorCSharpDocument csharpDocument
364349

365350
private bool TryMapToRazorDocumentRangeInclusive(RazorCSharpDocument csharpDocument, LinePositionSpan csharpRange, out LinePositionSpan rangeRange)
366351
{
367-
if (csharpDocument.CodeDocument is not { } codeDocument)
368-
{
369-
throw new InvalidOperationException("Cannot use document mapping service on a generated document that has a null CodeDocument.");
370-
}
371-
372352
rangeRange = default;
373353

374354
var csharpSourceText = csharpDocument.Text;
@@ -417,7 +397,7 @@ private bool TryMapToRazorDocumentRangeInclusive(RazorCSharpDocument csharpDocum
417397
// We're intersecting or overlapping a single mapping, lets choose that.
418398

419399
var mapping = candidateMappings[0];
420-
rangeRange = codeDocument.Source.Text.GetLinePositionSpan(mapping.OriginalSpan);
400+
rangeRange = csharpDocument.CodeDocument.Source.Text.GetLinePositionSpan(mapping.OriginalSpan);
421401
return true;
422402
}
423403
else
@@ -442,11 +422,6 @@ bool IntersectsWith(int position, SourceSpan span)
442422

443423
private bool TryMapToRazorDocumentRangeInferred(RazorCSharpDocument csharpDocument, LinePositionSpan csharpRange, out LinePositionSpan razorRange)
444424
{
445-
if (csharpDocument.CodeDocument is not { } codeDocument)
446-
{
447-
throw new InvalidOperationException("Cannot use document mapping service on a generated document that has a null CodeDocument.");
448-
}
449-
450425
// Inferred mapping behavior is a superset of inclusive mapping behavior so if the range is "inclusive" lets use that mapping.
451426
if (TryMapToRazorDocumentRangeInclusive(csharpDocument, csharpRange, out razorRange))
452427
{
@@ -492,7 +467,7 @@ private bool TryMapToRazorDocumentRangeInferred(RazorCSharpDocument csharpDocume
492467
return false;
493468
}
494469

495-
var sourceDocument = codeDocument.Source;
470+
var sourceDocument = csharpDocument.CodeDocument.Source;
496471
var originalSpanBeforeGeneratedRange = mappingBeforeGeneratedRange.OriginalSpan;
497472
var originalEndBeforeGeneratedRange = originalSpanBeforeGeneratedRange.AbsoluteIndex + originalSpanBeforeGeneratedRange.Length;
498473
var inferredStartPosition = sourceDocument.Text.GetLinePosition(originalEndBeforeGeneratedRange);

0 commit comments

Comments
 (0)