Skip to content

Commit e640e79

Browse files
authored
Respect consolidated views in all document classifiers (#31008)
* Respect consolidated views in all document classifiers * Address feedback from peer review
1 parent a3a8f45 commit e640e79

6 files changed

+51
-114
lines changed

src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/ConsolidatedMvcViewDocumentClassifierPass.cs

-90
This file was deleted.

src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/MvcViewDocumentClassifierPass.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
99
{
1010
public class MvcViewDocumentClassifierPass : DocumentClassifierPassBase
1111
{
12+
private bool _useConsolidatedMvcViews = false;
13+
1214
public static readonly string MvcViewDocumentKind = "mvc.1.0.view";
1315

1416
protected override string DocumentKind => MvcViewDocumentKind;
1517

1618
protected override bool IsMatch(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) => true;
1719

20+
public MvcViewDocumentClassifierPass() : this(false) { }
21+
22+
public MvcViewDocumentClassifierPass(bool useConsolidatedMvcViews)
23+
{
24+
_useConsolidatedMvcViews = useConsolidatedMvcViews;
25+
}
26+
1827
protected override void OnDocumentStructureCreated(
1928
RazorCodeDocument codeDocument,
2029
NamespaceDeclarationIntermediateNode @namespace,
@@ -25,7 +34,7 @@ protected override void OnDocumentStructureCreated(
2534

2635
if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var namespaceName))
2736
{
28-
@namespace.Content = "AspNetCore";
37+
@namespace.Content = _useConsolidatedMvcViews ? "AspNetCoreGeneratedDocument" : "AspNetCore";
2938
}
3039
else
3140
{
@@ -47,7 +56,15 @@ protected override void OnDocumentStructureCreated(
4756

4857
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
4958
@class.Modifiers.Clear();
50-
@class.Modifiers.Add("public");
59+
if (_useConsolidatedMvcViews)
60+
{
61+
@class.Modifiers.Add("internal");
62+
@class.Modifiers.Add("sealed");
63+
}
64+
else
65+
{
66+
@class.Modifiers.Add("public");
67+
}
5168

5269
method.MethodName = "ExecuteAsync";
5370
method.Modifiers.Clear();
@@ -87,4 +104,4 @@ private static string GetClassNameFromPath(string path)
87104
return CSharpIdentifier.SanitizeIdentifier(path);
88105
}
89106
}
90-
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
#nullable enable
2-
Microsoft.AspNetCore.Mvc.Razor.Extensions.ConsolidatedMvcViewDocumentClassifierPass
3-
Microsoft.AspNetCore.Mvc.Razor.Extensions.ConsolidatedMvcViewDocumentClassifierPass.ConsolidatedMvcViewDocumentClassifierPass() -> void
4-
~static readonly Microsoft.AspNetCore.Mvc.Razor.Extensions.ConsolidatedMvcViewDocumentClassifierPass.MvcViewDocumentKind -> string
2+
Microsoft.AspNetCore.Mvc.Razor.Extensions.MvcViewDocumentClassifierPass.MvcViewDocumentClassifierPass(bool useConsolidatedMvcViews) -> void
3+
Microsoft.AspNetCore.Mvc.Razor.Extensions.RazorPageDocumentClassifierPass.RazorPageDocumentClassifierPass(bool useConsolidatedMvcViews) -> void

src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/RazorExtensions.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,9 @@ public static void Register(RazorProjectEngineBuilder builder)
3737
builder.Features.Add(new PagesPropertyInjectionPass());
3838
builder.Features.Add(new ViewComponentTagHelperPass());
3939
builder.Features.Add(new RazorPageDocumentClassifierPass());
40-
41-
if (builder.Configuration.UseConsolidatedMvcViews)
42-
{
43-
builder.Features.Add(new ConsolidatedMvcViewDocumentClassifierPass());
44-
}
45-
else
46-
{
47-
builder.Features.Add(new MvcViewDocumentClassifierPass());
48-
}
40+
41+
builder.Features.Add(new RazorPageDocumentClassifierPass(builder.Configuration.UseConsolidatedMvcViews));
42+
builder.Features.Add(new MvcViewDocumentClassifierPass(builder.Configuration.UseConsolidatedMvcViews));
4943

5044
builder.Features.Add(new MvcImportProjectFeature());
5145

src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/RazorPageDocumentClassifierPass.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
1111
{
1212
public class RazorPageDocumentClassifierPass : DocumentClassifierPassBase
1313
{
14+
private bool _useConsolidatedMvcViews = false;
15+
1416
public static readonly string RazorPageDocumentKind = "mvc.1.0.razor-page";
1517
public static readonly string RouteTemplateKey = "RouteTemplate";
1618

19+
public RazorPageDocumentClassifierPass() : this(false) { }
20+
21+
public RazorPageDocumentClassifierPass(bool useConsolidatedMvcViews)
22+
{
23+
_useConsolidatedMvcViews = useConsolidatedMvcViews;
24+
}
25+
1726
private static readonly RazorProjectEngine LeadingDirectiveParsingEngine = RazorProjectEngine.Create(
1827
RazorConfiguration.Create(RazorLanguageVersion.Version_3_0, "leading-directive-parser", Array.Empty<RazorExtension>()),
1928
RazorProjectFileSystem.Create("/"),
@@ -50,7 +59,7 @@ protected override void OnDocumentStructureCreated(
5059

5160
if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var namespaceName))
5261
{
53-
@namespace.Content = "AspNetCore";
62+
@namespace.Content = _useConsolidatedMvcViews ? "AspNetCoreGeneratedDocument" : "AspNetCore";
5463
}
5564
else
5665
{
@@ -71,7 +80,15 @@ protected override void OnDocumentStructureCreated(
7180

7281
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page";
7382
@class.Modifiers.Clear();
74-
@class.Modifiers.Add("public");
83+
if (_useConsolidatedMvcViews)
84+
{
85+
@class.Modifiers.Add("internal");
86+
@class.Modifiers.Add("sealed");
87+
}
88+
else
89+
{
90+
@class.Modifiers.Add("public");
91+
}
7592

7693
method.MethodName = "ExecuteAsync";
7794
method.Modifiers.Clear();
@@ -178,4 +195,4 @@ private static string GetClassNameFromPath(string path)
178195
return CSharpIdentifier.SanitizeIdentifier(path);
179196
}
180197
}
181-
}
198+
}

src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/ConsolidatedMvcViewDocumentClassifierPassTest.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsDifferentNamespace()
1919

2020
var projectEngine = CreateProjectEngine();
2121
var irDocument = CreateIRDocument(projectEngine, codeDocument);
22-
var pass = new ConsolidatedMvcViewDocumentClassifierPass
22+
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
2323
{
2424
Engine = projectEngine.Engine
2525
};
@@ -42,7 +42,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsClass()
4242

4343
var projectEngine = CreateProjectEngine();
4444
var irDocument = CreateIRDocument(projectEngine, codeDocument);
45-
var pass = new ConsolidatedMvcViewDocumentClassifierPass
45+
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
4646
{
4747
Engine = projectEngine.Engine
4848
};
@@ -67,7 +67,7 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
6767

6868
var projectEngine = CreateProjectEngine();
6969
var irDocument = CreateIRDocument(projectEngine, codeDocument);
70-
var pass = new ConsolidatedMvcViewDocumentClassifierPass
70+
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
7171
{
7272
Engine = projectEngine.Engine
7373
};
@@ -86,15 +86,15 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
8686
[Theory]
8787
[InlineData("/Views/Home/Index.cshtml", "_Views_Home_Index")]
8888
[InlineData("/Areas/MyArea/Views/Home/About.cshtml", "_Areas_MyArea_Views_Home_About")]
89-
public void MvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
89+
public void ConsolidatedMvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
9090
{
9191
// Arrange
9292
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
9393
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));
9494

9595
var projectEngine = CreateProjectEngine();
9696
var irDocument = CreateIRDocument(projectEngine, codeDocument);
97-
var pass = new ConsolidatedMvcViewDocumentClassifierPass
97+
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
9898
{
9999
Engine = projectEngine.Engine
100100
};
@@ -117,7 +117,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod()
117117

118118
var projectEngine = CreateProjectEngine();
119119
var irDocument = CreateIRDocument(projectEngine, codeDocument);
120-
var pass = new ConsolidatedMvcViewDocumentClassifierPass
120+
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
121121
{
122122
Engine = projectEngine.Engine
123123
};

0 commit comments

Comments
 (0)