Skip to content

Support more expressions in @page directive #11805

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -293,7 +293,7 @@ public void Execute_EscapesViewPathWhenAddingAttributeToViews()
}

[Fact]
public void Execute_AddsRazorPagettribute_ToPage()
public void Execute_AddsRazorPageAttribute_ToPage()
{
// Arrange
var source = TestRazorSourceDocument.Create("test", RazorSourceDocumentProperties.Create(filePath: null, relativePath: "/Views/Index.cshtml"));
Expand Down Expand Up @@ -406,4 +406,211 @@ public void Execute_EscapesViewPathAndRouteWhenAddingAttributeToPage()
},
node => Assert.Same(@namespace, node));
}

[Fact]
public void Execute_AddsRazorPageAttribute_WithCustomExpression()
{
// Arrange
var source = TestRazorSourceDocument.Create("test", RazorSourceDocumentProperties.Create(filePath: null, relativePath: "/Views/Index.cshtml"));
var codeDocument = ProjectEngine.CreateCodeDocument(source);

var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@\"/Views/Index.cshtml\", typeof(SomeNamespace.SomeName), global::SomeNamespace.SomeName.__RouteTemplate)]";

var documentNode = new DocumentIntermediateNode()
{
DocumentKind = RazorPageDocumentClassifierPass.RazorPageDocumentKind,
Options = codeDocument.CodeGenerationOptions
};

var builder = IntermediateNodeBuilder.Create(documentNode);

var pageDirective = new DirectiveIntermediateNode
{
Directive = PageDirective.Directive,
Children =
{
new DirectiveTokenIntermediateNode
{
Content = "AppRoutes.Home",
}
},
};

builder.Add(pageDirective);

var @namespace = new NamespaceDeclarationIntermediateNode
{
Content = "SomeNamespace",
Annotations =
{
[CommonAnnotations.PrimaryNamespace] = CommonAnnotations.PrimaryNamespace
}
};

builder.Push(@namespace);

var @class = new ClassDeclarationIntermediateNode
{
ClassName = "SomeName",
Annotations =
{
[CommonAnnotations.PrimaryClass] = CommonAnnotations.PrimaryClass,
}
};

builder.Add(@class);

// Act
ProjectEngine.ExecutePass<AssemblyAttributeInjectionPass>(codeDocument, documentNode);

// Assert
Assert.Collection(documentNode.Children,
node => Assert.Same(pageDirective, node),
node =>
{
var csharpCode = Assert.IsType<CSharpCodeIntermediateNode>(node);
var token = Assert.IsAssignableFrom<IntermediateToken>(Assert.Single(csharpCode.Children));
Assert.Equal(TokenKind.CSharp, token.Kind);
Assert.Equal(expectedAttribute, token.Content);
},
node => Assert.Same(@namespace, node));
}

[Fact]
public void Execute_AddsRazorPageAttribute_WithStringConcatenationExpression()
{
// Arrange
var source = TestRazorSourceDocument.Create("test", RazorSourceDocumentProperties.Create(filePath: null, relativePath: "/Views/Index.cshtml"));
var codeDocument = ProjectEngine.CreateCodeDocument(source);

var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@\"/Views/Index.cshtml\", typeof(SomeNamespace.SomeName), global::SomeNamespace.SomeName.__RouteTemplate)]";

var documentNode = new DocumentIntermediateNode()
{
DocumentKind = RazorPageDocumentClassifierPass.RazorPageDocumentKind,
Options = codeDocument.CodeGenerationOptions
};

var builder = IntermediateNodeBuilder.Create(documentNode);

var pageDirective = new DirectiveIntermediateNode
{
Directive = PageDirective.Directive,
Children =
{
new LazyIntermediateToken
{
Content = "AppRoutes.Content + AppRoutes.Content.Home",
}
},
};

builder.Add(pageDirective);

var @namespace = new NamespaceDeclarationIntermediateNode
{
Content = "SomeNamespace",
Annotations =
{
[CommonAnnotations.PrimaryNamespace] = CommonAnnotations.PrimaryNamespace
}
};

builder.Push(@namespace);

var @class = new ClassDeclarationIntermediateNode
{
ClassName = "SomeName",
Annotations =
{
[CommonAnnotations.PrimaryClass] = CommonAnnotations.PrimaryClass,
}
};

builder.Add(@class);

// Act
ProjectEngine.ExecutePass<AssemblyAttributeInjectionPass>(codeDocument, documentNode);

// Assert
Assert.Collection(documentNode.Children,
node => Assert.Same(pageDirective, node),
node =>
{
var csharpCode = Assert.IsType<CSharpCodeIntermediateNode>(node);
var token = Assert.IsAssignableFrom<IntermediateToken>(Assert.Single(csharpCode.Children));
Assert.Equal(TokenKind.CSharp, token.Kind);
Assert.Equal(expectedAttribute, token.Content);
},
node => Assert.Same(@namespace, node));
}

