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; return;
} }
var directiveNodes = new List<IntermediateNode>(); var directiveNodes = new List<IntermediateNodeReference>();
foreach (var functions in documentNode.FindDirectiveReferences(FunctionsDirective.Directive)) directiveNodes.AddRange(documentNode.FindDirectiveReferences(FunctionsDirective.Directive));
{
directiveNodes.Add(functions.Node);
}
if (FileKinds.IsComponent(codeDocument.GetFileKind())) if (FileKinds.IsComponent(codeDocument.GetFileKind()))
{ {
foreach (var code in documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive)) directiveNodes.AddRange(documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive));
{
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. // 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); var orderedDirectives = directiveNodes.OrderBy(n => n.Node.Source?.AbsoluteIndex);
foreach (var node in orderedDirectives) foreach (var directiveReference in orderedDirectives)
{ {
var node = directiveReference.Node;
for (var i = 0; i < node.Children.Count; i++) for (var i = 0; i < node.Children.Count; i++)
{ {
@class.Children.Add(node.Children[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)); node => CSharpCode(" var value = true; ", node));
var method = @class.Children[0]; var method = @class.Children[0];
Children( Assert.Empty(method.Children);
method,
node => Assert.IsType<DirectiveIntermediateNode>(node));
} }
[Fact] [Fact]
@ -113,9 +111,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
node => CSharpCode(" var value = true; ", node)); node => CSharpCode(" var value = true; ", node));
var method = @class.Children[0]; var method = @class.Children[0];
Children( Assert.Empty(method.Children);
method,
node => Assert.IsType<DirectiveIntermediateNode>(node));
} }
[Fact] [Fact]
@ -161,10 +157,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
var method = @class.Children[0]; var method = @class.Children[0];
Children( Children(
method, method,
node => Assert.IsType<HtmlContentIntermediateNode>(node), 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)