Skip to content

Commit

Permalink
Fix @inherits mapping for fuse (#10985)
Browse files Browse the repository at this point in the history
* Make base type a token rather than a string
* Track inherits source location and emit it when present
* Add simple inherits test for components
* Add razor page inherits test
  • Loading branch information
chsienki authored Oct 9, 2024
1 parent 6f378ca commit 81d9296
Show file tree
Hide file tree
Showing 53 changed files with 561 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -103,7 +103,7 @@ @model Type2
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -129,7 +129,7 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType", @class.BaseType);
Assert.Equal("BaseType", @class.BaseType.Content);
}

[Fact]
Expand All @@ -154,7 +154,7 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -179,7 +179,7 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down Expand Up @@ -209,7 +209,7 @@ @model SomeType
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<SomeType>", @class.BaseType);
Assert.Equal("BaseType<SomeType>", @class.BaseType.Content);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void MvcViewDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -122,7 +122,7 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_ec563e63d931b806184cb02f79875e4f3b21d1ca043ad06699424459128b58c0", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -103,7 +103,7 @@ @model Type2
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -129,7 +129,7 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType", @class.BaseType);
Assert.Equal("BaseType", @class.BaseType.Content);
}

[Fact]
Expand All @@ -154,7 +154,7 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -179,7 +179,7 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down Expand Up @@ -209,7 +209,7 @@ @model SomeType
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<SomeType>", @class.BaseType);
Assert.Equal("BaseType<SomeType>", @class.BaseType.Content);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void MvcViewDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -122,7 +122,7 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_ec563e63d931b806184cb02f79875e4f3b21d1ca043ad06699424459128b58c0", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void RazorPageDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -249,7 +249,7 @@ public void RazorPageDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_c3b458108610c1a2aa6eede0a5685ede853e036732db515609b2a23ca15359e1", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ namespace AspNetCore
#line default
#line hidden
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"Sha256", @"1ca5ae8e569aefa6575a68e8d8b2d375ed79deec6565fb893b72b89423f55abd", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsViewModel.cshtml")]
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel : MyBasePageForViews<MyModel>
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsViewModel :
#line (1,11)-(1,37) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsViewModel.cshtml"
MyBasePageForViews<MyModel>

#line default
#line hidden

{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ namespace AspNetCore
#line hidden
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"Sha256", @"a652fac42d6a27ace9b45de079bd1bd21d47f29255b96899785aaa55a4a8e354", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsWithViewImports.cshtml")]
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"Sha256", @"0fd3e40bc660f76f39c803bba3ce5cbaf8ca19f7a8c1563212571e769673e06a", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/_ViewImports.cshtml")]
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports : MyPageModel<MyModel>
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InheritsWithViewImports :
#line (1,11)-(1,30) "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\_ViewImports.cshtml"
MyPageModel<MyModel>

#line default
#line hidden

{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void ConsolidatedMvcViewDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "internal", "sealed" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -81,7 +81,7 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "internal", "sealed" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_ec563e63d931b806184cb02f79875e4f3b21d1ca043ad06699424459128b58c0", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,31 @@ @inherits MyBasePage<TestModel?>
AssertCSharpDocumentMatchesBaseline(csharp);
CompileToAssembly(generated);
}

[Fact]
public void InheritsDirective_RazorPages_Runtime()
{
// Arrange
AddCSharpSyntaxTree("""
public abstract class MyBase : global::Microsoft.AspNetCore.Mvc.RazorPages.Page {

}
""");

// Act
var generated = CompileToCSharp("""
@page
@inherits MyBase
""");

// Assert
var intermediate = generated.CodeDocument.GetDocumentIntermediateNode();
var csharp = generated.CodeDocument.GetCSharpDocument();
AssertDocumentNodeMatchesBaseline(intermediate);
AssertCSharpDocumentMatchesBaseline(csharp);
CompileToAssembly(generated);
}

#endregion

#region DesignTime
Expand Down Expand Up @@ -1467,6 +1492,30 @@ public void RazorPageWithNoLeadingPageDirective_DesignTime()
Assert.Equal("RZ3906", Assert.Single(diagnotics).Id);
}

[Fact]
public void InheritsDirective_RazorPages_DesignTime()
{
// Arrange
AddCSharpSyntaxTree("""
public abstract class MyBase : global::Microsoft.AspNetCore.Mvc.RazorPages.Page {

}
""");

// Act
var generated = CompileToCSharp("""
@page
@inherits MyBase
""", designTime: true);

// Assert
var intermediate = generated.CodeDocument.GetDocumentIntermediateNode();
var csharp = generated.CodeDocument.GetCSharpDocument();
AssertDocumentNodeMatchesBaseline(intermediate);
AssertCSharpDocumentMatchesBaseline(csharp);
CompileToAssembly(generated);
}

#endregion

[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/razor/issues/7286")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -102,7 +102,7 @@ @model Type2
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -128,7 +128,7 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType", @class.BaseType);
Assert.Equal("BaseType", @class.BaseType.Content);
}

[Fact]
Expand All @@ -153,7 +153,7 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
}

[Fact]
Expand All @@ -178,7 +178,7 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down Expand Up @@ -208,7 +208,7 @@ @model SomeType
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<SomeType>", @class.BaseType);
Assert.Equal("BaseType<SomeType>", @class.BaseType.Content);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void MvcViewDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -122,7 +122,7 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_ec563e63d931b806184cb02f79875e4f3b21d1ca043ad06699424459128b58c0", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void RazorPageDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -249,7 +249,7 @@ public void RazorPageDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_c3b458108610c1a2aa6eede0a5685ede853e036732db515609b2a23ca15359e1", visitor.Class.ClassName);
}
Expand Down
Loading

0 comments on commit 81d9296

Please sign in to comment.