Skip to content

Commit

Permalink
RazorEngineFeatureBase: Clean up and add Initialize method
Browse files Browse the repository at this point in the history
This is the same as the previous commit but results in more affected downstream code due to the changes to IRazorEngineFeature. For simplicity, each IRazorEngineFeature implementation now inherits from RazorEngineFeatureBase.
  • Loading branch information
DustinCampbell committed Dec 26, 2024
1 parent 1e065d1 commit e36c4be
Show file tree
Hide file tree
Showing 27 changed files with 222 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#nullable disable

using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
Expand Down Expand Up @@ -339,7 +338,7 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
}
}

private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
private class DesignTimeOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
{
private readonly bool _designTime;

Expand All @@ -350,8 +349,6 @@ public DesignTimeOptionsFeature(bool designTime)

public int Order { get; }

public RazorEngine Engine { get; set; }

public void Configure(RazorParserOptionsBuilder options)
{
options.SetDesignTime(_designTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#nullable disable

using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
Expand Down Expand Up @@ -339,7 +338,7 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
}
}

private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
private class DesignTimeOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
{
private readonly bool _designTime;

Expand All @@ -350,8 +349,6 @@ public DesignTimeOptionsFeature(bool designTime)

public int Order { get; }

public RazorEngine Engine { get; set; }

public void Configure(RazorParserOptionsBuilder options)
{
options.SetDesignTime(_designTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

#nullable disable

using System;
using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Moq;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
Expand Down Expand Up @@ -136,8 +133,10 @@ public void Pass_SetsNamespace_ComputedFromImports()

var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));

var pass = new NamespaceDirective.Pass();
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
var pass = new NamespaceDirective.Pass()
{
Engine = new RazorEngine(features: [], phases: [])
};

// Act
pass.Execute(codeDocument, document);
Expand Down Expand Up @@ -183,8 +182,10 @@ public void Pass_SetsNamespace_ComputedFromSource()

var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));

var pass = new NamespaceDirective.Pass();
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
var pass = new NamespaceDirective.Pass()
{
Engine = new RazorEngine(features: [], phases: [])
};

// Act
pass.Execute(codeDocument, document);
Expand Down Expand Up @@ -221,8 +222,10 @@ public void Pass_SetsNamespace_SanitizesClassAndNamespace()

var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage-Info/Add+User.cshtml"));

var pass = new NamespaceDirective.Pass();
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
var pass = new NamespaceDirective.Pass()
{
Engine = new RazorEngine(features: [], phases: [])
};

// Act
pass.Execute(codeDocument, document);
Expand Down Expand Up @@ -268,8 +271,10 @@ public void Pass_SetsNamespace_ComputedFromSource_ForView()

var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));

var pass = new NamespaceDirective.Pass();
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
var pass = new NamespaceDirective.Pass()
{
Engine = new RazorEngine(features: [], phases: [])
};

// Act
pass.Execute(codeDocument, document);
Expand Down Expand Up @@ -306,8 +311,10 @@ public void Pass_SetsNamespace_VerbatimFromImports()

var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));

var pass = new NamespaceDirective.Pass();
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
var pass = new NamespaceDirective.Pass()
{
Engine = new RazorEngine(features: [], phases: [])
};

// Act
pass.Execute(codeDocument, document);
Expand Down Expand Up @@ -342,8 +349,10 @@ public void Pass_DoesNothing_ForUnknownDocumentKind()

var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("ignored", "/Account/Manage/AddUser.cshtml"));

var pass = new NamespaceDirective.Pass();
pass.Engine = new RazorEngine(ImmutableArray<IRazorEngineFeature>.Empty, ImmutableArray<IRazorEnginePhase>.Empty);
var pass = new NamespaceDirective.Pass()
{
Engine = new RazorEngine(features: [], phases: [])
};

// Act
pass.Execute(codeDocument, document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
}
}

