Skip to content

Commit e347b28

Browse files
authored
Add rule URL resolver for Textlint (#987)
1 parent f0d386b commit e347b28

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

docs/input/documentation/issue-providers/tap/features.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ The [Cake.Issues.Tap addin](https://cakebuild.net/extensions/cake-issues-tap/){t
2323
- [x] Provides URLs for rules shipped with stylelint.
2424
- [x] Support for custom URL resolving using the `TapStylelintAddRuleUrlResolver` alias.
2525
- [x] [TextlintLogFileFormat]{target="_blank"} alias for reading TAP files generated by [Textlint](https://textlint.github.io/){target="_blank"}.
26+
- [x] Provides URLs for rules shipped with Textlint.
27+
- [x] Support for custom URL resolving using the `TapTextlintAddRuleUrlResolver` alias.
2628

2729
## Supported IIssue properties
2830

src/Cake.Issues.Tap.Tests/LogFileFormat/TextlintLogFileFormatTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void Should_Read_Issue_Correct()
4040
"TAP")
4141
.InFile(@"file.md", 3, 3)
4242
.WithPriority(IssuePriority.Error)
43-
.OfRule("no-todo")
43+
.OfRule("no-todo", new Uri("https://github.com/textlint-rule/textlint-rule-no-todo"))
4444
.Create());
4545
}
4646
}

src/Cake.Issues.Tap/LogFileFormat/TextlintLogFileFormat.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public override IEnumerable<IIssue> ReadIssues(
4747

4848
if (dataDict != null && dataDict.ContainsKey("line"))
4949
{
50-
issueBuilder = issueBuilder.OfRule(dataDict["ruleId"].ToString());
50+
var ruleId = dataDict["ruleId"].ToString();
51+
issueBuilder = issueBuilder.OfRule(ruleId, TextlintRuleUrlResolver.Instance.ResolveRuleUrl(ruleId));
5152
}
5253

5354
result.Add(issueBuilder.Create());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Cake.Issues.Tap.LogFileFormat;
2+
3+
using System;
4+
5+
/// <summary>
6+
/// Class for retrieving a URL linking to a site describing a rule.
7+
/// </summary>
8+
internal class TextlintRuleUrlResolver : BaseRuleUrlResolver<BaseRuleDescription>
9+
{
10+
private static readonly Lazy<TextlintRuleUrlResolver> InstanceValue =
11+
new(() => new TextlintRuleUrlResolver());
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="TextlintRuleUrlResolver"/> class.
15+
/// </summary>
16+
private TextlintRuleUrlResolver()
17+
{
18+
this.AddUrlResolver(x => new Uri("https://github.com/textlint-rule/textlint-rule-" + x.Rule));
19+
}
20+
21+
/// <summary>
22+
/// Gets the instance of the rule resolver.
23+
/// </summary>
24+
public static TextlintRuleUrlResolver Instance => InstanceValue.Value;
25+
26+
/// <inheritdoc/>
27+
protected override bool TryGetRuleDescription(string rule, BaseRuleDescription ruleDescription) => true;
28+
}

src/Cake.Issues.Tap/TapIssuesAliases.TextlintLogFileFormat.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Cake.Issues.Tap;
22

3+
using System;
34
using Cake.Core;
45
using Cake.Core.Annotations;
56
using Cake.Issues.Tap.LogFileFormat;
@@ -23,4 +24,68 @@ public static BaseTapLogFileFormat TextlintLogFileFormat(
2324

2425
return new TextlintLogFileFormat(context.Log);
2526
}
27+
28+
/// <summary>
29+
/// Registers a new URL resolver with default priority of 0.
30+
/// </summary>
31+
/// <param name="context">The context.</param>
32+
/// <param name="resolver">Resolver which returns an <see cref="Uri"/> linking to a site
33+
/// containing help for a specific <see cref="BaseRuleDescription"/>.</param>
34+
/// <example>
35+
/// <para>Adds a provider with default priority of 0 returning a link for all rules starting
36+
/// with the string <c>Foo</c> to search with Google for the rule:</para>
37+
/// <code>
38+
/// <![CDATA[
39+
/// TapTextlintAddRuleUrlResolver(x =>
40+
/// x.Rule.StartsWith("Foo") ?
41+
/// new Uri("https://www.google.com/search?q=%22" + x.Rule + "%22") :
42+
/// null)
43+
/// ]]>
44+
/// </code>
45+
/// </example>
46+
[CakeMethodAlias]
47+
[CakeAliasCategory(IssuesAliasConstants.IssueProviderCakeAliasCategory)]
48+
public static void TapTextlintAddRuleUrlResolver(
49+
this ICakeContext context,
50+
Func<BaseRuleDescription, Uri> resolver)
51+
{
52+
context.NotNull();
53+
resolver.NotNull();
54+
55+
TextlintRuleUrlResolver.Instance.AddUrlResolver(resolver);
56+
}
57+
58+
/// <summary>
59+
/// Registers a new URL resolver with a specific priority.
60+
/// </summary>
61+
/// <param name="context">The context.</param>
62+
/// <param name="resolver">Resolver which returns an <see cref="Uri"/> linking to a site
63+
/// containing help for a specific <see cref="BaseRuleDescription"/>.</param>
64+
/// <param name="priority">Priority of the resolver. Resolver with a higher priority are considered
65+
/// first during resolving of the URL.</param>
66+
/// <example>
67+
/// <para>Adds a provider of priority 5 returning a link for all rules starting with the string
68+
/// <c>Foo</c> to search with Google for the rule:</para>
69+
/// <code>
70+
/// <![CDATA[
71+
/// TapTextlintAddRuleUrlResolver(x =>
72+
/// x.Rule.StartsWith("Foo") ?
73+
/// new Uri("https://www.google.com/search?q=%22" + x.Rule + "%22") :
74+
/// null,
75+
/// 5)
76+
/// ]]>
77+
/// </code>
78+
/// </example>
79+
[CakeMethodAlias]
80+
[CakeAliasCategory(IssuesAliasConstants.IssueProviderCakeAliasCategory)]
81+
public static void TapTextlintAddRuleUrlResolver(
82+
this ICakeContext context,
83+
Func<BaseRuleDescription, Uri> resolver,
84+
int priority)
85+
{
86+
context.NotNull();
87+
resolver.NotNull();
88+
89+
TextlintRuleUrlResolver.Instance.AddUrlResolver(resolver, priority);
90+
}
2691
}

0 commit comments

Comments
 (0)