Skip to content

Commit 9abf75c

Browse files
Replace "file kind" strings with an enum (#11722)
Throughout the compiler and tooling Razor files have a "kind" that is represented as a string. Prior to the Razor API being taken internal, using a string made it theoretically possible for the compiler to be extended to file types other than those that the Razor compiler already knows about. With extensibility no longer a priority, file kinds can be represented as an enum (`RazorFileKind`) that provides the values that the compiler supports, e.g.. `Component`, `ComponentImport`, `Legacy`. This makes checks for the file kind much cheaper, since they were all string comparisons before. There are a lot of files changed, but much of the work is mechanical. > [!IMPORTANT] > This is a breaking change for the RazorSDK. When the Razor compiler flows to the .NET SDK, it will introduce build errors that can be fixed by applying this commit: dotnet/sdk@f33a5e7. VS Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/626687
2 parents ac21bbe + 2abd127 commit 9abf75c

File tree

191 files changed

+2942
-3033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

191 files changed

+2942
-3033
lines changed

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/CreateNewOnMetadataUpdateAttributePassTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Execute_NoOpsForBlazorComponents()
5757
{
5858
// Arrange
5959
var source = TestRazorSourceDocument.Create("Hello world", filePath: "ignored", relativePath: "Test.razor");
60-
var codeDocument = ProjectEngine.CreateCodeDocument(source, FileKinds.Component);
60+
var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
6161
var processor = CreateCodeDocumentProcessor(codeDocument);
6262

6363
// Act

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void Basic_Runtime()
223223
public void BasicComponent_Runtime()
224224
{
225225
// Arrange
226-
var projectItem = CreateProjectItemFromFile(fileKind: FileKinds.Component);
226+
var projectItem = CreateProjectItemFromFile(fileKind: RazorFileKind.Component);
227227

228228
// Act
229229
var compiled = CompileToAssembly(projectItem, designTime: false);
@@ -1137,7 +1137,7 @@ public void Basic_DesignTime()
11371137
public void BasicComponent_DesignTime()
11381138
{
11391139
// Arrange
1140-
var projectItem = CreateProjectItemFromFile(fileKind: FileKinds.Component);
1140+
var projectItem = CreateProjectItemFromFile(fileKind: RazorFileKind.Component);
11411141

11421142
// Act
11431143
var compiled = CompileToAssembly(projectItem, designTime: true);

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/Legacy/HtmlAttributeTest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
6-
using System;
74
using Xunit;
85

96
namespace Microsoft.AspNetCore.Razor.Language.Legacy;
@@ -305,12 +302,12 @@ public void ConditionalAttributesWithWeirdSpacingAreDisabledForDataAttributesInD
305302
[Fact]
306303
public void ComponentFileKind_ParsesDirectiveAttributesAsMarkup()
307304
{
308-
ParseDocumentTest("<span @class='@foo'></span>", fileKind: FileKinds.Component);
305+
ParseDocumentTest("<span @class='@foo'></span>", RazorFileKind.Component);
309306
}
310307

311308
[Fact]
312309
public void ComponentFileKind_ParsesDirectiveAttributesWithParameterAsMarkup()
313310
{
314-
ParseDocumentTest("<span @class:param='@foo'></span>", fileKind: FileKinds.Component);
311+
ParseDocumentTest("<span @class:param='@foo'></span>", RazorFileKind.Component);
315312
}
316313
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/Legacy/TagHelperBlockRewriterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ public void FeatureDisabled_AddsErrorForMinimizedBooleanBoundAttributes()
22072207
];
22082208

22092209
// Act & Assert
2210-
EvaluateData(descriptors, document, languageVersion: RazorLanguageVersion.Version_2_0, fileKind: FileKinds.Legacy);
2210+
EvaluateData(descriptors, document, languageVersion: RazorLanguageVersion.Version_2_0, fileKind: RazorFileKind.Legacy);
22112211
}
22122212

22132213
[Fact]

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/Legacy/TagHelperParseTreeRewriterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ public void DoesntAllowSimpleHtmlCommentsAsChildrenWhenFeatureFlagIsOff()
760760
descriptors,
761761
document,
762762
languageVersion: RazorLanguageVersion.Version_2_0,
763-
fileKind: FileKinds.Legacy);
763+
fileKind: RazorFileKind.Legacy);
764764
}
765765

