<commit_before># Public: Check the manifest tokens for any resource titles / namevars that
# are not quoted and record a warning for each instance found.
#
# https://docs.puppet.com/guides/style_guide.html#resource-names
PuppetLint.new_check(:unquoted_resource_title) do
  def check
    title_tokens.each do |token|
      if token.type == :NAME
        notify :warning, {
          :message => 'unquoted resource title',
          :line    => token.line,
          :column  => token.column,
          :token   => token,
        }
      end
    end
  end

  def fix(problem)
    problem[:token].type = :SSTRING
  end
end

# Public: Check the tokens of each resource instance for an ensure parameter
# and if found, check that it is the first parameter listed.  If it is not
# the first parameter, record a warning.
#
# https://docs.puppet.com/guides/style_guide.html#attribute-ordering
PuppetLint.new_check(:ensure_first_param) do
  def check
    resource_indexes.each do |resource|
      next if [:CLASS].include? resource[:type].type
      ensure_attr_index = resource[:param_tokens].index { |param_token|
        param_token.value == 'ensure'
      }

      unless ensure_attr_index.nil?
        if ensure_attr_index > 0
          ensure_token = resource[:param_tokens][ensure_attr_index]
          notify :warning, {
            :message  => "ensure found on line but it's not the first attribute",
            :line     => ensure_token.line,
            :column   => ensure_token.column,
            :resource => resource,
          }
        end
      end
    end
  end
  def fix(problem)
    # We find the first and ensure paramss boundaries
    first_param_name_token = nil
    first_param_name_idx = nil
    first_param_comma_token = nil
    first_param_comma_idx = nil
    ensure_param_name_token = nil
    ensure_param_name_idx = nil
    ensure_param_comma_token = nil
    ensure_param_comma_idx = nil
    tokens[(problem[:resource][:start])..(problem[:resource][:end])].each_with_index do |token, token_idx|
      if first_param_name_token.nil?
        if token.type == :NAME
          first_param_name_token = token
          first_param_name_idx = problem[:resource][:start] + token_idx
        end
      elsif first_param_comma_token.nil?
        if token.type == :COMMA
          first_param_comma_token = token
          first_param_comma_idx = problem[:resource][:start] + token_idx
        end
      elsif ensure_param_name_token.nil?
        if token.type == :NAME and token.value == 'ensure'
          ensure_param_name_token = token
          ensure_param_name_idx = problem[:resource][:start] + token_idx
        end
      elsif ensure_param_comma_token.nil?
        if token.type == :COMMA or token.type == :SEMIC
          ensure_param_comma_token = token
          ensure_param_comma_idx = problem[:resource][:start] + token_idx
          break
        end
      end
    end
    if first_param_name_token.nil? or first_param_comma_token.nil? or ensure_param_name_token.nil? or ensure_param_comma_token.nil?
      raise PuppetLint::NoFix
    end
    # Flip params
    prev_token = first_param_name_token.prev_token
    first_param_name_token.prev_token = ensure_param_name_token.prev_token
    ensure_param_name_token.prev_token = prev_token

    prev_code_token = first_param_name_token.prev_code_token
    first_param_name_token.prev_code_token = ensure_param_name_token.prev_code_token
    ensure_param_name_token.prev_code_token = prev_code_token

    next_token = first_param_comma_token.next_token
    first_param_comma_token = ensure_param_comma_token.next_token
    ensure_param_comma_token.next_token = next_token

    next_code_token = first_param_comma_token.next_code_token
    first_param_comma_code_token = ensure_param_comma_token.next_code_token
    ensure_param_comma_token.next_code_token = next_code_token

    # Update index
    ensure_tmp = tokens.slice!(ensure_param_name_idx..ensure_param_comma_idx-1)
    first_tmp = tokens.slice!(first_param_name_idx..first_param_comma_idx-1)
    ensure_tmp.reverse_each do |item|
      tokens.insert(first_param_name_idx, item)
    end
    first_tmp.reverse_each do |item|
      tokens.insert(ensure_param_name_idx + ensure_tmp.length - first_tmp.length, item)
    end
  end
end

# Public: Check the tokens of each resource instance for any duplicate
# parameters and record a warning for each instance found.<commit_msg>Refactor ensure_first_param fix method to use PuppetLint::Lexer::Token#next_token_of<commit_after># Public: Check the manifest tokens for any resource titles / namevars that
# are not quoted and record a warning for each instance found.
#
# https://docs.puppet.com/guides/style_guide.html#resource-names
PuppetLint.new_check(:unquoted_resource_title) do
  def check
    title_tokens.each do |token|
      if token.type == :NAME
        notify :warning, {
          :message => 'unquoted resource title',
          :line    => token.line,
          :column  => token.column,
          :token   => token,
        }
      end
    end
  end

  def fix(problem)
    problem[:token].type = :SSTRING
  end
end

# Public: Check the tokens of each resource instance for an ensure parameter
# and if found, check that it is the first parameter listed.  If it is not
# the first parameter, record a warning.
#
# https://docs.puppet.com/guides/style_guide.html#attribute-ordering
PuppetLint.new_check(:ensure_first_param) do
  def check
    resource_indexes.each do |resource|
      next if [:CLASS].include? resource[:type].type
      ensure_attr_index = resource[:param_tokens].index { |param_token|
        param_token.value == 'ensure'
      }

      unless ensure_attr_index.nil?
        if ensure_attr_index > 0
          ensure_token = resource[:param_tokens][ensure_attr_index]
          notify :warning, {
            :message  => "ensure found on line but it's not the first attribute",
            :line     => ensure_token.line,
            :column   => ensure_token.column,
            :resource => resource,
          }
        end
      end
    end
  end

  def fix(problem)
    first_param_name_token = tokens[problem[:resource][:start]].next_token_of(:NAME)
    first_param_comma_token = first_param_name_token.next_token_of(:COMMA)
    ensure_param_name_token = first_param_comma_token.next_token_of(:NAME, :value => 'ensure')
    ensure_param_comma_token = ensure_param_name_token.next_token_of([:COMMA, :SEMIC])

    if first_param_name_token.nil? or first_param_comma_token.nil? or ensure_param_name_token.nil? or ensure_param_comma_token.nil?
      raise PuppetLint::NoFix
    end

    first_param_name_idx = tokens.index(first_param_name_token)
    first_param_comma_idx = tokens.index(first_param_comma_token)
    ensure_param_name_idx = tokens.index(ensure_param_name_token)
    ensure_param_comma_idx = tokens.index(ensure_param_comma_token)

    # Flip params
    prev_token = first_param_name_token.prev_token
    first_param_name_token.prev_token = ensure_param_name_token.prev_token
    ensure_param_name_token.prev_token = prev_token

    prev_code_token = first_param_name_token.prev_code_token
    first_param_name_token.prev_code_token = ensure_param_name_token.prev_code_token
    ensure_param_name_token.prev_code_token = prev_code_token

    next_token = first_param_comma_token.next_token
    first_param_comma_token = ensure_param_comma_token.next_token
    ensure_param_comma_token.next_token = next_token

    next_code_token = first_param_comma_token.next_code_token
    first_param_comma_code_token = ensure_param_comma_token.next_code_token
    ensure_param_comma_token.next_code_token = next_code_token

    # Update index
    ensure_tmp = tokens.slice!(ensure_param_name_idx..ensure_param_comma_idx-1)
    first_tmp = tokens.slice!(first_param_name_idx..first_param_comma_idx-1)
    ensure_tmp.reverse_each do |item|
      tokens.insert(first_param_name_idx, item)
    end
    first_tmp.reverse_each do |item|
      tokens.insert(ensure_param_name_idx + ensure_tmp.length - first_tmp.length, item)
    end
  end
end

# Public: Check the tokens of each resource instance for any duplicate
# parameters and record a warning for each instance found.
{'fraction_non_alphanumeric': 0.0746487477092242, 'fraction_numerical': 0.0007330482590103849, 'mean_word_length': 3.4928649835345773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 4, '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': '2223275', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2564, 'n_words': 537}