示例#1
0
 /// <summary>
 /// Reads the value of custom setting.
 /// </summary>
 private bool ReadSetting(ViolationEventArgs e, string settingName)
 {
     return(SettingsManager.GetValue <bool>(
                m_parent,
                e.Element.Document.Settings,
                settingName));
 }
 /// <summary>
 /// Raises the <see cref="ViolationEncountered"/> event.
 /// </summary>
 /// <param name="e">An <see cref="ViolationEventArgs"/> containing event data.</param>
 private void OnViolationEncountered(ViolationEventArgs e)
 {
     if (this.ViolationEncountered != null)
     {
         this.ViolationEncountered(this, e);
     }
 }
        void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            violationCount++;
            var path = e.SourceCode.Path.Replace(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, "");

            Console.WriteLine("{0}:L{1}: [{2}] {3}", path, e.LineNumber, e.Violation.Rule.CheckId, e.Message);
        }
        private bool IsViolationIgnored(ViolationEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            var checkId = e.Violation.Rule.CheckId;

            var alwaysIgnoredRuleIds =
                this.AlwaysIgnoredRuleIds ?? new string[0];
            var mandatoryRuleIds =
                this.MandatoryRuleIds ?? new string[0];

            var ignore = this.IgnoreVoluntaryRules;

            if (!ignore)
            {
                if (alwaysIgnoredRuleIds.Any(c => checkId == c))
                {
                    ignore = true;
                }
            }

            if (ignore)
            {
                // Do not ignore a mandatory rule
                if (mandatoryRuleIds.Any(c => checkId == c))
                {
                    ignore = false;
                }
            }

            return(ignore);
        }
示例#5
0
        /// <summary>
        /// Handles encountered custom violations.
        /// </summary>
        private void OnCustomViolationEncountered(object sender, ViolationEventArgs e)
        {
            RemoveCustomViolation(e);

            switch (e.Violation.Rule.CheckId)
            {
            case "SA1502":
                Handle1502(e);
                break;

            case "SA1513":
                Handle1513(e);
                break;

            case "SA1516":
                Handle1516(e);
                break;

            case "SA1642":
                Handle1642(e);
                break;

            case "SA1643":
                Handle1643(e);
                break;
            }
        }
示例#6
0
        /// <summary>
        /// Called when the StyleCopCore.ViolationEncountered event is raised. Converts
        /// <see cref="ViolationEventArgs"/>into ReSharper Violation.
        /// </summary>
        /// <param name="sender">
        /// Object that raised the event.
        /// </param>
        /// <param name="e">
        /// Data Structure containing information about the Violation encountered.
        /// </param>
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            string path       = e.SourceCode.Path;
            int    lineNumber = e.LineNumber;

            // if violations fire in the related files we ignore them as we only want to highlight in the current file
            if (path == this.file.Location.FullPath)
            {
                JB::JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine> line = (JB::JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine>)lineNumber;

                JB::JetBrains.Util.TextRange textRange = Utils.GetTextRange(this.file, line.Minus1());

                // The TextRange could be a completely blank line. If it is just return the line and don't trim it.
                DocumentRange documentRange = new DocumentRange(this.file, textRange);

                if (!textRange.IsEmpty)
                {
                    // Once we have a TextRange for the entire line reduce it to not include whitespace at the left or whitespace at the right
                    // if it wasn't empty
                    documentRange = Utils.TrimWhitespaceFromDocumentRange(documentRange);
                }

                string fileName = this.file.Location.Name;

                if (e.Violation.Element != null)
                {
                    fileName = e.Violation.Element.Document.SourceCode.Name;
                }

                IHighlighting violation = StyleCopViolationFactory.GetHighlight(e, documentRange, fileName, lineNumber);

                this.CreateViolation(documentRange, violation);
            }
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="solution">
        /// The current solution.
        /// </param>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ISolution solution, ViolationEventArgs violation, DocumentRange documentRange)
        {
            string ruleID      = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, solution);

            switch (severity)
            {
            case Severity.ERROR:
                return(new StyleCopHighlightingError(violation, documentRange));

            case Severity.HINT:
                return(new StyleCopHighlightingHint(violation, documentRange));

            case Severity.INFO:
                return(new StyleCopHighlightingError(violation, documentRange));

            case Severity.SUGGESTION:
                return(new StyleCopHighlightingSuggestion(violation, documentRange));

            case Severity.WARNING:
                return(new StyleCopHighlightingWarning(violation, documentRange));

            default:
                return(new StyleCopHighlightingDoNotShow(violation, documentRange));
            }
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <param name="fileName">
        /// <see cref="CsElement"/>of the violation.
        /// </param>
        /// <param name="lineNumber">
        /// Line number where the violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
        {
            string ruleID = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            VSSolutionManager solutionManager = Shell.Instance.GetComponent<VSSolutionManager>();

            IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solutionManager.CurrentSolution);

            Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, settingsStore);

            switch (severity)
            {
                case Severity.ERROR:
                    return new StyleCopHighlightingError(violation, documentRange, fileName, lineNumber);
                case Severity.HINT:
                    return new StyleCopHighlightingHint(violation, documentRange, fileName, lineNumber);
                case Severity.INFO:
                    return new StyleCopHighlightingError(violation, documentRange, fileName, lineNumber);
                case Severity.SUGGESTION:
                    return new StyleCopHighlightingSuggestion(violation, documentRange, fileName, lineNumber);
                case Severity.WARNING:
                    return new StyleCopHighlightingWarning(violation, documentRange, fileName, lineNumber);
                default:
                    return new StyleCopHighlightingDoNotShow(violation, documentRange, fileName, lineNumber);
            }
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <param name="fileName">
        /// <see cref="CsElement"/>of the violation.
        /// </param>
        /// <param name="lineNumber">
        /// Line number where the violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
        {
            string ruleID      = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            VSSolutionManager solutionManager = Shell.Instance.GetComponent <VSSolutionManager>();

            IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solutionManager.CurrentSolution);

            Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, settingsStore);

            switch (severity)
            {
            case Severity.ERROR:
                return(new StyleCopHighlightingError(violation, documentRange, fileName, lineNumber));

            case Severity.HINT:
                return(new StyleCopHighlightingHint(violation, documentRange, fileName, lineNumber));

            case Severity.INFO:
                return(new StyleCopHighlightingError(violation, documentRange, fileName, lineNumber));

            case Severity.SUGGESTION:
                return(new StyleCopHighlightingSuggestion(violation, documentRange, fileName, lineNumber));

            case Severity.WARNING:
                return(new StyleCopHighlightingWarning(violation, documentRange, fileName, lineNumber));

            default:
                return(new StyleCopHighlightingDoNotShow(violation, documentRange, fileName, lineNumber));
            }
        }
