private static void CheckCompanyName(SyntaxTreeAnalysisContext context, DocumentationSettings documentationSettings, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement) { var companyName = copyrightElement.Attribute("company")?.Value; if (string.IsNullOrWhiteSpace(companyName)) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1640Descriptor, location)); return; } if (compilation.IsAnalyzerSuppressed(SA1641Descriptor)) { return; } if (string.Equals(documentationSettings.CompanyName, DocumentationSettings.DefaultCompanyName, StringComparison.OrdinalIgnoreCase)) { // The company name is meaningless until configured by the user. return; } if (string.CompareOrdinal(companyName, documentationSettings.CompanyName) != 0) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1641Descriptor, location)); } }
private static void CheckSummaryHeader(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader) { var summaryElement = fileHeader.GetElement("summary"); if (summaryElement == null) { context.ReportDiagnostic(Diagnostic.Create(SA1639Descriptor, fileHeader.GetLocation(context.Tree))); return; } if (string.IsNullOrWhiteSpace(summaryElement.Value)) { var location = fileHeader.GetElementLocation(context.Tree, summaryElement); context.ReportDiagnostic(Diagnostic.Create(SA1639Descriptor, location)); } }
private static void CheckCopyrightText(SyntaxTreeAnalysisContext context, DocumentationSettings documentationSettings, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement) { var copyrightText = copyrightElement.Value; if (string.IsNullOrWhiteSpace(copyrightText)) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1635Descriptor, location)); return; } if (compilation.IsAnalyzerSuppressed(SA1636Descriptor)) { return; } string fileName = Path.GetFileName(context.Tree.FilePath); var settingsCopyrightText = documentationSettings.GetCopyrightText(fileName); if (string.Equals(settingsCopyrightText, DocumentationSettings.DefaultCopyrightText, StringComparison.OrdinalIgnoreCase)) { // The copyright text is meaningless until the company name is configured by the user. return; } // trim any leading / trailing new line or whitespace characters (those are a result of the XML formatting) if (!CompareCopyrightText(context, documentationSettings, copyrightText.Trim('\r', '\n', ' ', '\t'))) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1636Descriptor, location)); } }
private static void CheckFile(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement) { var fileAttribute = copyrightElement.Attribute("file"); if (fileAttribute == null) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1637Descriptor, location)); return; } if (compilation.IsAnalyzerSuppressed(SA1638Descriptor)) { return; } var fileName = Path.GetFileName(context.Tree.FilePath); if (string.CompareOrdinal(fileAttribute.Value, fileName) != 0) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1638Descriptor, location)); } }
private static void CheckCopyrightHeader(SyntaxTreeAnalysisContext context, DocumentationSettings documentationSettings, Compilation compilation, XmlFileHeader fileHeader) { var copyrightElement = fileHeader.GetElement("copyright"); if (copyrightElement == null) { context.ReportDiagnostic(Diagnostic.Create(SA1634Descriptor, fileHeader.GetLocation(context.Tree))); return; } if (!compilation.IsAnalyzerSuppressed(SA1637Descriptor)) { CheckFile(context, compilation, fileHeader, copyrightElement); } if (!compilation.IsAnalyzerSuppressed(SA1640Descriptor)) { CheckCompanyName(context, documentationSettings, compilation, fileHeader, copyrightElement); } if (!compilation.IsAnalyzerSuppressed(SA1635Descriptor)) { CheckCopyrightText(context, documentationSettings, compilation, fileHeader, copyrightElement); } }
private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document document, SyntaxNode root, StyleCopSettings settings, int commentIndex, XmlFileHeader header) { SyntaxTriviaList trivia = root.GetLeadingTrivia(); var commentTrivia = trivia[commentIndex]; // Is the comment pushed in by a prefix? var commentIndentation = string.Empty; if (commentIndex > 0) { var prefixTrivia = trivia[commentIndex - 1]; if (prefixTrivia.IsKind(SyntaxKind.WhitespaceTrivia)) { commentIndentation = prefixTrivia.ToFullString(); } } var triviaString = commentTrivia.ToFullString(); var startIndex = triviaString.IndexOf("/*", StringComparison.Ordinal) + 2; var endIndex = triviaString.LastIndexOf("*/", StringComparison.Ordinal); var commentContext = triviaString.Substring(startIndex, endIndex - startIndex).Trim(' ', '\t').TrimEnd(); var triviaStringParts = commentContext.Replace("\r\n", "\n").Split('\n'); // Assume we have comments that have a leading * string interlinePadding = " *"; int minExpectedLength = (commentIndentation + interlinePadding).Length; string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp); // Examine second line to see if we should have stars or not if it's blank // set the interline padding to be blank also. if ((triviaStringParts.Length > 2) && (triviaStringParts[1].Length > minExpectedLength) && string.IsNullOrWhiteSpace(triviaStringParts[1].Substring(0, minExpectedLength))) { interlinePadding = " "; } // Pad line that used to be next to a /* triviaStringParts[0] = commentIndentation + interlinePadding + " " + triviaStringParts[0]; StringBuilder sb = StringBuilderPool.Allocate(); string fileName = GetFileName(document); var copyrightText = GetCopyrightText(commentIndentation + interlinePadding, settings.DocumentationRules.GetCopyrightText(fileName), newLineText); var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, fileName, settings, newLineText); sb.Append(commentIndentation); sb.Append("/*"); if (header.GetElement("copyright") == null) { // No copyright element at the moment so add us. sb.Append(newHeader.Substring(minExpectedLength)); sb.Append(newLineText); // Append the original stuff foreach (var oldLine in triviaStringParts) { sb.Append(oldLine.TrimEnd()); sb.Append(newLineText); } } else { bool firstLine = true; bool inCopyright = false; foreach (var oldLine in triviaStringParts) { var openingTag = oldLine.Contains("<copyright "); var closingTag = oldLine.Contains("</copyright>") || (openingTag && oldLine.Trim().EndsWith("/>")); if (openingTag) { inCopyright = !closingTag; sb.Append(newHeader.Substring(firstLine ? minExpectedLength : 0)); sb.Append(newLineText); } if (inCopyright) { inCopyright = !closingTag; } else { sb.Append(oldLine.Substring(firstLine ? minExpectedLength : 0)); sb.Append(newLineText); } firstLine = false; } } sb.Append(commentIndentation); sb.Append(" */"); // Get rid of any trailing spaces. var lines = sb.ToString().Split(new string[] { newLineText }, StringSplitOptions.None); sb.Clear(); for (int i = 0; i < lines.Length; i++) { sb.Append((i == 0 ? string.Empty : newLineText) + lines[i].TrimEnd()); } var newTrivia = SyntaxFactory.SyntaxTrivia(SyntaxKind.MultiLineCommentTrivia, StringBuilderPool.ReturnAndFree(sb)); return(root.WithLeadingTrivia(trivia.Replace(commentTrivia, newTrivia))); }
private static void CheckCompanyName(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement, StyleCopSettings settings) { var companyName = copyrightElement.Attribute("company")?.Value; if (string.IsNullOrWhiteSpace(companyName)) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1640Descriptor, location)); return; } if (compilation.IsAnalyzerSuppressed(SA1641Identifier)) { return; } if (string.CompareOrdinal(companyName, settings.DocumentationRules.CompanyName) != 0) { var location = fileHeader.GetElementLocation(context.Tree, copyrightElement); context.ReportDiagnostic(Diagnostic.Create(SA1641Descriptor, location)); } }