Skip to content
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

Fix MSTEST0002 (Test classes should have valid layout) codefix crash on records #4580

Merged
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 @@ -47,7 +47,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

ClassDeclarationSyntax declaration = syntaxToken.Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().FirstOrDefault();
TypeDeclarationSyntax declaration = syntaxToken.Parent.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First();

// Register a code action that will invoke the fix.
context.RegisterCodeFix(
Expand All @@ -58,7 +58,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
diagnostic);
}

private static async Task<Document> FixClassDeclarationAsync(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken cancellationToken)
private static async Task<Document> FixClassDeclarationAsync(Document document, TypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -70,9 +70,9 @@ private static async Task<Document> FixClassDeclarationAsync(Document document,

// Remove the static modifier if it exists
SyntaxTokenList modifiers = SyntaxFactory.TokenList(
classDeclaration.Modifiers.Where(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)));
typeDeclaration.Modifiers.Where(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)));

if (!classDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword))
if (!typeDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword))
{
// Determine the visibility modifier
SyntaxToken visibilityModifier = canDiscoverInternals
Expand All @@ -84,8 +84,8 @@ private static async Task<Document> FixClassDeclarationAsync(Document document,
}

// Create a new class declaration with the updated modifiers.
ClassDeclarationSyntax newClassDeclaration = classDeclaration.WithModifiers(modifiers);
editor.ReplaceNode(classDeclaration, newClassDeclaration);
TypeDeclarationSyntax newTypeDeclaration = typeDeclaration.WithModifiers(modifiers);
editor.ReplaceNode(typeDeclaration, newTypeDeclaration);
SyntaxNode newRoot = editor.GetChangedRoot();

return document.WithSyntaxRoot(newRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ await VerifyCS.VerifyCodeFixAsync(
fixedCode);
}

[TestMethod]
public async Task WhenClassIsInternalAndTestRecord_Diagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
internal record {|#0:MyTestClass|}
{
}
""";

string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public record MyTestClass
{
}
""";

await VerifyCS.VerifyCodeFixAsync(
code,
VerifyCS.Diagnostic(TestClassShouldBeValidAnalyzer.TestClassShouldBeValidRule)
.WithLocation(0)
.WithArguments("MyTestClass"),
fixedCode);
}

[DataRow("private")]
[DataRow("internal")]
[TestMethod]
Expand Down
Loading