From 9a456a5aeb36bb598464178b5f93e98d53741925 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 2 May 2019 13:40:25 -0700 Subject: [PATCH] Fix crash in functions block (dotnet/aspnetcore-tooling#550) \n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/3fe99e9d7fede30fd8d2b3f94d488ba63aadcb28 --- .../src/Extensions/FunctionsDirectivePass.cs | 21 +++++++++---------- .../Extensions/FunctionsDirectivePassTest.cs | 13 +++--------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs index c8498259b2..0b0758c9dd 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/FunctionsDirectivePass.cs @@ -18,28 +18,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions return; } - var directiveNodes = new List(); - foreach (var functions in documentNode.FindDirectiveReferences(FunctionsDirective.Directive)) - { - directiveNodes.Add(functions.Node); - } + var directiveNodes = new List(); + 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(); } } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs index 97d80af404..ff60131973 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/FunctionsDirectivePassTest.cs @@ -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(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(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(node), - node => Assert.IsType(node), - node => Assert.IsType(node), - node => Assert.IsType(node)); + node => Assert.IsType(node)); } private static DocumentIntermediateNode Lower(RazorCodeDocument codeDocument, RazorProjectEngine projectEngine)