diff --git a/src/Microsoft.AspNet.Razor/Chunks/ChunkTree.cs b/src/Microsoft.AspNet.Razor/Chunks/ChunkTree.cs index 8d45935c3c..9f497d7284 100644 --- a/src/Microsoft.AspNet.Razor/Chunks/ChunkTree.cs +++ b/src/Microsoft.AspNet.Razor/Chunks/ChunkTree.cs @@ -1,17 +1,9 @@ // 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.Collections.Generic; - namespace Microsoft.AspNet.Razor.Chunks { - public class ChunkTree + public class ChunkTree : ParentChunk { - public ChunkTree() - { - Chunks = new List(); - } - - public IList Chunks { get; set; } } } diff --git a/src/Microsoft.AspNet.Razor/Chunks/ChunkTreeBuilder.cs b/src/Microsoft.AspNet.Razor/Chunks/ChunkTreeBuilder.cs index fe828e4752..5c34de2f42 100644 --- a/src/Microsoft.AspNet.Razor/Chunks/ChunkTreeBuilder.cs +++ b/src/Microsoft.AspNet.Razor/Chunks/ChunkTreeBuilder.cs @@ -13,11 +13,14 @@ namespace Microsoft.AspNet.Razor.Chunks public ChunkTreeBuilder() { - ChunkTree = new ChunkTree(); + Root = new ChunkTree(); _parentStack = new Stack(); + _parentStack.Push(Root); } - public ChunkTree ChunkTree { get; private set; } + public ParentChunk Current => _parentStack.Peek(); + + public ChunkTree Root { get; } public void AddChunk(Chunk chunk, SyntaxTreeNode association, bool topLevel = false) { @@ -27,13 +30,13 @@ namespace Microsoft.AspNet.Razor.Chunks chunk.Association = association; // If we're not in the middle of a parent chunk - if (_parentStack.Count == 0 || topLevel == true) + if (topLevel) { - ChunkTree.Chunks.Add(chunk); + Root.Children.Add(chunk); } else { - _parentStack.Peek().Children.Add(chunk); + Current.Children.Add(chunk); } } diff --git a/src/Microsoft.AspNet.Razor/Chunks/Generators/TagHelperChunkGenerator.cs b/src/Microsoft.AspNet.Razor/Chunks/Generators/TagHelperChunkGenerator.cs index fbe2f28b71..2617565b86 100644 --- a/src/Microsoft.AspNet.Razor/Chunks/Generators/TagHelperChunkGenerator.cs +++ b/src/Microsoft.AspNet.Razor/Chunks/Generators/TagHelperChunkGenerator.cs @@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators // Populates the chunk tree with chunks associated with attributes attribute.Value.Accept(chunkGenerator); - var chunks = chunkGenerator.Context.ChunkTreeBuilder.ChunkTree.Chunks; + var chunks = chunkGenerator.Context.ChunkTreeBuilder.Root.Children; var first = chunks.FirstOrDefault(); attributeChunkValue = new ParentChunk diff --git a/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpCodeGenerator.cs b/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpCodeGenerator.cs index 5d46d4f23e..bad134b7f8 100644 --- a/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpCodeGenerator.cs +++ b/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpCodeGenerator.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators { } - private ChunkTree Tree { get { return Context.ChunkTreeBuilder.ChunkTree; } } + private ChunkTree Tree { get { return Context.ChunkTreeBuilder.Root; } } public RazorEngineHost Host { get { return Context.Host; } } /// @@ -64,10 +64,10 @@ namespace Microsoft.AspNet.Razor.CodeGenerators var csharpCodeVisitor = CreateCSharpCodeVisitor(writer, Context); - new CSharpTypeMemberVisitor(csharpCodeVisitor, writer, Context).Accept(Tree.Chunks); + new CSharpTypeMemberVisitor(csharpCodeVisitor, writer, Context).Accept(Tree.Children); CreateCSharpDesignTimeCodeVisitor(csharpCodeVisitor, writer, Context) .AcceptTree(Tree); - new CSharpTagHelperFieldDeclarationVisitor(writer, Context).Accept(Tree.Chunks); + new CSharpTagHelperFieldDeclarationVisitor(writer, Context).Accept(Tree.Children); BuildConstructor(writer); @@ -78,8 +78,8 @@ namespace Microsoft.AspNet.Razor.CodeGenerators { using (writer.BuildMethodDeclaration("public override async", "Task", Host.GeneratedClassContext.ExecuteMethodName)) { - new CSharpTagHelperRunnerInitializationVisitor(writer, Context).Accept(Tree.Chunks); - csharpCodeVisitor.Accept(Tree.Chunks); + new CSharpTagHelperRunnerInitializationVisitor(writer, Context).Accept(Tree.Children); + csharpCodeVisitor.Accept(Tree.Children); } } } @@ -131,7 +131,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators protected virtual CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer) { var baseTypeVisitor = new CSharpBaseTypeVisitor(writer, Context); - baseTypeVisitor.Accept(Tree.Chunks); + baseTypeVisitor.Accept(Tree.Children); var baseType = baseTypeVisitor.CurrentBaseType ?? Host.DefaultBaseClass; @@ -153,7 +153,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators { // Write out using directives var usingVisitor = new CSharpUsingVisitor(writer, Context); - foreach (Chunk chunk in Tree.Chunks) + foreach (var chunk in Tree.Children) { usingVisitor.Accept(chunk); } diff --git a/src/Microsoft.AspNet.Razor/CodeGenerators/Visitors/CSharpDesignTimeCodeVisitor.cs b/src/Microsoft.AspNet.Razor/CodeGenerators/Visitors/CSharpDesignTimeCodeVisitor.cs index 3d533dc097..21df5fe4a0 100644 --- a/src/Microsoft.AspNet.Razor/CodeGenerators/Visitors/CSharpDesignTimeCodeVisitor.cs +++ b/src/Microsoft.AspNet.Razor/CodeGenerators/Visitors/CSharpDesignTimeCodeVisitor.cs @@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators.Visitors protected virtual void AcceptTreeCore(ChunkTree tree) { - Accept(tree.Chunks); + Accept(tree.Children); } protected override void Visit(SetBaseTypeChunk chunk) diff --git a/src/Microsoft.AspNet.Razor/RazorTemplateEngine.cs b/src/Microsoft.AspNet.Razor/RazorTemplateEngine.cs index b89ae9c0f4..91668c4d1f 100644 --- a/src/Microsoft.AspNet.Razor/RazorTemplateEngine.cs +++ b/src/Microsoft.AspNet.Razor/RazorTemplateEngine.cs @@ -402,7 +402,7 @@ namespace Microsoft.AspNet.Razor var codeGeneratorResult = codeGenerator.Generate(); // Collect results and return - return new GeneratorResults(results, codeGeneratorResult, codeGeneratorContext.ChunkTreeBuilder.ChunkTree); + return new GeneratorResults(results, codeGeneratorResult, codeGeneratorContext.ChunkTreeBuilder.Root); } protected internal virtual RazorChunkGenerator CreateChunkGenerator( diff --git a/test/Microsoft.AspNet.Razor.Test/Chunks/Generators/ChunkTreeBuilderTest.cs b/test/Microsoft.AspNet.Razor.Test/Chunks/Generators/ChunkTreeBuilderTest.cs index 440e71bb05..cf3cf208ea 100644 --- a/test/Microsoft.AspNet.Razor.Test/Chunks/Generators/ChunkTreeBuilderTest.cs +++ b/test/Microsoft.AspNet.Razor.Test/Chunks/Generators/ChunkTreeBuilderTest.cs @@ -26,12 +26,12 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators builder.EndParentChunk(); // Assert - Assert.Equal(2, builder.ChunkTree.Chunks.Count); + Assert.Equal(2, builder.Root.Children.Count); - var parentChunk = Assert.IsType(builder.ChunkTree.Chunks.First()); + var parentChunk = Assert.IsType(builder.Root.Children.First()); Assert.Empty(parentChunk.Children); - var addTagHelperChunk = Assert.IsType(builder.ChunkTree.Chunks.Last()); + var addTagHelperChunk = Assert.IsType(builder.Root.Children.Last()); Assert.Equal(addTagHelperChunk.LookupText, "some text"); } @@ -50,12 +50,12 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators builder.EndParentChunk(); // Assert - Assert.Equal(2, builder.ChunkTree.Chunks.Count); + Assert.Equal(2, builder.Root.Children.Count); - var parentChunk = Assert.IsType(builder.ChunkTree.Chunks.First()); + var parentChunk = Assert.IsType(builder.Root.Children.First()); Assert.Empty(parentChunk.Children); - var removeTagHelperChunk = Assert.IsType(builder.ChunkTree.Chunks.Last()); + var removeTagHelperChunk = Assert.IsType(builder.Root.Children.Last()); Assert.Equal(removeTagHelperChunk.LookupText, "some text"); } @@ -71,7 +71,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators builder.AddLiteralChunk("some text", previousSpan); // Assert - var chunk = Assert.Single(builder.ChunkTree.Chunks); + var chunk = Assert.Single(builder.Root.Children); var literalChunk = Assert.IsType(chunk); Assert.Equal("some text", literalChunk.Text); Assert.Same(previousSpan, literalChunk.Association); @@ -91,7 +91,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators builder.AddLiteralChunk("

", newSpan); // Assert - var chunk = Assert.Single(builder.ChunkTree.Chunks); + var chunk = Assert.Single(builder.Root.Children); var literalChunk = Assert.IsType(chunk); Assert.Equal("

", literalChunk.Text); var span = Assert.IsType(literalChunk.Association); @@ -114,7 +114,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators builder.AddLiteralChunk("

", literalSpan); // Assert - var chunks = builder.ChunkTree.Chunks; + var chunks = builder.Root.Children; Assert.Equal(2, chunks.Count); var statementChunk = Assert.IsType(chunks[0]); Assert.Equal("int a = 10;", statementChunk.Code);