Skip to content

Commit

Permalink
Implement rule description parsing for CA*
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed Sep 8, 2023
1 parent dfd2a53 commit b80ef65
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public string FormatQualityRule(CodeStyleRoslynQualityRuleConfiguration rule)
builder.AddH2($"{rule.Rule.Title} ({rule.Rule.RuleId})");
builder.AddEmptyLine();
builder.AddText($"Severity: {rule.Severity}");
builder.AddEmptyLine();
builder.AddText(rule.Rule.Description);

return builder.Build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ public IReadOnlyCollection<RoslynQualityRule> ParseQualityRules(string info)

IReadOnlyCollection<RoslynRuleId> ruleIds = ParseQualityRuleTableIdRow(ruleId);

string description = ParseCaRuleDescription(markdownHeadedBlocks);

return ruleIds
.Select(id => new RoslynQualityRule(
id,
title.Value,
category.Value,
// TODO: #41 parse description
description: string.Empty))
description))
.ToList();
}

Expand All @@ -160,6 +161,20 @@ public IReadOnlyCollection<RoslynStyleRuleOption> ParseAdditionalFormattingOptio
return ParseOptions(markdownHeadedBlocks);
}

private string ParseCaRuleDescription(IReadOnlyCollection<MarkdownHeadedBlock> markdownHeadedBlocks)
{
MarkdownHeadedBlock? headedBlock = markdownHeadedBlocks.FirstOrDefault(b => b.HeaderText == "Rule description");
if (headedBlock is null)
throw new ConfiguinException("Quality rule page does not contains Rule description block.");

string overviewText = headedBlock
.Content
.Select(_textExtractor.ExtractText)
.Aggregate((a, b) => $"{a}{Environment.NewLine}{b}");

return overviewText;
}

private IReadOnlyCollection<RoslynRuleId> ParseQualityRuleTableIdRow(MsLearnPropertyValueDescriptionTableRow ruleId)
{
if (ruleId.Value.Contains("-"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public void FormatQualityRule_ForCA1064_ReturnExpected()

Severity: Warning

An internal exception is only visible inside its own internal scope. After the exception falls outside the internal scope, only the base exception can be used to catch the exception. If the internal exception is inherited from xref:System.Exception, xref:System.SystemException, or xref:System.ApplicationException, the external code will not have sufficient information to know what to do with the exception.
But, if the code has a public exception that later is used as the base for an internal exception, it is reasonable to assume the code further out will be able to do something intelligent with the base exception. The public exception will have more information than what is provided by xref:System.Exception, xref:System.SystemException, or xref:System.ApplicationException.

""";

RoslynQualityRule ca1064 = WellKnownRoslynRuleDefinitions.CA1064();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Kysect.Configuin.Tests.MsLearnDocumentation;

// TODO: extract all rule descriptions to WellKnownRoslynRuleDefinitions
public class MsLearnDocumentationParserTests
{
private static readonly MsLearnRepositoryPathProvider MsLearnRepositoryPathProvider = TestImplementations.CreateRepositoryPathProvider();
Expand Down Expand Up @@ -159,13 +160,11 @@ public void ParseQualityRule_CA1865_CA1867_ReturnExpectedResult()
{
string fileText = GetPathToCa("ca1865-ca1867.md");

// TODO: parse description
var expected = new RoslynQualityRule(
RoslynRuleId.Parse("CA1865"),
"Use 'string.Method(char)' instead of 'string.Method(string)' for string with single char",
category: "Performance",
// TODO: parse description?
description: string.Empty);
description: "The overload that takes a char parameter performs better than the overload that takes a string parameter.");

IReadOnlyCollection<RoslynQualityRule> qualityRules = _parser.ParseQualityRules(fileText);

Expand Down Expand Up @@ -296,9 +295,7 @@ public void Parse_CodeStyleRefactoringOptions_ReturnExpectedResult()
codeStyleRefactoringOptions.ElementAt(0).Should().BeEquivalentTo(dotnet_style_operator_placement_when_wrapping);
}

// TODO: remove ignore
[Test]
[Ignore("Need to fix all related problems")]
public void Parse_MsDocsRepository_FinishWithoutError()
{
MsLearnDocumentationInfoLocalProvider repositoryPathProvider = TestImplementations.CreateDocumentationInfoLocalProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ class MyClass
public static RoslynQualityRule CA1064()
{
return new RoslynQualityRule(
RoslynRuleId.Parse("CA1064"),
"Exceptions should be public",
"Design",
// TODO: parse description
string.Empty);
ruleId: RoslynRuleId.Parse("CA1064"),
title: "Exceptions should be public",
category: "Design",
description: """
An internal exception is only visible inside its own internal scope. After the exception falls outside the internal scope, only the base exception can be used to catch the exception. If the internal exception is inherited from xref:System.Exception, xref:System.SystemException, or xref:System.ApplicationException, the external code will not have sufficient information to know what to do with the exception.
But, if the code has a public exception that later is used as the base for an internal exception, it is reasonable to assume the code further out will be able to do something intelligent with the base exception. The public exception will have more information than what is provided by xref:System.Exception, xref:System.SystemException, or xref:System.ApplicationException.
""");
}
}

0 comments on commit b80ef65

Please sign in to comment.