-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XSG] Refactor ProjectItem #31427
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
[XSG] Refactor ProjectItem #31427
Conversation
There was a problem hiding this 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 |
| 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; | ||
| } |
Copilot
AI
Sep 1, 2025
There was a problem hiding this comment.
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).
avoid passing flags over and over
353db45 to
12a0769
Compare
Description of Change
avoid passing flags over and over