Skip to content

Commit

Permalink
fix(analyzer release tracking): fix RS2003
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Zaycev committed Feb 19, 2024
1 parent 3857e1a commit 22dfdf5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|------------------------------------------------
IM0001 | Design | Error | Immutable classes should not contain mutable members.
IM0002 | Design | Error | Members of immutable classes shoud not have public setters.
Rule ID | Category | Severity | Notes
--------|----------|----------|-------------------------------------------------------------
IM0001 | Design | Error | Immutable classes should not contain mutable members.
IM0002 | Design | Error | Members of immutable classes shoud not have public setters.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Immutable;
using System.Linq;
using ImmutableAnalyzer.Extensions;
using Microsoft.CodeAnalysis;
using ImmutableAnalyzer.Extensions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -10,40 +7,6 @@ namespace ImmutableAnalyzer.Analyzers;

internal abstract class BaseImmutableAnalyzer : DiagnosticAnalyzer
{
/// <summary>
/// Diagnostic Identifier.
/// </summary>
protected abstract string DiagnosticId { get; }

/// <summary>
/// Diagnostic title.
/// </summary>
protected abstract string Title { get; }

/// <summary>
/// Diagnostic description.
/// </summary>
protected abstract string Description { get; }

/// <summary>
/// Diagnostic message format.
/// </summary>
protected abstract string MessageFormat { get; }

/// <summary>
/// Diagnostic category.
/// </summary>
protected abstract string Category { get; }

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

/// <summary>
/// Diagnostic descriptor.
/// </summary>
protected DiagnosticDescriptor Rule => new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error,
isEnabledByDefault: true, description: Description);

/// <inheritdoc />
public override void Initialize(AnalysisContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImmutableAnalyzer.Extensions;
using System.Collections.Immutable;
using ImmutableAnalyzer.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -10,13 +11,24 @@ namespace ImmutableAnalyzer.Analyzers;
/// Analyzer to check property types of immutable classes.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class ImmutablePropertyTypeAnalyzer : BaseImmutableAnalyzer
internal sealed class ImmutablePropertyTypeAnalyzer : BaseImmutableAnalyzer
{
protected override string DiagnosticId => "IM0001";
protected override string Title => "Mutable member in immutable class";
protected override string MessageFormat => "Immutable class can't have property of type '{0}'";
protected override string Description => "Class member must have immutable type";
protected override string Category => "Design";
private const string DiagnosticId = "IM0001";
private const string Title = "Mutable member in immutable class";
private const string MessageFormat = "Immutable class can't have property of type '{0}'";
private const string Description = "Class member must have immutable type.";
private const string Category = "Design";

/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

/// <summary>
/// Diagnostic descriptor.
/// </summary>
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId, Title, MessageFormat,
Category, DiagnosticSeverity.Error, true, Description
);

/// <inheritdoc/>
protected override void AnalyzeSyntax(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -9,14 +10,24 @@ namespace ImmutableAnalyzer.Analyzers;
/// Analyzer to check set accessor of properties of immutable classes.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class ImmutableSetAccessorAnalyzer : BaseImmutableAnalyzer
internal sealed class ImmutableSetAccessorAnalyzer : BaseImmutableAnalyzer
{
protected override string DiagnosticId => "IM0002";
protected override string Title => "Public setter violates class immutability";
protected override string MessageFormat => "Member of immutable class can't have '{0}' accessor";
protected override string Description => "Setter can't be public, because it's give possibility to change member from the outer.";
private const string DiagnosticId = "IM0002";
private const string Title = "Public setter violates class immutability";
private const string MessageFormat = "Member of immutable class can't have '{0}' accessor";
private const string Description = "Setter can't be public, because it's give possibility to change member from the outer.";
private const string Category = "Design";

/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

protected override string Category => "Design";
/// <summary>
/// Diagnostic descriptor.
/// </summary>
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId, Title, MessageFormat,
Category, DiagnosticSeverity.Error, true, Description
);

protected override void AnalyzeSyntax(ClassDeclarationSyntax classDeclarationNode, SyntaxNodeAnalysisContext context)
{
Expand Down
5 changes: 5 additions & 0 deletions ImmutableAnalyzer/ImmutableAnalyzer/ImmutableAnalyzer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
</ItemGroup>

</Project>

0 comments on commit 22dfdf5

Please sign in to comment.