示例#10
0
        /// <summary>
        /// Callback method for when a violation is encountered.
        /// </summary>
        /// <param name="sender">
        /// The object that caused this event.
        /// </param>
        /// <param name="e">
        /// The ViolationEventArgs object that describes this violation.
        /// </param>
        private void ViolationEncountered(
            object sender,
            ViolationEventArgs e)
        {
            // Add the violation.
            var sfiles =
                this.Report.SourceCodeFiles.Where(
                    r => r.Path == e.SourceCode.Path);

            foreach (var sf in sfiles)
            {
                var lines = File.ReadAllLines(sf.Path);

                lock (this.Report)
                {
                    var violation = this.Report.Violations.AddViolationsRow(
                        e.LineNumber,
                        e.Message,
                        sf,
                        lines[e.LineNumber - 1]);

                    // Add the rule.
                    this.Report.Rules.AddRulesRow(
                        e.Violation.Rule.CheckId,
                        e.Violation.Rule.Description,
                        e.Violation.Rule.EnabledByDefault,
                        e.Violation.Rule.Name,
                        e.Violation.Rule.Namespace,
                        e.Violation.Rule.RuleGroup,
                        e.Violation.Rule.Warning,
                        violation);
                }
            }
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <param name="fileName">
        /// <see cref="CsElement"/>of the violation.
        /// </param>
        /// <param name="lineNumber">
        /// Line number where the violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
        {
            string ruleID = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            // To get the correct severity for a highlight we need to specify the current solution.
            ISolutionManager solutionManager = Shell.Instance.GetComponent<ISolutionManager>();
            Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, solutionManager.CurrentSolution);

            switch (severity)
            {
                case Severity.ERROR:
                    return new StyleCopViolationError(violation, documentRange, fileName, lineNumber);
                case Severity.HINT:
                    return new StyleCopViolationHint(violation, documentRange, fileName, lineNumber);
                case Severity.INFO:
                    return new StyleCopViolationError(violation, documentRange, fileName, lineNumber);
                case Severity.SUGGESTION:
                    return new StyleCopViolationSuggestion(violation, documentRange, fileName, lineNumber);
                case Severity.WARNING:
                    return new StyleCopViolationWarning(violation, documentRange, fileName, lineNumber);
                default:
                    return new StyleCopViolationDoNotShow(violation, documentRange, fileName, lineNumber);
            }
        }
示例#12
0
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv.SelectedIndices.Count <= 0)
            {
                return;
            }
            int          i = lv.SelectedIndices[0];
            ListViewItem v = lv.Items[i];

            if (ef == null)
            {
                return;
            }

            if (v.Tag == null)
            {
                return;
            }
            if (v.Tag.GetType() == typeof(Microsoft.Build.Framework.BuildErrorEventArgs))
            {
                Microsoft.Build.Framework.BuildErrorEventArgs b = v.Tag as Microsoft.Build.Framework.BuildErrorEventArgs;

                string project = b.ProjectFile;
                string file    = Path.GetDirectoryName(b.ProjectFile) + "\\" + b.File;
                int    c       = b.LineNumber;
                int    p       = b.ColumnNumber;
                int    es      = b.EndLineNumber;
                int    ep      = b.EndColumnNumber;

                ef.OpenFileLine(file, c.ToString(), p);
            }
            else if (v.Tag.GetType() == typeof(Microsoft.Build.Framework.BuildWarningEventArgs))
            {
                Microsoft.Build.Framework.BuildWarningEventArgs b = v.Tag as Microsoft.Build.Framework.BuildWarningEventArgs;

                string project = b.ProjectFile;
                string file    = Path.GetDirectoryName(b.ProjectFile) + "\\" + b.File;
                int    c       = b.LineNumber;
                int    p       = b.ColumnNumber;
                int    es      = b.EndLineNumber;
                int    ep      = b.EndColumnNumber;

                ef.OpenFileLine(file, c.ToString(), p);
            }
            else if (v.Tag.GetType() == typeof(ViolationEventArgs))
            {
                ViolationEventArgs b = v.Tag as ViolationEventArgs;

                string project = b.SourceCode.Path;
                string file    = Path.GetDirectoryName(project) + "\\" + b.SourceCode.Name;
                int    c       = b.LineNumber;
                int    p       = b.LineNumber;

                //ef.BeginInvoke(new Action(() => { ef.OpenFileLineHighlight(file, c.ToString(), p); }));

                AsyncCallback callBack = new AsyncCallback(ProcessInformation);
                GotoWarning   g        = Warning;
                g.BeginInvoke(file, c.ToString(), p, callBack, "null");
            }
        }
示例#13
0
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <param name="fileName">
        /// <see cref="CsElement"/>of the violation.
        /// </param>
        /// <param name="lineNumber">
        /// Line number where the violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
        {
            string ruleID      = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            Severity severity = HighlightingSettingsManager.Instance.Settings.GetSeverity(highlightID);

            switch (severity)
            {
            case Severity.ERROR:
                return(new StyleCopViolationError(violation, documentRange, fileName, lineNumber));

            case Severity.HINT:
                return(new StyleCopViolationHint(violation, documentRange, fileName, lineNumber));

            case Severity.INFO:
                return(new StyleCopViolationError(violation, documentRange, fileName, lineNumber));

            case Severity.SUGGESTION:
                return(new StyleCopViolationSuggestion(violation, documentRange, fileName, lineNumber));

            case Severity.WARNING:
                return(new StyleCopViolationWarning(violation, documentRange, fileName, lineNumber));

            default:
                return(new StyleCopViolationDoNotShow(violation, documentRange, fileName, lineNumber));
            }
        }
示例#14
0
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <param name="fileName">
        /// <see cref="CsElement"/>of the violation.
        /// </param>
        /// <param name="lineNumber">
        /// Line number where the violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
        {
            string ruleID      = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            // To get the correct severity for a highlight we need to specify the current solution.
            ISolutionManager solutionManager = Shell.Instance.GetComponent <ISolutionManager>();
            Severity         severity        = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, solutionManager.CurrentSolution);

            switch (severity)
            {
            case Severity.ERROR:
                return(new StyleCopViolationError(violation, documentRange, fileName, lineNumber));

            case Severity.HINT:
                return(new StyleCopViolationHint(violation, documentRange, fileName, lineNumber));

            case Severity.INFO:
                return(new StyleCopViolationError(violation, documentRange, fileName, lineNumber));

            case Severity.SUGGESTION:
                return(new StyleCopViolationSuggestion(violation, documentRange, fileName, lineNumber));

            case Severity.WARNING:
                return(new StyleCopViolationWarning(violation, documentRange, fileName, lineNumber));

            default:
                return(new StyleCopViolationDoNotShow(violation, documentRange, fileName, lineNumber));
            }
        }
