-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[apex] New Rule: Annotations should be pascal case #5822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…annotations This commit introduces a new rule that checks if Apex annotations are written in PascalCase, ensuring adherence to naming conventions for improved readability and maintainability. Additionally, relevant XML configuration and test cases have been added to support this rule.
Compared to main: (comment created at 2025-06-10 16:18:08+00:00 for 88512fe) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR!
Can you please do two changes?
- move the rule to the category "code style", as this is definitely a code style issue.
- try to remove the check "isValidAnnotationContext" - I think, this is not necessary and removing this makes the rule even simpler.
* Checks if the annotation is in a valid context (direct child of a | ||
* modifier node that is a direct child of a valid parent type). | ||
*/ | ||
private boolean isValidAnnotationContext(ASTAnnotation annotation) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this check really necessary? If an annotation is used in a context where no annotations are allowed, this would anyway result in a syntax error. I think, this check is not necessary.
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.apex.rule.design; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rule should be in category "codestyle" and not in "design".
@Override | ||
public Object visit(ASTAnnotation annotation, Object data) { | ||
if (annotation.isResolved() && isValidAnnotationContext(annotation) && !isPascalCase(annotation)) { | ||
asCtx(data).addViolation(annotation); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could even provide a specific message like: The annotation @future should be in PascalCase: @Future
:
asCtx(data).addViolation(annotation); | |
asCtx(data).addViolation(annotation, annotation.getRawName(), annotation.getName()); |
And down below when defining the rule, we need to use this message:
message="The annotation @{0} should be in PascalCase: @{1}"
Then we can already see in the rule violation message the problem and the solution.
Oh, and I think this rule would be a good candidate to be part of the quickstart.xml ruleset and be enabled by default. It seems, that the rule is reliable (as it tests against the fixed set of known annotations). |
Describe the PR
This PR introduces a new rule
AnnotationsShouldBePascalCase
for Apex that enforces PascalCase naming convention for annotations. The rule checks if Apex annotations are written in PascalCase format, where each word in the annotation name starts with a capital letter.The rule validates annotations in the following contexts:
The rule ensures that annotations like
@AuraEnabled
,@Deprecated
,@Future
, etc. are written in PascalCase format and reports violations when they are written in any other case (e.g.,@auraenabled
,@deprecated
,@FUTURE
).Related issues
Ready?
./mvnw clean verify
passes (checked automatically by github actions)