Skip to content

Commit

Permalink
Merge pull request #169 from AdmiringWorm/feature/tag-commas
Browse files Browse the repository at this point in the history
(GH-70) Implemented rule to check if tags contains a comma
  • Loading branch information
gep13 committed Mar 9, 2019
2 parents 33a3a0a + 2d5fcbc commit f844eef
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
27 changes: 27 additions & 0 deletions docs/input/docs/rules/choco0012.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
Title: Tags Found With Commas
Description: Tags have been incorrectly separated with commas
Category: Requirements
---

:::{.alert .alert-warning}
**Preliminary Notice**
This rule is not yet available in chocolatey-vscode.
It is a planned rule for 0.8.0.
:::

## Issue

In the nuspec in the `<tags />` element, the verifier found that the tags were separated with commas. They should only be separated with spaces.

## Recommended Solution

Please remove the commas from the `<tags />` element.

## Reasoning

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.

## See also

- [Package validator rule](https://github.com/chocolatey/package-validator/wiki/TagsAreSpaceSeparated){target = _blank}
7 changes: 5 additions & 2 deletions src/Chocolatey.Language.Server/Validations/NuspecRuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ public abstract class NuspecRuleBase : INuspecRule
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="metaValue"/> is <c>null</c>.
/// </exception>
protected Diagnostic CreateDiagnostic(MetaValue metaValue, string message)
protected Diagnostic CreateDiagnostic(MetaValue metaValue, string message, bool useTextIndex = false)
{
if (metaValue == null)
{
throw new ArgumentNullException(nameof(metaValue));
}

return CreateDiagnostic(metaValue.ElementStart, metaValue.ElementEnd, message);
int start = useTextIndex ? metaValue.TextStart : metaValue.ElementStart;
int end = useTextIndex ? metaValue.TextEnd : metaValue.ElementEnd;

return CreateDiagnostic(start, end, message);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Chocolatey.Language.Server.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Chocolatey.Language.Server.Validations
{
/// <summary>
/// Handler to validate that no tags contain a comma.
/// </summary>
/// <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>
public sealed class TagsAreSpaceSeparated : NuspecRuleBase
{
private const string VALIDATION_MESSAGE = "Tags (tags) are space separated values for referencing categories for software. Please don't use comma to separate tags.";

public override string Id => "choco0012";

public override string DocumentationUrl => $"https://gep13.github.io/chocolatey-vscode/docs/rules/{Id}";

public override ValidationType ValidationType => ValidationType.Requirement;

public override IEnumerable<Diagnostic> Validate(Package package)
{
if (!package.Tags.Any())
{
yield break;
}

foreach (var tag in package.Tags)
{
if (tag.Value.Contains(',', StringComparison.OrdinalIgnoreCase))
{
yield return CreateDiagnostic(tag, VALIDATION_MESSAGE, true);
}
}
}
}
}

0 comments on commit f844eef

Please sign in to comment.