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 @@ -58,7 +58,7 @@ private static async Task<Document> Refactor(Document document, Diagnostic diagn
// Create local function
var returnTypeSyntax = generator.TypeExpression(symbol.ReturnType);
var localFunctionSyntaxNode = LocalFunctionStatement((TypeSyntax)returnTypeSyntax, symbol.Name);
localFunctionSyntaxNode = localFunctionSyntaxNode.WithParameterList(CopyParametersWithoutDefaultValues(methodSyntaxNode.ParameterList));
localFunctionSyntaxNode = localFunctionSyntaxNode.WithParameterList(CreateLocalFunctionParameterList(methodSyntaxNode.ParameterList));
if (methodSyntaxNode.Modifiers.Any(m => m.IsKind(SyntaxKind.AsyncKeyword)))
{
localFunctionSyntaxNode = localFunctionSyntaxNode.AddModifiers(Token(SyntaxKind.AsyncKeyword));
Expand Down Expand Up @@ -95,9 +95,13 @@ private static async Task<Document> Refactor(Document document, Diagnostic diagn
return editor.GetChangedDocument();
}

private static ParameterListSyntax CopyParametersWithoutDefaultValues(ParameterListSyntax parameterListSyntax)
private static ParameterListSyntax CreateLocalFunctionParameterList(ParameterListSyntax parameterListSyntax)
{
return ParameterList(SeparatedList(parameterListSyntax.Parameters.Select(p => p.WithDefault(null))));
// Default values are useless as the local function is always called with all the arguments.
// The "this" modifier must be removed as a local function cannot be an extension method (CS1106).
return ParameterList(SeparatedList(parameterListSyntax.Parameters.Select(p => p
.WithDefault(null)
.WithModifiers(TokenList(p.Modifiers.Where(modifier => !modifier.IsKind(SyntaxKind.ThisKeyword)))))));
}

private static ParameterListSyntax RemoveEnumeratorCancellationAttribute(ParameterListSyntax parameterListSyntax, SemanticModel semanticModel, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,47 @@ await CreateProjectBuilder()
.ShouldFixCodeWith(CodeFix)
.ValidateAsync();
}

[Fact]
public async Task ReportDiagnostic_ExtensionMethod()
{
const string SourceCode = """
using System;
using System.Collections.Generic;
static class TypeName
{
public static IEnumerable<int> [|A|](this string a)
{
if (a is null)
throw new ArgumentNullException(nameof(a));

yield return 1;
}
}
""";

const string CodeFix = """
using System;
using System.Collections.Generic;
static class TypeName
{
public static IEnumerable<int> A(this string a)
{
if (a is null)
throw new ArgumentNullException(nameof(a));

return A(a);
IEnumerable<int> A(string a)
{
yield return 1;
}
}
}
""";

await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ShouldFixCodeWith(CodeFix)
.ValidateAsync();
}
}
Loading