Ejemplo n.º 1
0
        private string CheckAttribute(XElement element,
                                      string attributeName,
                                      string defaultValue,
                                      Func <string, bool> isGoodValue,
                                      DiagnosticDescriptor diagnosticDescriptor,
                                      XElement lastFoundElement,
                                      AdditionalText file,
                                      CompilationAnalysisContext context)
        {
            var attributeValue = element?.Attribute(attributeName);
            var value          = attributeValue?.Value ?? defaultValue;

            var v = value.Trim();

            if (isGoodValue(v))
            {
                return(v);
            }

            var lineInfo   = (IXmlLineInfo)lastFoundElement;
            int lineNumber = lastFoundElement != null && lineInfo.HasLineInfo() ? lineInfo.LineNumber : 1;

            context.ReportDiagnostic(ExternalDiagnostic.Create(diagnosticDescriptor,
                                                               file.Path,
                                                               lineNumber,
                                                               lastFoundElement.ToStringStartElement()));

            return(v);
        }
        public void AnalyzeFile(AdditionalText file, CompilationAnalysisContext context)
        {
            var text    = file.GetText();
            var content = text.ToString();

            foreach (Match match in Regex.Matches(content))
            {
                context.ReportDiagnostic(ExternalDiagnostic.Create(RuleValidateRequest,
                                                                   file.Path,
                                                                   text.Lines.GetLinePosition(match.Index).Line + 1,
                                                                   match.Value));
            }
        }