Skip to content

Commit 69ae12c

Browse files
committed
Correctly stitch @ characters to following identifiers when rewriting tag helpers
Because of how we were rewriting tag helper attribute values, we didn't output a single, unified token when moving an implicit C# transition back to standard C#. This meant that, when we emit `#line` directives, we considered the `@` and the identifier that followed it to be separate tokens; when we added more precise info for FUSE, this then meant that they were emitted on separate lines during runtime codegen and everything fell apart. To fix this, we now unify those implicit transition tokens, rather than trying to keep them as separate tokens. Fixes #10186, for real this time.
1 parent 05aed40 commit 69ae12c

File tree

48 files changed

+887
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+887
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
Code span at (13:0,13 [1] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [14] )
22
Code span at (14:0,14 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [14] )
33
Code span at (16:0,16 [1] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (16:0,16 [7] )
4-
Code span at (17:0,17 [1] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (17:0,17 [6] )
5-
Code span at (18:0,18 [5] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (17:0,17 [6] )
4+
Code span at (17:0,17 [6] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (17:0,17 [6] )
65
Code span at (23:0,23 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [14] )
76
Code span at (25:0,25 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [14] )
87
Code span at (39:0,39 [6] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (39:0,39 [46] )
9-
Code span at (45:0,45 [1] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (45:0,45 [11] )
10-
Code span at (46:0,46 [10] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (45:0,45 [11] )
8+
Code span at (45:0,45 [11] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (45:0,45 [11] )
119
Code span at (56:0,56 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (39:0,39 [46] )
1210
Code span at (58:0,58 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (58:0,58 [3] )
1311
Code span at (60:0,60 [1] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (58:0,58 [3] )
1412
Code span at (61:0,61 [8] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (39:0,39 [46] )
1513
Code span at (69:0,69 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (39:0,39 [46] )
1614
Code span at (71:0,71 [1] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (71:0,71 [14] )
17-
Code span at (72:0,72 [1] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (72:0,72 [13] )
18-
Code span at (73:0,73 [12] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (72:0,72 [13] )
15+
Code span at (72:0,72 [13] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (72:0,72 [13] )

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes7.stree.txt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
Transition;[<Missing>];
2828
CSharpImplicitExpressionBody - [17..23)::6
2929
CSharpCodeBlock - [17..23)::6
30-
CSharpExpressionLiteral - [17..18)::1 - [@] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
31-
Transition;[@];
32-
CSharpExpressionLiteral - [18..23)::5 - [value] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
33-
Identifier;[value];
30+
CSharpExpressionLiteral - [17..23)::6 - [@value] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
31+
Identifier;[@value];
3432
CSharpExpressionLiteral - [23..25)::2 - [ +] - Gen<None> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
3533
Whitespace;[ ];
3634
Text;[+];
@@ -57,10 +55,8 @@
5755
Transition;[<Missing>];
5856
CSharpImplicitExpressionBody - [45..56)::11
5957
CSharpCodeBlock - [45..56)::11
60-
CSharpExpressionLiteral - [45..46)::1 - [@] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
61-
Transition;[@];
62-
CSharpExpressionLiteral - [46..56)::10 - [Bag["val"]] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
63-
Identifier;[Bag];
58+
CSharpExpressionLiteral - [45..56)::11 - [@Bag["val"]] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
59+
Identifier;[@Bag];
6460
LeftBracket;[[];
6561
StringLiteral;["val"];
6662
RightBracket;[]];
@@ -87,10 +83,8 @@
8783
Transition;[<Missing>];
8884
CSharpImplicitExpressionBody - [72..85)::13
8985
CSharpCodeBlock - [72..85)::13
90-
CSharpExpressionLiteral - [72..73)::1 - [@] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
91-
Transition;[@];
92-
CSharpExpressionLiteral - [73..85)::12 - [DateTime.Now] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
93-
Identifier;[DateTime];
86+
CSharpExpressionLiteral - [72..85)::13 - [@DateTime.Now] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
87+
Identifier;[@DateTime];
9488
Dot;[.];
9589
Identifier;[Now];
9690
MarkupTextLiteral - [85..86)::1 - ['] - Gen<Markup> - SpanEditHandler;Accepts:Any

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.CompilerServices;
1010
using Microsoft.AspNetCore.Razor.Language.Extensions;
1111
using Microsoft.AspNetCore.Razor.Test.Common;
12+
using Roslyn.Test.Utilities;
1213

1314
namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests;
1415

@@ -255,6 +256,9 @@ public class CodeGenerationIntegrationTest(bool designTime = false)
255256
[IntegrationTestFact]
256257
public void AddTagHelperDirective() => RunTest();
257258

259+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
260+
public void EscapedIdentifier() => RunTagHelpersTest(TestTagHelperDescriptors.SimpleTagHelperDescriptors);
261+
258262
public override string GetTestFileName(string testName)
259263
{
260264
return base.GetTestFileName(testName) + (designTime ? "_DesignTime" : "_Runtime");

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/TagHelperBlockRewriterTest.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Immutable;
99
using System.Globalization;
1010
using Microsoft.AspNetCore.Razor.Language.Components;
11+
using Roslyn.Test.Utilities;
1112
using Xunit;
1213
using static Microsoft.AspNetCore.Razor.Language.CommonMetadata;
1314

@@ -555,6 +556,83 @@ public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes12()
555556
EvaluateData(CodeTagHelperAttributes_Descriptors, "<person age=\"@{flag == 0 ? 11 : 12}\" />");
556557
}
557558

559+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
560+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes13()
561+
{
562+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
563+
@{
564+
var count = "1";
565+
}
566+
<person age="Convert.ToInt32(@count)" />
567+
""");
568+
}
569+
570+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
571+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes14()
572+
{
573+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
574+
@{
575+
var @string = "1";
576+
}
577+
<person age="Convert.ToInt32(@string)" />
578+
""");
579+
}
580+
581+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
582+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes15()
583+
{
584+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
585+
@{
586+
var count = "1";
587+
}
588+
<person age=Convert.ToInt32(@count) />
589+
""");
590+
}
591+
592+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
593+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes16()
594+
{
595+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
596+
@{
597+
var count = "1";
598+
}
599+
<person age='Convert.ToInt32(@count + "2")' />
600+
""");
601+
}
602+
603+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
604+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes17()
605+
{
606+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
607+
@{
608+
var count = 1;
609+
}
610+
<person age='@@count' />
611+
""");
612+
}
613+
614+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
615+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes18()
616+
{
617+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
618+
@{
619+
var count = 1;
620+
}
621+
<person age="@@count" />
622+
""");
623+
}
624+
625+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/10186")]
626+
public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes19()
627+
{
628+
EvaluateData(CodeTagHelperAttributes_Descriptors, """
629+
@{
630+
var count = 1;
631+
}
632+
<person age=@@count />
633+
""");
634+
}
635+
558636
[Fact]
559637
public void TagHelperParseTreeRewriter_CreatesErrorForIncompleteTagHelper1()
560638
{

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@
217217
LazyIntermediateToken - (933:25,21 [2] ComplexTagHelpers.cshtml) - CSharp - +
218218
LazyIntermediateToken - (935:25,23 [1] ComplexTagHelpers.cshtml) - CSharp -
219219
CSharpExpression - (936:25,24 [24] ComplexTagHelpers.cshtml)
220-
LazyIntermediateToken - (936:25,24 [1] ComplexTagHelpers.cshtml) - CSharp - @
221-
LazyIntermediateToken - (937:25,25 [23] ComplexTagHelpers.cshtml) - CSharp - DateTimeOffset.Now.Year
220+
LazyIntermediateToken - (936:25,24 [24] ComplexTagHelpers.cshtml) - CSharp - @DateTimeOffset.Now.Year
222221
DefaultTagHelperExecute -
223222
HtmlContent - (1075:27,12 [10] ComplexTagHelpers.cshtml)
224223
LazyIntermediateToken - (1075:27,12 [10] ComplexTagHelpers.cshtml) - Html - \n

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.mappings.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,10 @@ Source Location: (935:25,23 [1] TestFiles/IntegrationTests/CodeGenerationIntegra
189189
Generated Location: (9254:213,40 [1] )
190190
| |
191191

192-
Source Location: (936:25,24 [1] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
193-
|@|
194-
Generated Location: (9255:213,41 [1] )
195-
|@|
196-
197-
Source Location: (937:25,25 [23] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
198-
|DateTimeOffset.Now.Year|
199-
Generated Location: (9256:213,42 [23] )
200-
|DateTimeOffset.Now.Year|
192+
Source Location: (936:25,24 [24] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
193+
|@DateTimeOffset.Now.Year|
194+
Generated Location: (9255:213,41 [24] )
195+
|@DateTimeOffset.Now.Year|
201196

202197
Source Location: (1155:29,28 [30] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
203198
|DateTimeOffset.Now.Year > 2014|

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.codegen.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,8 @@ public async System.Threading.Tasks.Task ExecuteAsync()
493493
#line hidden
494494
#nullable disable
495495
#nullable restore
496-
#line (26,25)-(26,26) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml"
497-
@
498-
499-
#line default
500-
#line hidden
501-
#nullable disable
502-
#nullable restore
503-
#line (26,26)-(26,49) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml"
504-
DateTimeOffset.Now.Year
496+
#line (26,25)-(26,49) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml"
497+
@DateTimeOffset.Now.Year
505498

506499
#line default
507500
#line hidden

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.ir.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@
218218
LazyIntermediateToken - (933:25,21 [2] ComplexTagHelpers.cshtml) - CSharp - +
219219
LazyIntermediateToken - (935:25,23 [1] ComplexTagHelpers.cshtml) - CSharp -
220220
CSharpExpression - (936:25,24 [24] ComplexTagHelpers.cshtml)
221-
LazyIntermediateToken - (936:25,24 [1] ComplexTagHelpers.cshtml) - CSharp - @
222-
LazyIntermediateToken - (937:25,25 [23] ComplexTagHelpers.cshtml) - CSharp - DateTimeOffset.Now.Year
221+
LazyIntermediateToken - (936:25,24 [24] ComplexTagHelpers.cshtml) - CSharp - @DateTimeOffset.Now.Year
223222
DefaultTagHelperExecute -
224223
HtmlContent - (1075:27,12 [10] ComplexTagHelpers.cshtml)
225224
LazyIntermediateToken - (1075:27,12 [10] ComplexTagHelpers.cshtml) - Html - \n
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@addTagHelper "*, TestAssembly"
2+
3+
@{
4+
var count = "1";
5+
}
6+
<input age="Convert.ToInt32(@count)" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles
4+
{
5+
#line hidden
6+
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_EscapedIdentifier_DesignTime
7+
{
8+
#line hidden
9+
#pragma warning disable 0649
10+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext;
11+
#pragma warning restore 0649
12+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner();
13+
private global::InputTagHelper __InputTagHelper;
14+
#pragma warning disable 219
15+
private void __RazorDirectiveTokenHelpers__() {
16+
((global::System.Action)(() => {
17+
#nullable restore
18+
#line 1 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EscapedIdentifier.cshtml"
19+
global::System.Object __typeHelper = "*, TestAssembly";
20+
21+
#line default
22+
#line hidden
23+
#nullable disable
24+
}
25+
))();
26+
}
27+
#pragma warning restore 219
28+
#pragma warning disable 0414
29+
private static object __o = null;
30+
#pragma warning restore 0414
31+
#pragma warning disable 1998
32+
public async System.Threading.Tasks.Task ExecuteAsync()
33+
{
34+
#nullable restore
35+
#line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EscapedIdentifier.cshtml"
36+
37+
var count = "1";
38+
39+
#line default
40+
#line hidden
41+
#nullable disable
42+
__InputTagHelper = CreateTagHelper<global::InputTagHelper>();
43+
#nullable restore
44+
#line 6 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EscapedIdentifier.cshtml"
45+
__InputTagHelper.AgeProp = Convert.ToInt32(@count);
46+
47+
#line default
48+
#line hidden
49+
#nullable disable
50+
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
51+
}
52+
#pragma warning restore 1998
53+
}
54+
}
55+
#pragma warning restore 1591

0 commit comments

Comments
 (0)