Skip to content

Commit b4c3326

Browse files
Remove SyntaxTrivia from the compiler (#11702)
Currently, the syntax tree produced by the Razor compiler _looks_ like it might support trivia. It even has a `SyntaxTrivia` class, methods on `SyntaxNode` and `SyntaxToken` to handle leading and trailing trivia, and methods on `SyntaxVisitor`, `SyntaxRewriter` and `SyntaxWalker` to visit trivia. There're even separate properties that look like they might provide different values depending on trivia, such as `Width` and `FullWIdth`. None of that is real. The Razor parser doesn't actually produce trivia. The leading and trailing trivia for a `SyntaxToken` is always empty. The `FullWIdth` and `Width` properties always return the same value, as do `FullSpan` and `Span`. Since there's so much infrastructure to support trivia, it seems like the Razor compiler _might_ have generated it at some point, though I suspect it was just left over from an old copy-paste from Roslyn and never cleaned up.' Honestly, it would be a major endeavor if the Razor compiler were updated to support trivia, and I personally don't believe the value is worth the effort. So, this change removes all of the compiler infrastructure in support of syntax trivia. The goal is to make steps toward eventually converting `SyntaxToken` to a struct. Today, `SyntaxToken` is defined as a class, which consistently shows up as a major source of allocations in perf traces. I was pretty meticulous with my commit history and recommend reviewing commit-by-commit. Most of the changes are deletions. 😄
2 parents 4cc89e4 + 684cae0 commit b4c3326

File tree

46 files changed

+149
-1852
lines changed

Some content is hidden

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

46 files changed

+149
-1852
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/Legacy/CSharpToMarkupSwitchTest.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using Microsoft.AspNetCore.Razor.Language.Components;
5+
using Microsoft.AspNetCore.Razor.Language.Syntax;
76
using Roslyn.Test.Utilities;
87
using Xunit;
98

@@ -356,16 +355,22 @@ public void CodeBlocksTrailingWhitespace_10()
356355
""",
357356
directives: [ComponentCodeDirective.Directive]);
358357

359-
var codeBlock = tree1.Root.ChildNodes()[0].ChildNodes()[1];
360-
Assert.Equal("CSharpCodeBlockSyntax<CSharpCodeBlock> at 0::11", codeBlock.ToString());
358+
var codeBlock = Assert.IsType<CSharpCodeBlockSyntax>(tree1.Root.ChildNodes()[0].ChildNodes()[1]);
359+
Assert.Equal(SyntaxKind.CSharpCodeBlock, codeBlock.Kind);
360+
Assert.Equal(0, codeBlock.Position);
361+
Assert.Equal(11, codeBlock.Width);
361362

362363
var children = codeBlock.ChildNodes();
363364
Assert.Equal(2, children.Count);
364365

365-
var directive = children[0];
366-
Assert.Equal("RazorDirectiveSyntax<RazorDirective> at 0::9", directive.ToString());
366+
var directive = Assert.IsType<RazorDirectiveSyntax>(children[0]);
367+
Assert.Equal(SyntaxKind.RazorDirective, directive.Kind);
368+
Assert.Equal(0, directive.Position);
369+
Assert.Equal(9, directive.Width);
367370

368-
var whitespace = children[1];
369-
Assert.Equal("RazorMetaCodeSyntax<RazorMetaCode> at 9::2", whitespace.ToString());
371+
var whitespace = Assert.IsType<RazorMetaCodeSyntax>(children[1]);
372+
Assert.Equal(SyntaxKind.RazorMetaCode, whitespace.Kind);
373+
Assert.Equal(9, whitespace.Position);
374+
Assert.Equal(2, whitespace.Width);
370375
}
371376
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/legacyTest/Legacy/TagHelperRewritingTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal void EvaluateData(
4747
var binder = new TagHelperBinder(tagHelperPrefix, descriptors);
4848
var rewrittenTree = TagHelperParseTreeRewriter.Rewrite(syntaxTree, binder, out _);
4949

50-
Assert.Equal(syntaxTree.Root.FullWidth, rewrittenTree.Root.FullWidth);
50+
Assert.Equal(syntaxTree.Root.Width, rewrittenTree.Root.Width);
5151

5252
BaselineTest(rewrittenTree);
5353
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal void EvaluateData(
4747
var binder = new TagHelperBinder(tagHelperPrefix, descriptors);
4848
var rewrittenTree = TagHelperParseTreeRewriter.Rewrite(syntaxTree, binder, out _);
4949

50-
Assert.Equal(syntaxTree.Root.FullWidth, rewrittenTree.Root.FullWidth);
50+
Assert.Equal(syntaxTree.Root.Width, rewrittenTree.Root.Width);
5151

5252
BaselineTest(rewrittenTree);
5353
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorIntermediateNodeLoweringPhase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ private void Combine(HtmlContentIntermediateNode node, SyntaxNode item)
12511251
node.Source.Value.AbsoluteIndex,
12521252
node.Source.Value.LineIndex,
12531253
node.Source.Value.CharacterIndex,
1254-
node.Source.Value.Length + item.FullWidth,
1254+
node.Source.Value.Length + item.Width,
12551255
node.Source.Value.LineCount,
12561256
node.Source.Value.EndCharacterIndex);
12571257
}
@@ -2223,7 +2223,7 @@ private void Combine(HtmlContentIntermediateNode node, SyntaxNode item)
22232223
node.Source.Value.AbsoluteIndex,
22242224
node.Source.Value.LineIndex,
22252225
node.Source.Value.CharacterIndex,
2226-
node.Source.Value.Length + item.FullWidth,
2226+
node.Source.Value.Length + item.Width,
22272227
node.Source.Value.LineCount,
22282228
node.Source.Value.EndCharacterIndex);
22292229
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ private RazorDirectiveBodySyntax ParseTagHelperDirective(
12441244

12451245
// Accept the directive name
12461246
var keywordToken = EatCurrentToken();
1247-
var keywordLength = keywordToken.FullWidth + 1 /* @ */;
1247+
var keywordLength = keywordToken.Width + 1 /* @ */;
12481248

12491249
var foundWhitespace = At(SyntaxKind.Whitespace);
12501250

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/HtmlMarkupParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ private static bool IsTypeAttribute(MarkupAttributeBlockSyntax attributeBlock)
17831783
return false;
17841784
}
17851785

1786-
var trimmedStartContent = attributeBlock.Name.ToFullString().TrimStart();
1786+
var trimmedStartContent = attributeBlock.Name.ToString().TrimStart();
17871787
if (trimmedStartContent.StartsWith("type", StringComparison.OrdinalIgnoreCase) &&
17881788
(trimmedStartContent.Length == 4 ||
17891789
ValidAfterTypeAttributeNameCharacters.Contains(trimmedStartContent[4])))

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/ImplicitExpressionEditHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected override PartialParseResultInternal CanAcceptChange(SyntaxNode target,
8585

8686
// Get the edit context
8787
char? lastChar = null;
88-
if (changeRelativePosition > 0 && target.FullWidth > 0)
88+
if (changeRelativePosition > 0 && target.Width > 0)
8989
{
9090
lastChar = target.GetContent()[changeRelativePosition - 1];
9191
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/TagHelperBlockRewriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static MarkupTagHelperStartTagSyntax Rewrite(
108108
// TODO: Accept more than just Markup attributes: https://github.com/aspnet/Razor/issues/96.
109109
// Something like:
110110
// <input @checked />
111-
var location = new SourceSpan(codeBlock.GetSourceLocation(source), codeBlock.FullWidth);
111+
var location = new SourceSpan(codeBlock.GetSourceLocation(source), codeBlock.Width);
112112
var diagnostic = RazorDiagnosticFactory.CreateParsing_TagHelpersCannotHaveCSharpInTagDeclaration(location, tagName);
113113
errorSink.OnError(diagnostic);
114114
break;

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/TagHelperParseTreeRewriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ private void ValidateBinding(
474474
{
475475
_errorSink.OnError(
476476
RazorDiagnosticFactory.CreateTagHelper_InconsistentTagStructure(
477-
new SourceSpan(tagBlock.GetSourceLocation(_source), tagBlock.FullWidth),
477+
new SourceSpan(tagBlock.GetSourceLocation(_source), tagBlock.Width),
478478
baseDescriptor!.DisplayName,
479479
descriptor.DisplayName,
480480
tagName));

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/SourceChange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ internal string GetOriginalText(SyntaxNode node)
102102
throw new ArgumentNullException(nameof(node));
103103
}
104104

105-
if (node.FullWidth == 0)
105+
if (node.Width == 0)
106106
{
107107
return string.Empty;
108108
}

0 commit comments

Comments
 (0)