Skip to content

Commit

Permalink
Fix crash in DateTimeNowCodeFix when there is no member access syntax (
Browse files Browse the repository at this point in the history
…#233)

Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
  • Loading branch information
Youssef1313 and Vannevelj authored Dec 30, 2022
1 parent 4e53f4d commit 27797dd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# CHANGELOG
https://keepachangelog.com/en/1.0.0/

## [1.16.15] - 2022-12-30
- `DateTimeNow`: Correctly handles static imports

## [1.16.14] - 2022-12-30
- Internal code cleanup

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ or add a reference yourself:

```xml
<ItemGroup>
<PackageReference Include="SharpSource" Version="1.16.14" PrivateAssets="All" />
<PackageReference Include="SharpSource" Version="1.16.15" PrivateAssets="All" />
</ItemGroup>
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Simplification;
using SharpSource.Utilities;

namespace SharpSource.Diagnostics;

Expand All @@ -29,14 +26,14 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

var statement = root.FindNode(diagnosticSpan).DescendantNodesAndSelf().OfType<MemberAccessExpressionSyntax>().First();
var statement = root.FindNode(diagnosticSpan, getInnermostNodeForTie: true);

context.RegisterCodeFix(
CodeAction.Create("Use DateTime.UtcNow",
x => UseUtc(context.Document, root, statement), DateTimeNowAnalyzer.Rule.Id), diagnostic);
}

private Task<Document> UseUtc(Document document, SyntaxNode root, MemberAccessExpressionSyntax statement)
private static Task<Document> UseUtc(Document document, SyntaxNode root, SyntaxNode statement)
{
var newRoot = root.ReplaceNode(statement, SyntaxFactory.ParseExpression("System.DateTime.UtcNow").WithAdditionalAnnotations(Simplifier.Annotation));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
Expand Down
2 changes: 1 addition & 1 deletion SharpSource/SharpSource.Package/SharpSource.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<PropertyGroup>
<PackageId>SharpSource</PackageId>
<PackageVersion>1.16.14</PackageVersion>
<PackageVersion>1.16.15</PackageVersion>
<Authors>Jeroen Vannevel</Authors>
<PackageLicenseUrl>https://github.com/Vannevelj/SharpSource/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/Vannevelj/SharpSource</PackageProjectUrl>
Expand Down
33 changes: 33 additions & 0 deletions SharpSource/SharpSource.Test/DateTimeNowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ void Method()
await VerifyFix(original, result);
}

[TestMethod]
public async Task DateTimeNow_Now_Expression_UsingStatic()
{
var original = @"
using static System.DateTime;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
System.Console.WriteLine(Now);
}
}
}";

var result = @"
using static System.DateTime;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
System.Console.WriteLine(UtcNow);
}
}
}";

await VerifyDiagnostic(original, "Use DateTime.UtcNow to get a locale-independent value");
await VerifyFix(original, result);
}

[TestMethod]
public async Task DateTimeNow_FullName()
{
Expand Down

0 comments on commit 27797dd

Please # to comment.