Skip to content

refactor(source-generators): modularize code generation and improve generated file organization#280

Merged
j-d-ha merged 5 commits into
mainfrom
refactor/update-generated-file-structure
Jan 9, 2026
Merged

refactor(source-generators): modularize code generation and improve generated file organization#280
j-d-ha merged 5 commits into
mainfrom
refactor/update-generated-file-structure

Conversation

@j-d-ha

@j-d-ha j-d-ha commented Jan 9, 2026

Copy link
Copy Markdown
Collaborator

🚀 Pull Request

📋 Summary

This PR refactors the source generator architecture to improve modularity, maintainability, and generated code organization. The monolithic MinimalLambdaEmitter class has been split into specialized emitters, each responsible for a specific type of code generation.

Key Changes:

  • Split MinimalLambdaEmitter into three focused emitters:
    • InvocationHandlerEmitter - handles MapHandler call generation
    • LifecycleHandlerEmitter - handles OnInit and OnShutdown call generation
    • MiddlewareClassEmitter - handles UseMiddleware<T> call generation
  • Changed from single LambdaHandler.g.cs file to multiple specialized files:
    • MinimalLambda.InvocationHandlers.g.cs
    • MinimalLambda.OnInitHandlers.g.cs
    • MinimalLambda.OnShutdownHandlers.g.cs
    • MinimalLambda.UseMiddleware.g.cs
  • Each generated file is now self-contained with its own namespace, using directives, and utility classes
  • Removed InterceptsLocationAttribute.scriban template (now embedded in each template)
  • Removed GeneratorConstants.cs (constants moved to emitters)
  • Simplified MinimalLambdaGenerator registration logic
  • Improved diagnostic reporting by reporting diagnostics directly in RegisterSourceOutput
  • Removed redundant Tap extension method from FunctionalExtensions
  • Updated all snapshot tests to reflect new file structure

Benefits:

  • Better separation of concerns - each emitter has a single responsibility
  • Improved testability - easier to test individual emitters in isolation
  • Cleaner generated code organization - related code grouped in dedicated files
  • Reduced coupling - emitters are independent of each other
  • More maintainable - easier to add new emitters or modify existing ones

✅ Checklist

  • My changes build cleanly
  • I've added/updated relevant tests (snapshot tests updated)
  • I've added/updated documentation or README
  • I've followed the coding style for this project
  • I've tested the changes locally (if applicable)

🧪 Related Issues or PRs

Part of ongoing source generator improvements to enhance code quality and maintainability.


💬 Notes for Reviewers

  • The functional behavior remains unchanged - this is purely a structural refactoring
  • All existing snapshot tests have been updated to reflect the new generated file names
  • The generated code output is semantically equivalent, just organized differently
  • Example middleware usage added to MinimalLambda.Example.ClassMiddleware/Program.cs to demonstrate the feature

j-d-ha added 3 commits January 8, 2026 15:56
…mponents

- Removed unused constants and templates in `GeneratorConstants`.
- Split handler generation into separate emitters for better maintainability.
- Added `InvocationHandlerEmitter` and `LifecycleHandlerEmitter` for handling Map and Lifecycle events.
- Introduced `MiddlewareClassEmitter` for `UseMiddleware<T>` handling.
- Updated template files to include generated code attributes for clarity.
- Enhanced `CompilationInfo` and `LifecycleMethodInfo` to better represent handler-specific data.
- Simplified diagnostics and generation logic in `MinimalLambdaGenerator`.
- Deleted obsolete snapshot files for `LambdaHandler.g.cs`.
- Updated remaining snapshot names for clarity and consistency.
- Ensured all references align with current `InvocationHandlers`, `OnInitHandlers`, and `UseMiddleware`.
…sions`

- Eliminated the `Tap` method as it was unused and provided no additional functionality.
- Simplifies the `FunctionalExtensions` class to reduce unnecessary code.
@github-actions github-actions Bot added the type: refactor Code refactoring label Jan 9, 2026
@codecov

codecov Bot commented Jan 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.53086% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...Generators/Models/Middleware/UseMiddlewareTInfo.cs 80.00% 1 Missing ⚠️
....SourceGenerators/Models/Shared/CompilationInfo.cs 0.00% 1 Missing ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #280      +/-   ##
==========================================
- Coverage   88.47%   87.49%   -0.99%     
==========================================
  Files         129      131       +2     
  Lines        3297     3246      -51     
  Branches      319      318       -1     
==========================================
- Hits         2917     2840      -77     
- Misses        276      302      +26     
  Partials      104      104              
Files with missing lines Coverage Δ
...rceGenerators/Emitters/InvocationHandlerEmitter.cs 100.00% <100.00%> (ø)
...urceGenerators/Emitters/LifecycleHandlerEmitter.cs 100.00% <100.00%> (ø)
...ourceGenerators/Emitters/MiddlewareClassEmitter.cs 100.00% <100.00%> (ø)
...Lambda.SourceGenerators/Emitters/TemplateHelper.cs 55.31% <100.00%> (+9.16%) ⬆️
...ourceGenerators/Extensions/FunctionalExtensions.cs 100.00% <ø> (+66.66%) ⬆️
...s/Extensions/IncrementalValueProviderExtensions.cs 100.00% <100.00%> (ø)
...MinimalLambda.SourceGenerators/GeneratorContext.cs 100.00% <100.00%> (ø)
...lLambda.SourceGenerators/MinimalLambdaGenerator.cs 100.00% <100.00%> (ø)
...eGenerators/Models/Handlers/LifecycleMethodInfo.cs 92.63% <100.00%> (+0.24%) ⬆️
...enerators/Models/Middleware/MiddlewareClassInfo.cs 96.90% <100.00%> (ø)
... and 2 more

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2574fa1...526b603. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

j-d-ha added 2 commits January 8, 2026 20:01
…dleware<T>` handling

- Added `WhereNoErrors` extension to streamline error filtering logic across handlers.
- Refactored `MinimalLambdaGenerator` to use `WhereNoErrors` for cleaner error checks.
- Enhanced `UseMiddlewareTInfo` with the `MethodType.UseMiddlewareT` property for consistency.
- Updated `MethodType` with a new `UseMiddlewareT` entry.
- Improved modularization in `IncrementalValueProvider
- Bumped `<Version>` in `Directory.Build.props` from `2.1.0-beta.6` to `2.2.0-beta.1`.
- Ensures proper version alignment for upcoming release.
@sonarqubecloud

sonarqubecloud Bot commented Jan 9, 2026

Copy link
Copy Markdown

@j-d-ha
j-d-ha enabled auto-merge (squash) January 9, 2026 01:08
@j-d-ha
j-d-ha merged commit dd3ca93 into main Jan 9, 2026
6 checks passed
@j-d-ha
j-d-ha deleted the refactor/update-generated-file-structure branch January 9, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: refactor Code refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant