-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement positional properties analyzer
- Loading branch information
Showing
17 changed files
with
187 additions
and
13 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
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,12 @@ | ||
#### Prefer named properties instead of positional ones | ||
|
||
Noncompliant Code Examples: | ||
```csharp | ||
Log.Error("Disk quota {0} MB exceeded by {1}", quota, user); | ||
``` | ||
|
||
|
||
Compliant Solution: | ||
```csharp | ||
Log.Error("Disk quota {Quota} MB exceeded by {User}", quota, user); | ||
``` |
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,4 +1,4 @@ | ||
##### Duplicate template property | ||
#### Duplicate template property | ||
|
||
Noncompliant Code Example: | ||
```csharp | ||
|
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
52 changes: 52 additions & 0 deletions
52
src/ReSharper.Structured.Logging/Analyzer/PositionalPropertiesUsageAnalyzer.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,52 @@ | ||
using System.Linq; | ||
|
||
using JetBrains.ReSharper.Feature.Services.Daemon; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
using JetBrains.ReSharper.Psi.Util; | ||
|
||
using ReSharper.Structured.Logging.Extensions; | ||
using ReSharper.Structured.Logging.Highlighting; | ||
using ReSharper.Structured.Logging.Serilog.Parsing; | ||
|
||
namespace ReSharper.Structured.Logging.Analyzer | ||
{ | ||
[ElementProblemAnalyzer(typeof(IInvocationExpression))] | ||
public class PositionalPropertiesUsageAnalyzer : ElementProblemAnalyzer<IInvocationExpression> | ||
{ | ||
private readonly MessageTemplateParser _messageTemplateParser; | ||
|
||
public PositionalPropertiesUsageAnalyzer(MessageTemplateParser messageTemplateParser) | ||
{ | ||
_messageTemplateParser = messageTemplateParser; | ||
} | ||
|
||
protected override void Run( | ||
IInvocationExpression element, | ||
ElementProblemAnalyzerData data, | ||
IHighlightingConsumer consumer) | ||
{ | ||
var templateArgument = element.GetTemplateArgument(); | ||
if (templateArgument == null) | ||
{ | ||
return; | ||
} | ||
|
||
var stringLiteral = StringLiteralAltererUtil.TryCreateStringLiteralByExpression(templateArgument.Value); | ||
if (stringLiteral == null) | ||
{ | ||
return; | ||
} | ||
|
||
var messageTemplate = _messageTemplateParser.Parse(stringLiteral.Expression.GetUnquotedText()); | ||
if (messageTemplate.PositionalProperties == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var property in messageTemplate.PositionalProperties) | ||
{ | ||
consumer.AddHighlighting(new PositionalPropertyUsedWarning(stringLiteral, property, stringLiteral.GetTokenDocumentRange(property))); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/ReSharper.Structured.Logging/Highlighting/PositionalPropertyUsedWarning.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,61 @@ | ||
using JetBrains.DocumentModel; | ||
using JetBrains.ReSharper.Feature.Services.Daemon; | ||
using JetBrains.ReSharper.Psi.CSharp; | ||
using JetBrains.ReSharper.Psi.Util; | ||
|
||
using ReSharper.Structured.Logging.Highlighting; | ||
using ReSharper.Structured.Logging.Serilog.Parsing; | ||
|
||
[assembly: | ||
RegisterConfigurableSeverity( | ||
PositionalPropertyUsedWarning.SeverityId, | ||
null, | ||
HighlightingGroupIds.CompilerWarnings, | ||
PositionalPropertyUsedWarning.Message, | ||
PositionalPropertyUsedWarning.Message, | ||
Severity.WARNING)] | ||
|
||
namespace ReSharper.Structured.Logging.Highlighting | ||
{ | ||
[ConfigurableSeverityHighlighting( | ||
SeverityId, | ||
CSharpLanguage.Name, | ||
OverlapResolve = OverlapResolveKind.WARNING, | ||
ToolTipFormatString = Message)] | ||
public class PositionalPropertyUsedWarning : IHighlighting | ||
{ | ||
public const string Message = "Prefer named properties instead of positional ones"; | ||
|
||
public const string SeverityId = "PositionalPropertyUsedProblem"; | ||
|
||
public PositionalPropertyUsedWarning( | ||
IStringLiteralAlterer stringLiteral, | ||
PropertyToken namedProperty, | ||
DocumentRange documentRange) | ||
{ | ||
StringLiteral = stringLiteral; | ||
NamedProperty = namedProperty; | ||
Range = documentRange; | ||
} | ||
|
||
public string ErrorStripeToolTip => ToolTip; | ||
|
||
public PropertyToken NamedProperty { get; } | ||
|
||
public DocumentRange Range { get; } | ||
|
||
public IStringLiteralAlterer StringLiteral { get; } | ||
|
||
public string ToolTip => Message; | ||
|
||
public DocumentRange CalculateRange() | ||
{ | ||
return Range; | ||
} | ||
|
||
public bool IsValid() | ||
{ | ||
return Range.IsValid(); | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
test/data/Analyzers/PositionalPropertiesUsage/SerilogPositionProperty.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,12 @@ | ||
using Serilog; | ||
|
||
namespace ConsoleApp | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main() | ||
{ | ||
Log.Logger.Information("{0}", 1); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/data/Analyzers/PositionalPropertiesUsage/SerilogPositionProperty.cs.gold
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,16 @@ | ||
using Serilog; | ||
|
||
namespace ConsoleApp | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main() | ||
{ | ||
Log.Logger.Information("||{0}|(0)|(1)", 1); | ||
} | ||
} | ||
} | ||
|
||
--------------------------------------------------------- | ||
(0): ReSharper Format String Item: | ||
(1): ReSharper Warning: Prefer named properties instead of positional ones |
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
11 changes: 11 additions & 0 deletions
11
test/src/Analyzer/PositionalPropertiesUsageAnalyzerTests.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,11 @@ | ||
using NUnit.Framework; | ||
|
||
namespace ReSharper.Structured.Logging.Tests.Analyzer | ||
{ | ||
public class PositionalPropertiesUsageAnalyzerTests : MessageTemplateAnalyzerTestBase | ||
{ | ||
protected override string SubPath => "PositionalPropertiesUsage"; | ||
|
||
[Test] public void TestSerilogPositionProperty() => DoNamedTest2(); | ||
} | ||
} |