コード例 #1
0
        public void Analyze(SemanticModelAnalysisContext context)
        {
            var semanticModel     = context.SemanticModel;
            var syntaxTree        = semanticModel.SyntaxTree;
            var cancellationToken = context.CancellationToken;

            if (!context.GetIdeAnalyzerOptions().DetectAndOfferEditorFeaturesForProbableJsonStrings)
            {
                return;
            }

            var detector = JsonLanguageDetector.GetOrCreate(semanticModel.Compilation, _info);
            var root     = syntaxTree.GetRoot(cancellationToken);

            Analyze(context, detector, root, cancellationToken);
        }
コード例 #2
0
        public void Analyze(SemanticModelAnalysisContext context)
        {
            var semanticModel     = context.SemanticModel;
            var syntaxTree        = semanticModel.SyntaxTree;
            var cancellationToken = context.CancellationToken;

            var option = context.Options.GetIdeOptions().ReportInvalidJsonPatterns;

            if (!option)
            {
                return;
            }

            var detector = JsonLanguageDetector.GetOrCreate(semanticModel.Compilation, _info);
            var root     = syntaxTree.GetRoot(cancellationToken);

            Analyze(context, detector, root, cancellationToken);
        }
コード例 #3
0
        private void Analyze(
            SemanticModelAnalysisContext context,
            JsonLanguageDetector detector,
            SyntaxNode node,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            foreach (var child in node.ChildNodesAndTokens())
            {
                if (child.IsNode)
                {
                    Analyze(context, detector, child.AsNode() !, cancellationToken);
                }
                else
                {
                    var token = child.AsToken();

                    // If we have a string, and it's not being passed to a known JSON api (and it doesn't have a
                    // lang=json comment), but it is parseable as JSON with enough structure that we are confident it is
                    // json, then report that json features could light up here.
                    if (_info.IsAnyStringLiteral(token.RawKind) &&
                        detector.TryParseString(token, context.SemanticModel, includeProbableStrings: false, cancellationToken) == null &&
                        detector.IsProbablyJson(token, out _))
                    {
                        var chars      = _info.VirtualCharService.TryConvertToVirtualChars(token);
                        var strictTree = JsonParser.TryParse(chars, JsonOptions.Strict);
                        var properties = strictTree != null && strictTree.Diagnostics.Length == 0
                            ? s_strictProperties
                            : ImmutableDictionary <string, string?> .Empty;

                        // Show this as a hidden diagnostic so the user can enable json features explicitly if they
                        // want, but do not spam them with a ... notification if they don't want it.
                        context.ReportDiagnostic(DiagnosticHelper.Create(
                                                     this.Descriptor,
                                                     token.GetLocation(),
                                                     ReportDiagnostic.Hidden,
                                                     additionalLocations: null,
                                                     properties));
                    }
                }
            }
        }
コード例 #4
0
        private void Analyze(
            SemanticModelAnalysisContext context,
            JsonLanguageDetector detector,
            SyntaxNode node,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            foreach (var child in node.ChildNodesAndTokens())
            {
                if (child.IsNode)
                {
                    Analyze(context, detector, child.AsNode() !, cancellationToken);
                }
                else
                {
                    var token = child.AsToken();
                    if (_info.IsAnyStringLiteral(token.RawKind))
                    {
                        var tree = detector.TryParseString(token, context.SemanticModel, includeProbableStrings: false, cancellationToken);
                        if (tree != null)
                        {
                            foreach (var diag in tree.Diagnostics)
                            {
                                context.ReportDiagnostic(DiagnosticHelper.Create(
                                                             this.Descriptor,
                                                             Location.Create(context.SemanticModel.SyntaxTree, diag.Span),
                                                             ReportDiagnostic.Warn,
                                                             additionalLocations: null,
                                                             properties: null,
                                                             diag.Message));
                            }
                        }
                    }
                }
            }
        }