108 lines
4.1 KiB
C#
108 lines
4.1 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Razor.Language;
|
|
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
|
|
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
|
|
|
namespace Microsoft.AspNetCore.Blazor.Razor
|
|
{
|
|
internal class PageDirectivePass : IntermediateNodePassBase, IRazorDirectiveClassifierPass
|
|
{
|
|
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
|
|
{
|
|
if (codeDocument == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(codeDocument));
|
|
}
|
|
|
|
if (documentNode == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(documentNode));
|
|
}
|
|
|
|
var @namespace = documentNode.FindPrimaryNamespace();
|
|
var @class = documentNode.FindPrimaryClass();
|
|
if (@namespace == null || @class == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var directives = documentNode.FindDirectiveReferences(PageDirective.Directive);
|
|
if (directives.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// We don't allow @page directives in imports
|
|
for (var i = 0; i < directives.Count; i++)
|
|
{
|
|
var directive = directives[i];
|
|
if (directive.Node.IsImported())
|
|
{
|
|
directive.Node.Diagnostics.Add(BlazorDiagnosticFactory.CreatePageDirective_CannotBeImported(directive.Node.Source.Value));
|
|
}
|
|
}
|
|
|
|
// Insert the attributes 'on-top' of the class declaration, since classes don't directly support attributes.
|
|
var index = 0;
|
|
for (; index < @namespace.Children.Count; index++)
|
|
{
|
|
if (object.ReferenceEquals(@class, @namespace.Children[index]))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < directives.Count; i++)
|
|
{
|
|
var pageDirective = (DirectiveIntermediateNode)directives[i].Node;
|
|
|
|
// The parser also adds errors for invalid syntax, we just need to not crash.
|
|
var routeToken = pageDirective.Tokens.FirstOrDefault();
|
|
|
|
if (routeToken != null &&
|
|
routeToken.Content.Length >= 3 &&
|
|
routeToken.Content[0] == '\"' &&
|
|
routeToken.Content[1] == '/' &&
|
|
routeToken.Content[routeToken.Content.Length - 1] == '\"')
|
|
{
|
|
var template = routeToken.Content.Substring(1, routeToken.Content.Length - 2);
|
|
@namespace.Children.Insert(index++, new RouteAttributeExtensionNode(template));
|
|
}
|
|
else
|
|
{
|
|
pageDirective.Diagnostics.Add(BlazorDiagnosticFactory.CreatePageDirective_MustSpecifyRoute(pageDirective.Source));
|
|
}
|
|
}
|
|
}
|
|
|
|
private class RouteAttributeExtensionNode : ExtensionIntermediateNode
|
|
{
|
|
public RouteAttributeExtensionNode(string template)
|
|
{
|
|
Template = template;
|
|
}
|
|
|
|
public string Template { get; }
|
|
|
|
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly;
|
|
|
|
public override void Accept(IntermediateNodeVisitor visitor) => AcceptExtensionNode(this, visitor);
|
|
|
|
public override void WriteNode(CodeTarget target, CodeRenderingContext context)
|
|
{
|
|
context.CodeWriter.Write("[");
|
|
context.CodeWriter.Write(BlazorApi.RouteAttribute.FullTypeName);
|
|
context.CodeWriter.Write("(\"");
|
|
context.CodeWriter.Write(Template);
|
|
context.CodeWriter.Write("\")");
|
|
context.CodeWriter.Write("]");
|
|
context.CodeWriter.WriteLine();
|
|
}
|
|
}
|
|
}
|
|
}
|