feat(generators): Add Singleton pattern generator#107
Conversation
- Add [Singleton] attribute with Mode (Eager/Lazy) and Threading options - Add [SingletonFactory] for custom factory methods - Implement PKSNG001-006 diagnostics per spec - Support partial class and partial record class
- Eager singleton returns same instance - Lazy thread-safe singleton using Lazy<T> - Custom factory method support - All diagnostic scenarios (PKSNG001-006)
- Overview, attributes, diagnostics - Examples for eager/lazy/factory patterns - Best practices and troubleshooting
- AppClock: eager singleton for consistent time source - ConfigManager: lazy singleton with custom factory - ServiceRegistry: thread-safe service locator pattern - Comprehensive tests for all examples
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
Test Results337 tests 337 ✅ 2m 9s ⏱️ Results for commit ccb7756. ♻️ This comment has been updated with latest results. |
🔍 PR Validation ResultsVersion: `` ✅ Validation Steps
📊 ArtifactsDry-run artifacts have been uploaded and will be available for 7 days. This comment was automatically generated by the PR validation workflow. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #107 +/- ##
==========================================
+ Coverage 82.52% 87.55% +5.03%
==========================================
Files 175 180 +5
Lines 16706 17018 +312
Branches 2366 2407 +41
==========================================
+ Hits 13786 14900 +1114
+ Misses 2291 2118 -173
+ Partials 629 0 -629
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds a new Singleton pattern source generator to PatternKit, including the public attribute surface area, diagnostics registration, documentation, and example/demo coverage as described in issue #32.
Changes:
- Implemented
SingletonGeneratorwith eager/lazy modes, threading options, optional factory method support, and the six specified diagnostics. - Added generator tests plus a demo project (and demo tests) showcasing typical singleton use cases.
- Added end-user documentation for the singleton generator and updated analyzer release notes.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| test/PatternKit.Generators.Tests/SingletonGeneratorTests.cs | Adds generator-focused tests for singleton codegen and diagnostics scenarios. |
| test/PatternKit.Examples.Tests/SingletonGeneratorDemo/SingletonGeneratorDemoTests.cs | Adds runtime tests covering the demo singletons’ behavior. |
| src/PatternKit.Generators/Singleton/SingletonGenerator.cs | Implements the incremental source generator and diagnostics for Singleton. |
| src/PatternKit.Generators/AnalyzerReleases.Unshipped.md | Registers the new PKSNG00x diagnostics in analyzer release notes. |
| src/PatternKit.Generators.Abstractions/Singleton/SingletonAttributes.cs | Adds [Singleton], [SingletonFactory], and associated enums in the public abstractions assembly. |
| src/PatternKit.Examples/SingletonGeneratorDemo/ServiceRegistry.cs | Adds a thread-safe lazy singleton demo using a small service registry. |
| src/PatternKit.Examples/SingletonGeneratorDemo/ConfigManager.cs | Adds a lazy singleton demo using a [SingletonFactory] for configuration loading. |
| src/PatternKit.Examples/SingletonGeneratorDemo/AppClock.cs | Adds an eager singleton demo for a clock/time source. |
| docs/generators/singleton.md | Adds documentation describing usage, options, and diagnostics for the singleton generator. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
test/PatternKit.Examples.Tests/SingletonGeneratorDemo/SingletonGeneratorDemoTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Add PKSNG007-009 diagnostics to AnalyzerReleases.Unshipped.md - Add PKSNG007-009 to singleton.md documentation diagnostics table - IsValidCSharpIdentifier: Reject reserved keywords (unless @-prefixed) - HasNameConflict: Check inherited members by walking base types - FindFactoryMethods: Exclude generic methods (TypeParameters.Length == 0) - HasPublicConstructor: Detect implicit public constructors - Add tests for all new validation logic - Add XML doc to thread-safe test explaining runtime tests are in demo
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
test/PatternKit.Examples.Tests/SingletonGeneratorDemo/SingletonGeneratorDemoTests.cs
Outdated
Show resolved
Hide resolved
Code Coverage |
Closes #32
Summary
Implements the Singleton pattern source generator as specified in issue #32.
Changes
Generator Implementation (src/PatternKit.Generators/Singleton/)
Tests ( est/PatternKit.Generators.Tests/)
Documentation (docs/generators/singleton.md)
Demo (src/PatternKit.Examples/SingletonGeneratorDemo/)
Validation