Skip to content

Added Add AutoCtor Code Fix#477

Open
distantcam wants to merge 4 commits into
mainfrom
feature/add-autoctor-fix
Open

Added Add AutoCtor Code Fix#477
distantcam wants to merge 4 commits into
mainfrom
feature/add-autoctor-fix

Conversation

@distantcam
Copy link
Copy Markdown
Owner

No description provided.

Copy link
Copy Markdown

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 adds a new analyzer + code fix rule (ACTR008) to suggest and apply [AutoConstruct] on eligible types that have no explicit constructors, and refactors shared helper logic to support the new rule and existing code fixes.

Changes:

  • Added ACTR008 analyzer (AddAutoConstructAnalyzer) and code fix provider (AddAutoConstructCodeFixer) plus associated tests/examples.
  • Refactored shared helper utilities (renamed ModelUtilitiesUtilities) and extracted a reusable AutoCtorRewriter.
  • Updated/expanded ACTR007 test/example layout and renamed ACTR007 test classes for clarity.

Reviewed changes

Copilot reviewed 23 out of 90 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Tests/Tests.csproj Updates test project includes/excludes for new ACTR007/ACTR008 example folders.
src/Tests/ACTR008_CodeFixTests.cs Adds code-fix verification test harness for ACTR008.
src/Tests/ACTR008_CodeFixExamples/Standard.cs New ACTR008 code-fix input example.
src/Tests/ACTR008_CodeFixExamples/Standard.cs.verified.txt Expected ACTR008 code-fix output snapshot.
src/Tests/ACTR008_CodeAnalyzerTests.cs Adds diagnostic verification test harness for ACTR008.
src/Tests/ACTR008_CodeAnalyzerExamples/Standard.cs New ACTR008 analyzer input example.
src/Tests/ACTR008_CodeAnalyzerExamples/Standard.cs.verified.txt Expected ACTR008 analyzer diagnostics snapshot.
src/Tests/ACTR008_CodeAnalyzerExamples/AlreadyAutoCtor.cs New ACTR008 “no diagnostics” example.
src/Tests/ACTR008_CodeAnalyzerExamples/AlreadyAutoCtor.cs.verified.txt Expected empty diagnostics snapshot for AlreadyAutoCtor.
src/Tests/ACTR007_CodeFixTests.cs Renames test class + updates example directory name.
src/Tests/ACTR007_CodeFixExamples/StructExample.cs New ACTR007 code-fix input example.
src/Tests/ACTR007_CodeFixExamples/StructExample.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/StandardExample.cs New ACTR007 code-fix input example.
src/Tests/ACTR007_CodeFixExamples/StandardExample.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/Region.cs New ACTR007 code-fix input example.
src/Tests/ACTR007_CodeFixExamples/Region.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/RecordExample.cs New ACTR007 code-fix input example.
src/Tests/ACTR007_CodeFixExamples/RecordExample.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/NestedNoDeclaredVisibility.cs New ACTR007 code-fix input example.
src/Tests/ACTR007_CodeFixExamples/NestedNoDeclaredVisibility.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/Nested.cs New ACTR007 code-fix input example.
src/Tests/ACTR007_CodeFixExamples/Nested.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/MultipleTypes.cs New ACTR007 code-fix input example with multiple top-level types.
src/Tests/ACTR007_CodeFixExamples/MultipleTypes.cs.verified.txt Expected ACTR007 code-fix output snapshot for MultipleTypes.
src/Tests/ACTR007_CodeFixExamples/MultipleNested.cs New ACTR007 code-fix input example (nested types).
src/Tests/ACTR007_CodeFixExamples/MultipleNested.cs.verified.txt Expected ACTR007 code-fix output snapshot for MultipleNested.
src/Tests/ACTR007_CodeFixExamples/MultipleMatchingCtors.cs New ACTR007 code-fix input example (multiple matching ctors).
src/Tests/ACTR007_CodeFixExamples/MultipleMatchingCtors.cs.verified.txt Expected ACTR007 code-fix output snapshot for MultipleMatchingCtors.
src/Tests/ACTR007_CodeFixExamples/MultipleConstructors.cs New ACTR007 code-fix input example (multiple ctors).
src/Tests/ACTR007_CodeFixExamples/MultipleConstructors.cs.verified.txt Expected ACTR007 code-fix output snapshot for MultipleConstructors.
src/Tests/ACTR007_CodeFixExamples/MixedFieldsAndProperties.cs New ACTR007 code-fix input example (fields + properties).
src/Tests/ACTR007_CodeFixExamples/MixedFieldsAndProperties.cs.verified.txt Expected ACTR007 code-fix output snapshot for MixedFieldsAndProperties.
src/Tests/ACTR007_CodeFixExamples/ExistingUsingGlobalAutoCtor.cs New ACTR007 code-fix input example (global using).
src/Tests/ACTR007_CodeFixExamples/ExistingUsingGlobalAutoCtor.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/ExistingUsingAutoCtor.cs New ACTR007 code-fix input example (normal using).
src/Tests/ACTR007_CodeFixExamples/ExistingUsingAutoCtor.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/ExistingAutoConstructAttribute.cs New ACTR007 code-fix input example (existing AutoConstruct attribute forms).
src/Tests/ACTR007_CodeFixExamples/ExistingAutoConstructAttribute.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/ExistingAttribute.cs New ACTR007 code-fix input example (other attributes present).
src/Tests/ACTR007_CodeFixExamples/ExistingAttribute.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeFixExamples/AlreadyPartial.cs New ACTR007 code-fix input example (already partial).
src/Tests/ACTR007_CodeFixExamples/AlreadyPartial.cs.verified.txt Expected ACTR007 code-fix output snapshot.
src/Tests/ACTR007_CodeAnalyzerTests.cs Renames ACTR007 analyzer test class + updates example directory name.
src/Tests/ACTR007_CodeAnalyzerExamples/OnlyPropertyAssignments.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/OnlyPropertyAssignments.cs.verified.txt Expected ACTR007 analyzer diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/OnlyFieldAssignments.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/OnlyFieldAssignments.cs.verified.txt Expected ACTR007 analyzer diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/OnlyFieldAssignments.CheckDiagnostics.verified.txt Adds/updates verify snapshot artifact for ACTR007 tests.
src/Tests/ACTR007_CodeAnalyzerExamples/NotAllFieldsAssigned.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/NotAllFieldsAssigned.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/NoDeclaredVisibility.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/NoDeclaredVisibility.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/MultipleConstructors.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/MultipleConstructors.cs.verified.txt Expected ACTR007 analyzer diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/InternalConstructor.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/InternalConstructor.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/IgnoredFieldIsNotAssigned.cs New ACTR007 analyzer input example (ignored member).
src/Tests/ACTR007_CodeAnalyzerExamples/IgnoredFieldIsNotAssigned.cs.verified.txt Expected ACTR007 analyzer diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/HasBaseInitializer.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/HasBaseInitializer.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/FieldHasInitializer.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/FieldHasInitializer.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithNonAssignment.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithNonAssignment.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExtraParameter.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExtraParameter.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExplicitDefault.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExplicitDefault.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithAttribute.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithAttribute.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorAssignsIgnoredField.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorAssignsIgnoredField.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/BlankConstructor.cs New ACTR007 analyzer input example.
src/Tests/ACTR007_CodeAnalyzerExamples/BlankConstructor.cs.verified.txt Expected empty diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyPartial.cs New ACTR007 analyzer input example (partial).
src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyPartial.cs.verified.txt Expected ACTR007 analyzer diagnostics snapshot.
src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyHasAutoConstruct.cs New ACTR007 analyzer input example (already attribute).
src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyHasAutoConstruct.cs.verified.txt Expected empty diagnostics snapshot.
src/Shared/Models/TypeModel.cs Switches from ModelUtilities to Utilities for member filtering.
src/Shared/Models/ParameterModel.cs Switches service-key lookup to Utilities.
src/Shared/Models/MemberModel.cs Updates static import to new Utilities name.
src/Shared/Helpers/Utilities.cs Renames helper class and adds helper APIs for eligibility/attribute detection.
src/Shared/Constants/Diagnostics.cs Adds ACTR008 diagnostic descriptor.
src/Shared/AutoConstructSourceGenerator/UseAutoConstructAnalyzer.cs Refactors eligible-member lookup to use shared utilities.
src/Shared/AutoConstructSourceGenerator/AddAutoConstructAnalyzer.cs Introduces ACTR008 analyzer implementation.
src/Shared/AnalyzerReleases.Unshipped.md Registers ACTR008 as a new analyzer rule.
src/AutoCtor.CodeFixes/UseAutoConstructCodeFixer.cs Refactors to use extracted AutoCtorRewriter.
src/AutoCtor.CodeFixes/AutoCtorRewriter.cs Adds reusable syntax rewriter used by multiple code fixes.
src/AutoCtor.CodeFixes/AutoCtor.CodeFixes.csproj Broadens shared constant includes and adds shared Utilities to code-fix assembly.
src/AutoCtor.CodeFixes/AddAutoConstructCodeFixer.cs Introduces ACTR008 code fix provider implementation.

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

