Skip to content

feat: Add AutoInjectExtensionsAttribute support#11

Merged
ling921 merged 14 commits intomasterfrom
develop
Dec 1, 2025
Merged

feat: Add AutoInjectExtensionsAttribute support#11
ling921 merged 14 commits intomasterfrom
develop

Conversation

@ling921
Copy link
Copy Markdown
Owner

@ling921 ling921 commented Dec 1, 2025

This pull request introduces new analyzers and diagnostics to improve validation and customization of dependency injection (DI) code generation. It adds support for class-level customization via AutoInjectExtensionsAttribute, enhances error reporting for misconfiguration, and refines the documentation to point to more detailed usage instructions. The changes are grouped below by theme.

Analyzer and Diagnostic Improvements

  • Added AutoInjectConfigAttributeAnalyzer to validate assembly-level AutoInjectConfig values, reporting errors for invalid naming and unnecessary configuration when class-level customization is present.
  • Added AutoInjectExtensionsAttributeAnalyzer to enforce correct usage of AutoInjectExtensionsAttribute, including checks for valid method names, single usage per assembly, and requiring the target class to be static partial.
  • Updated diagnostics in AnalyzerReleases.Shipped.md to reflect new rules for replace registration, multiple extension declarations, static partial class requirements, and redundant configuration. [1] [2]

Feature and API Enhancements

  • Added support for service replacement (Replace = true) and class-level customization via AutoInjectExtensionsAttribute, including optional IConfiguration parameter support, as documented in README.md.
  • Updated code generation to include the new AutoInjectExtensionsAttribute source.

Refactoring and Documentation

  • Removed the obsolete InvalidAutoInjectConfigAnalyzer and replaced its functionality with more targeted analyzers.
  • Updated the main README.md to reference the package-level documentation for detailed usage, streamlining the top-level instructions.

Other Minor Changes

  • Renamed ConflictingExtensionAnalyzer to MethodNameConflictAnalyzer for clarity.
  • Updated version suffix in Directory.Build.props for new release.
  • Minor code improvements and doc comments for clarity in analyzers. [1] [2] [3]

These changes collectively improve the flexibility and correctness of DI code generation, making it easier to customize registration methods and catch configuration errors early.

- Introduced `AutoInjectExtensionsAttribute` to generate extension methods for service registration.
- Added diagnostics: `LAI006` (multiple extensions), `LAI007` (static partial class), and `LAI104` (unnecessary config).
- Enhanced generator to prioritize `AutoInjectExtensionsAttribute` over `AutoInjectConfigAttribute`.
- Improved analyzer to validate method names and detect multiple or invalid extensions.
- Refactored helper logic into `CompilationExtensions` and added `CSharpIdentifierHelper`.
- Updated tests to cover new diagnostics and generator behavior.
- Updated resources and documentation for new diagnostics.
- Adjusted project configuration to include generator output.
- Removed deprecated `AttributeConflictAnalyzer` and unused code.
@ling921 ling921 self-assigned this Dec 1, 2025
Copilot AI review requested due to automatic review settings December 1, 2025 04:52
@ling921 ling921 added the enhancement New feature or request label Dec 1, 2025
@ling921
Copy link
Copy Markdown
Owner Author

ling921 commented Dec 1, 2025

@copilot There are some naming issues that I need your help with, including the following:

  1. All DiagnosticAnalyzer type names

  2. All test type names and method names

If there are better or more suitable names to choose from, please modify them.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #12, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
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 work-in-progress PR adds support for the AutoInjectExtensionsAttribute, which allows users to customize the generated service registration extension methods by decorating a static partial class. This provides more flexibility than the assembly-level AutoInjectConfigAttribute approach.

Key Changes:

  • Introduces AutoInjectExtensionsAttribute for class-level customization of generated extension methods
  • Adds partial method support (AddAdditionalServices) to allow custom service registrations
  • Refactors compilation helper methods from static class to extension methods
  • Updates analyzers to validate the new attribute usage patterns

Reviewed changes