private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
private class DesignTimeOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
{
private readonly bool _designTime;

Expand All @@ -345,8 +345,6 @@ public DesignTimeOptionsFeature(bool designTime)

public int Order { get; }

public RazorEngine Engine { get; set; }

public void Configure(RazorParserOptionsBuilder options)
{
options.SetDesignTime(_designTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ public class ComponentDuplicateAttributeDiagnosticPassTest
{
public ComponentDuplicateAttributeDiagnosticPassTest()
{
Pass = new ComponentMarkupDiagnosticPass();
ProjectEngine = RazorProjectEngine.Create(
RazorConfiguration.Default,
RazorProjectFileSystem.Create(Environment.CurrentDirectory),
b =>
{
// Don't run the markup mutating passes.
b.Features.Remove(b.Features.OfType<ComponentMarkupDiagnosticPass>().Single());
// Don't run the markup mutating passes.
b.Features.Remove(b.Features.OfType<ComponentMarkupDiagnosticPass>().Single());
b.Features.Remove(b.Features.OfType<ComponentMarkupBlockPass>().Single());
b.Features.Remove(b.Features.OfType<ComponentMarkupEncodingPass>().Single());
});
Engine = ProjectEngine.Engine;

Pass.Engine = Engine;
Pass = new ComponentMarkupDiagnosticPass()
{
Engine = Engine
};
}

private RazorProjectEngine ProjectEngine { get; }
Expand Down Expand Up @@ -181,16 +183,4 @@ private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)
Engine.GetFeatures<ComponentDocumentClassifierPass>().Single().Execute(codeDocument, document);
return document;
}

private class StaticTagHelperFeature : ITagHelperFeature
{
public RazorEngine Engine { get; set; }

public List<TagHelperDescriptor> TagHelpers { get; set; }

public IReadOnlyList<TagHelperDescriptor> GetDescriptors()
{
return TagHelpers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class ComponentMarkupBlockPassTest
{
public ComponentMarkupBlockPassTest()
{
Pass = new ComponentMarkupBlockPass(RazorLanguageVersion.Latest);
ProjectEngine = RazorProjectEngine.Create(
RazorConfiguration.Default,
RazorProjectFileSystem.Create(Environment.CurrentDirectory),
Expand All @@ -29,7 +28,10 @@ public ComponentMarkupBlockPassTest()
});
Engine = ProjectEngine.Engine;

Pass.Engine = Engine;
Pass = new ComponentMarkupBlockPass(RazorLanguageVersion.Latest)
{
Engine = Engine
};
}

private RazorProjectEngine ProjectEngine { get; }
Expand Down Expand Up @@ -470,16 +472,4 @@ private DocumentIntermediateNode Lower(RazorCodeDocument codeDocument)
Engine.GetFeatures<ComponentMarkupDiagnosticPass>().Single().Execute(codeDocument, document);
return document;
}

private class StaticTagHelperFeature : ITagHelperFeature
{
public RazorEngine Engine { get; set; }

public List<TagHelperDescriptor> TagHelpers { get; set; }

public IReadOnlyList<TagHelperDescriptor> GetDescriptors()
{
return TagHelpers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public ComponentMarkupEncodingPassTest()
});
Engine = ProjectEngine.Engine;

Pass.Engine = Engine;
Pass = new ComponentMarkupEncodingPass(RazorLanguageVersion.Latest)
{
Engine = Engine
};
}

private RazorProjectEngine ProjectEngine { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ComponentWhitespacePassTest
{
public ComponentWhitespacePassTest()
{
Pass = new ComponentWhitespacePass();
ProjectEngine = RazorProjectEngine.Create(
RazorConfiguration.Default,
RazorProjectFileSystem.Create(Environment.CurrentDirectory),
Expand All @@ -28,7 +27,10 @@ public ComponentWhitespacePassTest()
});
Engine = ProjectEngine.Engine;

Pass.Engine = Engine;
Pass = new ComponentWhitespacePass()
{
Engine = Engine
};
}

private RazorProjectEngine ProjectEngine { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


#nullable disable

using Microsoft.AspNetCore.Razor.Language.Intermediate;
Expand All @@ -26,8 +25,10 @@ public void Execute_IgnoresDocumentsWithDocumentKind()
Options = RazorCodeGenerationOptions.Default,
};

var pass = new DefaultDocumentClassifierPass();
pass.Engine = CreateProjectEngine().Engine;
var pass = new DefaultDocumentClassifierPass()
{
Engine = CreateProjectEngine().Engine
};

// Act
pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode);
Expand All @@ -46,8 +47,10 @@ public void Execute_CreatesClassStructure()
Options = RazorCodeGenerationOptions.Default,
};

var pass = new DefaultDocumentClassifierPass();
pass.Engine = CreateProjectEngine().Engine;
var pass = new DefaultDocumentClassifierPass()
{
Engine = CreateProjectEngine().Engine
};

// Act
pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,35 @@ public void Execute_ExecutesPhasesInOrder()

var firstPass = new Mock<IRazorDirectiveClassifierPass>(MockBehavior.Strict);
firstPass.SetupGet(m => m.Order).Returns(0);
firstPass.SetupProperty(m => m.Engine);

RazorEngine firstPassEngine = null;
firstPass
.SetupGet(m => m.Engine)
.Returns(() => firstPassEngine);
firstPass
.Setup(m => m.Initialize(It.IsAny<RazorEngine>()))
.Callback((RazorEngine engine) => firstPassEngine = engine);

firstPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() =>
{
originalNode.Children.Add(firstPassNode);
});

