diff --git a/src/RazorBlade.Analyzers.Tests/RazorBladeSourceGeneratorTests.cs b/src/RazorBlade.Analyzers.Tests/RazorBladeSourceGeneratorTests.cs index a6f48d6..2af93dd 100644 --- a/src/RazorBlade.Analyzers.Tests/RazorBladeSourceGeneratorTests.cs +++ b/src/RazorBlade.Analyzers.Tests/RazorBladeSourceGeneratorTests.cs @@ -395,7 +395,7 @@ private static GeneratorDriverRunResult Generate(string input, var analyzerConfigOptionsProvider = new AnalyzerConfigOptionsProviderMock { { "IsRazorBlade", "True" }, - { "Namespace", "TestNamespace" } + { "HintNamespace", "TestNamespace" } }; if (embeddedLibrary) diff --git a/src/RazorBlade.Analyzers/RazorBladeSourceGenerator.cs b/src/RazorBlade.Analyzers/RazorBladeSourceGenerator.cs index 9d912d0..cef60c2 100644 --- a/src/RazorBlade.Analyzers/RazorBladeSourceGenerator.cs +++ b/src/RazorBlade.Analyzers/RazorBladeSourceGenerator.cs @@ -67,12 +67,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context) if (!string.Equals(isTargetFile, bool.TrueString, StringComparison.OrdinalIgnoreCase)) return null; - if (!options.TryGetValue("build_metadata.AdditionalFiles.Namespace", out var ns)) - ns = null; + if (!options.TryGetValue("build_metadata.AdditionalFiles.HintNamespace", out var hintNamespace)) + hintNamespace = null; return new InputFile( additionalText, - ns, + hintNamespace, CSharpIdentifier.SanitizeIdentifier(Path.GetFileNameWithoutExtension(additionalText.Path)) ); } @@ -92,14 +92,14 @@ private static void Generate(SourceProductionContext context, InputFile file, Gl context.ReportDiagnostic(diagnostic.ToDiagnostic()); context.AddSource( - $"{file.Namespace}.{file.ClassName}.Razor.g.cs", + $"{file.HintNamespace}.{file.ClassName}.Razor.g.cs", csharpDoc.GeneratedCode ); if (!string.IsNullOrEmpty(libraryCode)) { context.AddSource( - $"{file.Namespace}.{file.ClassName}.RazorBlade.g.cs", + $"{file.HintNamespace}.{file.ClassName}.RazorBlade.g.cs", libraryCode ); } @@ -122,7 +122,7 @@ private static RazorCSharpDocument GenerateRazorCode(SourceText sourceText, Inpu configurationFeature.ConfigureNamespace.Add((codeDoc, node) => { node.Content = NamespaceVisitor.GetNamespaceDirectiveContent(codeDoc) - ?? file.Namespace + ?? file.HintNamespace ?? "Razor"; }); @@ -177,7 +177,7 @@ private static string GenerateLibrarySpecificCode(RazorCSharpDocument generatedD static partial void OnGenerate(); - private record InputFile(AdditionalText AdditionalText, string? Namespace, string ClassName); + private record InputFile(AdditionalText AdditionalText, string? HintNamespace, string ClassName); private record GlobalOptions(CSharpParseOptions ParseOptions, ImmutableArray AdditionalSyntaxTrees); } diff --git a/src/RazorBlade.IntegrationTest/Program.cs b/src/RazorBlade.IntegrationTest/Program.cs index 69de8c2..7c8be8a 100644 --- a/src/RazorBlade.IntegrationTest/Program.cs +++ b/src/RazorBlade.IntegrationTest/Program.cs @@ -21,6 +21,13 @@ private static void WriteTemplate(RazorTemplate template) private static void WriteSeparator() => Console.WriteLine("--------------------------------------------------"); + + private static void CheckNamespaces() + { + _ = typeof(global::RazorBlade.IntegrationTest.TestTemplate); + _ = typeof(global::RazorBlade.IntegrationTest.Examples.ExampleTemplate); + _ = typeof(global::FooBar.OtherNamespace.TestTemplateWithNamespace); + } } public class FooBarModelClass diff --git a/src/RazorBlade.IntegrationTest/TestTemplateWithNamespace.cshtml b/src/RazorBlade.IntegrationTest/TestTemplateWithNamespace.cshtml new file mode 100644 index 0000000..b3e57b6 --- /dev/null +++ b/src/RazorBlade.IntegrationTest/TestTemplateWithNamespace.cshtml @@ -0,0 +1,2 @@ +@inherits RazorBlade.HtmlTemplate +@namespace FooBar.OtherNamespace diff --git a/src/RazorBlade/RazorBlade.targets b/src/RazorBlade/RazorBlade.targets index da48472..5515edf 100644 --- a/src/RazorBlade/RazorBlade.targets +++ b/src/RazorBlade/RazorBlade.targets @@ -6,7 +6,7 @@ - + @@ -22,7 +22,7 @@ - +