Skip to content

Commit 26ce354

Browse files
authored
Correctly stitch @ characters to following identifiers when rewriting tag helpers (#10331)
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 2f71d65 commit 26ce354

File tree

52 files changed

+893
-114
lines changed

Some content is hidden

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

52 files changed

+893
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
Code span at (13:0,13 [4] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [9] )
2-
Code span at (17:0,17 [1] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (17:0,17 [5] )
3-
Code span at (18:0,18 [4] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (17:0,17 [5] )
2+
Code span at (17:0,17 [5] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (17:0,17 [5] )
43
Code span at (22:0,22 [2] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [11] )

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
Transition;[<Missing>];
2323
CSharpImplicitExpressionBody - [17..22)::5
2424
CSharpCodeBlock - [17..22)::5
25-
CSharpExpressionLiteral - [17..18)::1 - [@] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
26-
Transition;[@];
27-
CSharpExpressionLiteral - [18..22)::4 - [true] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
28-
Keyword;[true];
25+
CSharpExpressionLiteral - [17..22)::5 - [@true] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
26+
Keyword;[@true];
2927
CSharpExpressionLiteral - [22..24)::2 - [ ] - Gen<None> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
3028
Whitespace;[ ];
3129
MarkupTextLiteral - [24..25)::1 - ['] - Gen<Markup> - SpanEditHandler;Accepts:Any
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
Code span at (13:0,13 [1] ) (Accepts:AnyExceptNewline) - Parent: Markup block at (13:0,13 [19] )
2-
Code span at (14:0,14 [1] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (14:0,14 [18] )
3-
Code span at (15:0,15 [17] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (14:0,14 [18] )
2+
Code span at (14:0,14 [18] ) (Accepts:AnyExceptNewline) - Parent: Expression block at (14:0,14 [18] )

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
Transition;[<Missing>];
2323
CSharpImplicitExpressionBody - [14..32)::18
2424
CSharpCodeBlock - [14..32)::18
25-
CSharpExpressionLiteral - [14..15)::1 - [@] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
26-
Transition;[@];
27-
CSharpExpressionLiteral - [15..32)::17 - [DateTime.Now.Year] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
28-
Identifier;[DateTime];
25+
CSharpExpressionLiteral - [14..32)::18 - [@DateTime.Now.Year] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:AnyExceptNewline;ImplicitExpression[ATD];K15
26+
Identifier;[@DateTime];
2927
Dot;[.];
3028
Identifier;[Now];
3129
Dot;[.];
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|

0 commit comments

Comments
 (0)