766766
[Fact]

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/Legacy/TagHelperRewritingTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal void EvaluateData(
3939
string documentContent,
4040
string tagHelperPrefix = null,
4141
RazorLanguageVersion languageVersion = null,
42-
string fileKind = null,
42+
RazorFileKind? fileKind = null,
4343
Action<RazorParserOptions.Builder> configureParserOptions = null)
4444
{
4545
var syntaxTree = ParseDocument(languageVersion, documentContent, directives: null, fileKind: fileKind, configureParserOptions: configureParserOptions);

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentDocumentClassifierPassTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void Execute_SetsDocumentKind()
2020
{
2121
// Arrange
2222
var source = TestRazorSourceDocument.Create("some-content", filePath: "Test.razor");
23-
var codeDocument = ProjectEngine.CreateCodeDocument(source, FileKinds.Component);
23+
var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
2424
var processor = CreateCodeDocumentProcessor(codeDocument);
2525

2626
// Act
@@ -42,7 +42,7 @@ public void ComponentDocumentClassifierPass_SetsNamespace()
4242
});
4343

4444
var source = TestRazorSourceDocument.Create("some-content", filePath: "/MyApp/Test.razor", relativePath: "Test.razor");
45-
var codeDocument = projectEngine.CreateCodeDocument(source, FileKinds.Component);
45+
var codeDocument = projectEngine.CreateCodeDocument(source, RazorFileKind.Component);
4646
var processor = CreateCodeDocumentProcessor(projectEngine, codeDocument);
4747

4848
// Act
@@ -65,7 +65,7 @@ public void ComponentDocumentClassifierPass_SetsClass()
6565
});
6666

6767
var source = TestRazorSourceDocument.Create("some-content", filePath: "/MyApp/Test.razor", relativePath: "Test.razor");
68-
var codeDocument = projectEngine.CreateCodeDocument(source, FileKinds.Component);
68+
var codeDocument = projectEngine.CreateCodeDocument(source, RazorFileKind.Component);
6969
var processor = CreateCodeDocumentProcessor(projectEngine, codeDocument);
7070

7171
// Act
@@ -91,7 +91,7 @@ public void ComponentDocumentClassifierPass_UsesRelativePathToGenerateTypeNameAn
9191

9292
var relativePath = "/Pages/Announcements/Banner.razor";
9393
var source = TestRazorSourceDocument.Create("some-content", filePath: $"/MyApp{relativePath}", relativePath: relativePath);
94-
var codeDocument = projectEngine.CreateCodeDocument(source, FileKinds.Component);
94+
var codeDocument = projectEngine.CreateCodeDocument(source, RazorFileKind.Component);
9595
var processor = CreateCodeDocumentProcessor(projectEngine, codeDocument);
9696

9797
// Act
@@ -116,7 +116,7 @@ public void ComponentDocumentClassifierPass_SanitizesClassName()
116116
});
117117

118118
var source = TestRazorSourceDocument.Create("some-content", filePath: @"x:\My.+App\path.with+invalid-chars.razor", relativePath: "path.with+invalid-chars.razor");
119-
var codeDocument = projectEngine.CreateCodeDocument(source, FileKinds.Component);
119+
var codeDocument = projectEngine.CreateCodeDocument(source, RazorFileKind.Component);
120120
var processor = CreateCodeDocumentProcessor(projectEngine, codeDocument);
121121

122122
// Act
@@ -136,7 +136,7 @@ public void ComponentDocumentClassifierPass_SetsUpMainMethod()
136136
{
137137
// Arrange
138138
var source = TestRazorSourceDocument.Create("some-content", filePath: "Test.razor");
139-
var codeDocument = ProjectEngine.CreateCodeDocument(source, FileKinds.Component);
139+
var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
140140
var processor = CreateCodeDocumentProcessor(codeDocument);
141141

142142
// Act

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentDuplicateAttributeDiagnosticPassTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private RazorCodeDocument CreateDocument(string content)
164164
content = content.Replace("\n", "\r\n");
165165

166166
var source = RazorSourceDocument.Create(content, "test.cshtml");
167-
return ProjectEngine.CreateCodeDocument(source, FileKinds.Component);
167+
return ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
168168
}
169169

170170
private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ private RazorCodeDocument CreateDocument(string content)
452452
content = content.Replace("\n", "\r\n");
453453

454454
var source = RazorSourceDocument.Create(content, "test.cshtml");
455-
return ProjectEngine.CreateCodeDocument(source, FileKinds.Component);
455+
return ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
456456
}
457457

458458
private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupEncodingPassTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private RazorCodeDocument CreateDocument(string content)
191191
content = content.Replace("\n", "\r\n");
192192

193193
var source = RazorSourceDocument.Create(content, "test.cshtml");
194-
return ProjectEngine.CreateCodeDocument(source, FileKinds.Component);
194+
return ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
195195
}
196196

197197
private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)

0 commit comments

Comments
 (0)