Skip to content

Allow to apply code fix in top-level statements#1168

Closed
meziantou wants to merge 1 commit into
spectreconsole:mainfrom
meziantou:fix-top-level-statements
Closed

Allow to apply code fix in top-level statements#1168
meziantou wants to merge 1 commit into
spectreconsole:mainfrom
meziantou:fix-top-level-statements

Conversation

@meziantou

@meziantou meziantou commented Feb 19, 2023

Copy link
Copy Markdown
Contributor

When you apply the code fix in the following code, you get an exception:

using System;

Console.WriteLine("Hello, World");
System.InvalidOperationException : Sequence contains no elements
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Spectre.Console.Analyzer.CodeActions.SwitchToAnsiConsoleAction.GetAnsiConsoleParameterDeclaration()
   at async Spectre.Console.Analyzer.CodeActions.SwitchToAnsiConsoleAction.GetChangedDocumentAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.PreviewChangesSuggestedAction.CreateAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.GetPreviewChangesFlavorAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.CreateAllFlavorsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

if (root != null)
{
var methodDeclaration = root.FindNode(context.Span).FirstAncestorOrSelf<InvocationExpressionSyntax>();
var methodDeclaration = root.FindNode(context.Span, getInnermostNodeForTie: true).FirstAncestorOrSelf<InvocationExpressionSyntax>();

@meziantou meziantou Feb 19, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is needed, but as it uses FirstAncestorOrSelf, it should find the right node anyway. Let me know if you want me to revert this change.

Comment thread test/Spectre.Console.Analyzer.Tests/SpectreAnalyzerVerifier.cs Outdated
@phil-scott-78

Copy link
Copy Markdown
Contributor

Closed this, merged as part of #1169

@meziantou meziantou deleted the fix-top-level-statements branch February 25, 2023 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants