Remove DirectiveIRNode from the IR document after the default directive classifier has run

This commit is contained in:
Ajay Bhargav Baaskaran 2017-02-09 11:41:47 -08:00
parent 02675da467
commit 260b869a07
2 changed files with 37 additions and 17 deletions

View File

@ -50,8 +50,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
if (string.Equals(node.Name, CSharpCodeParser.FunctionsDirectiveDescriptor.Name, StringComparison.Ordinal)) if (string.Equals(node.Name, CSharpCodeParser.FunctionsDirectiveDescriptor.Name, StringComparison.Ordinal))
{ {
node.Parent.Children.Remove(node);
foreach (var child in node.Children.Except(node.Tokens)) foreach (var child in node.Children.Except(node.Tokens))
{ {
child.Parent = _classNode; child.Parent = _classNode;
@ -60,8 +58,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution
} }
else if (string.Equals(node.Name, CSharpCodeParser.InheritsDirectiveDescriptor.Name, StringComparison.Ordinal)) else if (string.Equals(node.Name, CSharpCodeParser.InheritsDirectiveDescriptor.Name, StringComparison.Ordinal))
{ {
node.Parent.Children.Remove(node);
var token = node.Tokens.FirstOrDefault(); var token = node.Tokens.FirstOrDefault();
if (token != null) if (token != null)
@ -72,7 +68,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution
else if (string.Equals(node.Name, CSharpCodeParser.SectionDirectiveDescriptor.Name, StringComparison.Ordinal)) else if (string.Equals(node.Name, CSharpCodeParser.SectionDirectiveDescriptor.Name, StringComparison.Ordinal))
{ {
var sectionIndex = node.Parent.Children.IndexOf(node); var sectionIndex = node.Parent.Children.IndexOf(node);
node.Parent.Children.Remove(node);
var defineSectionEndStatement = new CSharpStatementIRNode() var defineSectionEndStatement = new CSharpStatementIRNode()
{ {
@ -94,18 +89,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution
node.Parent.Children.Insert(sectionIndex, defineSectionStartStatement); node.Parent.Children.Insert(sectionIndex, defineSectionStartStatement);
} }
else if (string.Equals(node.Name, CSharpCodeParser.AddTagHelperDirectiveDescriptor.Name, StringComparison.Ordinal))
{ node.Parent.Children.Remove(node);
node.Parent.Children.Remove(node);
}
else if (string.Equals(node.Name, CSharpCodeParser.RemoveTagHelperDirectiveDescriptor.Name, StringComparison.Ordinal))
{
node.Parent.Children.Remove(node);
}
else if (string.Equals(node.Name, CSharpCodeParser.TagHelperPrefixDirectiveDescriptor.Name, StringComparison.Ordinal))
{
node.Parent.Children.Remove(node);
}
} }
} }
} }

View File

@ -167,6 +167,41 @@ namespace Microsoft.AspNetCore.Razor.Evolution
node => CSharpStatement("});", node)); node => CSharpStatement("});", node));
} }
[Fact]
public void Execute_Custom_RemovesDirectiveIRNodeFromIRDocument()
{
// Arrange
var content = "@custom Hello";
var sourceDocument = TestRazorSourceDocument.Create(content);
var codeDocument = RazorCodeDocument.Create(sourceDocument);
var defaultEngine = RazorEngine.Create(b =>
{
var customDirective = DirectiveDescriptorBuilder.Create("custom").AddString().Build();
b.AddDirective(customDirective);
});
var originalIRDocument = Lower(codeDocument, defaultEngine);
var pass = new DefaultDirectiveIRPass()
{
Engine = defaultEngine,
};
// Act
var irDocument = pass.Execute(codeDocument, originalIRDocument);
// Assert
Children(irDocument,
node => Assert.IsType<ChecksumIRNode>(node),
node => Assert.IsType<NamespaceDeclarationIRNode>(node));
var @namespace = irDocument.Children[1];
Children(@namespace,
node => Assert.IsType<UsingStatementIRNode>(node),
node => Assert.IsType<UsingStatementIRNode>(node),
node => Assert.IsType<ClassDeclarationIRNode>(node));
var @class = @namespace.Children[2];
var method = SingleChild<RazorMethodDeclarationIRNode>(@class);
Assert.Empty(method.Children);
}
private static DocumentIRNode Lower(RazorCodeDocument codeDocument) private static DocumentIRNode Lower(RazorCodeDocument codeDocument)
{ {
var engine = RazorEngine.Create(); var engine = RazorEngine.Create();