[Fact]
public void Execute_AddsRazorPageAttribute_WithInterpolatedStringExpression()
{
// Arrange
var source = TestRazorSourceDocument.Create("test", RazorSourceDocumentProperties.Create(filePath: null, relativePath: "/Views/Index.cshtml"));
var codeDocument = ProjectEngine.CreateCodeDocument(source);

var expectedAttribute = "[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@\"/Views/Index.cshtml\", typeof(SomeNamespace.SomeName), global::SomeNamespace.SomeName.__RouteTemplate)]";

var documentNode = new DocumentIntermediateNode()
{
DocumentKind = RazorPageDocumentClassifierPass.RazorPageDocumentKind,
Options = codeDocument.CodeGenerationOptions
};

var builder = IntermediateNodeBuilder.Create(documentNode);

var pageDirective = new DirectiveIntermediateNode
{
Directive = PageDirective.Directive,
Children =
{
new LazyIntermediateToken
{
Content = "$\"{AppRoutes.Content}{AppRoutes.Content.Home}\"",
}
},
};

builder.Add(pageDirective);

var @namespace = new NamespaceDeclarationIntermediateNode
{
Content = "SomeNamespace",
Annotations =
{
[CommonAnnotations.PrimaryNamespace] = CommonAnnotations.PrimaryNamespace
}
};

builder.Push(@namespace);

var @class = new ClassDeclarationIntermediateNode
{
ClassName = "SomeName",
Annotations =
{
[CommonAnnotations.PrimaryClass] = CommonAnnotations.PrimaryClass,
}
};

builder.Add(@class);

// Act
ProjectEngine.ExecutePass<AssemblyAttributeInjectionPass>(codeDocument, documentNode);

// Assert
Assert.Collection(documentNode.Children,
node => Assert.Same(pageDirective, node),
node =>
{
var csharpCode = Assert.IsType<CSharpCodeIntermediateNode>(node);
var token = Assert.IsAssignableFrom<IntermediateToken>(Assert.Single(csharpCode.Children));
Assert.Equal(TokenKind.CSharp, token.Kind);
Assert.Equal(expectedAttribute, token.Content);
},
node => Assert.Same(@namespace, node));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void MalformedPageDirective_Runtime()
AssertCSharpDocumentMatchesBaseline(compiled.CodeDocument.GetCSharpDocument());

var diagnotics = compiled.CodeDocument.GetCSharpDocument().Diagnostics;
Assert.Equal("RZ1016", Assert.Single(diagnotics).Id);
Assert.Equal("RZ1046", Assert.Single(diagnotics).Id);
}

[Fact]
Expand Down Expand Up @@ -620,7 +620,7 @@ public void MalformedPageDirective_DesignTime()
AssertSourceMappingsMatchBaseline(compiled.CodeDocument);

var diagnotics = compiled.CodeDocument.GetCSharpDocument().Diagnostics;
Assert.Equal("RZ1016", Assert.Single(diagnotics).Id);
Assert.Equal("RZ1046", Assert.Single(diagnotics).Id);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ public void TryGetPageDirective_ReturnsTrue_IfPageIsMalformed()
var documentNode = processor.GetDocumentNode();

// Act
var result = PageDirective.TryGetPageDirective(documentNode, out var pageDirective);
Assert.True(PageDirective.TryGetPageDirective(documentNode, out var pageDirective));

// Assert
Assert.True(result);
Assert.Equal("some-route-template", pageDirective.RouteTemplate);
Assert.NotNull(pageDirective.DirectiveNode);
}
Expand All @@ -51,10 +50,9 @@ public void TryGetPageDirective_ReturnsTrue_IfPageIsImported()
var documentNode = processor.GetDocumentNode();

// Act
var result = PageDirective.TryGetPageDirective(documentNode, out var pageDirective);
Assert.True(PageDirective.TryGetPageDirective(documentNode, out var pageDirective));

// Assert
Assert.True(result);
Assert.Null(pageDirective.RouteTemplate);
}

Expand Down Expand Up @@ -87,10 +85,9 @@ public void TryGetPageDirective_ReturnsTrue_IfPageDoesStartWithDirective()
var documentNode = processor.GetDocumentNode();