Comment on lines +34 to +54
var typeSyntax = (TypeDeclarationSyntax)context.Node;

if (Utilities.HasAutoConstructAttribute(typeSyntax))
return;

if (context.SemanticModel.GetDeclaredSymbol(typeSyntax, context.CancellationToken) is not INamedTypeSymbol type)
return;

// no constructors
if (type.InstanceConstructors.Any(c => !c.IsImplicitlyDeclared))
return;

if (!Utilities.HasEligibleMember(type))
return;

foreach (var location in type.Locations)
{
context.ReportDiagnostic(Diagnostic.Create(
Diagnostics.ACTR008_AddAutoConstruct,
location,
type.Name));
Comment on lines 23 to 28
context.RegisterCompilationStartAction(static compilationContext =>
{
var autoConstructType = compilationContext.Compilation
.GetTypeByMetadataName(AttributeNames.AutoConstruct);
if (autoConstructType is null)
return;

compilationContext.RegisterSyntaxNodeAction(
ctx => AnalyzeConstructor(ctx, autoConstructType),
AnalyzeConstructor,
SyntaxKind.ConstructorDeclaration);
});
Comment on lines +1 to +6
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
using System.Composition;
Comment on lines +1 to +4
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
Copy link
Copy Markdown

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

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

Comment on lines +78 to +82
var newText = await newDocument.GetTextAsync(TestHelper.CancellationToken)
.ConfigureAwait(false);

var verifyText = string.Join(Environment.NewLine, newText);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants