Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,49 @@ void Start()

await VerifyCSharpDiagnosticAsync(test, diagnostic1, diagnostic2);
}

[Fact]
public async Task AnimatorPlayWithStringLiteralTrivia()
{
const string test = @"
using UnityEngine;

class Test : MonoBehaviour
{
private Animator _animator = null;

void Start()
{
// comment before
_animator.Play(/* inline before */ ""Attack"" /* inline after */);
// comment after
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(11, 9)
.WithArguments("Play", "Attack");

await VerifyCSharpDiagnosticAsync(test, diagnostic);

const string fixedTest = @"
using UnityEngine;

class Test : MonoBehaviour
{
private static readonly int AttackHash = Animator.StringToHash(""Attack"");
private Animator _animator = null;

void Start()
{
// comment before
_animator.Play(/* inline before */ AttackHash /* inline after */);
// comment after
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
}
}
37 changes: 37 additions & 0 deletions src/Microsoft.Unity.Analyzers.Tests/DestroyTransformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,43 @@ public void Update() {
Object.DestroyImmediate(transform.gameObject, true);
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
}

[Fact]
public async Task TestDestroyTrivia()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
public void Update() {
// comment before
Destroy(/* inline before */ transform /* inline after */ );
// comment after
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(8, 9);

await VerifyCSharpDiagnosticAsync(test, diagnostic);

const string fixedTest = @"
using UnityEngine;

class Camera : MonoBehaviour
{
public void Update() {
// comment before
Destroy(/* inline before */ transform.gameObject /* inline after */ );
// comment after
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.Unity.Analyzers/AnimatorStringToHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ private static async Task<Document> ExtractToHashFieldAsync(
editor.InsertMembers(classDecl, 0, [fieldDecl]);
}

var newArgument = stringArgument.WithExpression(SyntaxFactory.IdentifierName(fieldName));
var newArgument = stringArgument
.WithExpression(SyntaxFactory.IdentifierName(fieldName))
.WithTrailingTrivia(stringArgument.GetTrailingTrivia());

editor.ReplaceNode(stringArgument, newArgument);

return editor.GetChangedDocument();
Expand Down
11 changes: 5 additions & 6 deletions src/Microsoft.Unity.Analyzers/DestroyTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.Unity.Analyzers.Resources;

namespace Microsoft.Unity.Analyzers;
Expand Down Expand Up @@ -131,13 +132,11 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> UseGameObjectAsync(Document document, ExpressionSyntax argument, CancellationToken cancellationToken)
{
var gameObject = SyntaxFactory.IdentifierName("gameObject");
var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, argument, gameObject);
var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, argument.WithoutTrailingTrivia(), gameObject);

var root = await document
.GetSyntaxRootAsync(cancellationToken)
.ConfigureAwait(false);
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(argument, memberAccess.WithTrailingTrivia(argument.GetTrailingTrivia()));

var newRoot = root?.ReplaceNode(argument, memberAccess);
return newRoot == null ? document : document.WithSyntaxRoot(newRoot);
return editor.GetChangedDocument();
}
}
Loading