Copilot reviewed 19 out of 22 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
src/Ling.AutoInject.SourceGenerators/Extensions/CompilationExtensions.cs New file with compilation extension methods (contains critical syntax error)
src/Ling.AutoInject.SourceGenerators/Helpers/CSharpIdentifierHelper.cs New validation helper for C# identifiers
src/Ling.AutoInject.SourceGenerators/Helpers/CompilationHelpers.cs Deleted - replaced by CompilationExtensions
src/Ling.AutoInject.SourceGenerators/SourceCodes.cs Adds AutoInjectExtensionsAttribute definition and removes redundant constructor from AutoInjectConfigAttribute
src/Ling.AutoInject.SourceGenerators/AutoInjectGenerator.cs Updated to support AutoInjectExtensionsAttribute, generate partial methods, and include optional IConfiguration parameter
src/Ling.AutoInject.SourceGenerators/Analyzers/ExtensionMethodAnalyzer.cs New analyzer for validating AutoInjectExtensionsAttribute usage
src/Ling.AutoInject.SourceGenerators/Analyzers/InvalidAutoInjectConfigAnalyzer.cs Updated to use new helper methods and detect unnecessary AutoInjectConfig usage
src/Ling.AutoInject.SourceGenerators/Analyzers/AutoInjectAttributeAnalyzer.cs New file consolidating attribute validation logic
src/Ling.AutoInject.SourceGenerators/Analyzers/NotSupportedFeatureAnalyzer.cs Updated import from CompilationHelpers to CompilationExtensions
src/Ling.AutoInject.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs Adds new diagnostic rules for extensions validation and unnecessary config usage
src/Ling.AutoInject.SourceGenerators/Resources/SR.resx Adds localized strings for new diagnostics
src/Ling.AutoInject.SourceGenerators/Resources/SR.zh-CN.resx Adds Chinese localized strings for new diagnostics
src/Ling.AutoInject.SourceGenerators/Resources/SR.Designer.cs Auto-generated designer file for resources
src/Ling.AutoInject.SourceGenerators/Constants.cs Adds AutoInjectExtensionsAttributeFullName constant
src/Ling.AutoInject.SourceGenerators/AutoInjectSymbols.cs Adds AutoInjectExtensionsAttributeSymbol property
src/Ling.AutoInject.SourceGenerators/AnalyzerReleases.Shipped.md Documents new diagnostic IDs for the release
src/Ling.AutoInject/Ling.AutoInject.csproj Changes IncludeBuildOutput from false to true
src/Directory.Build.props Removes trailing whitespace
tests/Ling.AutoInject.SourceGenerators.Tests/Verifiers/CSharpAnalyzerVerifier.cs Adds AutoInjectExtensionsAttribute to test sources
tests/Ling.AutoInject.SourceGenerators.Tests/AutoInjectGeneratorTests.cs Adds tests for AutoInjectExtensionsAttribute and partial class scenarios
tests/Ling.AutoInject.SourceGenerators.Tests/Analyzers/InvalidAutoInjectConfigAnalyzerTests.cs Updates tests to use new diagnostic descriptors
tests/Ling.AutoInject.SourceGenerators.Tests/Analyzers/ExtensionMethodAnalyzerTests.cs New test file for ExtensionMethodAnalyzer
Files not reviewed (1)
  • src/Ling.AutoInject.SourceGenerators/Resources/SR.Designer.cs: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Comment thread src/Ling.AutoInject.SourceGenerators/AutoInjectGenerator.cs Outdated
Comment thread src/Ling.AutoInject.SourceGenerators/AutoInjectGenerator.cs Outdated
Comment thread tests/Ling.AutoInject.SourceGenerators.Tests/AutoInjectGeneratorTests.cs Outdated
Comment thread src/Ling.AutoInject.SourceGenerators/Helpers/CSharpIdentifierHelper.cs Outdated
Comment thread src/Ling.AutoInject.SourceGenerators/AutoInjectGenerator.cs Outdated
Comment thread src/Ling.AutoInject.SourceGenerators/AutoInjectGenerator.cs Outdated
Copilot AI and others added 4 commits December 1, 2025 13:20
* Initial plan

* Rename DiagnosticAnalyzer types and tests for better naming consistency

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

* Unify test method naming styles across all test files

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
…sionsAttributeAnalyzer.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #13, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #14, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #15, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits December 1, 2025 13:39
)

* Initial plan

* Refactor IsValidNamespace to use LINQ All() instead of foreach loop

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
* Initial plan

* Add length check in IsValidIdentifierAllowAt for "@" edge case

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
…ach loop (#14)

* Initial plan

* Use explicit .Where() filter instead of implicit if statement in foreach loop

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

* Use FirstOrDefault instead of Where with foreach loop

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #16, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #17, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits December 1, 2025 13:56
…16)

* Initial plan

* Refactor foreach loop to use explicit .Where() filter for AutoInjectConfigAttribute

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
…Symbol (#17)

* Initial plan

* Refactor foreach loop to use .Select() for mapping classDecl to classSymbol

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
Simplified the comment in `AutoInjectConfigAttributeAnalyzer.cs`
to make it more concise. Removed the phrase "or contain invalid
characters" from the description of invalid C# identifiers.
@ling921
Copy link
Copy Markdown
Owner Author

ling921 commented Dec 1, 2025

@copilot README.md and src/Ling.AutoInject/README.md seems to missing content of service replace and AutoInjectExtensionsAttribute

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #18, to work on those changes. Once the pull request is ready, I'll request review from you.

… features (#18)

* Initial plan

* docs: Add documentation for Replace and AutoInjectExtensionsAttribute features

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

* docs: Refactor README.md to reference package README for usage documentation

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

* Clarify usage instructions in README

Updated usage instructions for clarity.

* Clarify usage instructions in README

Updated usage instructions for clarity.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
Co-authored-by: Jing Ling <lingjing0921@live.com>
@ling921
Copy link
Copy Markdown
Owner Author

ling921 commented Dec 1, 2025

@copilot I found that Features part of README.md has not describe replace service and AutoInjectExtensionsAttribute

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #19, to work on those changes. Once the pull request is ready, I'll request review from you.

ling921 and others added 2 commits December 1, 2025 14:46
Updated the <VersionSuffix> property in Directory.Build.props
from `beta-20251119095118` to `beta-20251201143733`.
…tions (#19)

* Initial plan

* Add replace service and AutoInjectExtensionsAttribute to Features sections in README files

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

* Improve wording for service replacement and AutoInjectExtensionsAttribute descriptions

Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ling921 <88094578+ling921@users.noreply.github.com>
@ling921
Copy link
Copy Markdown
Owner Author

ling921 commented Dec 1, 2025

@copilot this PR is ready to merge, please remove [WIP], change the title and description of PR.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@ling921 I've opened a new pull request, #20, to work on those changes. Once the pull request is ready, I'll request review from you.

@ling921 ling921 changed the title [WIP] Add AutoInjectExtensionsAttribute support feat: Add AutoInjectExtensionsAttribute support Dec 1, 2025
@ling921 ling921 merged commit 1e021eb into master Dec 1, 2025
1 check passed
@ling921 ling921 deleted the develop branch December 1, 2025 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants