-
-
Notifications
You must be signed in to change notification settings - Fork 7
Add analyzer for weighing-machine #109
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
ErikSchierboom
merged 22 commits into
exercism:main
from
Grenkin1988:weighing-machine-analyzer
Nov 30, 2021
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6c63798
Start writing code
Grenkin1988 3b18356
Add more analysis for properties
Grenkin1988 118bdd7
Provate field analysis
Grenkin1988 b140a1e
Add IsRoundMethodCalledInDisplayWeightProperty
Grenkin1988 559a7ab
Fix some comments
Grenkin1988 4eea02f
Merge branch 'exercism:main' into weighing-machine-analyzer
Grenkin1988 895d47d
Add some comments, and implemet finding correct baking field
Grenkin1988 41c9b12
More comments
Grenkin1988 6cb3e15
Include files and revert launch
Grenkin1988 5c1fb57
Try adding tests
Grenkin1988 f8772de
Remove explicit solution entries in csproj
ErikSchierboom a0fd8d0
Remove comments integration from analysis
ErikSchierboom 5f43e51
Merge remote-tracking branch 'ErikSchierboom/remove-website-copy-inte…
Grenkin1988 8278ab6
Merge remote-tracking branch 'origin/main' into weighing-machine-anal…
Grenkin1988 f3716b3
Merge branch 'weighing-machine-analyzer' of https://github.com/Grenki…
Grenkin1988 1e537c7
All tests pass
Grenkin1988 0cb889d
Auto property case
Grenkin1988 0c72335
csharp.weighingmachine.property_setter_is_not_private
Grenkin1988 062d381
All tests done and fixed logic
Grenkin1988 1216c5c
Merge remote-tracking branch 'origin/main' into weighing-machine-anal…
Grenkin1988 c780424
Move some comments to shared and add approve case
Grenkin1988 8f9d964
Fix project and test
Grenkin1988 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
64 changes: 64 additions & 0 deletions
64
src/Exercism.Analyzers.CSharp/Analyzers/WeighingMachine/WeighingMachineAnalyzer.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,64 @@ | ||
using Exercism.Analyzers.CSharp.Analyzers.Shared; | ||
|
||
using static Exercism.Analyzers.CSharp.Analyzers.Shared.SharedComments; | ||
using static Exercism.Analyzers.CSharp.Analyzers.WeighingMachine.WeighingMachineSolution; | ||
|
||
namespace Exercism.Analyzers.CSharp.Analyzers.WeighingMachine | ||
{ | ||
internal class WeighingMachineAnalyzer : SharedAnalyzer<WeighingMachineSolution> | ||
{ | ||
protected override SolutionAnalysis DisapproveWhenInvalid(WeighingMachineSolution solution) | ||
{ | ||
if (solution.MissingWeighingMachineClass) | ||
{ | ||
solution.AddComment(MissingClass(WeighingMachineClassName)); | ||
return solution.Disapprove(); | ||
} | ||
|
||
foreach (var missing in solution.MissingRequiredProperties()) | ||
{ | ||
solution.AddComment(MissingProperty(missing)); | ||
return solution.Disapprove(); | ||
} | ||
|
||
if (!solution.PrecisionIsAutoProperty) | ||
{ | ||
solution.AddComment(SharedComments.PropertyIsNotAutoProperty("Precision")); | ||
} | ||
|
||
if (!solution.TareAdjustmentIsAutoProperty) | ||
{ | ||
solution.AddComment(SharedComments.PropertyIsNotAutoProperty("TareAdjustment")); | ||
} | ||
|
||
if (solution.PrecisionPropertyHasNonPrivateSetter()) | ||
{ | ||
solution.AddComment(SharedComments.PrecisionPropertyHasNonPrivateSetter("Precision")); | ||
} | ||
|
||
if (!solution.WeightFieldNameIsPrivate(out var fieldName) && !string.IsNullOrWhiteSpace(fieldName)) | ||
{ | ||
solution.AddComment(UsePrivateVisibility(fieldName)); | ||
} | ||
|
||
if (!solution.IsRoundMethodCalledInDisplayWeightProperty()) | ||
{ | ||
solution.AddComment(WeighingMachineComments.RoundMethodNotCalledInDisplayWeightProperty); | ||
} | ||
|
||
return solution.HasComments | ||
? solution.Disapprove() | ||
: solution.ContinueAnalysis(); | ||
} | ||
|
||
protected override SolutionAnalysis ApproveWhenValid(WeighingMachineSolution solution) | ||
{ | ||
if (!solution.TareAdjustmentHasInitializer) | ||
{ | ||
solution.AddComment(SharedComments.PropertyBetterUseInitializer("TareAdjustment")); | ||
} | ||
|
||
return solution.Approve(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Exercism.Analyzers.CSharp/Analyzers/WeighingMachine/WeighingMachineComments.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,11 @@ | ||
| ||
using static Exercism.Analyzers.CSharp.Analyzers.Shared.SharedCommentParameters; | ||
|
||
namespace Exercism.Analyzers.CSharp.Analyzers.WeighingMachine | ||
{ | ||
internal class WeighingMachineComments | ||
{ | ||
public static SolutionComment RoundMethodNotCalledInDisplayWeightProperty = | ||
new("csharp.weighingmachine.round_called_in_display_weight"); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/Exercism.Analyzers.CSharp/Analyzers/WeighingMachine/WeighingMachineSolution.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,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Exercism.Analyzers.CSharp.Syntax; | ||
using Exercism.Analyzers.CSharp.Syntax.Comparison; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Exercism.Analyzers.CSharp.Analyzers.WeighingMachine | ||
{ | ||
internal class WeighingMachineSolution : Solution | ||
{ | ||
public const string WeighingMachineClassName = "WeighingMachine"; | ||
public const string PrecisionPropertyName = "Precision"; | ||
public const string WeightPropertyName = "Weight"; | ||
public const string TareAdjustmentPropertyName = "TareAdjustment"; | ||
public const string DisplayWeightPropertyName = "DisplayWeight"; | ||
|
||
public WeighingMachineSolution(Solution solution) : base(solution) | ||
{ | ||
} | ||
|
||
private ClassDeclarationSyntax WeighingMachineClass => SyntaxRoot.GetClass(WeighingMachineClassName); | ||
|
||
public bool MissingWeighingMachineClass => WeighingMachineClass is null; | ||
|
||
private PropertyDeclarationSyntax PrecisionProperty => WeighingMachineClass?.GetProperty(PrecisionPropertyName); | ||
|
||
private PropertyDeclarationSyntax WeightProperty => WeighingMachineClass?.GetProperty(WeightPropertyName); | ||
|
||
private PropertyDeclarationSyntax TareAdjustmentProperty => WeighingMachineClass?.GetProperty(TareAdjustmentPropertyName); | ||
|
||
private PropertyDeclarationSyntax DisplayWeightProperty => WeighingMachineClass?.GetProperty(DisplayWeightPropertyName); | ||
|
||
public IEnumerable<string> MissingRequiredProperties() | ||
{ | ||
if (PrecisionProperty is null) yield return PrecisionPropertyName; | ||
if (WeightProperty is null) yield return WeightPropertyName; | ||
if (TareAdjustmentProperty is null) yield return TareAdjustmentPropertyName; | ||
if (DisplayWeightProperty is null) yield return DisplayWeightPropertyName; | ||
} | ||
|
||
public bool PrecisionIsAutoProperty => PrecisionProperty?.IsAutoProperty() == true; | ||
|
||
public bool TareAdjustmentIsAutoProperty => TareAdjustmentProperty?.IsAutoProperty() == true; | ||
|
||
public bool TareAdjustmentHasInitializer => TareAdjustmentProperty?.HasInitializer() == true; | ||
|
||
public bool PrecisionPropertyHasNonPrivateSetter() | ||
{ | ||
var setAccessor = PrecisionProperty.GetSetAccessor(); | ||
return setAccessor is not null && | ||
(setAccessor.Modifiers.Count == 0 || | ||
!setAccessor.Modifiers.Any(m => m.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.PrivateKeyword))); | ||
} | ||
|
||
public bool WeightFieldNameIsPrivate(out string fieldName) | ||
{ | ||
fieldName = WeightProperty.GetBakingFieldName(); | ||
|
||
if (string.IsNullOrEmpty(fieldName)) | ||
{ | ||
return false; | ||
} | ||
|
||
return WeighingMachineClass?.GetField(fieldName)?.IsPrivate() ?? false; | ||
} | ||
|
||
public bool IsRoundMethodCalledInDisplayWeightProperty() => | ||
DisplayWeightProperty?.GetMethodCalled("Round") is not null; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/Exercism.Analyzers.CSharp/Syntax/PropertyDeclarationSyntaxExtensions.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,61 @@ | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Exercism.Analyzers.CSharp.Syntax | ||
{ | ||
public static class PropertyDeclarationSyntaxExtensions | ||
Grenkin1988 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public const string GetAccessorName = "get"; | ||
public const string SetAccessorName = "set"; | ||
|
||
public static bool IsAutoProperty(this PropertyDeclarationSyntax property) | ||
{ | ||
if (property.AccessorList != null) | ||
{ | ||
return property.AccessorList.Accessors.All(a => a.Body is null && a.ExpressionBody is null); | ||
} | ||
|
||
return property.ExpressionBody is null; | ||
} | ||
|
||
public static bool HasInitializer(this PropertyDeclarationSyntax property) => property?.Initializer is not null; | ||
|
||
public static AccessorDeclarationSyntax GetAccessor(this PropertyDeclarationSyntax property, SyntaxKind accessor) | ||
{ | ||
return property?.AccessorList?.Accessors.FirstOrDefault(ac => ac.IsKind(accessor)); | ||
} | ||
|
||
public static AccessorDeclarationSyntax GetGetAccessor(this PropertyDeclarationSyntax property) => | ||
property?.GetAccessor(SyntaxKind.GetAccessorDeclaration); | ||
|
||
public static AccessorDeclarationSyntax GetSetAccessor(this PropertyDeclarationSyntax property) => | ||
property?.GetAccessor(SyntaxKind.SetAccessorDeclaration); | ||
|
||
public static string GetBakingFieldName(this PropertyDeclarationSyntax property) | ||
{ | ||
var get = property.GetGetAccessor(); | ||
var returns = get?.Body.DescendantNodes<ReturnStatementSyntax>().FirstOrDefault(); | ||
var fieldIdentifier = returns?.Expression as IdentifierNameSyntax; | ||
if (fieldIdentifier is not null) | ||
{ | ||
return fieldIdentifier.Identifier.ValueText; | ||
} | ||
|
||
var set = property.GetSetAccessor(); | ||
var setValue = set?.Body | ||
.DescendantNodes<AssignmentExpressionSyntax>() | ||
.FirstOrDefault(s => s.OperatorToken.IsKind(SyntaxKind.EqualsToken) | ||
&& s.Right is IdentifierNameSyntax ident && ident.Identifier.ValueText == "value"); | ||
|
||
if (setValue is not null && setValue.Left is IdentifierNameSyntax ident) | ||
{ | ||
return ident.Identifier.ValueText; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
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.