Skip to content

Conversation

@StephaneDelcroix
Copy link
Contributor

Description of Change

avoid passing flags over and over

Copilot AI review requested due to automatic review settings September 1, 2025 07:52
@StephaneDelcroix StephaneDelcroix requested a review from a team as a code owner September 1, 2025 07:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the ProjectItem class to eliminate the need for passing boolean flags (EnableLineInfo, EnableDiagnostics) and file path information repeatedly throughout the source generation pipeline. The main change converts ProjectItem from a record with mutable properties to a record with computed properties that derive their values from AnalyzerConfigOptions.

Key Changes

  • Refactored ProjectItem constructor to take AnalyzerConfigOptions and compute properties dynamically
  • Removed redundant property assignments in SourceGenContext creation
  • Updated all file path references to use ProjectItem.RelativePath instead of context.FilePath
  • Simplified the ProjectItem creation logic in GeneratorHelpers

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
ProjectItem.cs Complete refactor from mutable record to computed properties using AnalyzerConfigOptions
SourceGenContext.cs Simplified constructor to include ProjectItem parameter and removed redundant properties
SetPropertiesVisitor.cs Updated all file path and flag references to use ProjectItem properties
ExpandMarkupsVisitor.cs Updated file path references to use ProjectItem.RelativePath
CreateValuesVisitor.cs Updated file path references to use ProjectItem.RelativePath
NodeSGExtensions.cs Updated file path and diagnostic flag references to use ProjectItem
KnownTypeConverters.cs Updated file path references to use ProjectItem.RelativePath
KnownMarkups.cs Updated file path references to use ProjectItem.RelativePath
InitializeComponentCodeWriter.cs Simplified SourceGenContext creation by removing redundant property assignments
GeneratorHelpers.cs Simplified ProjectItem creation logic using new constructor
CompiledBindingMarkup.cs Updated file path reference to use ProjectItem.RelativePath

Comment on lines +39 to 46
public static ProjectItem? ComputeProjectItem((AdditionalText additionalText, AnalyzerConfigOptionsProvider optionsProvider) tuple, CancellationToken cancellationToken)
{
var (additionalText, optionsProvider) = tuple;
var fileOptions = optionsProvider.GetOptions(additionalText);
if (!fileOptions.TryGetValue("build_metadata.additionalfiles.GenKind", out string? kind) || kind is null)
if (cancellationToken.IsCancellationRequested)
return null;

fileOptions.TryGetValue("build_metadata.additionalfiles.TargetPath", out var targetPath);
fileOptions.TryGetValue("build_metadata.additionalfiles.ManifestResourceName", out var manifestResourceName);
fileOptions.TryGetValue("build_metadata.additionalfiles.RelativePath", out var relativePath);
fileOptions.TryGetValue("build_property.targetframework", out var targetFramework);
fileOptions.TryGetValue("build_property.Configuration", out var configuration);

bool enableDiagnostics = false;
if (fileOptions.TryGetValue("build_property.EnableMauiXamlDiagnostics", out var enDiag) && string.Compare(enDiag, "true", StringComparison.OrdinalIgnoreCase) == 0)
enableDiagnostics = true;
if (fileOptions.TryGetValue("build_property.additionalfiles.EnableDiagnostics", out enDiag) && string.Compare(enDiag, "true", StringComparison.OrdinalIgnoreCase) == 0)
enableDiagnostics = true;
if (fileOptions.TryGetValue("build_property.additionalfiles.EnableDiagnostics", out enDiag) && string.Compare(enDiag, "false", StringComparison.OrdinalIgnoreCase) == 0)
enableDiagnostics = false;

var xamlinflator = 0;
if (fileOptions.TryGetValue("build_metadata.additionalfiles.Inflator", out var inflator) && !string.IsNullOrEmpty(inflator))
{
var parts = inflator!.Split(',');
for (int i = 0; i < parts.Length; i++)
{
var trimmed = parts[i].Trim();
if (!Enum.TryParse<XamlInflator>(trimmed, true, out var xinfl))
throw new InvalidOperationException($"Invalid inflator '{trimmed}' for {additionalText.Path}.");
xamlinflator |= (int)xinfl;
}
}

var enableLineInfo = true;
if (fileOptions.TryGetValue("build_property.MauiXamlLineInfo", out var lineInfo) && string.Compare(lineInfo, "disable", StringComparison.OrdinalIgnoreCase) == 0)
enableLineInfo = false;
if (fileOptions.TryGetValue("build_metadata.additionalfiles.LineInfo", out lineInfo) && string.Compare(lineInfo, "enable", StringComparison.OrdinalIgnoreCase) == 0)
enableLineInfo = true;
if (fileOptions.TryGetValue("build_metadata.additionalfiles.LineInfo", out lineInfo) && string.Compare(lineInfo, "disable", StringComparison.OrdinalIgnoreCase) == 0)
enableLineInfo = false;

string noWarn = "";
if (fileOptions.TryGetValue("build_property.MauiXamlNoWarn", out var noWarnValue))
noWarn = noWarnValue;
if (fileOptions.TryGetValue("build_metadata.additionalfiles.NoWarn", out noWarnValue))
noWarn = noWarnValue;

return new ProjectItem
{
AdditionalText = additionalText,
TargetPath = targetPath,
RelativePath = relativePath,
ManifestResourceName = manifestResourceName,
Kind = kind,
Inflator = (XamlInflator)xamlinflator,
EnableLineInfo = enableLineInfo,
EnableDiagnostics = enableDiagnostics,
NoWarn = noWarn,
TargetFramework = targetFramework,
Configuration = configuration!,
};
var projectItem = new ProjectItem(tuple.additionalText, tuple.optionsProvider.GetOptions(tuple.additionalText));
return projectItem.Kind == "None" ? null : projectItem;
}
Copy link

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

The method parameter name tuple doesn't match the usage inside the method. Line 44 uses tuple.optionsProvider but the parameter is destructured as (AdditionalText additionalText, AnalyzerConfigOptionsProvider optionsProvider), so it should be optionsProvider.GetOptions(additionalText).

Copilot uses AI. Check for mistakes.
simonrozsival
simonrozsival previously approved these changes Sep 2, 2025
@StephaneDelcroix StephaneDelcroix merged commit 2ec472d into net10.0 Sep 3, 2025
148 checks passed
@StephaneDelcroix StephaneDelcroix deleted the dev/stdelc/xsg/projitem branch September 3, 2025 07:56
@github-actions github-actions bot locked and limited conversation to collaborators Oct 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants