Пример #1
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            var interpolatedString = (InterpolatedStringExpressionSyntax)context.Node;

            if (ReplaceInterpolatedStringWithStringLiteralRefactoring.CanRefactor(interpolatedString))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AvoidInterpolatedStringWithNoInterpolation,
                    Location.Create(context.SyntaxTree(), GetDollarSpan(interpolatedString)));
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out InterpolatedStringExpressionSyntax interpolatedString))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AvoidInterpolatedStringWithNoInterpolation:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove $",
                        cancellationToken => ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                        GetEquivalenceKey(diagnostic.Id));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.UnnecessaryInterpolatedString:
                {
                    var interpolation = (InterpolationSyntax)interpolatedString.Contents[0];

                    CodeAction codeAction = CodeAction.Create(
                        $"Replace interpolated string with '{interpolation.Expression}'",
                        cancellationToken => UnnecessaryInterpolatedStringRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                        GetEquivalenceKey(diagnostic.Id));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.ReplaceInterpolatedStringWithConcatenation:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Replace interpolated string with concatenation",
                        cancellationToken => ReplaceInterpolatedStringWithConcatenationRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                        GetEquivalenceKey(diagnostic.Id));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out InterpolatedStringExpressionSyntax interpolatedString))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove $",
                cancellationToken => ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.AvoidInterpolatedStringWithNoInterpolation));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Пример #4
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            InterpolatedStringExpressionSyntax interpolatedString = root
                                                                    .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                                    .FirstAncestorOrSelf <InterpolatedStringExpressionSyntax>();

            if (interpolatedString == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove $",
                cancellationToken => ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                DiagnosticIdentifiers.AvoidInterpolatedStringWithNoInterpolation + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out InterpolatedStringExpressionSyntax interpolatedString))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AvoidInterpolatedStringWithNoInterpolation:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove $",
                        cancellationToken => ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                        GetEquivalenceKey(diagnostic.Id));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AvoidInterpolatedStringWithNoInterpolatedText:
                {
                    string title = (interpolatedString.Contents.Count == 1)
                                ? "Extract expression from interpolated string"
                                : "Extract expressions from interpolated string";

                    CodeAction codeAction = CodeAction.Create(
                        title,
                        cancellationToken => AvoidInterpolatedStringWithNoInterpolatedTextRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken),
                        GetEquivalenceKey(diagnostic.Id));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Пример #6
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var interpolatedString = (InterpolatedStringExpressionSyntax)context.Node;

            if (ReplaceInterpolatedStringWithStringLiteralRefactoring.CanRefactor(interpolatedString))
            {
                SyntaxToken token = interpolatedString.StringStartToken;

                if (token.Text.StartsWith("$"))
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.AvoidInterpolatedStringWithNoInterpolation,
                        Location.Create(interpolatedString.SyntaxTree, new TextSpan(token.SpanStart, 1)));
                }
            }
        }