Skip to content

Commit

Permalink
Merge pull request #1718 from riganti/warning-suppress-prefixed-attri…
Browse files Browse the repository at this point in the history
…bute

Suppress the uppercase html attribute warning if it must have a prefix
  • Loading branch information
tomasherceg authored Oct 22, 2023
2 parents 6d1a350 + 09d4379 commit 8e8ce4d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,18 @@ attribute.ValueNode is DothtmlValueBindingNode binding &&
{
var pGroup = groupedProperty.PropertyGroup;
var name = groupedProperty.GroupMemberName;
if (pGroup.Name.EndsWith("Attributes") && name.ToLowerInvariant() != name)
var prefix = attribute.AttributeName.Substring(0, attribute.AttributeName.Length - name.Length);
// If the HTML attribute is used with a prefix such as `Item`, it might be clearer if the first character is uppercased
// e.g. ItemClass reads better than Itemclass
// we supress the warning in such case
var allowFirstCharacterUppercase = prefix.Length > 0 && char.IsLetter(prefix[prefix.Length - 1]);
if (pGroup.Name.EndsWith("Attributes") &&
name.Substring(allowFirstCharacterUppercase ? 1 : 0).ToLowerInvariant() != name.Substring(allowFirstCharacterUppercase ? 1 : 0))
{
// properties with at most two typos
var similarNameProperties =
control.Metadata.AllProperties
.Where(p => StringSimilarity.DamerauLevenshteinDistance(p.Name.ToLowerInvariant(), name.ToLowerInvariant()) <= 2)
.Where(p => StringSimilarity.DamerauLevenshteinDistance(p.Name.ToLowerInvariant(), (prefix + name).ToLowerInvariant()) <= 2)
.Select(p => p.Name)
.ToArray();
var similarPropertyHelp =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,25 @@ @viewModel System.Boolean
Assert.AreEqual("HTML attribute name 'Visble' should not contain uppercase letters. Did you mean Visible, or another DotVVM property?", attribute3.AttributeNameNode.NodeWarnings.Single());
}

[TestMethod]
public void DefaultViewCompiler_NonExistenPropertyWarning_PrefixedGroup()
{
var markup = $@"
@viewModel System.Boolean
<dot:HierarchyRepeater ItemClass=AA ItemIncludeInPage=false />
";
var repeater = ParseSource(markup)
.Content.SelectRecursively(c => c.Content)
.Single(c => c.Metadata.Type == typeof(HierarchyRepeater));

var elementNode = (DothtmlElementNode)repeater.DothtmlNode;
var attribute1 = elementNode.Attributes.Single(a => a.AttributeName == "ItemClass");
var attribute2 = elementNode.Attributes.Single(a => a.AttributeName == "ItemIncludeInPage");

Assert.AreEqual(0, attribute1.AttributeNameNode.NodeWarnings.Count(), attribute1.AttributeNameNode.NodeWarnings.StringJoin(", "));
Assert.AreEqual("HTML attribute name 'IncludeInPage' should not contain uppercase letters. Did you intent to use a DotVVM property instead?", XAssert.Single(attribute2.AttributeNameNode.NodeWarnings));
}

[TestMethod]
public void DefaultViewCompiler_UnsupportedCallSite_ResourceBinding_Warning()
{
Expand Down

0 comments on commit 8e8ce4d

Please sign in to comment.