Skip to content

Commit e664422

Browse files
Return non-nullable ImmutableArray<T> from GetImportSyntaxTrees()
1 parent e2839bf commit e664422

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument)
122122
document.Diagnostics.Add(syntaxTree.Diagnostics[i]);
123123
}
124124

125-
if (imports is ImmutableArray<RazorSyntaxTree> importsArray)
125+
if (imports is { IsDefault: false } importsArray)
126126
{
127127
foreach (var import in importsArray)
128128
{
@@ -165,15 +165,15 @@ private static IReadOnlyList<UsingReference> ImportDirectives(
165165
DocumentIntermediateNode document,
166166
IntermediateNodeBuilder builder,
167167
RazorParserOptions options,
168-
ImmutableArray<RazorSyntaxTree>? imports)
168+
ImmutableArray<RazorSyntaxTree> imports)
169169
{
170-
if (imports == null)
170+
if (imports.IsDefaultOrEmpty)
171171
{
172172
return Array.Empty<UsingReference>();
173173
}
174174

175175
var importsVisitor = new ImportsVisitor(document, builder, options.FeatureFlags);
176-
foreach (var import in imports.GetValueOrDefault())
176+
foreach (var import in imports)
177177
{
178178
importsVisitor.SourceDocument = import.Source;
179179
importsVisitor.Visit(import.Root);

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorTagHelperContextDiscoveryPhase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument)
5151
visitor = new TagHelperDirectiveVisitor(descriptors);
5252
}
5353

54-
if (codeDocument.GetImportSyntaxTrees() is ImmutableArray<RazorSyntaxTree> imports)
54+
if (codeDocument.GetImportSyntaxTrees() is { IsDefault: false } imports)
5555
{
5656
foreach (var import in imports)
5757
{

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ virtual Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.OnInitialized()
360360
~static Microsoft.AspNetCore.Razor.Language.BoundAttributeDescriptorExtensions.GetGloballyQualifiedTypeName(this Microsoft.AspNetCore.Razor.Language.BoundAttributeDescriptor attribute) -> string
361361
~static Microsoft.AspNetCore.Razor.Language.DirectiveDescriptorBuilderExtensions.AddIdentifierOrExpression(this Microsoft.AspNetCore.Razor.Language.IDirectiveDescriptorBuilder builder, string name, string description) -> Microsoft.AspNetCore.Razor.Language.IDirectiveDescriptorBuilder
362362
~static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeExtensions.IsDesignTimePropertyAccessHelper(this Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNode tagHelper) -> bool
363-
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.GetImportSyntaxTrees(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document) -> System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree>?
363+
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.GetImportSyntaxTrees(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document) -> System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree>
364364
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.GetPreTagHelperSyntaxTree(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document) -> Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree
365365
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.SetImportSyntaxTrees(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document, System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree> syntaxTrees) -> void
366366
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.SetPreTagHelperSyntaxTree(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document, Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree syntaxTree) -> void

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeDocumentExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public static void SetSyntaxTree(this RazorCodeDocument document, RazorSyntaxTre
122122
document.Items[typeof(RazorSyntaxTree)] = syntaxTree;
123123
}
124124

125-
public static ImmutableArray<RazorSyntaxTree>? GetImportSyntaxTrees(this RazorCodeDocument document)
125+
public static ImmutableArray<RazorSyntaxTree> GetImportSyntaxTrees(this RazorCodeDocument document)
126126
{
127127
if (document == null)
128128
{
129129
throw new ArgumentNullException(nameof(document));
130130
}
131131

132-
return (document.Items[typeof(ImportSyntaxTreesHolder)] as ImportSyntaxTreesHolder)?.SyntaxTrees;
132+
return (document.Items[typeof(ImportSyntaxTreesHolder)] as ImportSyntaxTreesHolder)?.SyntaxTrees ?? default;
133133
}
134134

135135
public static void SetImportSyntaxTrees(this RazorCodeDocument document, ImmutableArray<RazorSyntaxTree> syntaxTrees)
@@ -139,6 +139,11 @@ public static void SetImportSyntaxTrees(this RazorCodeDocument document, Immutab
139139
throw new ArgumentNullException(nameof(document));
140140
}
141141

142+
if (syntaxTrees.IsDefault)
143+
{
144+
throw new ArgumentException("", nameof(syntaxTrees));
145+
}
146+
142147
document.Items[typeof(ImportSyntaxTreesHolder)] = new ImportSyntaxTreesHolder(syntaxTrees);
143148
}
144149

@@ -343,7 +348,7 @@ bool TryComputeNamespaceCore(RazorCodeDocument document, bool fallbackToRootName
343348
var lastNamespaceContent = string.Empty;
344349
var lastNamespaceLocation = SourceSpan.Undefined;
345350

346-
if (document.GetImportSyntaxTrees() is ImmutableArray<RazorSyntaxTree> importSyntaxTrees)
351+
if (document.GetImportSyntaxTrees() is { IsDefault: false } importSyntaxTrees)
347352
{
348353
// ImportSyntaxTrees is usually set. Just being defensive.
349354
foreach (var importSyntaxTree in importSyntaxTrees)

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/DefaultRazorParsingPhaseTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ public void Execute_ParsesImports()
7373
phase.Execute(codeDocument);
7474

7575
// Assert
76+
var importSyntaxTrees = codeDocument.GetImportSyntaxTrees();
77+
Assert.False(importSyntaxTrees.IsDefault);
7678
Assert.Collection(
77-
codeDocument.GetImportSyntaxTrees().AssumeNotNull(),
79+
importSyntaxTrees,
7880
t => { Assert.Same(t.Source, imports[0]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); },
7981
t => { Assert.Same(t.Source, imports[1]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); });
8082
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/RazorCodeDocumentExtensionsTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ public void GetAndSetImportSyntaxTrees_ReturnsSyntaxTrees()
5353
codeDocument.SetImportSyntaxTrees(expected);
5454

5555
// Act
56-
var actual = codeDocument.GetImportSyntaxTrees().AssumeNotNull();
56+
var actual = codeDocument.GetImportSyntaxTrees();
5757

5858
// Assert
59+
Assert.False(actual.IsDefault);
5960
Assert.Equal<RazorSyntaxTree>(expected, actual);
6061
}
6162

0 commit comments

Comments
 (0)