Skip to content

Commit

Permalink
Respect consolidated views in all document classifiers (#31008)
Browse files Browse the repository at this point in the history
* Respect consolidated views in all document classifiers

* Address feedback from peer review
  • Loading branch information
captainsafia authored Mar 18, 2021
1 parent a3a8f45 commit e640e79
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 114 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
public class MvcViewDocumentClassifierPass : DocumentClassifierPassBase
{
private bool _useConsolidatedMvcViews = false;

public static readonly string MvcViewDocumentKind = "mvc.1.0.view";

protected override string DocumentKind => MvcViewDocumentKind;

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

public MvcViewDocumentClassifierPass() : this(false) { }

public MvcViewDocumentClassifierPass(bool useConsolidatedMvcViews)
{
_useConsolidatedMvcViews = useConsolidatedMvcViews;
}

protected override void OnDocumentStructureCreated(
RazorCodeDocument codeDocument,
NamespaceDeclarationIntermediateNode @namespace,
Expand All @@ -25,7 +34,7 @@ protected override void OnDocumentStructureCreated(

if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var namespaceName))
{
@namespace.Content = "AspNetCore";
@namespace.Content = _useConsolidatedMvcViews ? "AspNetCoreGeneratedDocument" : "AspNetCore";
}
else
{
Expand All @@ -47,7 +56,15 @@ protected override void OnDocumentStructureCreated(

@class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
@class.Modifiers.Clear();
@class.Modifiers.Add("public");
if (_useConsolidatedMvcViews)
{
@class.Modifiers.Add("internal");
@class.Modifiers.Add("sealed");
}
else
{
@class.Modifiers.Add("public");
}

method.MethodName = "ExecuteAsync";
method.Modifiers.Clear();
Expand Down Expand Up @@ -87,4 +104,4 @@ private static string GetClassNameFromPath(string path)
return CSharpIdentifier.SanitizeIdentifier(path);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable enable
Microsoft.AspNetCore.Mvc.Razor.Extensions.ConsolidatedMvcViewDocumentClassifierPass
Microsoft.AspNetCore.Mvc.Razor.Extensions.ConsolidatedMvcViewDocumentClassifierPass.ConsolidatedMvcViewDocumentClassifierPass() -> void
~static readonly Microsoft.AspNetCore.Mvc.Razor.Extensions.ConsolidatedMvcViewDocumentClassifierPass.MvcViewDocumentKind -> string
Microsoft.AspNetCore.Mvc.Razor.Extensions.MvcViewDocumentClassifierPass.MvcViewDocumentClassifierPass(bool useConsolidatedMvcViews) -> void
Microsoft.AspNetCore.Mvc.Razor.Extensions.RazorPageDocumentClassifierPass.RazorPageDocumentClassifierPass(bool useConsolidatedMvcViews) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ public static void Register(RazorProjectEngineBuilder builder)
builder.Features.Add(new PagesPropertyInjectionPass());
builder.Features.Add(new ViewComponentTagHelperPass());
builder.Features.Add(new RazorPageDocumentClassifierPass());

if (builder.Configuration.UseConsolidatedMvcViews)
{
builder.Features.Add(new ConsolidatedMvcViewDocumentClassifierPass());
}
else
{
builder.Features.Add(new MvcViewDocumentClassifierPass());
}

builder.Features.Add(new RazorPageDocumentClassifierPass(builder.Configuration.UseConsolidatedMvcViews));
builder.Features.Add(new MvcViewDocumentClassifierPass(builder.Configuration.UseConsolidatedMvcViews));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
public class RazorPageDocumentClassifierPass : DocumentClassifierPassBase
{
private bool _useConsolidatedMvcViews = false;

public static readonly string RazorPageDocumentKind = "mvc.1.0.razor-page";
public static readonly string RouteTemplateKey = "RouteTemplate";

public RazorPageDocumentClassifierPass() : this(false) { }

public RazorPageDocumentClassifierPass(bool useConsolidatedMvcViews)
{
_useConsolidatedMvcViews = useConsolidatedMvcViews;
}

private static readonly RazorProjectEngine LeadingDirectiveParsingEngine = RazorProjectEngine.Create(
RazorConfiguration.Create(RazorLanguageVersion.Version_3_0, "leading-directive-parser", Array.Empty<RazorExtension>()),
RazorProjectFileSystem.Create("/"),
Expand Down Expand Up @@ -50,7 +59,7 @@ protected override void OnDocumentStructureCreated(

if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var namespaceName))
{
@namespace.Content = "AspNetCore";
@namespace.Content = _useConsolidatedMvcViews ? "AspNetCoreGeneratedDocument" : "AspNetCore";
}
else
{
Expand All @@ -71,7 +80,15 @@ protected override void OnDocumentStructureCreated(

@class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page";
@class.Modifiers.Clear();
@class.Modifiers.Add("public");
if (_useConsolidatedMvcViews)
{
@class.Modifiers.Add("internal");
@class.Modifiers.Add("sealed");
}
else
{
@class.Modifiers.Add("public");
}

method.MethodName = "ExecuteAsync";
method.Modifiers.Clear();
Expand Down Expand Up @@ -178,4 +195,4 @@ private static string GetClassNameFromPath(string path)
return CSharpIdentifier.SanitizeIdentifier(path);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsDifferentNamespace()

var projectEngine = CreateProjectEngine();
var irDocument = CreateIRDocument(projectEngine, codeDocument);
var pass = new ConsolidatedMvcViewDocumentClassifierPass
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
{
Engine = projectEngine.Engine
};
Expand All @@ -42,7 +42,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsClass()

var projectEngine = CreateProjectEngine();
var irDocument = CreateIRDocument(projectEngine, codeDocument);
var pass = new ConsolidatedMvcViewDocumentClassifierPass
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
{
Engine = projectEngine.Engine
};
Expand All @@ -67,7 +67,7 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()

var projectEngine = CreateProjectEngine();
var irDocument = CreateIRDocument(projectEngine, codeDocument);
var pass = new ConsolidatedMvcViewDocumentClassifierPass
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
{
Engine = projectEngine.Engine
};
Expand All @@ -86,15 +86,15 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
[Theory]
[InlineData("/Views/Home/Index.cshtml", "_Views_Home_Index")]
[InlineData("/Areas/MyArea/Views/Home/About.cshtml", "_Areas_MyArea_Views_Home_About")]
public void MvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
public void ConsolidatedMvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
{
// Arrange
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

var projectEngine = CreateProjectEngine();
var irDocument = CreateIRDocument(projectEngine, codeDocument);
var pass = new ConsolidatedMvcViewDocumentClassifierPass
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
{
Engine = projectEngine.Engine
};
Expand All @@ -117,7 +117,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod()

var projectEngine = CreateProjectEngine();
var irDocument = CreateIRDocument(projectEngine, codeDocument);
var pass = new ConsolidatedMvcViewDocumentClassifierPass
var pass = new MvcViewDocumentClassifierPass(useConsolidatedMvcViews: true)
{
Engine = projectEngine.Engine
};
Expand Down

0 comments on commit e640e79

Please sign in to comment.