Skip to content

Add TestSelectorSystemArgument cop - #12

Open
myabc wants to merge 1 commit into
mainfrom
feature/test-selector-system-argument
Open

myabc wants to merge 1 commit into
mainfrom
feature/test-selector-system-argument

Conversation

@myabc

@myabc myabc commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Ticket

https://community.openproject.org/wp/DREAM-906

What are you trying to accomplish?

Flags data-test-selector passed to a ViewComponent through data: instead of the test_selector: system argument, which Primer only renders outside production. Autocorrects by moving the key out of data:. Limited to component constructors, slot calls and configured forwarding helpers. Skips Primer form controls, which render the argument as a literal attribute.

Screenshots

What approach did you choose and why?

AI involvement

Collaborative – AI generated a substantial part of the code; I reviewed and understand every line.

Merge checklist

  • Added/updated tests
  • Added/updated documentation in Lookbook (patterns, previews, etc)
  • Tested major browsers (Chrome, Firefox, Edge, ...)

Flags data-test-selector passed to a ViewComponent through data:
instead of the test_selector: system argument, which Primer only
renders outside production. Autocorrects by moving the key out of
data:. Limited to component constructors, slot calls and configured
forwarding helpers. Skips Primer form controls, which render the
argument as a literal attribute.
Copilot AI balanced review requested due to automatic review settings October 13, 2026 14:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Autocorrection can alter evaluation order or create duplicate keywords, while broad with_* matching causes false positives.

3 open findings
What changed in this PR

Adds a RuboCop cop that moves ViewComponent test selectors into Primer’s environment-aware system argument.

Changes:

  • Implements detection and autocorrection for component, slot, and forwarding-helper calls.
  • Adds configuration, exclusions, tests, registration, and release notes.
  • Covers nested arguments and multiple selector syntaxes.
File Description
lib/​rubocop/​cop/​open_project/​test_selector_system_argument.rb Implements the cop and autocorrection.
spec/​rubocop/​cop/​open_project/​test_selector_system_argument_spec.rb Tests detection, correction, and exclusions.
lib/​rubocop/​cop/​open_project_cops.rb Registers the cop.
config/​default.yml Enables and configures the cop.
.changeset/​test-selector-system-argument.md Documents the new feature.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +97 to +116
def check_arguments(arguments)
arguments.pairs.each do |pair|
name = key_name(pair)

if name == "data" && pair.value.hash_type?
selector = pair.value.pairs.find { |data_pair| DATA_KEYS.include?(key_name(data_pair)) }
register(arguments, pair, selector) if selector
elsif FLAT_KEYS.include?(name)
register(arguments, nil, pair)
end
end
end

def register(arguments, data_pair, selector)
add_offense(selector) do |corrector|
next if arguments.pairs.any? { |pair| key_name(pair) == "test_selector" }

move_selector(corrector, data_pair, selector)
end
end
corrector.replace(data_pair || selector, replacement)
else
corrector.remove(range_with_separator(data_pair.value, selector))
corrector.insert_before(data_pair, "#{replacement},#{separator_before(data_pair)}")
Comment on lines +70 to +71
elsif name.start_with?(SLOT_PREFIX)
!config_list("IgnoredSlots").include?(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants