diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-30 12:10:38 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-30 12:10:38 +0300 |
commit | 4310bd9b3d275c371bc26045ba52dead17a96260 (patch) | |
tree | d0217024611eef99004a08b3feb5445f8fa8ea43 /rubocop | |
parent | bb1221318722595bcf917354f9d29621ca7413d5 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/capybara/testid_finders.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rubocop/cop/capybara/testid_finders.rb b/rubocop/cop/capybara/testid_finders.rb new file mode 100644 index 00000000000..c45af1eb66c --- /dev/null +++ b/rubocop/cop/capybara/testid_finders.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'rubocop-rspec' + +module RuboCop + module Cop + module Capybara + # Prefer to use data-testid helpers for Capybara + # + # @example + # # bad + # find('[data-testid="some-testid"]') + # within('[data-testid="some-testid"]') + # + # # good + # find_by_testid('some-testid') + # within_testid('some-testid') + # + class TestidFinders < RuboCop::Cop::Base + MSG = 'Prefer to use custom helper method `%{replacement}(%{testid})`.' + + METHOD_MAP = { + find: 'find_by_testid', + within: 'within_testid' + }.freeze + + RESTRICT_ON_SEND = METHOD_MAP.keys.to_set.freeze + + # @!method find_argument(node) + def_node_matcher :argument, <<~PATTERN + (send _ ${RESTRICT_ON_SEND} (str $_) ...) + PATTERN + + def on_send(node) + argument(node) do |method, arg| + selector = data_testid_selector(arg) + next unless selector + + replacement = METHOD_MAP.fetch(method) + msg = format(MSG, testid: selector, replacement: replacement) + + add_offense(node, message: msg) + end + end + + private + + def data_testid_selector(arg) + arg[/^\[data-testid=[",'](.+)[",']\]$/, 1] + end + end + end + end +end |