Skip to content

Style/ConstantVisibility does not recognize multiple constants in visibility declarations #14626

@Yuhi-Sato

Description

@Yuhi-Sato

Description

Style/ConstantVisibility doesn't recognize visibility declarations when multiple constants are specified together, causing false positives.

Current Behavior

These valid Ruby patterns incorrectly trigger offenses:

class Foo
  BAR = 42
  BAZ = 43
  private_constant :BAR, :BAZ  # Not recognized
end
class Foo
  BAR = 42
  BAZ = 43
  public_constant [:BAR, :BAZ]  # Not recognized
end

Both produce false positives:

Explicitly make `BAR` public or private using either `#public_constant` or `#private_constant`.
Explicitly make `BAZ` public or private using either `#public_constant` or `#private_constant`.

Expected Behavior

The cop should recognize these valid forms for both public_constant and private_constant:

  • Single: private_constant :FOO ✅ (currently works)
  • Multiple arguments: private_constant :FOO, :BAR ❌ (doesn't work)
  • Array: private_constant [:FOO, :BAR] ❌ (doesn't work)

Proposed Solution

I'd like to submit a PR that:

  • Enhances visibility_declaration_for? to handle multiple constant declarations
  • Adds test cases for all scenarios
  • Ensures all existing tests pass

Would this enhancement be welcome?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions