<commit_before>class PuppetLint::Plugins::CheckResources < PuppetLint::CheckPlugin
  # Public: Check the manifest tokens for any resource titles / namevars that
  # are not quoted and record a warning for each instance found.
  #
  # Return nothing.
  check 'unquoted_resource_title' do
    title_tokens.each do |token|
      if token.type == :NAME
        notify :warning, {
          :message    => 'unquoted resource title',
          :linenumber => token.line,
          :column     => token.column,
        }
      end
    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.
  #
  # Returns nothing.
  check 'ensure_first_param' do
    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      ensure_attr_index = resource_tokens.index { |token|
        token.type == :NAME and token.value == 'ensure'
      }

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

  # Public: Check the tokens of each File resource instance for a mode
  # parameter and if found, record a warning if the value of that parameter is
  # not a quoted string.
  #
  # Returns nothing.
  check 'unquoted_file_mode' do
    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      stripped_tokens = tokens[0..resource[:start]].reject { |r|
        formatting_tokens.include? r.type
      }

      res_type_idx = stripped_tokens.rindex { |r|
        r.type == :LBRACE
      } - 1

      resource_type_token = stripped_tokens[res_type_idx]
      if resource_type_token.value == "file"
        resource_tokens.each_index do |resource_token_idx|
          attr_token = resource_tokens[resource_token_idx]
          if attr_token.type == :NAME and attr_token.value == 'mode'
            value_token = resource_tokens[resource_token_idx + 2]
            if value_token.type == :NAME
              notify :warning, {
                :message    => 'unquoted file mode',
                :linenumber => value_token.line,
                :column     => value_token.column,
              }
            end
          end
        end
      end
    end
  end

  # Public: Check the tokens of each File resource instance for a mode
  # parameter and if found, record a warning if the value of that parameter is
  # not a 4 digit octal value (0755) or a symbolic mode ('o=rwx,g+r').
  #
  # Returns nothing.
  check 'file_mode' do
    msg = 'mode should be represented as a 4 digit octal value or symbolic mode'
    sym_mode = /\A([ugoa]*[-=+][-=+rstwxXugo]*)(,[ugoa]*[-=+][-=+rstwxXugo]*)*\Z/

    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      stripped_tokens = tokens[0..resource[:start]].reject { |r|
        formatting_tokens.include? r.type
      }

      res_type_idx = stripped_tokens.rindex { |r|
        r.type == :LBRACE
      } - 1

      resource_type_token = stripped_tokens[res_type_idx]
      if resource_type_token.value == "file"
        resource_tokens.each_index do |resource_token_idx|
          attr_token = resource_tokens[resource_token_idx]
          if attr_token.type == :NAME and attr_token.value == 'mode'
            value_token = resource_tokens[resource_token_idx + 2]

            break if value_token.value =~ /\d{4}/
            break if value_token.type == :VARIABLE
            break if value_token.value =~ sym_mode

            notify :warning, {
              :message    => msg,
              :linenumber => value_token.line,
              :column     => value_token.column,
            }
          end
        end
      end
    end
  end

  check 'ensure_not_symlink_target' do
    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      stripped_tokens = tokens[0..resource[:start]].reject { |r|
        formatting_tokens.include? r.type
      }

      res_type_idx = stripped_tokens.rindex { |r|
        r.type == :LBRACE
      } - 1

      resource_type_token = stripped_tokens[res_type_idx]
      if resource_type_token.value == "file"
        resource_tokens.each_index do |resource_token_idx|<commit_msg>Document ensure_not_symlink_target check<commit_after>class PuppetLint::Plugins::CheckResources < PuppetLint::CheckPlugin
  # Public: Check the manifest tokens for any resource titles / namevars that
  # are not quoted and record a warning for each instance found.
  #
  # Return nothing.
  check 'unquoted_resource_title' do
    title_tokens.each do |token|
      if token.type == :NAME
        notify :warning, {
          :message    => 'unquoted resource title',
          :linenumber => token.line,
          :column     => token.column,
        }
      end
    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.
  #
  # Returns nothing.
  check 'ensure_first_param' do
    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      ensure_attr_index = resource_tokens.index { |token|
        token.type == :NAME and token.value == 'ensure'
      }

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

  # Public: Check the tokens of each File resource instance for a mode
  # parameter and if found, record a warning if the value of that parameter is
  # not a quoted string.
  #
  # Returns nothing.
  check 'unquoted_file_mode' do
    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      stripped_tokens = tokens[0..resource[:start]].reject { |r|
        formatting_tokens.include? r.type
      }

      res_type_idx = stripped_tokens.rindex { |r|
        r.type == :LBRACE
      } - 1

      resource_type_token = stripped_tokens[res_type_idx]
      if resource_type_token.value == "file"
        resource_tokens.each_index do |resource_token_idx|
          attr_token = resource_tokens[resource_token_idx]
          if attr_token.type == :NAME and attr_token.value == 'mode'
            value_token = resource_tokens[resource_token_idx + 2]
            if value_token.type == :NAME
              notify :warning, {
                :message    => 'unquoted file mode',
                :linenumber => value_token.line,
                :column     => value_token.column,
              }
            end
          end
        end
      end
    end
  end

  # Public: Check the tokens of each File resource instance for a mode
  # parameter and if found, record a warning if the value of that parameter is
  # not a 4 digit octal value (0755) or a symbolic mode ('o=rwx,g+r').
  #
  # Returns nothing.
  check 'file_mode' do
    msg = 'mode should be represented as a 4 digit octal value or symbolic mode'
    sym_mode = /\A([ugoa]*[-=+][-=+rstwxXugo]*)(,[ugoa]*[-=+][-=+rstwxXugo]*)*\Z/

    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      stripped_tokens = tokens[0..resource[:start]].reject { |r|
        formatting_tokens.include? r.type
      }

      res_type_idx = stripped_tokens.rindex { |r|
        r.type == :LBRACE
      } - 1

      resource_type_token = stripped_tokens[res_type_idx]
      if resource_type_token.value == "file"
        resource_tokens.each_index do |resource_token_idx|
          attr_token = resource_tokens[resource_token_idx]
          if attr_token.type == :NAME and attr_token.value == 'mode'
            value_token = resource_tokens[resource_token_idx + 2]

            break if value_token.value =~ /\d{4}/
            break if value_token.type == :VARIABLE
            break if value_token.value =~ sym_mode

            notify :warning, {
              :message    => msg,
              :linenumber => value_token.line,
              :column     => value_token.column,
            }
          end
        end
      end
    end
  end

  # Public: Check the tokens of each File resource instance for an ensure
  # parameter and record a warning if the value of that parameter looks like
  # a symlink target (starts with a '/').
  #
  # Returns nothing.
  check 'ensure_not_symlink_target' do
    resource_indexes.each do |resource|
      resource_tokens = tokens[resource[:start]..resource[:end]].reject { |r|
        formatting_tokens.include? r.type
      }

      stripped_tokens = tokens[0..resource[:start]].reject { |r|
        formatting_tokens.include? r.type
      }

      res_type_idx = stripped_tokens.rindex { |r|
        r.type == :LBRACE
      } - 1

      resource_type_token = stripped_tokens[res_type_idx]
      if resource_type_token.value == "file"
        resource_tokens.each_index do |resource_token_idx|
{'fraction_non_alphanumeric': 0.09498548693824442, 'fraction_numerical': 0.0032028825943349014, 'mean_word_length': 2.3340006673340006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6948875', 'n_tokens_mistral': 3001, 'n_tokens_neox': 2926, 'n_words': 784}