// Act
var result = PageDirective.TryGetPageDirective(documentNode, out var pageDirective);
Assert.True(PageDirective.TryGetPageDirective(documentNode, out var pageDirective));

// Assert
Assert.True(result);
Assert.Null(pageDirective.RouteTemplate);
Assert.NotNull(pageDirective.DirectiveNode);
}
Expand All @@ -106,10 +103,9 @@ public void TryGetPageDirective_ReturnsTrue_IfContentHasDirective()
var documentNode = processor.GetDocumentNode();

// Act
var result = PageDirective.TryGetPageDirective(documentNode, out var pageDirective);
Assert.True(PageDirective.TryGetPageDirective(documentNode, out var pageDirective));

// Assert
Assert.True(result);
Assert.Null(pageDirective.RouteTemplate);
}

Expand All @@ -124,10 +120,9 @@ public void TryGetPageDirective_ParsesRouteTemplate()
var documentNode = processor.GetDocumentNode();

// Act
var result = PageDirective.TryGetPageDirective(documentNode, out var pageDirective);
Assert.True(PageDirective.TryGetPageDirective(documentNode, out var pageDirective));

// Assert
Assert.True(result);
Assert.Equal("some-route-template", pageDirective.RouteTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Incomplete
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
((global::System.Action)(() => {
#line 4 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml"


#line default
#line hidden
}
))();
((global::System.Action)(() => {
#line 8 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml"


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(4,1): Error RZ2001: The 'page' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(5,1): Error RZ2001: The 'page' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(5,7): Error RZ1016: The 'page' directive expects a string surrounded by double quotes.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(5,7): Error RZ1046: The 'page' directive expects an identifier or explicit razor expression ("@()") or a string surrounded by double quotes.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(7,7): Error RZ1013: The 'model' directive expects a type name.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(8,1): Error RZ2001: The 'model' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(8,8): Error RZ1013: The 'model' directive expects a type name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
DirectiveToken - (617:12,14 [96] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor
DirectiveToken - (729:13,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor
DirectiveToken - (832:14,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor
DirectiveToken - (100:3,6 [0] IncompleteDirectives.cshtml) -
DirectiveToken - (128:7,7 [0] IncompleteDirectives.cshtml) -
DirectiveToken - (149:10,8 [0] IncompleteDirectives.cshtml) -
DirectiveToken - (159:11,8 [17] IncompleteDirectives.cshtml) - MyService<TModel>
Expand All @@ -37,6 +38,7 @@
HtmlContent - (85:1,0 [2] IncompleteDirectives.cshtml)
LazyIntermediateToken - (85:1,0 [2] IncompleteDirectives.cshtml) - Html - \n
MalformedDirective - (94:3,0 [8] IncompleteDirectives.cshtml) - page
DirectiveToken - (100:3,6 [0] IncompleteDirectives.cshtml) -
MalformedDirective - (102:4,0 [6] IncompleteDirectives.cshtml) - page
HtmlContent - (108:4,6 [5] IncompleteDirectives.cshtml)
LazyIntermediateToken - (108:4,6 [5] IncompleteDirectives.cshtml) - Html - "\n\n
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
Source Location: (128:7,7 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
Source Location: (100:3,6 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (813:21,0 [0] )
||

Source Location: (128:7,7 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (1011:29,0 [0] )
||

Source Location: (149:10,8 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (1012:29,0 [0] )
Generated Location: (1210:37,0 [0] )
||

Source Location: (159:11,8 [17] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
|MyService<TModel>|
Generated Location: (1211:37,0 [17] )
Generated Location: (1409:45,0 [17] )
|MyService<TModel>|

Source Location: (203:14,11 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (1450:45,0 [0] )
Generated Location: (1648:53,0 [0] )
||

Source Location: (119:6,6 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (1910:60,6 [0] )
Generated Location: (2108:68,6 [0] )
||

Source Location: (139:9,7 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (2047:65,7 [0] )
Generated Location: (2245:73,7 [0] )
||

Source Location: (190:13,10 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml)
||
Generated Location: (2187:70,10 [0] )
Generated Location: (2385:78,10 [0] )
||

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(4,1): Error RZ2001: The 'page' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(5,1): Error RZ2001: The 'page' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(5,7): Error RZ1016: The 'page' directive expects a string surrounded by double quotes.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(5,7): Error RZ1046: The 'page' directive expects an identifier or explicit razor expression ("@()") or a string surrounded by double quotes.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(7,7): Error RZ1013: The 'model' directive expects a type name.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(8,1): Error RZ2001: The 'model' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(8,8): Error RZ1013: The 'model' directive expects a type name.
Expand Down
Loading
Loading