示例#15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopHighlightingBase"/> class.
 /// </summary>
 /// <param name="violationEventArgs">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the Violation data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 protected StyleCopHighlightingBase(ViolationEventArgs violationEventArgs, DocumentRange documentRange)
 {
     this.CheckId       = violationEventArgs.Violation.Rule.CheckId;
     this.ToolTip       = violationEventArgs.Message + " [StyleCop Rule: " + this.CheckId + "]";
     this.documentRange = documentRange;
     this.Rule          = violationEventArgs.Violation.Rule;
     this.Violation     = violationEventArgs.Violation;
 }
        public void ViolationEncountered(object sender, ViolationEventArgs args)
        {
            _totalViolations++;

            Cake.Common.Diagnostics.LoggingAliases.Error(
                _context,
                string.Format("{0}: {1} @ Line {2}", args.Violation.Rule.CheckId, args.Message, args.LineNumber));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopHighlightingBase"/> class.
 /// </summary>
 /// <param name="violationEventArgs">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the Violation data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 protected StyleCopHighlightingBase(ViolationEventArgs violationEventArgs, DocumentRange documentRange)
 {
     this.CheckId = violationEventArgs.Violation.Rule.CheckId;
     this.ToolTip = violationEventArgs.Message + " [StyleCop Rule: " + this.CheckId + "]";
     this.documentRange = documentRange;
     this.Rule = violationEventArgs.Violation.Rule;
     this.Violation = violationEventArgs.Violation;
 }
示例#18
0
 private void OnViolationEncountered(object sender, ViolationEventArgs e)
 {
     if (e.SourceCode != null)
     {
         var violation = new Violation(e.Violation.Rule.CheckId, e.Message, e.LineNumber);
         this.violations.AddViolationToFile(e.SourceCode.Path, violation);
     }
 }
示例#19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopViolationBase"/> class.
 /// </summary>
 /// <param name="violation">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the Violation data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 /// <param name="fileName">
 /// The file Name.
 /// </param>
 /// <param name="lineNumber">
 /// Line number of where the violation happened.
 /// </param>
 protected StyleCopViolationBase(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
 {
     this.CheckId       = violation.Violation.Rule.CheckId;
     this.ToolTip       = violation.Message + " [StyleCop Rule: " + this.CheckId + "]";
     this.DocumentRange = documentRange;
     this.FileName      = fileName;
     this.LineNumber    = lineNumber;
     this.Rule          = violation.Violation.Rule;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopViolationBase"/> class.
 /// </summary>
 /// <param name="violation">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the Violation data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 /// <param name="fileName">
 /// The file Name.
 /// </param>
 /// <param name="lineNumber">
 /// Line number of where the violation happened.
 /// </param>
 protected StyleCopViolationBase(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
 {
     this.CheckId = violation.Violation.Rule.CheckId;
     this.ToolTip = violation.Message + " [StyleCop Rule: " + this.CheckId + "]";
     this.DocumentRange = documentRange;
     this.FileName = fileName;
     this.LineNumber = lineNumber;
     this.Rule = violation.Violation.Rule;
 }
示例#21
0
        /// <summary>
        /// Removes violation got from the custom analyzer.
        /// </summary>
        public static void RemoveCustomViolation(ViolationEventArgs e)
        {
            Dictionary <int, Violation> violations = (Dictionary <int, Violation>) typeof(CsElement).InvokeMember(
                "violations",
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField,
                null,
                e.Element,
                null);

            violations.Remove(e.Violation.Key);
        }
示例#22
0
        private static void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (!m_LastPrinted)
            {
                m_LastPrinted = true;
                Log(e.SourceCode.Path);
            }

            _encounteredViolations++;
            WriteLineViolationMessage(string.Format("  Line {0}: {1} ({2})", e.LineNumber, e.Message,
                                                    e.Violation.Rule.CheckId));
        }
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            List <Violation> list;

            if (!this.violations.TryGetValue(e.Violation.Rule.CheckId, out list))
            {
                list = new List <Violation>();
                this.violations.Add(e.Violation.Rule.CheckId, list);
            }

            list.Add(e.Violation);
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="solution">
        /// The current solution.
        /// </param>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ISolution solution, ViolationEventArgs violation, DocumentRange documentRange)
        {
            string ruleID      = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            if (ruleID == "SA0102") // Syntax error from parser
            {
                return(new StyleCopErrorHighlighting(violation, documentRange));
            }

            Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, sourceFile: null, solution: solution);

            return(new StyleCopHighlighting(violation, documentRange, severity, highlightID));
        }
示例#25
0
        /// <summary>
        /// Called when a violation is found.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void CoreViolationEncountered(object sender, ViolationEventArgs e)
        {
            Param.AssertNotNull(e, "e");
            Param.Ignore(sender);

            // Make sure this is running on the main thread.
            if (InvisibleForm.Instance.InvokeRequired)
            {
                EventHandler <ViolationEventArgs> violationDelegate = this.CoreViolationEncountered;
                InvisibleForm.Instance.Invoke(violationDelegate, sender, e);
            }
            else
            {
                if (!e.Warning)
                {
                    ++this.violationCount;
                }

                // Check the count. At some point we don't allow any more violations
                // so we cancel the analyze run.
                if (this.maxViolationCount > 0 && this.violationCount == this.maxViolationCount)
                {
                    this.Cancel();
                }

                ICodeElement  element   = e.Element;
                ViolationInfo violation = new ViolationInfo();
                violation.Description = string.Concat(e.Violation.Rule.CheckId, ": ", e.Message);
                violation.LineNumber  = e.LineNumber;
                violation.Rule        = e.Violation.Rule;

                if (element != null)
                {
                    violation.File = element.Document.SourceCode.Path;
                }
                else
                {
                    string file = string.Empty;
                    if (e.SourceCode != null)
                    {
                        file = e.SourceCode.Path;
                    }

                    violation.File = file;
                }

                this.violations.Add(violation);
            }
        }
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (this.IsViolationIgnored(e))
            {
                return;
            }

            if (this.violationLimit >= 0 && this.violationCount >= this.violationLimit)
            {
                return;
            }

            this.violationCount++;

            // Does the violation qualify for breaking the build?
            if (!(e.Warning || this.inputTreatErrorsAsWarnings))
            {
                this.succeeded = false;
            }

            string path = string.Empty;

            if (e.SourceCode != null && !string.IsNullOrEmpty(e.SourceCode.Path))
            {
                path = e.SourceCode.Path;
            }
            else if (e.Element != null &&
                     e.Element.Document != null &&
                     e.Element.Document.SourceCode != null &&
                     e.Element.Document.SourceCode.Path != null)
            {
                path = e.Element.Document.SourceCode.Path;
            }

            // Prepend the rule check-id to the message.
            string message = string.Concat(e.Violation.Rule.CheckId, ": ", e.Message);

            lock (this)
            {
                if (e.Warning || this.inputTreatErrorsAsWarnings)
                {
                    this.Log.LogWarning(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, message);
                }
                else
                {
                    this.Log.LogError(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, message);
                }
            }
        }
示例#27
0
        /// <summary>
        /// Handles encountered violations.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="ViolationEventArgs"/> instance containing the event data.</param>
        private static void OnViolationEncountered(
            object sender,
            ViolationEventArgs e)
        {
            string elementName = (null != e.Element) ? e.Element.FullyQualifiedName : string.Empty;

            Console.WriteLine(
                "{0},{1},{2},{3},{4},{5}",
                e.Violation.Rule.CheckId,
                e.Message.Replace(',', ' '),
                e.SourceCode.Path.Replace(',', ' '),
                e.Violation.Rule.Namespace.Replace(',', ' '),
                elementName.Replace(',', ' '),
                e.LineNumber);
        }
示例#28
0
        /// <summary>
        /// Called when a violation is encoutered while running under fuzz test mode.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private static void OnFuzzViolationEncountered(object sender, ViolationEventArgs e)
        {
            string checkId = e.Violation.Rule.CheckId;

            if (checkId == "SA0001" || checkId == "SA0101")
            {
                issues++;
                string id   = String.Format("{0:0000}", issues);
                string path = e.Violation.SourceCode.Path;

                Console.WriteLine("{0} {1}: {2} {3}", id, checkId, path, e.Violation.Message);

                string saveAs = String.Format("ViolationFiles\\{0}.cs", id);
                SaveViolationFile(path, saveAs, e.Violation.Line);
            }
        }
示例#29
0
        /// <summary>
        /// Handles SA1516 violation.
        /// </summary>
        private void Handle1516(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            if (ReadSetting(e, AllowJoinedAccessorsFor1516))
            {
                if (element.ElementType == ElementType.Accessor)
                {
                    return;
                }
            }

            m_parent.AddViolation(
                element,
                Rules.ElementsMustBeSeparatedByBlankLine);
        }
示例#30
0
        /// <summary>
        /// Handles SA1643 violation.
        /// </summary>
        private void Handle1643(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            string text = CodeHelper.GetSummaryText(element);

            if (text == Resources.StandardDestructorSummaryText)
            {
                return;
            }

            m_parent.AddViolation(
                element,
                Rules.DestructorSummaryDocumentationMustBeginWithStandardText,
                new object[] { GetExampleSummaryTextForDestructor(m_customDocumentationAnalyzer) });
        }
示例#31
0
        /// <summary>
        /// Handles SA1513 violation.
        /// </summary>
        private void Handle1513(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            if (ReadSetting(e, AllowJoinedAccessorsFor1513))
            {
                if (element.ElementType == ElementType.Accessor)
                {
                    return;
                }
            }

            m_parent.AddViolation(
                element,
                e.LineNumber,
                Rules.ClosingCurlyBracketMustBeFollowedByBlankLine);
        }
        /// <summary>
        /// Called when the StyleCopCore.ViolationEncountered event is raised. Converts
        /// <see cref="ViolationEventArgs"/>into ReSharper Violation.
        /// </summary>
        /// <param name="sender">
        /// Object that raised the event.
        /// </param>
        /// <param name="e">
        /// Data Structure containing information about the Violation encountered.
        /// </param>
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (e == null || e.SourceCode == null || e.SourceCode.Path == null || e.Violation == null)
            {
                return;
            }

            if (this.file == null || this.file.Location == null)
            {
                return;
            }

            string path       = e.SourceCode.Path;
            int    lineNumber = e.LineNumber;

            // if violations fire in the related files we ignore them as we only want to highlight in the current file
            if (path == this.file.Location.FullPath)
            {
                JetBrains.Util.TextRange textRange;

                if (e.Violation.Location == null)
                {
                    textRange = Utils.GetTextRange(this.file, ((JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine>)lineNumber).Minus1());
                }
                else
                {
                    textRange = Utils.GetTextRange(this.file, e.Violation.Location.Value);
                }

                // The TextRange could be a completely blank line. If it is just return the line and don't trim it.
                DocumentRange documentRange = new DocumentRange(this.document, textRange);

                if (!textRange.IsEmpty)
                {
                    // Once we have a TextRange for the entire line reduce it to not include whitespace at the left or whitespace at the right
                    // if it wasn't empty
                    documentRange = Utils.TrimWhitespaceFromDocumentRange(documentRange);
                }

                ISolution solution = this.file.GetSolution();

                IHighlighting violation = StyleCopHighlightingFactory.GetHighlight(solution, e, documentRange);

                this.CreateViolation(documentRange, violation);
            }
        }
        /// <summary>
        /// Occurs when a source analysis violation is encountered during the analysis.
        /// </summary>
        /// <param name="sender">The <see cref="System.Object"/> that sent the event.</param>
        /// <param name="e">An <see cref="ViolationEventArgs"/> containing event data.</param>
        private void ViolationEncounteredCallback(object sender, ViolationEventArgs e)
        {
            Collection <Violation> violations = null;

            string path = e.Violation.SourceCode.Path;

            if (!this.cache.ContainsKey(path))
            {
                violations = new Collection <Violation>();
                this.cache.Add(path, violations);
            }
            else
            {
                violations = this.cache[path];
            }

            violations.Add(e.Violation);
        }
