Skip to content

Commit

Permalink
Recognize _Imports.razor in the source generator (#10992)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz authored Oct 10, 2024
1 parent 33f1199 commit fe4c0a9
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static (SourceGeneratorProjectItem?, Diagnostic?) ComputeProjectItems((A
.Replace(Path.DirectorySeparatorChar, '/')
.Replace("//", "/"),
relativePhysicalPath: relativePath,
fileKind: additionalText.Path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase) ? FileKinds.Component : FileKinds.Legacy,
fileKind: FileKinds.GetFileKindFromFilePath(additionalText.Path),
additionalText: additionalText,
cssScope: cssScope);
return (projectItem, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,69 @@ namespace Microsoft.NET.Sdk.Razor.SourceGenerators;

public sealed class RazorSourceGeneratorComponentTests : RazorSourceGeneratorTestsBase
{
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10991")]
public async Task ImportsRazor()
{
// Arrange
var project = CreateTestProject(new()
{
["Folder1/_Imports.razor"] = """
@using MyApp.MyNamespace.AndAnother
""",
["Folder1/Component1.razor"] = """
@{ var c = new Class1(); }
""",
["Folder2/Component2.razor"] = """
@{ var c = new Class1(); }
""",
}, new()
{
["Class1.cs"] = """
namespace MyApp.MyNamespace.AndAnother;

public class Class1 { }
""",
});
var compilation = await project.GetCompilationAsync();
var driver = await GetDriverAsync(project);

// Act
var result = RunGenerator(compilation!, ref driver,
// Folder2/Component2.razor(1,16): error CS0246: The type or namespace name 'Class1' could not be found (are you missing a using directive or an assembly reference?)
// var c = new Class1();
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "Class1").WithArguments("Class1").WithLocation(1, 16));

// Assert
result.Diagnostics.Verify();
Assert.Equal(3, result.GeneratedSources.Length);
result.VerifyOutputsMatchBaseline();
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10991")]
public async Task ImportsRazor_WithMarkup()
{
// Arrange
var project = CreateTestProject(new()
{
["_Imports.razor"] = """
@using System.Net.Http
<p>test</p>
""",
});
var compilation = await project.GetCompilationAsync();
var driver = await GetDriverAsync(project);

// Act
var result = RunGenerator(compilation!, ref driver);

// Assert
result.Diagnostics.Verify(
// _Imports.razor(2,1): error RZ10003: Markup, code and block directives are not valid in component imports.
Diagnostic("RZ10003").WithLocation(2, 1));
Assert.Single(result.GeneratedSources);
result.VerifyOutputsMatchBaseline();
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/8718")]
public async Task PartialClass()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma checksum "Folder1/Component1.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6b035b1ceae4651124a6a654ee9943a6c99618b3"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Folder1
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line (1,2)-(1,36) "Folder1/_Imports.razor"
using MyApp.MyNamespace.AndAnother

#line default
#line hidden
#nullable disable
;
#nullable restore
public partial class Component1 : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
#nullable restore
#line (1,3)-(1,26) "Folder1/Component1.razor"
var c = new Class1();

#line default
#line hidden
#nullable disable

}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma checksum "Folder1/_Imports.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "4b635952fa51f5f9709e03900447fd0d79a5bbf4"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Folder1
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line (1,2)-(1,36) "Folder1/_Imports.razor"
using MyApp.MyNamespace.AndAnother

#line default
#line hidden
#nullable disable
;
#nullable restore
public partial class _Imports : System.Object
#nullable disable
{
#pragma warning disable 1998
protected void Execute()
{
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma checksum "Folder2/Component2.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6b035b1ceae4651124a6a654ee9943a6c99618b3"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp.Folder2
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#line default
#line hidden
#nullable restore
public partial class Component2 : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
#nullable restore
#line (1,3)-(1,26) "Folder2/Component2.razor"
var c = new Class1();

#line default
#line hidden
#nullable disable

}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma checksum "_Imports.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "26a98239eb5029d034398cd0d9647d5b05517dc7"
// <auto-generated/>
#pragma warning disable 1591
namespace MyApp
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line (1,2)-(1,23) "_Imports.razor"
using System.Net.Http

#line default
#line hidden
#nullable disable
;
#nullable restore
public partial class _Imports : System.Object
#nullable disable
{
#pragma warning disable 1998
protected void Execute()
{
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591

0 comments on commit fe4c0a9

Please sign in to comment.