Run DesignTimeDirectivePass later in the phase

This commit is contained in:
Ajay Bhargav Baaskaran 2017-06-28 16:34:33 -07:00
parent 0a283cdfdb
commit ff433f72b8
8 changed files with 22 additions and 30 deletions

View File

@ -94,18 +94,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
}
var baseType = visitor.Class?.BaseType?.Replace("<TModel>", "<" + modelType + ">");
for (var i = visitor.InheritsDirectives.Count - 1; i >= 0; i--)
{
var directive = visitor.InheritsDirectives[i];
var tokens = directive.Tokens.ToArray();
if (tokens.Length >= 1)
{
baseType = tokens[0].Content.Replace("<TModel>", "<" + modelType + ">");
tokens[0].Content = baseType;
break;
}
}
visitor.Class.BaseType = baseType;
}
}
@ -116,8 +104,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
public ClassDeclarationIntermediateNode Class { get; private set; }
public IList<DirectiveIntermediateNode> InheritsDirectives { get; } = new List<DirectiveIntermediateNode>();
public IList<DirectiveIntermediateNode> ModelDirectives { get; } = new List<DirectiveIntermediateNode>();
public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateNode node)
@ -146,10 +132,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
ModelDirectives.Add(node);
}
else if (node.Descriptor.Directive == "inherits")
{
InheritsDirectives.Add(node);
}
}
}
}

View File

@ -9,8 +9,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
{
internal const string DesignTimeVariable = "__o";
// This needs to run before other directive classifiers.
public override int Order => -10;
// This needs to run after other directive classifiers. Any DirectiveToken that is not removed
// by the previous classifiers will have auto-generated design time support.
public override int Order => DefaultFeatureOrder;
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
{

View File

@ -17,8 +17,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
foreach (var functions in documentNode.FindDirectiveReferences(FunctionsDirective.Directive))
{
functions.Remove();
for (var i = 0; i < functions.Node.Children.Count; i++)
{
@class.Children.Add(functions.Node.Children[i]);

View File

@ -19,12 +19,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
foreach (var inherits in documentNode.FindDirectiveReferences(InheritsDirective.Directive))
{
inherits.Remove();
var token = ((DirectiveIntermediateNode)inherits.Node).Tokens.FirstOrDefault();
if (token != null)
{
@class.BaseType = token.Content;
break;
}

View File

@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
section.Children.Add(directive.Node.Children[i]);
}
directive.Replace(section);
directive.InsertAfter(section);
}
}
}

View File

@ -259,6 +259,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
}
}
// InheritsDirectivePass needs to run before ModelDirective.
var pass = new InheritsDirectivePass()
{
Engine = engine
};
pass.Execute(codeDocument, codeDocument.GetDocumentIntermediateNode());
return codeDocument.GetDocumentIntermediateNode();
}

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
}
[Fact]
public void Execute_MovesStatementsToClassLevel()
public void Execute_AddsStatementsToClassLevel()
{
// Arrange
var engine = CreateEngine();
@ -68,8 +68,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
node => Assert.IsType<MethodDeclarationIntermediateNode>(node),
node => CSharpCode(" var value = true; ", node));
var method = (MethodDeclarationIntermediateNode)@class.Children[0];
Assert.Empty(method.Children);
var method = @class.Children[0];
Children(
method,
node => Assert.IsType<DirectiveIntermediateNode>(node));
}
private static RazorEngine CreateEngine()

View File

@ -66,7 +66,12 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
var @class = @namespace.Children[0];
var method = SingleChild<MethodDeclarationIntermediateNode>(@class);
var section = SingleChild<SectionIntermediateNode>(method);
Children(
method,
node => Assert.IsType<DirectiveIntermediateNode>(node),
node => Assert.IsType<SectionIntermediateNode>(node));
var section = method.Children[1] as SectionIntermediateNode;
Assert.Equal("Header", section.Name);
Children(section, c => Html(" <p>Hello World</p> ", c));
}