Fix crash in functions block (dotnet/aspnetcore-tooling#550)

\n\nCommit migrated from 3fe99e9d7f
This commit is contained in:
Ajay Bhargav Baaskaran 2019-05-02 13:40:25 -07:00 committed by GitHub
parent c21572bf8f
commit 9a456a5aeb
2 changed files with 13 additions and 21 deletions

View File

@ -18,28 +18,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
return;
}
var directiveNodes = new List<IntermediateNode>();
foreach (var functions in documentNode.FindDirectiveReferences(FunctionsDirective.Directive))
{
directiveNodes.Add(functions.Node);
}
var directiveNodes = new List<IntermediateNodeReference>();
directiveNodes.AddRange(documentNode.FindDirectiveReferences(FunctionsDirective.Directive));
if (FileKinds.IsComponent(codeDocument.GetFileKind()))
{
foreach (var code in documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive))
{
directiveNodes.Add(code.Node);
}
directiveNodes.AddRange(documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive));
}
// 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)
var orderedDirectives = directiveNodes.OrderBy(n => n.Node.Source?.AbsoluteIndex);
foreach (var directiveReference in orderedDirectives)
{
var node = directiveReference.Node;
for (var i = 0; i < node.Children.Count; i++)
{
@class.Children.Add(node.Children[i]);
}
// We don't want to keep the original directive node around anymore.
// Otherwise this can cause unintended side effects in the subsequent passes.
directiveReference.Remove();
}
}
}

View File

@ -72,9 +72,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
node => CSharpCode(" var value = true; ", node));
var method = @class.Children[0];
Children(
method,
node => Assert.IsType<DirectiveIntermediateNode>(node));
Assert.Empty(method.Children);
}
[Fact]
@ -113,9 +111,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
node => CSharpCode(" var value = true; ", node));
var method = @class.Children[0];
Children(
method,
node => Assert.IsType<DirectiveIntermediateNode>(node));
Assert.Empty(method.Children);
}
[Fact]
@ -161,10 +157,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
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));
node => Assert.IsType<HtmlContentIntermediateNode>(node));
}
private static DocumentIntermediateNode Lower(RazorCodeDocument codeDocument, RazorProjectEngine projectEngine)