示例#34
0
        /// <summary>
        /// Handles SA1502 violation.
        /// </summary>
        private void Handle1502(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            if (ReadSetting(e, AllowConstructorsFor1502))
            {
                if (element.ElementType == ElementType.Constructor)
                {
                    return;
                }
            }

            m_parent.AddViolation(
                element,
                e.LineNumber,
                Rules.ElementMustNotBeOnSingleLine,
                element.FriendlyTypeText);
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="solution">
        /// The current solution.
        /// </param>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ISolution solution, ViolationEventArgs violation, DocumentRange documentRange)
        {
            string ruleID = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, solution);

            switch (severity)
            {
                case Severity.ERROR:
                    return new StyleCopHighlightingError(violation, documentRange);
                case Severity.HINT:
                    return new StyleCopHighlightingHint(violation, documentRange);
                case Severity.INFO:
                    return new StyleCopHighlightingError(violation, documentRange);
                case Severity.SUGGESTION:
                    return new StyleCopHighlightingSuggestion(violation, documentRange);
                case Severity.WARNING:
                    return new StyleCopHighlightingWarning(violation, documentRange);
                default:
                    return new StyleCopHighlightingDoNotShow(violation, documentRange);
            }
        }
        /// <summary>
        /// Gets the highlight for the specified StyleCop Violation.
        /// </summary>
        /// <param name="violation">
        /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
        /// </param>
        /// <param name="documentRange">
        /// <see cref="DocumentRange"/>where the Violation happened.
        /// </param>
        /// <param name="fileName">
        /// <see cref="CsElement"/>of the violation.
        /// </param>
        /// <param name="lineNumber">
        /// Line number where the violation happened.
        /// </param>
        /// <returns>
        /// An <see cref="IHighlighting"/> for the specified Violation.
        /// </returns>
        public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
        {
            string ruleID = violation.Violation.Rule.CheckId;
            string highlightID = HighlightingRegistering.GetHighlightID(ruleID);

            Severity severity = HighlightingSettingsManager.Instance.Settings.GetSeverity(highlightID);

            switch (severity)
            {
                case Severity.ERROR:
                    return new StyleCopViolationError(violation, documentRange, fileName, lineNumber);
                case Severity.HINT:
                    return new StyleCopViolationHint(violation, documentRange, fileName, lineNumber);
                case Severity.INFO:
                    return new StyleCopViolationError(violation, documentRange, fileName, lineNumber);
                case Severity.SUGGESTION:
                    return new StyleCopViolationSuggestion(violation, documentRange, fileName, lineNumber);
                case Severity.WARNING:
                    return new StyleCopViolationWarning(violation, documentRange, fileName, lineNumber);
                default:
                    return new StyleCopViolationDoNotShow(violation, documentRange, fileName, lineNumber);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopHighlightingSuggestion"/> class.
 /// </summary>
 /// <param name="violationEventArgs">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 /// <param name="fileName">
 /// The file Name.
 /// </param>
 /// <param name="lineNumber">
 /// Line number of the violation.
 /// </param>
 public StyleCopHighlightingSuggestion(ViolationEventArgs violationEventArgs, DocumentRange documentRange, string fileName, int lineNumber)
     : base(violationEventArgs, documentRange, fileName, lineNumber)
 {
 }
        private bool IsViolationIgnored(ViolationEventArgs e)
        {
            if (e == null) throw new ArgumentNullException("e");

            var checkId = e.Violation.Rule.CheckId;

            var alwaysIgnoredRuleIds =
                this.AlwaysIgnoredRuleIds ?? new string[0];
            var mandatoryRuleIds =
                this.MandatoryRuleIds ?? new string[0];

            var ignore = this.IgnoreVoluntaryRules;
            if (!ignore)
            {
                if (alwaysIgnoredRuleIds.Any(c => checkId == c))
                {
                    ignore = true;
                }
            }

            if (ignore)
            {
                // Do not ignore a mandatory rule
                if (mandatoryRuleIds.Any(c => checkId == c))
                {
                    ignore = false;
                }
            }

            return ignore;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopHighlightingWarning"/> class.
 /// </summary>
 /// <param name="violationEventArgs">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 public StyleCopHighlightingWarning(ViolationEventArgs violationEventArgs, DocumentRange documentRange)
     : base(violationEventArgs, documentRange)
 {
 }
示例#40
0
        /// <summary>
        /// Called when the StyleCopCore.ViolationEncountered event is raised. Converts
        /// <see cref="ViolationEventArgs"/>into ReSharper Violation.
        /// </summary>
        /// <param name="sender">
        /// Object that raised the event.
        /// </param>
        /// <param name="e">
        /// Data Structure containing information about the Violation encountered.
        /// </param>
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (e == null || e.SourceCode == null || e.SourceCode.Path == null || e.Violation == null)
            {
                return;
            }

            if (this.file == null || this.file.Location == null)
            {
                return;
            }

            string path = e.SourceCode.Path;
            int lineNumber = e.LineNumber;

            // if violations fire in the related files we ignore them as we only want to highlight in the current file
            if (path == this.file.Location.FullPath)
            {
                JetBrains.Util.TextRange textRange;

                if (e.Violation.Location == null)
                {
                    textRange = Utils.GetTextRange(this.file, ((JetBrains.Util.dataStructures.TypedIntrinsics.Int32<DocLine>)lineNumber).Minus1());
                }
                else
                {
                    textRange = Utils.GetTextRange(this.file, e.Violation.Location.Value);
                }

                // The TextRange could be a completely blank line. If it is just return the line and don't trim it.
                DocumentRange documentRange = new DocumentRange(this.document, textRange);

                if (!textRange.IsEmpty)
                {
                    // Once we have a TextRange for the entire line reduce it to not include whitespace at the left or whitespace at the right
                    // if it wasn't empty
                    documentRange = Utils.TrimWhitespaceFromDocumentRange(documentRange);
                }

                string fileName = this.file.Location.Name;

                if (e.Violation.Element != null && e.Violation.Element.Document != null && e.Violation.Element.Document.SourceCode != null
                    && e.Violation.Element.Document.SourceCode.Name != null)
                {
                    fileName = e.Violation.Element.Document.SourceCode.Name;
                }

                IHighlighting violation = StyleCopHighlightingFactory.GetHighlight(e, documentRange, fileName, lineNumber);

                this.CreateViolation(documentRange, violation);
            }
        }
 private void OnViolationEncountered(object sender, ViolationEventArgs e)
 {
     this.Succeeded = false;
     string file = string.Empty;
     if ((e.SourceCode != null) && (!string.IsNullOrEmpty(e.SourceCode.Path)))
     {
         file = e.SourceCode.Path;
     }
     else if (((e.Element != null) && (e.Element.Document != null)) && ((e.Element.Document.SourceCode != null) && (e.Element.Document.SourceCode.Path != null)))
     {
         file = e.Element.Document.SourceCode.Path;
     }
     
     ITaskItem item = new TaskItem(file);
     item.SetMetadata("CheckId", e.Violation.Rule.CheckId ?? string.Empty);
     item.SetMetadata("RuleDescription", e.Violation.Rule.Description ?? string.Empty);
     item.SetMetadata("RuleName", e.Violation.Rule.Name ?? string.Empty);
     item.SetMetadata("RuleGroup", e.Violation.Rule.RuleGroup ?? string.Empty);
     item.SetMetadata("LineNumber", e.LineNumber.ToString(CultureInfo.CurrentCulture));
     item.SetMetadata("Message", e.Message ?? string.Empty);
     this.failedFiles.Add(item);
 }
    /// <summary>
    /// Called when a violation is found.
    /// </summary>
    /// <param name="sender">The event sender.</param>
    /// <param name="e">The event arguments.</param>
    internal static void StyleCopCoreViolationEncountered(object sender, ViolationEventArgs e)
    {
      Param.Ignore(sender, e);

      // Check the violation count. At some point we don't allow any more violations so we cancel the analyze run.
      if (e.SourceCode.Project.MaxViolationCount > 0 && styleCopAnalysisResultList.Count == e.SourceCode.Project.MaxViolationCount)
      {
        IdeApp.ProjectOperations.CancelStyleCopAnalysis();
      }

      string trimmedNamespace = e.Violation.Rule.Namespace;
      string searchValue = global::StyleCop.Constants.ProductName + ".";

      int indexOfProductName = trimmedNamespace.IndexOf(searchValue, StringComparison.Ordinal);
      if (indexOfProductName != -1 && indexOfProductName < trimmedNamespace.Length - 1)
      {
        trimmedNamespace = trimmedNamespace.Substring(indexOfProductName + searchValue.Length);

        int indexOfRulesString = trimmedNamespace.LastIndexOf("Rules", StringComparison.Ordinal);
        trimmedNamespace = indexOfRulesString != -1 ? trimmedNamespace.Substring(0, indexOfRulesString) : trimmedNamespace;
      }
      else
      {
        trimmedNamespace = string.Empty;
      }

      string fileName = string.Empty;
      if (e.Element != null)
      {
        fileName = e.Element.Document.SourceCode.Path;
      }
      else
      {
        if (e.SourceCode != null)
        {
          fileName = e.SourceCode.Path;
        }
      }

      TaskSeverity severity = TaskSeverity.Error;
      if (e.Warning || !e.SourceCode.Project.ViolationsAsErrors)
      {
        severity = TaskSeverity.Warning;
      }

      Task styleCopWarning = new Task(
                               fileName,
                               string.Concat(e.Violation.Rule.CheckId, " : ", trimmedNamespace, " : ", e.Message),
                               e.Location != null ? e.Location.Value.StartPoint.IndexOnLine : 1,
                               e.LineNumber,
                               severity,
                               TaskPriority.Normal,
                               ProjectUtilities.Instance.CachedProjects.GetProjectForFile(fileName),
                               ProjectOperationsExtensions.ownerObject);

      styleCopAnalysisResultList.Add(styleCopWarning);
    }
示例#43
0
 private static void OnViolationEncountered(object sender, ViolationEventArgs e)
 {
     _encounteredViolations++;
     WriteLineViolationMessage(string.Format("  Line {0}: {1} ({2})", e.LineNumber, e.Message,
         e.Violation.Rule.CheckId));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopHighlightingDoNotShow"/> class.
 /// </summary>
 /// <param name="violationEventArgs">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 public StyleCopHighlightingDoNotShow(ViolationEventArgs violationEventArgs, DocumentRange documentRange)
     : base(violationEventArgs, documentRange)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopHighlightingBase"/> class.
 /// </summary>
 /// <param name="violationEventArgs">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the Violation data.
 /// </param>
 protected StyleCopHighlightingBase(ViolationEventArgs violationEventArgs)
 {
     this.CheckId = violationEventArgs.Violation.Rule.CheckId;
     this.ToolTip = violationEventArgs.Message + " [StyleCop Rule: " + this.CheckId + "]";
 }
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (this.IsViolationIgnored(e))
            {
                return;
            }

            if (this.violationLimit >= 0 && this.violationCount >= this.violationLimit)
            {
                return;
            }

            this.violationCount++;

            // Does the violation qualify for breaking the build?
            if (!(e.Warning || this.inputTreatErrorsAsWarnings))
            {
                this.succeeded = false;
            }

            string path = string.Empty;
            if (e.SourceCode != null && !string.IsNullOrEmpty(e.SourceCode.Path))
            {
                path = e.SourceCode.Path;
            }
            else if (e.Element != null &&
                     e.Element.Document != null &&
                     e.Element.Document.SourceCode != null &&
                     e.Element.Document.SourceCode.Path != null)
            {
                path = e.Element.Document.SourceCode.Path;
            }

            // Prepend the rule check-id to the message.
            string message = string.Concat(e.Violation.Rule.CheckId, ": ", e.Message);

            lock (this)
            {
                if (e.Warning || this.inputTreatErrorsAsWarnings)
                {
                    this.Log.LogWarning(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, message);
                }
                else
                {
                    this.Log.LogError(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, message);
                }
            }
        }
示例#47
0
        /// <summary>
        /// Called when a violation is found.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void CoreViolationEncountered(object sender, ViolationEventArgs e)
        {
            Param.AssertNotNull(e, "e");
            Param.Ignore(sender);

            // Make sure this is running on the main thread.
            if (InvisibleForm.Instance.InvokeRequired)
            {
                EventHandler<ViolationEventArgs> violationDelegate = this.CoreViolationEncountered;
                InvisibleForm.Instance.Invoke(violationDelegate, sender, e);
            }
            else
            {
                if (!e.Warning)
                {
                    ++this.violationCount;
                }

                // Check the count. At some point we don't allow any more violations 
                // so we cancel the analyze run.
                if (this.maxViolationCount > 0 && this.violationCount == this.maxViolationCount)
                {
                    this.Cancel();
                }

                ICodeElement element = e.Element;
                ViolationInfo violation = new ViolationInfo();
                violation.Description = string.Concat(e.Violation.Rule.CheckId, ": ", e.Message);
                violation.LineNumber = e.LineNumber;
                violation.Rule = e.Violation.Rule;

                if (element != null)
                {
                    violation.File = element.Document.SourceCode.Path;
                }
                else
                {
                    string file = string.Empty;
                    if (e.SourceCode != null)
                    {
                        file = e.SourceCode.Path;
                    }

                    violation.File = file;
                }

                this.violations.Add(violation);
            }
        }
示例#48
0
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (this.violationLimit < 0 || this.violations.Count < this.violationLimit)
            {
                // Does the violation qualify for breaking the build?
                if (!(e.Warning || this.TreatViolationsErrorsAsWarnings.Get(this.ActivityContext)))
                {
                    this.exitCode = false;
                }

                string file = string.Empty;
                if (e.SourceCode != null && !string.IsNullOrEmpty(e.SourceCode.Path))
                {
                    file = e.SourceCode.Path;
                }
                else if (e.Element != null &&
                    e.Element.Document != null &&
                    e.Element.Document.SourceCode != null &&
                    e.Element.Document.SourceCode.Path != null)
                {
                    file = e.Element.Document.SourceCode.Path;
                }

                file += string.Format(CultureInfo.CurrentUICulture, ". LineNumber: {0}, ", e.LineNumber.ToString(CultureInfo.CurrentCulture));
                file += string.Format(CultureInfo.CurrentUICulture, "CheckId: {0}, ", e.Violation.Rule.CheckId ?? string.Empty);
                file += string.Format(CultureInfo.CurrentUICulture, "Message: {0}, ", e.Message);
                this.violations.Add(file);

                // Prepend the rule check-id to the message.
                string message = string.Concat(e.Violation.Rule.CheckId ?? "NoRuleCheckId", ": ", e.Message);

                lock (this)
                {
                    if (e.Warning || this.TreatViolationsErrorsAsWarnings.Get(this.ActivityContext))
                    {
                        this.LogBuildWarning(string.Format(CultureInfo.CurrentCulture, "{0} [{1}] Line {2}", message, file, e.LineNumber));
                    }
                    else
                    {
                        this.LogBuildError(string.Format(CultureInfo.CurrentCulture, "{0} [{1}] Line {2}", message, file, e.LineNumber));
                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopViolationInfo"/> class.
 /// </summary>
 /// <param name="violation">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 /// <param name="fileName">
 /// The file Name.
 /// </param>
 /// <param name="lineNumber">
 /// Line number of the violation.
 /// </param>
 public StyleCopViolationInfo(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
     : base(violation, documentRange, fileName, lineNumber)
 {
 }
示例#50
0
        /// <summary>
        /// Handles the ViolationEncounterered event.
        /// </summary>
        /// <param name="sender">The event firer.</param>
        /// <param name="e">The encountered violation.</param>
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            if (!e.Warning && !this.inputTreatErrorsAsWarnings)
            {
                this.succeeded = false;
            }

            string file = string.Empty;

            if (((e.SourceCode != null) && (e.SourceCode.Path != null)) && (e.SourceCode.Path.Length > 0))
            {
                file = e.SourceCode.Path;
            }
            else if (((e.Element != null) && (e.Element.Document != null)) && ((e.Element.Document.SourceCode != null) && (e.Element.Document.SourceCode.Path != null)))
            {
                file = e.Element.Document.SourceCode.Path;
            }

            lock (this)
            {
                if (e.Warning || this.inputTreatErrorsAsWarnings)
                {
                    this.Log.LogWarning(null, null, null, file, e.LineNumber, 1, 0, 0, e.Message, new object[0]);
                }
                else
                {
                    this.Log.LogError(null, null, null, file, e.LineNumber, 1, 0, 0, e.Message, new object[0]);
                }
            }
        }
示例#51
0
        /// <summary>
        /// Called when a violation is found.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void CoreViolationEncountered(object sender, ViolationEventArgs e)
        {
            Param.AssertNotNull(e, "e");
            Param.Ignore(sender);

            // Make sure this is running on the main thread.
            if (InvisibleForm.Instance.InvokeRequired)
            {
                EventHandler<ViolationEventArgs> violationDelegate = this.CoreViolationEncountered;
                InvisibleForm.Instance.Invoke(violationDelegate, sender, e);
            }
            else
            {
                // Check the violation only occured in the file we were analysing (or we were analysing a solution/project/folder)
                var sourceCodePath = e.SourceCode.Path;
                var documentFullName = this.analysisFilePath;

                if ((this.analysisType == AnalysisType.File && sourceCodePath.Equals(documentFullName, StringComparison.OrdinalIgnoreCase))
                    || this.analysisType == AnalysisType.Folder || this.analysisType == AnalysisType.Project || this.analysisType == AnalysisType.Solution
                    || this.analysisType == AnalysisType.Item)
                {
                    if (!e.Warning)
                    {
                        ++this.violationCount;
                    }

                    // Check the count. At some point we don't allow any more violations so we cancel the analyze run.
                    if (e.SourceCode.Project.MaxViolationCount > 0 && this.violationCount == e.SourceCode.Project.MaxViolationCount)
                    {
                        this.Cancel();
                    }

                    var element = e.Element;
                    var violationInfo = new ViolationInfo();

                    violationInfo.Severity = e.SourceCode.Project.ViolationsAsErrors ? TaskErrorCategory.Error : TaskErrorCategory.Warning;

                    var trimmedNamespace = e.Violation.Rule.Namespace.SubstringAfter(StyleCop.Constants.ProductName + ".", StringComparison.Ordinal);
                    trimmedNamespace = trimmedNamespace.SubstringBeforeLast("Rules", StringComparison.Ordinal);

                    violationInfo.Description = string.Concat(e.Violation.Rule.CheckId, " : ", trimmedNamespace, " : ", e.Message);
                    violationInfo.LineNumber = e.LineNumber;

                    violationInfo.ColumnNumber = e.Location != null ? e.Location.Value.StartPoint.IndexOnLine : 1;

                    violationInfo.Rule = e.Violation.Rule;

                    if (element != null)
                    {
                        violationInfo.File = element.Document.SourceCode.Path;
                    }
                    else
                    {
                        string file = string.Empty;
                        if (e.SourceCode != null)
                        {
                            file = e.SourceCode.Path;
                        }

                        violationInfo.File = file;
                    }

                    this.violations.Add(violationInfo);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopViolationBase"/> class.
 /// </summary>
 /// <param name="violation">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the Violation data.
 /// </param>
 protected StyleCopViolationBase(ViolationEventArgs violation)
 {
     this.CheckId = violation.Violation.Rule.CheckId;
     this.ToolTip = violation.Message + " [StyleCop Rule: " + this.CheckId + "]";
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StyleCopViolationHint"/> class.
 /// </summary>
 /// <param name="violation">
 /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data.
 /// </param>
 /// <param name="documentRange">
 /// Range where the Violation happened.
 /// </param>
 /// <param name="fileName">
 /// The file Name.
 /// </param>
 /// <param name="lineNumber">
 /// Line number of the violation.
 /// </param>
 public StyleCopViolationHint(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber)
     : base(violation, documentRange, fileName, lineNumber)
 {
     this.DocumentRange = documentRange;
 }
 /// <summary>
 /// Handle Violation reporting from StyleCop
 /// </summary>
 /// <param name="sender">Event source</param>
 /// <param name="e">Arguments supplied</param>
 private void ViolationEncountered(object sender, ViolationEventArgs e)
 {
     mViolations.Add(e.Violation);
 }