Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ambiguous_page_object_name.rb « qa « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4a2c04f61faa8fa66ddfe21bf96a8310d15ae92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

require_relative '../../qa_helpers'

module RuboCop
  module Cop
    module QA
      # This cop checks for the usage of the ambiguous name "page"
      #
      # @example
      #
      #   # bad
      #   Page::Object.perform do |page| do ...
      #   Page::Another.perform { |page| ... }
      #
      #   # good
      #   Page::Object.perform do |object| do ...
      #   Page::Another.perform { |another| ... }
      class AmbiguousPageObjectName < RuboCop::Cop::Cop
        include QAHelpers

        MESSAGE = "Don't use 'page' as a name for a Page Object. Use `%s` instead."

        def_node_matcher :ambiguous_page?, <<~PATTERN
          (block
            (send
              (const ...) :perform)
            (args
              (arg :page)) ...)
        PATTERN

        def on_block(node)
          return unless in_qa_file?(node)
          return unless ambiguous_page?(node)

          add_offense(node.arguments.each_node(:arg).first,
                      message: MESSAGE % page_object_name(node))
        end

        private

        def page_object_name(node)
          node.send_node.children[-2].const_name.downcase.split('::').last
        end
      end
    end
  end
end