var secondPass = new Mock<IRazorDirectiveClassifierPass>(MockBehavior.Strict);
secondPass.SetupGet(m => m.Order).Returns(1);
secondPass.SetupProperty(m => m.Engine);

RazorEngine secondPassEngine = null;
secondPass
.SetupGet(m => m.Engine)
.Returns(() => secondPassEngine);
secondPass
.Setup(m => m.Initialize(It.IsAny<RazorEngine>()))
.Callback((RazorEngine engine) => secondPassEngine = engine);

secondPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() =>
{
// Works only when the first pass has run before this.
originalNode.Children[0].Children.Add(secondPassNode);
// Works only when the first pass has run before this.
originalNode.Children[0].Children.Add(secondPassNode);
});

var phase = new DefaultRazorDirectiveClassifierPhase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,35 @@ public void Execute_ExecutesPhasesInOrder()

var firstPass = new Mock<IRazorDocumentClassifierPass>(MockBehavior.Strict);
firstPass.SetupGet(m => m.Order).Returns(0);
firstPass.SetupProperty(m => m.Engine);

RazorEngine firstPassEngine = null;
firstPass
.SetupGet(m => m.Engine)
.Returns(() => firstPassEngine);
firstPass
.Setup(m => m.Initialize(It.IsAny<RazorEngine>()))
.Callback((RazorEngine engine) => firstPassEngine = engine);

firstPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() =>
{
originalNode.Children.Add(firstPassNode);
});

var secondPass = new Mock<IRazorDocumentClassifierPass>(MockBehavior.Strict);
secondPass.SetupGet(m => m.Order).Returns(1);
secondPass.SetupProperty(m => m.Engine);

RazorEngine secondPassEngine = null;
secondPass
.SetupGet(m => m.Engine)
.Returns(() => secondPassEngine);
secondPass
.Setup(m => m.Initialize(It.IsAny<RazorEngine>()))
.Callback((RazorEngine engine) => secondPassEngine = engine);

secondPass.Setup(m => m.Execute(codeDocument, originalNode)).Callback(() =>
{
// Works only when the first pass has run before this.
originalNode.Children[0].Children.Add(secondPassNode);
// Works only when the first pass has run before this.
originalNode.Children[0].Children.Add(secondPassNode);
});

var phase = new DefaultRazorDocumentClassifierPhase();
Expand Down
Loading

0 comments on commit e36c4be

Please sign in to comment.