-
Notifications
You must be signed in to change notification settings - Fork 30
Add code fixes for EFP0001, EFP0002 and EFP0008, with tests #181
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/EntityFrameworkCore.Projectables.CodeFixes/AnalyzerReleases.Shipped.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | ||
|
|
1 change: 1 addition & 0 deletions
1
src/EntityFrameworkCore.Projectables.CodeFixes/AnalyzerReleases.Unshipped.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| |
58 changes: 58 additions & 0 deletions
58
src/EntityFrameworkCore.Projectables.CodeFixes/BlockBodyExperimentalCodeFixProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using System.Collections.Immutable; | ||
| using System.Composition; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.CodeFixes; | ||
|
|
||
| /// <summary> | ||
| /// Code fix provider for EFP0001 (Block-bodied member support is experimental). | ||
| /// Adds <c>AllowBlockBody = true</c> to the <c>[Projectable]</c> attribute. | ||
| /// </summary> | ||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(BlockBodyExperimentalCodeFixProvider))] | ||
| [Shared] | ||
| public sealed class BlockBodyExperimentalCodeFixProvider : CodeFixProvider | ||
| { | ||
| public override ImmutableArray<string> FixableDiagnosticIds => ["EFP0001"]; | ||
|
|
||
| public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var diagnostic = context.Diagnostics[0]; | ||
| var node = root.FindNode(diagnostic.Location.SourceSpan); | ||
| var member = node.AncestorsAndSelf().OfType<MemberDeclarationSyntax>().FirstOrDefault(); | ||
| if (member is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (!ProjectableCodeFixHelper.TryFindProjectableAttribute(member, out _)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| context.RegisterCodeFix( | ||
| CodeAction.Create( | ||
| title: "Add AllowBlockBody = true to [Projectable]", | ||
| createChangedDocument: ct => | ||
| ProjectableCodeFixHelper.AddOrReplaceNamedArgumentInProjectableAttributeAsync( | ||
| context.Document, | ||
| member, | ||
| "AllowBlockBody", | ||
| SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression), | ||
| ct), | ||
| equivalenceKey: "EFP0001_AddAllowBlockBody"), | ||
| diagnostic); | ||
| } | ||
| } | ||
|
|
15 changes: 15 additions & 0 deletions
15
...ityFrameworkCore.Projectables.CodeFixes/EntityFrameworkCore.Projectables.CodeFixes.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <!-- Override TargetFrameworks with an "s" from Directory.Build.props settings as needed --> | ||
| <TargetFrameworks>netstandard2.0;</TargetFrameworks> | ||
| <NoWarn>$(NoWarn);NU5128</NoWarn> | ||
| <IsPackable>false</IsPackable> | ||
| <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
84 changes: 84 additions & 0 deletions
84
...ityFrameworkCore.Projectables.CodeFixes/MissingParameterlessConstructorCodeFixProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| using System.Collections.Immutable; | ||
| using System.Composition; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Formatting; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.CodeFixes; | ||
|
|
||
| /// <summary> | ||
| /// Code fix provider for EFP0008 (Target class is missing a parameterless constructor). | ||
| /// Inserts a <c>public ClassName() { }</c> constructor into the class that carries the | ||
| /// <c>[Projectable]</c> constructor, satisfying the object-initializer requirement of the | ||
| /// generated expression tree. | ||
| /// </summary> | ||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MissingParameterlessConstructorCodeFixProvider))] | ||
| [Shared] | ||
| public sealed class MissingParameterlessConstructorCodeFixProvider : CodeFixProvider | ||
| { | ||
| public override ImmutableArray<string> FixableDiagnosticIds => ["EFP0008"]; | ||
|
|
||
| public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var diagnostic = context.Diagnostics[0]; | ||
| var node = root.FindNode(diagnostic.Location.SourceSpan); | ||
|
|
||
| // The diagnostic is reported on the [Projectable] constructor declaration. | ||
| // Walk up to find the containing type. | ||
| var typeDecl = node.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().FirstOrDefault(); | ||
| if (typeDecl is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var typeName = typeDecl.Identifier.Text; | ||
|
|
||
| context.RegisterCodeFix( | ||
| CodeAction.Create( | ||
| title: $"Add parameterless constructor to '{typeName}'", | ||
| createChangedDocument: ct => AddParameterlessConstructorAsync(context.Document, typeDecl, ct), | ||
| equivalenceKey: "EFP0008_AddParameterlessConstructor"), | ||
| diagnostic); | ||
| } | ||
|
|
||
| private async static Task<Document> AddParameterlessConstructorAsync( | ||
| Document document, | ||
| TypeDeclarationSyntax typeDecl, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| var parameterlessCtor = SyntaxFactory | ||
| .ConstructorDeclaration(typeDecl.Identifier.WithoutTrivia()) | ||
| .WithModifiers(SyntaxFactory.TokenList( | ||
| SyntaxFactory.Token(SyntaxKind.PublicKeyword) | ||
| .WithTrailingTrivia(SyntaxFactory.Space))) | ||
| .WithParameterList(SyntaxFactory.ParameterList()) | ||
| .WithBody(SyntaxFactory.Block()) | ||
| .WithAdditionalAnnotations(Formatter.Annotation) | ||
| .WithLeadingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed); | ||
|
|
||
| // Insert before the first existing member so it appears at the top of the class body. | ||
| var newTypeDecl = typeDecl.WithMembers( | ||
| typeDecl.Members.Insert(0, parameterlessCtor)); | ||
|
|
||
| var newRoot = root.ReplaceNode(typeDecl, newTypeDecl); | ||
| return document.WithSyntaxRoot(newRoot); | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.