2019-03-01 21:03:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-11 13:10:42 -04:00
|
|
|
require_relative '../../qa_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module QA
|
2019-03-01 21:03:43 -05:00
|
|
|
# This cop checks for the usage of patterns in QA elements
|
2018-10-11 13:10:42 -04:00
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
2019-03-01 21:03:43 -05:00
|
|
|
# element :some_element, "link_to 'something'"
|
|
|
|
# element :some_element, /link_to 'something'/
|
2018-10-11 13:10:42 -04:00
|
|
|
#
|
|
|
|
# # good
|
2019-03-01 21:03:43 -05:00
|
|
|
# element :some_element
|
|
|
|
# element :some_element, required: true
|
2018-10-11 13:10:42 -04:00
|
|
|
class ElementWithPattern < RuboCop::Cop::Cop
|
|
|
|
include QAHelpers
|
|
|
|
|
|
|
|
MESSAGE = "Don't use a pattern for element, create a corresponding `%s` instead.".freeze
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_qa_file?(node)
|
|
|
|
return unless method_name(node).to_s == 'element'
|
|
|
|
|
2019-03-01 21:03:43 -05:00
|
|
|
element_name, *args = node.arguments
|
|
|
|
|
|
|
|
return if args.first.nil?
|
2018-10-11 13:10:42 -04:00
|
|
|
|
2019-03-01 21:03:43 -05:00
|
|
|
args.first.each_node(:str) do |arg|
|
2019-07-09 17:44:16 -04:00
|
|
|
add_offense(arg, message: MESSAGE % "data-qa-selector=#{element_name.value}")
|
2019-03-01 21:03:43 -05:00
|
|
|
end
|
2018-10-11 13:10:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def method_name(node)
|
|
|
|
node.children[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|