Skip to content

Commit f844eef

Browse files
authored
Merge pull request #169 from AdmiringWorm/feature/tag-commas
(GH-70) Implemented rule to check if tags contains a comma
2 parents 33a3a0a + 2d5fcbc commit f844eef

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

docs/input/docs/rules/choco0012.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
Title: Tags Found With Commas
3+
Description: Tags have been incorrectly separated with commas
4+
Category: Requirements
5+
---
6+
7+
:::{.alert .alert-warning}
8+
**Preliminary Notice**
9+
This rule is not yet available in chocolatey-vscode.
10+
It is a planned rule for 0.8.0.
11+
:::
12+
13+
## Issue
14+
15+
In the nuspec in the `<tags />` element, the verifier found that the tags were separated with commas. They should only be separated with spaces.
16+
17+
## Recommended Solution
18+
19+
Please remove the commas from the `<tags />` element.
20+
21+
## Reasoning
22+
23+
We could just fix this on Chocolatey.org, but tags are used in other places besides just Chocolatey.org, and they are expected to be space separated.
24+
25+
## See also
26+
27+
- [Package validator rule](https://github.com/chocolatey/package-validator/wiki/TagsAreSpaceSeparated){target = _blank}

src/Chocolatey.Language.Server/Validations/NuspecRuleBase.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ public abstract class NuspecRuleBase : INuspecRule
4646
/// <exception cref="ArgumentNullException">
4747
/// Thrown when <paramref name="metaValue"/> is <c>null</c>.
4848
/// </exception>
49-
protected Diagnostic CreateDiagnostic(MetaValue metaValue, string message)
49+
protected Diagnostic CreateDiagnostic(MetaValue metaValue, string message, bool useTextIndex = false)
5050
{
5151
if (metaValue == null)
5252
{
5353
throw new ArgumentNullException(nameof(metaValue));
5454
}
5555

56-
return CreateDiagnostic(metaValue.ElementStart, metaValue.ElementEnd, message);
56+
int start = useTextIndex ? metaValue.TextStart : metaValue.ElementStart;
57+
int end = useTextIndex ? metaValue.TextEnd : metaValue.ElementEnd;
58+
59+
return CreateDiagnostic(start, end, message);
5760
}
5861

5962
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Chocolatey.Language.Server.Models;
5+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
6+
7+
namespace Chocolatey.Language.Server.Validations
8+
{
9+
/// <summary>
10+
/// Handler to validate that no tags contain a comma.
11+
/// </summary>
12+
/// <seealso href="https://github.com/chocolatey/package-validator/blob/master/src/chocolatey.package.validator/infrastructure.app/rules/TagsAreSpaceSeparatedRequirement.cs">Package validator comma separated tags validation rule.</seealso>
13+
public sealed class TagsAreSpaceSeparated : NuspecRuleBase
14+
{
15+
private const string VALIDATION_MESSAGE = "Tags (tags) are space separated values for referencing categories for software. Please don't use comma to separate tags.";
16+
17+
public override string Id => "choco0012";
18+
19+
public override string DocumentationUrl => $"https://gep13.github.io/chocolatey-vscode/docs/rules/{Id}";
20+
21+
public override ValidationType ValidationType => ValidationType.Requirement;
22+
23+
public override IEnumerable<Diagnostic> Validate(Package package)
24+
{
25+
if (!package.Tags.Any())
26+
{
27+
yield break;
28+
}
29+
30+
foreach (var tag in package.Tags)
31+
{
32+
if (tag.Value.Contains(',', StringComparison.OrdinalIgnoreCase))
33+
{
34+
yield return CreateDiagnostic(tag, VALIDATION_MESSAGE, true);
35+
}
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)