8000 [apex] New Rule: Annotations should be pascal case by mitchspano · Pull Request #5822 · pmd/pmd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mitchspano
Copy link
Contributor

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:

  • Class-level annotations
  • Interface-level annotations
  • Method-level annotations
  • Field-level annotations

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?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

…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.
@mitchspano mitchspano changed the title [apex] new rule: AnnotationsShouldBePascalCase [apex] New Rule: Annotations should be pascal case Jun 10, 2025
Copy link

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 60 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 34 errors and 8 configuration errors.

Regression Tester Report

(comment created at 2025-06-10 16:18:08+00:00 for 88512fe)

Copy link
Member
@adangel adangel left a 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?

  1. move the rule to the category "code style", as this is definitely a code style issue.
  2. 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) {
Copy link
Member

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;
Copy link
Member

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".

@adangel adangel added the a:new-rule Proposal to add a new built-in rule label Jun 18, 2025
@adangel adangel added this to the 7.15.0 milestone Jun 18, 2025
@Override
public Object visit(ASTAnnotation annotation, Object data) {
if (annotation.isResolved() && isValidAnnotationContext(annotation) && !isPascalCase(annotation)) {
asCtx(data).addViolation(annotation);
Copy link
Member

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:

Suggested change
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.

@adangel
Copy link
Member
adangel commented Jun 18, 2025

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:new-rule Proposal to add a new built-in rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[apex] New Rule: Annotations should be in pascal case
2 participants
0