<commit_before>module RuboCop
  module Cop
    module Lint
      class SpecsFOverFj < Cop
        include RuboCop::Cop::Consts::JQuerySelectors

        SUSPECT_METHOD_NAMES = [:fj, :ffj]

        def on_send(node)
          _receiver, method_name, *args = *node
          return unless SUSPECT_METHOD_NAMES.include?(method_name)
          return if jquery_necessary?(args.to_a.first.children.first)
          add_offense node, :expression, error_msg(method_name)
        end

        private

        def jquery_necessary?(selector)
          # TODO: inspect the value of the variable
          return true unless selector.is_a?(String)
          selector =~ JQUERY_SELECTORS_REGEX
        end

        def error_msg(method)
          without_j = method.to_s.chomp("j")
          "Prefer `#{without_j}` instead of `#{method}`; `#{method}` should only be used if you are doing jquery-fake-css selectors (e.g. `:visible`). Unlike `#{without_j}`, `#{method}` will not wait for the element to appear in the DOM, often contributing to flickering failures."
        end
      end
    end
  end
end<commit_msg>spec: add autocorrection to the f-over-fj cop<commit_after>module RuboCop
  module Cop
    module Lint
      class SpecsFOverFj < Cop
        include RuboCop::Cop::Consts::JQuerySelectors

        SUSPECT_METHOD_NAMES = {
          fj: 'f',
          ffj: 'ff'
        }

        def on_send(node)
          _receiver, method_name, *args = *node
          return unless SUSPECT_METHOD_NAMES.keys.include?(method_name)
          return if jquery_necessary?(args.to_a.first.children.first)
          add_offense node, :expression, error_msg(method_name)
        end

        private

        def jquery_necessary?(selector)
          # TODO: inspect the value of the variable
          return true unless selector.is_a?(String)
          selector =~ JQUERY_SELECTORS_REGEX
        end

        def error_msg(method)
          alternative = SUSPECT_METHOD_NAMES[method]
          "Prefer `#{alternative}` instead of `#{method}`; `#{method}` should only be used if you are doing jquery-fake-css selectors (e.g. `:visible`). Unlike `#{alternative}`, `#{method}` will not wait for the element to appear in the DOM, often contributing to flickering failures."
        end

        def autocorrect(node)
          _, method_name, *_ = *node
          lambda do |corrector|
            corrector.replace(node.loc.selector, SUSPECT_METHOD_NAMES[method_name])
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08818834796488428, 'fraction_numerical': 0.0, 'mean_word_length': 2.462707182320442, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3140568', 'n_tokens_mistral': 754, 'n_tokens_neox': 760, 'n_words': 206}