Shopify / Shopify/packwerk

ParsedConstantDefinitions doesn’t support fully qualified constants

Open
#14 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
1.9k
Forks
121
Avg merge
2m
Merged PRs (30d)
1

Description

RuboCop::AST::Node#const_name doesn’t reveal whether a constant is fully qualified (e.g. ::HELLO) or not:

>> RuboCop::ProcessedSource.new('::HELLO', 2.6).ast.const_name
=> "HELLO"

>> RuboCop::ProcessedSource.new('HELLO', 2.6).ast.const_name
=> "HELLO"

As a result, the implementation of ParsedConstantDefinitions#collect_local_definitions is unaware of whether a constant is fully qualified, so it’s unable to put it in the correct namespace:

>> definitions = ParsedConstantDefinitions.new(
     root_node: parse_code('module Sales; ::HELLO = "World"; end')
   )
=> #<Packwerk::ParsedConstantDefinitions
     @local_definitions=
      {"::Sales"=>#<Parser::Source::Range (string) 7...12>,
       "::Sales::HELLO"=>#<Parser::Source::Range (string) 16...21>}>

>> definitions.local_reference?('HELLO')
=> false # should be true

>> definitions.local_reference?('Sales::HELLO')
=> true # should be false

I’m not going to fix this right now because I’m trying to make progress on a different task, but I think it’s a bug that’s going to affect users.

cc: @tomstuart

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ParsedConstantDefinitions#collect_local_definitions and reproduce the two fully qualified and unqualified constant examples from the issue. Trace how RuboCop::AST::Node#const_name is used, then verify that local_reference?('HELLO') and local_reference?('Sales::HELLO') produce the expected results for the module example.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.