Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sg cleanup post cohost #11534

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

chsienki
Copy link
Member

Some minor cleanup refactoring to the source generator that I noticed while doing the cohosting work.

@chsienki chsienki added the area-compiler Umbrella for all compiler issues label Feb 21, 2025
@chsienki chsienki requested a review from a team as a code owner February 21, 2025 18:15
@chsienki
Copy link
Member Author

@dotnet/razor-compiler for review please

Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this change! As someone who spends far less time reading SG code than you have, I've really struggled to understand the data flow in this SG. Sp. I've left some feedback to call out spots that I feel would help readability more, a least for me. Feel free to take it or leave it.

Note that some of the feedback is duplicated in multiple places. That's mostly from just me doing a careful read through and trying not to miss something. 😉

@@ -318,7 +309,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

context.RegisterImplementationSourceOutput(csharpDocumentsWithSuppressionFlag, static (context, pair) =>
{
var ((hintName, _, csharpDocument), isGeneratorSuppressed) = pair;
var (hintName, _, csharpDocument, isGeneratorSuppressed) = pair;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a name other than pair, since it now contains more than two pieces of data.

@@ -344,7 +335,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterHostOutput(hostOutputs, (context, pair) =>
#pragma warning restore RSEXPERIMENTAL004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
{
var ((documents, tagHelpers), isGeneratorSuppressed) = pair;
var (documents, tagHelpers, isGeneratorSuppressed) = pair;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a name other than pair, since it now contains more than two pieces of data.

@@ -48,7 +46,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Combine(parseOptions)
.Combine(metadataRefs.Collect())
.SuppressIfNeeded(isGeneratorSuppressed)
.Select(ComputeRazorSourceGeneratorOptions)
.Select((p, _) => ComputeRazorSourceGeneratorOptions(p.Item1.Item1, p.Item1.Item2, p.Item1.Item3, p.Item2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, consider adding named arguments for the values passed to ComputeRazorSourceGeneratorOptions. Or, add a body and deconstruct p into variables that are passed separately. Or, add a ComputeRazorSourceGeneratorOptions overload that take the expected ValueTuple with named members.

{
var ((sourceItem, importFiles), razorSourceGeneratorOptions) = pair;
var (sourceItem, importFiles, razorSourceGeneratorOptions) = pair;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a name other than pair, since it now contains more than two pieces of data.

internal static IncrementalValuesProvider<ValueTuple<T1, T2, T3>> Combine<T1, T2, T3>(this IncrementalValuesProvider<ValueTuple<T1, T2>> provider, IncrementalValueProvider<T3> target)
{
return IncrementalValueProviderExtensions.Combine(provider, target)
.Select((p, _) => (p.Left.Item1, p.Left.Item2, p.Right));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to have a Select extension that takes a lambda that is only passed one argument?

.Combine(allTagHelpers)
.Select(static (pair, _) =>
{
var (projectEngine, filePath, document, allTagHelpers) = pair;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming document to codeDocument for consistency and to connect it to the data that has flowed from earlier.


var csharpDocuments = processed(designTime: false)
var csharpDocuments = processed
.Select(static (pair, _) =>
{
var (filePath, document) = pair;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming document to codeDocument for consistency and to connect it to the data that has flowed from earlier.

})
.Select((pair, _) =>
{
var (projectEngine, filePath, document) = pair;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming document to codeDocument for consistency and to connect it to the data that has flowed from earlier.


var document = projectEngine.ProcessInitialParse(sourceItem, designTime);
var document = projectEngine.ProcessInitialParse(sourceItem, designTime: false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming document to codeDocument since it will be deconstructed into codeDocument later.

RazorSourceGeneratorEventSource.Log.GenerateDeclarationCodeStart(sourceItem.FilePath);

var projectEngine = GetDeclarationProjectEngine(sourceItem, importFiles, razorSourceGeneratorOptions);

var codeGen = projectEngine.Process(sourceItem);
var codeGen = projectEngine.Process(sourceItem, cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the cancellation! Should more of the RazorProjectEngine "Process" methods (like ProcessRemaining) take a CancellationToken as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants