Skip to content

Commit

Permalink
Merge pull request #66825 from CyrusNajmabadi/useLocalFunction
Browse files Browse the repository at this point in the history
Move 'Use local function' down to CodeStyle layer.
  • Loading branch information
CyrusNajmabadi authored Feb 13, 2023
2 parents b8e1137 + 319b471 commit 87e5981
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseExpressionBodyForLambda\UseExpressionBodyForLambdaCodeActionHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseExpressionBodyForLambda\UseExpressionBodyForLambdaCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseInterpolatedVerbatimString\CSharpUseInterpolatedVerbatimStringCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseLocalFunction\CSharpUseLocalFunctionCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseNameofInAttribute\CSharpUseNameofInAttributeCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UsePatternCombinators\CSharpUsePatternCombinatorsCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\CSharpFileHeaderCodeFixProvider.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,27 @@ protected override async Task FixAllAsync(
var currentRoot = root.TrackNodes(nodesToTrack);

var languageVersion = semanticModel.SyntaxTree.Options.LanguageVersion();
bool makeStaticIfPossible;
var makeStaticIfPossible = false;

if (languageVersion >= LanguageVersion.CSharp8)
{
var options = (CSharpCodeGenerationOptions)await document.GetCodeGenerationOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
#if CODE_STYLE
var info = new CSharpCodeGenerationContextInfo(
CodeGenerationContext.Default, CSharpCodeGenerationOptions.Default, new CSharpCodeGenerationService(document.Project.Services), root.SyntaxTree.Options.LanguageVersion());
#else
var info = await document.GetCodeGenerationInfoAsync(CodeGenerationContext.Default, fallbackOptions, cancellationToken).ConfigureAwait(false);
#endif

var options = (CSharpCodeGenerationOptions)info.Options;
makeStaticIfPossible = options.PreferStaticLocalFunction.Value;
}
else
{
makeStaticIfPossible = false;
}

// Process declarations in reverse order so that we see the effects of nested
// declarations befor processing the outer decls.
// declarations before processing the outer decls.
foreach (var (localDeclaration, anonymousFunction, references) in nodesFromDiagnostics.OrderByDescending(nodes => nodes.function.SpanStart))
{
var delegateType = (INamedTypeSymbol)semanticModel.GetTypeInfo(anonymousFunction, cancellationToken).ConvertedType;
var parameterList = GenerateParameterList(anonymousFunction, delegateType.DelegateInvokeMethod);
var parameterList = GenerateParameterList(editor.Generator, anonymousFunction, delegateType.DelegateInvokeMethod);
var makeStatic = MakeStatic(semanticModel, makeStaticIfPossible, localDeclaration, cancellationToken);

var currentLocalDeclaration = currentRoot.GetCurrentNode(localDeclaration);
Expand Down Expand Up @@ -242,17 +245,17 @@ private static LocalFunctionStatementSyntax CreateLocalFunctionStatement(
}

private static ParameterListSyntax GenerateParameterList(
AnonymousFunctionExpressionSyntax anonymousFunction, IMethodSymbol delegateMethod)
SyntaxGenerator generator, AnonymousFunctionExpressionSyntax anonymousFunction, IMethodSymbol delegateMethod)
{
var parameterList = TryGetOrCreateParameterList(anonymousFunction);
var i = 0;

return parameterList != null
? parameterList.ReplaceNodes(parameterList.Parameters, (parameterNode, _) => PromoteParameter(parameterNode, delegateMethod.Parameters.ElementAtOrDefault(i++)))
? parameterList.ReplaceNodes(parameterList.Parameters, (parameterNode, _) => PromoteParameter(generator, parameterNode, delegateMethod.Parameters.ElementAtOrDefault(i++)))
: SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(delegateMethod.Parameters.Select(parameter =>
PromoteParameter(SyntaxFactory.Parameter(parameter.Name.ToIdentifierToken()), parameter))));
PromoteParameter(generator, SyntaxFactory.Parameter(parameter.Name.ToIdentifierToken()), parameter))));

static ParameterSyntax PromoteParameter(ParameterSyntax parameterNode, IParameterSymbol delegateParameter)
static ParameterSyntax PromoteParameter(SyntaxGenerator generator, ParameterSyntax parameterNode, IParameterSymbol delegateParameter)
{
// delegateParameter may be null, consider this case: Action x = (a, b) => { };
// we will still fall back to object
Expand All @@ -264,7 +267,7 @@ static ParameterSyntax PromoteParameter(ParameterSyntax parameterNode, IParamete

if (delegateParameter?.HasExplicitDefaultValue == true)
{
parameterNode = parameterNode.WithDefault(GetDefaultValue(delegateParameter));
parameterNode = parameterNode.WithDefault(GetDefaultValue(generator, delegateParameter));
}

return parameterNode;
Expand Down Expand Up @@ -317,7 +320,7 @@ private static int TryDetermineParameterIndex(NameColonSyntax argumentNameColon,
return method.Parameters.IndexOf(p => p.Name == name);
}

private static EqualsValueClauseSyntax GetDefaultValue(IParameterSymbol parameter)
=> SyntaxFactory.EqualsValueClause(ExpressionGenerator.GenerateExpression(CSharpSyntaxGenerator.Instance, parameter.Type, parameter.ExplicitDefaultValue, canUseFieldReference: true));
private static EqualsValueClauseSyntax GetDefaultValue(SyntaxGenerator generator, IParameterSymbol parameter)
=> SyntaxFactory.EqualsValueClause(ExpressionGenerator.GenerateExpression(generator, parameter.Type, parameter.ExplicitDefaultValue, canUseFieldReference: true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
Expand Down

0 comments on commit 87e5981

Please # to comment.