-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: warn on use of tuples (#257)
- Loading branch information
Showing
10 changed files
with
154 additions
and
19 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Dhgms.GripeWithRoslyn.Analyzer/Analyzers/Language/DoNotUseTuplesAnalyzer.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,71 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using Dhgms.GripeWithRoslyn.Analyzer.CodeCracker.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Dhgms.GripeWithRoslyn.Analyzer.Analyzers.Language | ||
{ | ||
/// <summary> | ||
/// Analyzer to ensure Tuples are not used. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class DoNotUseTuplesAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string Title = "Do not use Tuples."; | ||
|
||
private const string MessageFormat = Title; | ||
|
||
private const string Category = SupportedCategories.Design; | ||
|
||
private readonly DiagnosticDescriptor _rule; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DoNotUseTuplesAnalyzer"/> class. | ||
/// </summary> | ||
public DoNotUseTuplesAnalyzer() | ||
{ | ||
_rule = new DiagnosticDescriptor( | ||
DiagnosticIdsHelper.DoNotUseTuples, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: DiagnosticResultDescriptionFactory.DoNotUseTuples()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(_rule); | ||
|
||
/// <inheritdoc /> | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
context.RegisterSyntaxNodeAction(AnalyzeParameter, SyntaxKind.TupleElement, SyntaxKind.TupleExpression, SyntaxKind.TupleType); | ||
} | ||
|
||
private void AnalyzeParameter(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | ||
{ | ||
switch (syntaxNodeAnalysisContext.Node) | ||
{ | ||
case TupleElementSyntax tupleElementSyntax: | ||
syntaxNodeAnalysisContext.ReportDiagnostic(Diagnostic.Create(_rule, tupleElementSyntax.GetLocation())); | ||
break; | ||
case TupleExpressionSyntax tupleExpressionSyntax: | ||
syntaxNodeAnalysisContext.ReportDiagnostic(Diagnostic.Create(_rule, tupleExpressionSyntax.GetLocation())); | ||
break; | ||
case TupleTypeSyntax tupleTypeSyntax: | ||
syntaxNodeAnalysisContext.ReportDiagnostic(Diagnostic.Create(_rule, tupleTypeSyntax.GetLocation())); | ||
break; | ||
} | ||
} | ||
} | ||
} |
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
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,24 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Dhgms.GripeWithRoslyn.Analyzer.Analyzers.Language; | ||
|
||
namespace Dhgms.GripeWithRoslyn.Testing.Language | ||
{ | ||
/// <summary> | ||
/// Analyzer Proofs for <see cref="Tuple"/>. | ||
/// </summary> | ||
public static class TupleProof | ||
{ | ||
/// <summary> | ||
/// Proof of <see cref="Tuple"/> method invocation to trigger <see cref="DoNotUseTuplesAnalyzer"/>. | ||
/// </summary> | ||
/// <returns>A <see cref="Tuple"/>.</returns> | ||
public static (int Number, string Name) GetTuple() | ||
{ | ||
return (1, "Hello"); | ||
} | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
...hgms.GripeWithRoslyn.UnitTests/Analyzer/Analyzers/Language/DoNotUseTuplesAnalyzerTests.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,50 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using Dhgms.GripeWithRoslyn.Analyzer.Analyzers.EfCore; | ||
using Dhgms.GripeWithRoslyn.Analyzer.Analyzers.Language; | ||
using Dhgms.GripeWithRoslyn.UnitTests.Analyzer.Analyzers.EfCore; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Dhgms.GripeWithRoslyn.UnitTests.Analyzer.Analyzers.Language | ||
{ | ||
/// <summary> | ||
/// Unit Tests for <see cref="DoNotUseTuplesAnalyzer"/>. | ||
/// </summary> | ||
public sealed class DoNotUseTuplesAnalyzerTests : AbstractAnalyzerTest<DoNotUseTuplesAnalyzer> | ||
{ | ||
/// <inheritdoc/> | ||
protected override ExpectedDiagnosticModel[] GetExpectedDiagnosticLines() | ||
{ | ||
const string TupleProofFilePath = "Language\\TupleProof.cs"; | ||
|
||
return | ||
[ | ||
new ExpectedDiagnosticModel( | ||
TupleProofFilePath, | ||
DiagnosticSeverity.Error, | ||
18, | ||
22), | ||
|
||
new ExpectedDiagnosticModel( | ||
TupleProofFilePath, | ||
DiagnosticSeverity.Error, | ||
18, | ||
23), | ||
|
||
new ExpectedDiagnosticModel( | ||
TupleProofFilePath, | ||
DiagnosticSeverity.Error, | ||
18, | ||
35), | ||
|
||
new ExpectedDiagnosticModel( | ||
TupleProofFilePath, | ||
DiagnosticSeverity.Error, | ||
20, | ||
19), | ||
]; | ||
} | ||
} | ||
} |
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