-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add addParameterless option to generate parameterless constructor (#120)
Fixes #120
- Loading branch information
Showing
21 changed files
with
884 additions
and
135 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
; Unshipped analyzer release | ||
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
||
### New Rules | ||
|
||
Rule ID | Category | Severity | Notes | ||
--------|----------|----------|------- | ||
ACONS12 | Usage | Error | TypeWithoutBaseParameterlessConstructorAnalyzer, [Documentation](https://github.com/k94ll13nn3/AutoConstructor#ACONS12) |
This file contains 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
62 changes: 62 additions & 0 deletions
62
src/AutoConstructor.Generator/Analyzers/TypeWithoutBaseParameterlessConstructorAnalyzer.cs
This file contains 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,62 @@ | ||
using System.Collections.Immutable; | ||
using AutoConstructor.Generator.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace AutoConstructor.Generator.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class TypeWithoutBaseParameterlessConstructorAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptors.TypeWithoutBaseParameterlessConstructorRule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
_ = context ?? throw new ArgumentNullException(nameof(context)); | ||
|
||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType); | ||
} | ||
|
||
private static void AnalyzeSymbol(SymbolAnalysisContext context) | ||
{ | ||
var symbol = (INamedTypeSymbol)context.Symbol; | ||
|
||
if (symbol.GetAttribute(Source.AttributeFullName) is AttributeData attr) | ||
{ | ||
bool addParameterLess = symbol.DeclaringSyntaxReferences[0].GetSyntax() is TypeDeclarationSyntax | ||
&& symbol.GetAttribute(Source.AttributeFullName) is AttributeData attribute | ||
&& attribute.AttributeConstructor?.Parameters.Length > 0 | ||
&& attribute.GetBoolParameterValue("addParameterless"); | ||
if (addParameterLess) | ||
{ | ||
bool baseHasAccessibleParameterlessConstructor = BaseHasAccessibleParameterlessConstructor(symbol); | ||
if (!baseHasAccessibleParameterlessConstructor) | ||
{ | ||
SyntaxReference? propertyTypeIdentifier = attr.ApplicationSyntaxReference; | ||
if (propertyTypeIdentifier is not null) | ||
{ | ||
var location = Location.Create(propertyTypeIdentifier.SyntaxTree, propertyTypeIdentifier.Span); | ||
var diagnostic = Diagnostic.Create(DiagnosticDescriptors.TypeWithoutBaseParameterlessConstructorRule, location); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static bool BaseHasAccessibleParameterlessConstructor(INamedTypeSymbol symbol) | ||
{ | ||
INamedTypeSymbol? baseType = symbol.BaseType; | ||
if (baseType?.BaseType is null) | ||
{ | ||
return true; | ||
} | ||
IMethodSymbol? acceptableConstructor = baseType.Constructors.FirstOrDefault(d => | ||
!d.IsStatic && d.DeclaredAccessibility != Accessibility.Private && d.Parameters.Length == 0); | ||
return acceptableConstructor != null; | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.