From ff433f72b88e6bab61ec530b828bd70f459da79a Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Wed, 28 Jun 2017 16:34:33 -0700 Subject: [PATCH] Run DesignTimeDirectivePass later in the phase --- .../ModelDirective.cs | 18 ------------------ .../Extensions/DesignTimeDirectivePass.cs | 5 +++-- .../Extensions/FunctionsDirectivePass.cs | 2 -- .../Extensions/InheritsDirectivePass.cs | 3 --- .../Extensions/SectionDirectivePass.cs | 2 +- .../ModelDirectiveTest.cs | 7 +++++++ .../Extensions/FunctionsDirectivePassTest.cs | 8 +++++--- .../Extensions/SectionDirectivePassTest.cs | 7 ++++++- 8 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs index 6a3c68e0ae..e8a38f1e68 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ModelDirective.cs @@ -94,18 +94,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions } var baseType = visitor.Class?.BaseType?.Replace("", "<" + 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("", "<" + 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 InheritsDirectives { get; } = new List(); - public IList ModelDirectives { get; } = new List(); 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); - } } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectivePass.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectivePass.cs index 0a69d2d12f..bb347422f7 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectivePass.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectivePass.cs @@ -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) { diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirectivePass.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirectivePass.cs index eb39137922..32f79570cb 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirectivePass.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/FunctionsDirectivePass.cs @@ -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]); diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirectivePass.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirectivePass.cs index be87679197..f45d6fb982 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirectivePass.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/InheritsDirectivePass.cs @@ -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; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirectivePass.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirectivePass.cs index f980fdc133..612aeb682d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirectivePass.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirectivePass.cs @@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions section.Children.Add(directive.Node.Children[i]); } - directive.Replace(section); + directive.InsertAfter(section); } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ModelDirectiveTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ModelDirectiveTest.cs index 48ffafcd28..704718dcbd 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ModelDirectiveTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/ModelDirectiveTest.cs @@ -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(); } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/FunctionsDirectivePassTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/FunctionsDirectivePassTest.cs index 2566d75057..c0df28f513 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/FunctionsDirectivePassTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/FunctionsDirectivePassTest.cs @@ -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(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(node)); } private static RazorEngine CreateEngine() diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionDirectivePassTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionDirectivePassTest.cs index 69591573f4..ce5b1e8303 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionDirectivePassTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionDirectivePassTest.cs @@ -66,7 +66,12 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var @class = @namespace.Children[0]; var method = SingleChild(@class); - var section = SingleChild(method); + Children( + method, + node => Assert.IsType(node), + node => Assert.IsType(node)); + + var section = method.Children[1] as SectionIntermediateNode; Assert.Equal("Header", section.Name); Children(section, c => Html("

Hello World

", c)); }