-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Consolidated view classifier to make view types internal #30976
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Language.Intermediate; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions | ||
{ | ||
public sealed class ConsolidatedMvcViewDocumentClassifierPass : DocumentClassifierPassBase | ||
{ | ||
public static readonly string MvcViewDocumentKind = "mvc.1.0.view"; | ||
|
||
protected override string DocumentKind => MvcViewDocumentKind; | ||
|
||
protected override bool IsMatch(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) => true; | ||
|
||
protected override void OnDocumentStructureCreated( | ||
RazorCodeDocument codeDocument, | ||
NamespaceDeclarationIntermediateNode @namespace, | ||
ClassDeclarationIntermediateNode @class, | ||
MethodDeclarationIntermediateNode method) | ||
{ | ||
base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method); | ||
|
||
if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var namespaceName)) | ||
{ | ||
@namespace.Content = "AspNetCoreGeneratedDocument"; | ||
} | ||
else | ||
{ | ||
@namespace.Content = namespaceName; | ||
} | ||
|
||
if (!TryComputeClassName(codeDocument, out var className)) | ||
{ | ||
// It's possible for a Razor document to not have a file path. | ||
// Eg. When we try to generate code for an in memory document like default imports. | ||
var checksum = Checksum.BytesToString(codeDocument.Source.GetChecksum()); | ||
@class.ClassName = $"AspNetCore_{checksum}"; | ||
} | ||
else | ||
{ | ||
@class.ClassName = className; | ||
} | ||
|
||
@class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>"; | ||
@class.Modifiers.Clear(); | ||
@class.Modifiers.Add("internal"); | ||
@class.Modifiers.Add("sealed"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI @benaadams There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😍 Finally! 😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be interested to see if this shows up in benchmarks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm super glad about this tooooo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure MVC views/pages are currently running in aspnet/benchmarks 😢 |
||
|
||
method.MethodName = "ExecuteAsync"; | ||
method.Modifiers.Clear(); | ||
method.Modifiers.Add("public"); | ||
method.Modifiers.Add("async"); | ||
method.Modifiers.Add("override"); | ||
method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}"; | ||
} | ||
|
||
private bool TryComputeClassName(RazorCodeDocument codeDocument, out string className) | ||
{ | ||
var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath; | ||
if (string.IsNullOrEmpty(filePath)) | ||
{ | ||
className = null; | ||
return false; | ||
} | ||
|
||
className = GetClassNameFromPath(filePath); | ||
return true; | ||
} | ||
|
||
private static string GetClassNameFromPath(string path) | ||
{ | ||
const string cshtmlExtension = ".cshtml"; | ||
|
||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
return path; | ||
} | ||
|
||
if (path.EndsWith(cshtmlExtension, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
path = path.Substring(0, path.Length - cshtmlExtension.Length); | ||
} | ||
|
||
return CSharpIdentifier.SanitizeIdentifier(path); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
#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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Language.Intermediate; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions | ||
{ | ||
public class ConsolidatedMvcViewDocumentClassifierPassTest : RazorProjectEngineTestBase | ||
{ | ||
protected override RazorLanguageVersion Version => RazorLanguageVersion.Latest; | ||
|
||
[Fact] | ||
public void ConsolidatedMvcViewDocumentClassifierPass_SetsDifferentNamespace() | ||
{ | ||
// Arrange | ||
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml")); | ||
|
||
var projectEngine = CreateProjectEngine(); | ||
var irDocument = CreateIRDocument(projectEngine, codeDocument); | ||
var pass = new ConsolidatedMvcViewDocumentClassifierPass | ||
{ | ||
Engine = projectEngine.Engine | ||
}; | ||
|
||
// Act | ||
pass.Execute(codeDocument, irDocument); | ||
var visitor = new Visitor(); | ||
visitor.Visit(irDocument); | ||
|
||
// Assert | ||
Assert.Equal("AspNetCoreGeneratedDocument", visitor.Namespace.Content); | ||
} | ||
|
||
[Fact] | ||
public void ConsolidatedMvcViewDocumentClassifierPass_SetsClass() | ||
{ | ||
// Arrange | ||
var properties = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml"); | ||
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties)); | ||
|
||
var projectEngine = CreateProjectEngine(); | ||
var irDocument = CreateIRDocument(projectEngine, codeDocument); | ||
var pass = new ConsolidatedMvcViewDocumentClassifierPass | ||
{ | ||
Engine = projectEngine.Engine | ||
}; | ||
|
||
// Act | ||
pass.Execute(codeDocument, irDocument); | ||
var visitor = new Visitor(); | ||
visitor.Visit(irDocument); | ||
|
||
// Assert | ||
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType); | ||
Assert.Equal(new[] { "internal", "sealed" }, visitor.Class.Modifiers); | ||
Assert.Equal("Test", visitor.Class.ClassName); | ||
} | ||
|
||
[Fact] | ||
public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass() | ||
{ | ||
// Arrange | ||
var properties = new RazorSourceDocumentProperties(filePath: null, relativePath: null); | ||
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties)); | ||
|
||
var projectEngine = CreateProjectEngine(); | ||
var irDocument = CreateIRDocument(projectEngine, codeDocument); | ||
var pass = new ConsolidatedMvcViewDocumentClassifierPass | ||
{ | ||
Engine = projectEngine.Engine | ||
}; | ||
|
||
// Act | ||
pass.Execute(codeDocument, irDocument); | ||
var visitor = new Visitor(); | ||
visitor.Visit(irDocument); | ||
|
||
// Assert | ||
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType); | ||
Assert.Equal(new[] { "internal", "sealed" }, visitor.Class.Modifiers); | ||
Assert.Equal("AspNetCore_d9f877a857a7e9928eac04d09a59f25967624155", visitor.Class.ClassName); | ||
} | ||
|
||
[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) | ||
{ | ||
// 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 | ||
{ | ||
Engine = projectEngine.Engine | ||
}; | ||
|
||
// Act | ||
pass.Execute(codeDocument, irDocument); | ||
var visitor = new Visitor(); | ||
visitor.Visit(irDocument); | ||
|
||
// Assert | ||
Assert.Equal(expected, visitor.Class.ClassName); | ||
Assert.Equal(new[] { "internal", "sealed" }, visitor.Class.Modifiers); | ||
} | ||
|
||
[Fact] | ||
public void ConsolidatedMvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod() | ||
{ | ||
// Arrange | ||
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml")); | ||
|
||
var projectEngine = CreateProjectEngine(); | ||
var irDocument = CreateIRDocument(projectEngine, codeDocument); | ||
var pass = new ConsolidatedMvcViewDocumentClassifierPass | ||
{ | ||
Engine = projectEngine.Engine | ||
}; | ||
|
||
// Act | ||
pass.Execute(codeDocument, irDocument); | ||
var visitor = new Visitor(); | ||
visitor.Visit(irDocument); | ||
|
||
// Assert | ||
Assert.Equal("ExecuteAsync", visitor.Method.MethodName); | ||
Assert.Equal("global::System.Threading.Tasks.Task", visitor.Method.ReturnType); | ||
Assert.Equal(new[] { "public", "async", "override" }, visitor.Method.Modifiers); | ||
} | ||
|
||
private static DocumentIntermediateNode CreateIRDocument(RazorProjectEngine projectEngine, RazorCodeDocument codeDocument) | ||
{ | ||
for (var i = 0; i < projectEngine.Phases.Count; i++) | ||
{ | ||
var phase = projectEngine.Phases[i]; | ||
phase.Execute(codeDocument); | ||
|
||
if (phase is IRazorIntermediateNodeLoweringPhase) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return codeDocument.GetDocumentIntermediateNode(); | ||
} | ||
|
||
private class Visitor : IntermediateNodeWalker | ||
{ | ||
public NamespaceDeclarationIntermediateNode Namespace { get; private set; } | ||
|
||
public ClassDeclarationIntermediateNode Class { get; private set; } | ||
|
||
public MethodDeclarationIntermediateNode Method { get; private set; } | ||
|
||
public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node) | ||
{ | ||
Method = node; | ||
} | ||
|
||
public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateNode node) | ||
{ | ||
Namespace = node; | ||
base.VisitNamespaceDeclaration(node); | ||
} | ||
|
||
public override void VisitClassDeclaration(ClassDeclarationIntermediateNode node) | ||
{ | ||
Class = node; | ||
base.VisitClassDeclaration(node); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know MVC is still a 1.0 :)