diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultInstrumentationPass.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultInstrumentationPass.cs index f08c8dd5a5..f49e296190 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultInstrumentationPass.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultInstrumentationPass.cs @@ -104,6 +104,16 @@ namespace Microsoft.AspNetCore.Razor.Evolution VisitDefault(node); } + + public override void VisitAddTagHelperHtmlAttribute(AddTagHelperHtmlAttributeIRNode node) + { + // We don't want to instrument TagHelper attributes. Do nothing. + } + + public override void VisitSetTagHelperProperty(SetTagHelperPropertyIRNode node) + { + // We don't want to instrument TagHelper attributes. Do nothing. + } } } } diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/DefaultInstrumentationPassTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/DefaultInstrumentationPassTest.cs index 6bf3c28d51..ecf04b2d1c 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/DefaultInstrumentationPassTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/DefaultInstrumentationPassTest.cs @@ -111,6 +111,100 @@ namespace Microsoft.AspNetCore.Razor.Evolution n => CSharpExpression("Hi", n)); } + [Fact] + public void InstrumentationPass_SkipsCSharpExpression_InsideTagHelperAttribute() + { + // Arrange + var builder = RazorIRBuilder.Document(); + builder.Push(new TagHelperIRNode() + { + Source = CreateSource(3) + }); + + builder.Push(new AddTagHelperHtmlAttributeIRNode()); + + builder.Push(new CSharpExpressionIRNode() + { + Source = CreateSource(5) + }); + + builder.Add(new CSharpTokenIRNode() + { + Content = "Hi", + }); + + var irDocument = (DocumentIRNode)builder.Build(); + + var pass = new DefaultInstrumentationPass(); + + // Act + var result = pass.ExecuteCore(TestRazorCodeDocument.CreateEmpty(), irDocument); + + // Assert + Children( + irDocument, + n => + { + Assert.IsType(n); + Children( + n, + c => + { + Assert.IsType(c); + Children( + c, + s => CSharpExpression("Hi", s)); + }); + }); + } + + [Fact] + public void InstrumentationPass_SkipsCSharpExpression_InsideTagHelperProperty() + { + // Arrange + var builder = RazorIRBuilder.Document(); + builder.Push(new TagHelperIRNode() + { + Source = CreateSource(3) + }); + + builder.Push(new SetTagHelperPropertyIRNode()); + + builder.Push(new CSharpExpressionIRNode() + { + Source = CreateSource(5) + }); + + builder.Add(new CSharpTokenIRNode() + { + Content = "Hi", + }); + + var irDocument = (DocumentIRNode)builder.Build(); + + var pass = new DefaultInstrumentationPass(); + + // Act + var result = pass.ExecuteCore(TestRazorCodeDocument.CreateEmpty(), irDocument); + + // Assert + Children( + irDocument, + n => + { + Assert.IsType(n); + Children( + n, + c => + { + Assert.IsType(c); + Children( + c, + s => CSharpExpression("Hi", s)); + }); + }); + } + [Fact] public void InstrumentationPass_InstrumentsExecuteTagHelper_InsideTagHelper() { @@ -172,7 +266,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution }); } - [Fact] public void InstrumentationPass_SkipsExecuteTagHelper_MalformedTagHelper() { diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/InstrumentationPassIntegrationTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/InstrumentationPassIntegrationTest.cs index 53c2ace676..71170f3783 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/InstrumentationPassIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/InstrumentationPassIntegrationTest.cs @@ -36,7 +36,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests { Name = "value", PropertyName = "FooProp", - TypeName = "System.String" + TypeName = "System.String" // Gets preallocated + }, + new TagHelperAttributeDescriptor + { + Name = "date", + PropertyName = "BarProp", + TypeName = "System.DateTime" // Doesn't get preallocated } } } @@ -55,7 +61,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests // Assert AssertIRMatchesBaseline(document.GetIRDocument()); - AssertCSharpDocumentMatchesBaseline(document.GetCSharpDocument()); + var csharpDocument = document.GetCSharpDocument(); + AssertCSharpDocumentMatchesBaseline(csharpDocument); + Assert.Empty(csharpDocument.Diagnostics); } } } diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.codegen.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.codegen.cs index 4f7957ce1d..1b539696ee 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.codegen.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.codegen.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "7e1ceb3e8ebe2125cd44bea5d9f4ac1acf9ec12b" +#pragma checksum "TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "5898df8049fc1522f646a774050e93cbf5cafb84" namespace { #line hidden @@ -56,8 +56,14 @@ Write("Hello"); __tagHelperExecutionContext.Add(__InputTagHelper); __InputTagHelper.FooProp = (string)__tagHelperAttribute_0.Value; __tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_0); +#line 5 "TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml" +__InputTagHelper.BarProp = DateTime.Now; + +#line default +#line hidden + __tagHelperExecutionContext.AddTagHelperAttribute("date", __InputTagHelper.BarProp, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_1); - BeginContext(97, 33, false); + BeginContext(97, 52, false); await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); if (!__tagHelperExecutionContext.Output.IsContentModified) { @@ -66,7 +72,7 @@ Write("Hello"); Write(__tagHelperExecutionContext.Output); __tagHelperExecutionContext = __tagHelperScopeManager.End(); EndContext(); - BeginContext(130, 2, true); + BeginContext(149, 2, true); WriteLiteral("\r\n"); EndContext(); } @@ -74,7 +80,7 @@ Write("Hello"); __FormTagHelper = CreateTagHelper(); __tagHelperExecutionContext.Add(__FormTagHelper); __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_2); - BeginContext(71, 68, false); + BeginContext(71, 87, false); await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); if (!__tagHelperExecutionContext.Output.IsContentModified) { diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml index 38fbfa07dd..4cc967e577 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml @@ -2,5 +2,5 @@ Hola @("Hello")
- +
\ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.ir.txt b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.ir.txt index 125fcc7354..9c33ee24ab 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.ir.txt +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.ir.txt @@ -19,24 +19,27 @@ Document - CSharpStatement - - BeginContext(69, 2, true); HtmlContent - (69:2,10 [2] BasicTest.cshtml) - \n CSharpStatement - - EndContext(); - TagHelper - (71:3,0 [68] BasicTest.cshtml) + TagHelper - (71:3,0 [87] BasicTest.cshtml) InitializeTagHelperStructure - - form - TagMode.StartTagAndEndTag CSharpStatement - - BeginContext(91, 6, true); HtmlContent - (91:3,20 [6] BasicTest.cshtml) - \n CSharpStatement - - EndContext(); - TagHelper - (97:4,4 [33] BasicTest.cshtml) + TagHelper - (97:4,4 [52] BasicTest.cshtml) InitializeTagHelperStructure - - input - TagMode.SelfClosing CreateTagHelper - - InputTagHelper SetPreallocatedTagHelperProperty - - __tagHelperAttribute_0 - value - FooProp + SetTagHelperProperty - (121:4,28 [13] BasicTest.cshtml) - date - BarProp - HtmlAttributeValueStyle.DoubleQuotes + CSharpExpression - (122:4,29 [12] BasicTest.cshtml) + CSharpToken - (122:4,29 [12] BasicTest.cshtml) - DateTime.Now AddPreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_1 - CSharpStatement - - BeginContext(97, 33, false); + CSharpStatement - - BeginContext(97, 52, false); ExecuteTagHelpers - CSharpStatement - - EndContext(); - CSharpStatement - - BeginContext(130, 2, true); - HtmlContent - (130:4,37 [2] BasicTest.cshtml) - \n + CSharpStatement - - BeginContext(149, 2, true); + HtmlContent - (149:4,56 [2] BasicTest.cshtml) - \n CSharpStatement - - EndContext(); CreateTagHelper - - FormTagHelper AddPreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_2 - CSharpStatement - - BeginContext(71, 68, false); + CSharpStatement - - BeginContext(71, 87, false); ExecuteTagHelpers - CSharpStatement - - EndContext();