Exemple #1
0
        internal async Task ExecuteAnalysis(string filePath, IIssueConsumer consumer, CancellationToken cancellationToken)
        {
            telemetryManager.LanguageAnalyzed("js");

            // Switch to a background thread
            await TaskScheduler.Default;

            analysisStatusNotifier.AnalysisStarted(filePath);

            try
            {
                var stopwatch = Stopwatch.StartNew();
                var issues    = await eslintBridgeAnalyzer.Analyze(filePath, null, cancellationToken);

                analysisStatusNotifier.AnalysisFinished(filePath, issues.Count, stopwatch.Elapsed);

                if (issues.Any())
                {
                    consumer.Accept(filePath, issues);
                }
            }
            catch (TaskCanceledException)
            {
                analysisStatusNotifier.AnalysisCancelled(filePath);
            }
            catch (Exception ex) when(!ErrorHandler.IsCriticalException(ex))
            {
                analysisStatusNotifier.AnalysisFailed(filePath, ex);
            }
        }
Exemple #2
0
        internal /* for testing */ async Task TriggerAnalysisAsync(Request request, IIssueConsumer consumer, IAnalysisStatusNotifier statusNotifier, CancellationToken cancellationToken)
        {
            // For notes on VS threading, see https://github.com/microsoft/vs-threading/blob/master/doc/cookbook_vs.md
            // Note: we support multiple versions of VS which prevents us from using some threading helper methods
            // that are only available in newer versions of VS e.g. [Import] IThreadHandling.

            // Switch to a background thread
            await TaskScheduler.Default;

            var analysisStartTime = DateTime.Now;

            statusNotifier?.AnalysisStarted(request.File);
            int issueCount = 0;

            var handleMessage = consumer == null
                ? (Action <Message>)(message => { })
                : message => HandleMessage(message, request, consumer, ref issueCount);

            try
            {
                // We're tying up a background thread waiting for out-of-process analysis. We could
                // change the process runner so it works asynchronously. Alternatively, we could change the
                // RequestAnalysis method to be asynchronous, rather than fire-and-forget.
                CallSubProcess(handleMessage, request, settings, logger, cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    statusNotifier?.AnalysisCancelled(request.File);
                }
                else
                {
                    var analysisTime = DateTime.Now - analysisStartTime;
                    statusNotifier?.AnalysisFinished(request.File, issueCount, analysisTime);
                }
            }
            catch (Exception ex) when(!ErrorHandler.IsCriticalException(ex))
            {
                statusNotifier?.AnalysisFailed(request.File, ex);
            }

            telemetryManager.LanguageAnalyzed(request.CFamilyLanguage); // different keys for C and C++
        }
        internal async Task ExecuteAnalysis(string filePath, IIssueConsumer consumer, CancellationToken cancellationToken)
        {
            telemetryManager.LanguageAnalyzed("js");

            // Switch to a background thread
            await TaskScheduler.Default;

            analysisStatusNotifier.AnalysisStarted(filePath);

            try
            {
                await EnsureEslintBridgeClientIsInitialized(cancellationToken);

                var stopwatch        = Stopwatch.StartNew();
                var analysisResponse = await eslintBridgeClient.AnalyzeJs(filePath, cancellationToken);

                var numberOfIssues = analysisResponse.Issues?.Count() ?? 0;
                analysisStatusNotifier.AnalysisFinished(filePath, numberOfIssues, stopwatch.Elapsed);

                if (analysisResponse.ParsingError != null)
                {
                    LogParsingError(filePath, analysisResponse.ParsingError);
                    return;
                }

                var issues = ConvertIssues(filePath, analysisResponse.Issues);

                if (issues.Any())
                {
                    consumer.Accept(filePath, issues);
                }
            }
            catch (TaskCanceledException)
            {
                analysisStatusNotifier.AnalysisCancelled(filePath);
            }
            catch (Exception ex) when(!ErrorHandler.IsCriticalException(ex))
            {
                analysisStatusNotifier.AnalysisFailed(filePath, ex);
            }
        }
Exemple #4
0
        internal async Task ExecuteAnalysis(string filePath, IIssueConsumer consumer, CancellationToken cancellationToken)
        {
            telemetryManager.LanguageAnalyzed("ts");

            // Switch to a background thread
            await TaskScheduler.Default;

            analysisStatusNotifier.AnalysisStarted(filePath);

            try
            {
                var stopwatch = Stopwatch.StartNew();
                var tsConfig  = await tsConfigProvider.GetConfigForFile(filePath, cancellationToken);

                if (string.IsNullOrEmpty(tsConfig))
                {
                    analysisStatusNotifier.AnalysisFailed(filePath, Resources.ERR_NoTsConfig);
                    return;
                }
                logger.WriteLine("[TypescriptAnalyzer] time to find ts config: " + stopwatch.ElapsedMilliseconds);

                stopwatch.Restart();
                var issues = await eslintBridgeAnalyzer.Analyze(filePath, tsConfig, cancellationToken);

                analysisStatusNotifier.AnalysisFinished(filePath, issues.Count, stopwatch.Elapsed);

                if (issues.Any())
                {
                    consumer.Accept(filePath, issues);
                }
            }
            catch (TaskCanceledException)
            {
                analysisStatusNotifier.AnalysisCancelled(filePath);
            }
            catch (Exception ex) when(!ErrorHandler.IsCriticalException(ex))
            {
                analysisStatusNotifier.AnalysisFailed(filePath, ex);
            }
        }
        private async Task TriggerAnalysisAsync(Request request, IIssueConsumer consumer, CancellationToken cancellationToken)
        {
            // For notes on VS threading, see https://github.com/microsoft/vs-threading/blob/master/doc/cookbook_vs.md
            // Note: we support multiple versions of VS which prevents us from using some threading helper methods
            // that are only available in newer versions of VS e.g. [Import] IThreadHandling.

            // Switch a background thread
            await TaskScheduler.Default;

            logger.WriteLine($"Analyzing {request.File}");

            // We're tying up a background thread waiting for out-of-process analysis. We could
            // change the process runner so it works asynchronously. Alternatively, we could change the
            // RequestAnalysis method to be synchronous, rather than fire-and-forget.
            var response = CFamilyHelper.CallClangAnalyzer(request, new ProcessRunner(settings, logger), logger, cancellationToken);

            if (response != null)
            {
                Debug.Assert(response.Messages.All(m => m.Filename == request.File), "Issue for unexpected file returned");

                var issues = response.Messages
                             .Where(m => IsIssueForActiveRule(m, request.RulesConfiguration))
                             .Select(m => CFamilyHelper.ToSonarLintIssue(m, request.CFamilyLanguage, request.RulesConfiguration))
                             .ToList();

                telemetryManager.LanguageAnalyzed(request.CFamilyLanguage); // different keys for C and C++

                logger.WriteLine($"Found {issues.Count} issue(s)");

                // Switch back to the UI thread
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Note: the file being analyzed might have been closed by the time the analysis results are
                // returned. This doesn't cause a crash; all active taggers will have been detached from the
                // TextBufferIssueTracker when the file was closed, but the TextBufferIssueTracker will
                // still exist and handle the call.
                consumer.Accept(request.File, issues);
            }
        }