parent
2050916767
commit
9860cfc34b
|
|
@ -23,7 +23,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.AddDirective(Directive, FileKinds.Component);
|
builder.AddDirective(Directive, FileKinds.Component);
|
||||||
builder.Features.Add(new ComponentCodeDirectivePass());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
// 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 Microsoft.AspNetCore.Razor.Language.Intermediate;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language.Components
|
|
||||||
{
|
|
||||||
internal class ComponentCodeDirectivePass : IntermediateNodePassBase, IRazorDirectiveClassifierPass
|
|
||||||
{
|
|
||||||
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
|
|
||||||
{
|
|
||||||
var @class = documentNode.FindPrimaryClass();
|
|
||||||
if (@class == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var code in documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive))
|
|
||||||
{
|
|
||||||
for (var i = 0; i < code.Node.Children.Count; i++)
|
|
||||||
{
|
|
||||||
@class.Children.Add(code.Node.Children[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.AspNetCore.Razor.Language.Components;
|
||||||
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
|
|
@ -15,11 +18,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var directiveNodes = new List<IntermediateNode>();
|
||||||
foreach (var functions in documentNode.FindDirectiveReferences(FunctionsDirective.Directive))
|
foreach (var functions in documentNode.FindDirectiveReferences(FunctionsDirective.Directive))
|
||||||
{
|
{
|
||||||
for (var i = 0; i < functions.Node.Children.Count; i++)
|
directiveNodes.Add(functions.Node);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FileKinds.IsComponent(codeDocument.GetFileKind()))
|
||||||
|
{
|
||||||
|
foreach (var code in documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive))
|
||||||
{
|
{
|
||||||
@class.Children.Add(functions.Node.Children[i]);
|
directiveNodes.Add(code.Node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we have all the directive nodes, we want to add them to the end of the class node in document order.
|
||||||
|
var orderedDirectives = directiveNodes.OrderBy(n => n.Source?.AbsoluteIndex);
|
||||||
|
foreach (var node in orderedDirectives)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
@class.Children.Add(node.Children[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Razor.Language.Components;
|
||||||
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeAssert;
|
using static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeAssert;
|
||||||
|
|
@ -76,6 +77,96 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
node => Assert.IsType<DirectiveIntermediateNode>(node));
|
node => Assert.IsType<DirectiveIntermediateNode>(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Execute_ComponentCodeDirective_AddsStatementsToClassLevel()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var projectEngine = CreateProjectEngine(b => b.AddDirective(ComponentCodeDirective.Directive));
|
||||||
|
var pass = new FunctionsDirectivePass()
|
||||||
|
{
|
||||||
|
Engine = projectEngine.Engine,
|
||||||
|
};
|
||||||
|
|
||||||
|
var sourceDocument = TestRazorSourceDocument.Create("@code { var value = true; }");
|
||||||
|
var codeDocument = RazorCodeDocument.Create(sourceDocument);
|
||||||
|
codeDocument.SetFileKind(FileKinds.Component);
|
||||||
|
|
||||||
|
var irDocument = Lower(codeDocument, projectEngine);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
pass.Execute(codeDocument, irDocument);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Children(
|
||||||
|
irDocument,
|
||||||
|
node => Assert.IsType<NamespaceDeclarationIntermediateNode>(node));
|
||||||
|
|
||||||
|
var @namespace = irDocument.Children[0];
|
||||||
|
Children(
|
||||||
|
@namespace,
|
||||||
|
node => Assert.IsType<ClassDeclarationIntermediateNode>(node));
|
||||||
|
|
||||||
|
var @class = @namespace.Children[0];
|
||||||
|
Children(
|
||||||
|
@class,
|
||||||
|
node => Assert.IsType<MethodDeclarationIntermediateNode>(node),
|
||||||
|
node => CSharpCode(" var value = true; ", node));
|
||||||
|
|
||||||
|
var method = @class.Children[0];
|
||||||
|
Children(
|
||||||
|
method,
|
||||||
|
node => Assert.IsType<DirectiveIntermediateNode>(node));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Execute_FunctionsAndComponentCodeDirective_AddsStatementsToClassLevel()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var projectEngine = CreateProjectEngine(b => b.AddDirective(ComponentCodeDirective.Directive));
|
||||||
|
var pass = new FunctionsDirectivePass()
|
||||||
|
{
|
||||||
|
Engine = projectEngine.Engine,
|
||||||
|
};
|
||||||
|
|
||||||
|
var sourceDocument = TestRazorSourceDocument.Create(@"
|
||||||
|
@functions { var value1 = true; }
|
||||||
|
@code { var value2 = true; }
|
||||||
|
@functions { var value3 = true; }");
|
||||||
|
var codeDocument = RazorCodeDocument.Create(sourceDocument);
|
||||||
|
codeDocument.SetFileKind(FileKinds.Component);
|
||||||
|
|
||||||
|
var irDocument = Lower(codeDocument, projectEngine);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
pass.Execute(codeDocument, irDocument);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Children(
|
||||||
|
irDocument,
|
||||||
|
node => Assert.IsType<NamespaceDeclarationIntermediateNode>(node));
|
||||||
|
|
||||||
|
var @namespace = irDocument.Children[0];
|
||||||
|
Children(
|
||||||
|
@namespace,
|
||||||
|
node => Assert.IsType<ClassDeclarationIntermediateNode>(node));
|
||||||
|
|
||||||
|
var @class = @namespace.Children[0];
|
||||||
|
Children(
|
||||||
|
@class,
|
||||||
|
node => Assert.IsType<MethodDeclarationIntermediateNode>(node),
|
||||||
|
node => CSharpCode(" var value1 = true; ", node),
|
||||||
|
node => CSharpCode(" var value2 = true; ", node),
|
||||||
|
node => CSharpCode(" var value3 = true; ", node));
|
||||||
|
|
||||||
|
var method = @class.Children[0];
|
||||||
|
Children(
|
||||||
|
method,
|
||||||
|
node => Assert.IsType<HtmlContentIntermediateNode>(node),
|
||||||
|
node => Assert.IsType<DirectiveIntermediateNode>(node),
|
||||||
|
node => Assert.IsType<DirectiveIntermediateNode>(node),
|
||||||
|
node => Assert.IsType<DirectiveIntermediateNode>(node));
|
||||||
|
}
|
||||||
|
|
||||||
private static DocumentIntermediateNode Lower(RazorCodeDocument codeDocument, RazorProjectEngine projectEngine)
|
private static DocumentIntermediateNode Lower(RazorCodeDocument codeDocument, RazorProjectEngine projectEngine)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < projectEngine.Phases.Count; i++)
|
for (var i = 0; i < projectEngine.Phases.Count; i++)
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,6 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
features,
|
features,
|
||||||
feature => Assert.IsType<ComponentBindLoweringPass>(feature),
|
feature => Assert.IsType<ComponentBindLoweringPass>(feature),
|
||||||
feature => Assert.IsType<ComponentChildContentDiagnosticPass>(feature),
|
feature => Assert.IsType<ComponentChildContentDiagnosticPass>(feature),
|
||||||
feature => Assert.IsType<ComponentCodeDirectivePass>(feature),
|
|
||||||
feature => Assert.IsType<ComponentComplexAttributeContentPass>(feature),
|
feature => Assert.IsType<ComponentComplexAttributeContentPass>(feature),
|
||||||
feature => Assert.IsType<ComponentDocumentClassifierPass>(feature),
|
feature => Assert.IsType<ComponentDocumentClassifierPass>(feature),
|
||||||
feature => Assert.IsType<ComponentEventHandlerLoweringPass>(feature),
|
feature => Assert.IsType<ComponentEventHandlerLoweringPass>(feature),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue