GeezerVison is showing github.com — [jsp] NoClassAttribute: False positive for jsp:useBean · Issue #2033 · pmd/pmd
[jsp] NoClassAttribute: False positive for jsp:useBean #2033Activityadangel commented on Oct 3, 2019
What is the motivation for this rule? Is it to ensure that JSF tags use the styleclass attribute as against the class attribute for an HTML element? Or is to discourage in-lining of style elements in an HTML element, which is a best practice recommended for HTML? I'd have to assume it's the former. Either way, the above is still a bug.
Quoting the rule description:
Do not use an attribute called ‘class’. Use "styleclass" for CSS styles.
So, this rule indeed seems to ensure, that JSF tags only use the "styleClass" attribute instead of the directly the html attribute "class". (Note, there is no "styleclass" attribute with all lowercase letters...)
There are unfortunately no good test cases (see https://github.com/pmd/pmd/blob/master/pmd-jsp/src/test/resources/net/sourceforge/pmd/lang/jsp/rule/bestpractices/xml/NoClassAttribute.xml), e.g. no test case, that makes sure, that the alleged valid case with styleclass doesn't raise a violation (so protection against a false-positive even for the case the rule was written for).
The rule could be easily adjusted to exclude elements from the "jsf" namespace, e.g.
//Element[@HasNamespacePrefix = false() or @NamespacePrefix != 'jsp']
/Attribute[ upper-case(@Name)="CLASS" ]
(note: xpath 2.0 syntax)
The valid namespaces should then be supplied via a property, so that it can be adjusted by the rule users.
However, I can't say, whether this rule makes sense at all. E.g. the standard tag libraries (h.outputText don't support a class attribute anyway, you have to use styleClass.
Also quoting from your linked stackoverflow question:
And class attribute is used with HTML elements, and can't be used directly with JSF components.
Maybe the rule wanted to prevent using "style"? Unless it wanted to prevent that html is directly used and every rendering should be done through jsf components (or similar alternatives), but than the rule should have been named "NoHtml"... changed the title [-][JSP] NoClassAttribute rule is generating a false positive.[/-] [+][jsp] NoClassAttribute rule is generating a false positive[/+] on Oct 3, 2019linusjf commented on Oct 3, 2019 linusjf commented on Oct 4, 2019 Author show commentMore actions adangel commented on Nov 15, 2019
You kinda got me with that Xpath expression that amends the rule. Is there a central location
where these Xpath expressions for each individual rule is located so that they can be studied
individually without firing up the Rule Designer?
The xpath expressions for the built in rules are documented in the rule documentation. In the sources, they are in the corresponding category rulesets, e.g.
|
//Attribute[ upper-case(@Name)="CLASS" ] |
Another point that's unrelated, is PMD, at some point , going to introduce a feature that allows
users to track rules and the metrics associated with each rule as to how many times it occurs in
the code base? This would be internal with an option to enable sharing that data with PMD via
some sort of web service. Just a thought.
Maybe. I guess that requires some sort of "multi file analysis" then, since you want to get an overview of the whole project. Currently the rules operate only on single files and don't see the big picture. For certain metrics, this is enough, but for others (e.g. calculating the average class size and compare each class to this average) we need this multi file analysis. Reporting these metrics then another point: Currently PMD is a standalone application, so we would probably extend/add report formats for metrics. linusjf commented on Nov 15, 2019 Author show commentMore actions jsotuyod commented on May 15, 2020
Maybe the rule wanted to prevent using "style"? Unless it wanted to prevent that html is directly used and every rendering should be done through jsf components (or similar alternatives), but than the rule should have been named "NoHtml"...
I think this is actually trying to avoid abuse… When an ad-hoc attribute is set on a tag, if it's not interpreted by the tag implementation it's usually passed through as-is to the rendered HTML (but the tag can change this behavior). The styleClass attribute on the other hand is supported by the renderer itself, and is therefore more reliable.
Moreover, tags, such as the one cited, may give a different meaning to the class attribute, raising the importance of using styleClass instead.
In hindsight, this was probably a very poor choice when designing the JSF tags… but that ship has already sailed.
We should whitelist tags that define a class attribute (and assume the user knows it's intent). Namespaces are maybe too broad + can be set arbitrarily… The definitive solution would be using the taglib uri to infer the used prefix, and then checking for that prefix + tag, but that's going to be hard on XPath… In the meantime, simply whitelisting tag names or "standard prefix" + tagname should suffice. linusjf commented on May 15, 2020 linusjf commented on May 16, 2020 changed the title [-][jsp] NoClassAttribute rule is generating a false positive[/-] [+][jsp] NoClassAttribute: False positive for jsp:useBean[/+] on Aug 20, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|