New Rule: HTML Expected Attribute/s in Tag/s Rule Template

I am stealing an example of an existing custom rule for this as exact use cases vary, however, the foundation of checking tags for the presence of x attribute on y tag remains the same. I think this would be an incredibly valuable template to have because there are plenty of cases where making sure x attribute is in y tag would prevent lots of issues from cropping up. It also seems like it would be relatively easy to implement since it’s the inverse of an existing template.

This rule checks that the specified attributes ARE present in HTML tags.

Noncompliant Code Example

Expected attribute list of:

< a href=“blah .com” > ← Noncompliant; name attribute is not used in “a” tag.

Compliant Solution

< a href=“blah .com” name=“Blah link” > ← Compliant ; name attribute is used in “a” tag.


attributes Comma-separated list of tag.attributes that are expected. E.G. expects a “name” attribute in an “a” tag.

Type: Bug ← Severity determined by use case, could be info/low/med/high/crit depending on how critical the attribute’s presence is in the tag.

If there is a way to achieve this kind of rule without creating a new custom rule I am all ears, I tried finding a deprecated rule that may be able to achieve the same result but wasn’t able to find anything.

Bump at request of scrum master (hopefully this is okay since it has been 7 days)

We have similar requirement. We use to do our automation testing and their recommended best practice is to have a data-testid as a property of all input elements (input, anchor, button, etc). We would like to have Sonar flag this as a code smell if it’s missing.

Sonar Geniuses… is there a way?

1 Like