Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ internal class ComponentImportProjectFeature : IImportProjectFeature
// Using explicit newlines here to avoid fooling our baseline tests
private const string DefaultUsingImportContent =
"\r\n" +
"@using System\r\n" +
"@using System.Collections.Generic\r\n" +
"@using System.Linq\r\n" +
"@using System.Threading.Tasks\r\n" +
"@using " + ComponentsApi.RenderFragment.Namespace + "\r\n"; // Microsoft.AspNetCore.Components
"@using global::System\r\n" +
"@using global::System.Collections.Generic\r\n" +
"@using global::System.Linq\r\n" +
"@using global::System.Threading.Tasks\r\n" +
"@using global::" + ComponentsApi.RenderFragment.Namespace + "\r\n"; // Microsoft.AspNetCore.Components

public RazorProjectEngine ProjectEngine { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument)
var usingReferences = new List<UsingReference>(visitor.Usings);
for (var j = importedUsings.Count - 1; j >= 0; j--)
{
if (!usingReferences.Contains(importedUsings[j]))
var importedUsing = importedUsings[j];
if (!usingReferences.Contains(importedUsing) &&
// If the using is from the default import, avoid adding it
// if a user using exists which is the same except for the `global::` prefix.
(!TryRemoveGlobalPrefixFromDefaultUsing(in importedUsing, out var trimmedUsing) ||
!usingReferences.Contains(trimmedUsing)))
{
usingReferences.Insert(0, importedUsings[j]);
usingReferences.Insert(0, importedUsing);
}
}

Expand Down Expand Up @@ -125,6 +130,21 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument)
}

codeDocument.SetDocumentIntermediateNode(document);

static bool TryRemoveGlobalPrefixFromDefaultUsing(in UsingReference usingReference, out UsingReference trimmed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 It's difficult to review the impact of this change in isolation because baselines weren't updated with each change

Copy link
Member Author

@jjonescz jjonescz Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because this change caused failures - there was a new warning about duplicate usings in the new tests. I don't think anything changed in the old tests. But I could verify that.

{
const string globalPrefix = "global::";
if (usingReference.Source is { FilePath: null } && // the default import has null file path
usingReference.Namespace.StartsWith(globalPrefix, StringComparison.Ordinal))
{
trimmed = new UsingReference(
usingReference.Namespace.Substring(globalPrefix.Length),
usingReference.Source);
return true;
}
trimmed = usingReference;
return false;
}
}

private IReadOnlyList<UsingReference> ImportDirectives(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public override void VisitClassDeclaration(ClassDeclarationIntermediateNode node
new IntermediateToken()
{
Kind = TokenKind.CSharp,
Content = $"private static {typeof(object).FullName} {DesignTimeVariable} = null;",
Content = $"private static object {DesignTimeVariable} = null;",
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ private void WriteDesignTimeDirectiveToken(CodeRenderingContext context, DesignT
}

// Wrap the directive token in a lambda to isolate variable names.
context.CodeWriter
.Write("((")
.Write(typeof(Action).FullName)
.Write(")(");
Comment on lines -57 to -60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 I think I might prefer the form on the left, and just change (( to ((global::

context.CodeWriter.Write("((global::System.Action)(");
using (context.CodeWriter.BuildLambda())
{
var originalIndent = context.CodeWriter.CurrentIndent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8215,6 +8215,20 @@ @namespace New.Test
CompileToAssembly(generated);
}

[Fact] // https://github.com/dotnet/razor/issues/7091
public void Component_NamespaceDirective_ContainsSystem()
{
// Act
var generated = CompileToCSharp("""
@namespace X.System.Y
""");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void Component_PreserveWhitespaceDirective_InImports()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <auto-generated/>
#pragma warning disable 1591
namespace X.System.Y
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
((global::System.Action)(() => {
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
global::System.Object __typeHelper = nameof(X.System.Y);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I'm expecting this to be just object by the end of this change

Copy link
Member Author

@jjonescz jjonescz Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't plan to update everything to object (that's orthogonal to the issue), I just fixed cases where there was System.Object without global::, failing the new test cases. I could perhaps use global::System.Object everywhere but object seemed better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is definitely better.


#line default
#line hidden
#nullable disable
}
))();
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Document -
NamespaceDeclaration - - X.System.Y
UsingDirective - (3:1,1 [20] ) - global::System
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [25] ) - global::System.Linq
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
DirectiveToken - (11:0,11 [10] x:\dir\subdir\Test\TestComponent.cshtml) - X.System.Y
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Source Location: (11:0,11 [10] x:\dir\subdir\Test\TestComponent.cshtml)
|X.System.Y|
Generated Location: (649:17,44 [10] )
|X.System.Y|

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <auto-generated/>
#pragma warning disable 1591
namespace X.System.Y
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Document -
NamespaceDeclaration - - X.System.Y
UsingDirective - (3:1,1 [22] ) - global::System
UsingDirective - (26:2,1 [42] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [27] ) - global::System.Linq
UsingDirective - (97:4,1 [38] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [47] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree