class Object

Constants

BOOLEANS
CLASS_DEFINE_TOKENS

Public: Test the manifest tokens for any classes or defined types that are defined inside another class.

puppet.com/docs/puppet/latest/style_guide.html#nested-classes-or-defined-types

COMMENT_TOKENS

Public: Check the manifest tokens for any class or defined type that does not have a comment directly above it (hopefully, explaining the usage of it) and record a warning for each instance found.

puppet.com/docs/puppet/latest/style_guide.html#public-and-private

COMMENT_TYPES

Public: Check the manifest tokens for any arrows (=>) in a grouping ({}) that are not aligned with other arrows in that grouping.

puppet.com/docs/puppet/latest/style_guide.html#spacing-indentation-and-whitespace

DEFAULT_SCOPE_VARS

Public: Test the manifest tokens for any variables that are referenced in the manifest. If the variables are not fully qualified or one of the variables automatically created in the scope, check that they have been defined in the local scope and record a warning for each variable that has not.

puppet.com/docs/puppet/latest/style_guide.html#namespacing-variables

EASY_FACTS

These facts have a one to one correlation between a legacy fact and a new structured fact.

ESCAPE_CHAR_RE

Public: Check the manifest tokens for any double quoted strings that don’t contain any variables or common escape characters and record a warning for each instance found.

puppet.com/docs/puppet/latest/style_guide.html#quoting

HASH_KEY_TYPES

A list of valid hash key token types

IGNORE_TYPES
LEGACY_FACTS_VAR_TYPES

Public: A puppet-lint custom check to detect legacy facts.

This check will optionally convert from legacy facts like $::operatingsystem or legacy hashed facts like $facts to the new structured facts like $facts[‘name’].

This plugin was adopted in to puppet-lint from github.com/mmckinst/puppet-lint-legacy_facts-check Thanks to @mmckinst, @seanmil, @rodjek, @baurmatt, @bart2 and @joshcooper for the original work.

MODE_RE
MSG

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’).

puppet.com/docs/puppet/latest/style_guide.html#file-modes

POST_VAR_TOKENS
REGEX_FACTS

These facts will depend on how a system is set up and can’t just be enumerated like the EASY_FACTS below.

For example a server might have two block devices named ‘sda’ and ‘sdb’ so there would be a $blockdeivce_sda_vendor and $blockdeivce_sdb_vendor fact for each device. Or it could have 26 block devices going all the way up to ‘sdz’. There is no way to know what the possibilities are so we have to use a regex to match them.

STRING_TOKEN_TYPES

Public: Check the manifest tokens for any variables in a string that have not been enclosed by braces ({}) and record a warning for each instance found.

puppet.com/docs/puppet/latest/style_guide.html#quoting

STRING_TYPES

Public: Check the manifest tokens for any double or single quoted strings containing only a boolean value and record a warning for each instance found.

No style guide reference

SYM_RE
TOKEN_TYPES

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.

puppet.com/docs/puppet/latest/style_guide.html#file-modes

TOP_SCOPE_FACTS_VAR_TYPES

Public: A puppet-lint plugin that will check for the use of top scope facts. For example, the fact ‘$facts` should be used over `$::kernel`.

The check only finds facts using the top-scope: ie it will find $::operatingsystem but not $operatingsystem. It also all top scope variables are facts. If you have top scope variables that aren’t facts you should configure the linter to ignore them.

You can whitelist top scope variables to ignore via the Rake task. You should insert the following line to your Rakefile. ‘PuppetLint.configuration.top_scope_variables = [’location’, ‘role’]‘

This plugin was adopted in to puppet-lint from github.com/mmckinst/puppet-lint-top_scope_facts-check Thanks to @mmckinst, @seanmil and @alexjfisher for the original work.

UNCONVERTIBLE_FACTS

These facts that can’t be converted to new facts.

VARIABLE_DASH_TYPES

Public: Test the manifest tokens for variables that contain a dash and record a warning for each instance found.

No style guide reference

VARIABLE_LOWERCASE_TYPES

Public: Test the manifest tokens for variables that contain an uppercase letter and record a warning for each instance found.

No style guide reference

VAR_TYPES

Public: Check the manifest tokens for double quoted strings that contain a single variable only and record a warning for each instance found.

puppet.com/docs/puppet/latest/style_guide.html#quoting

WHITESPACE_TOKENS
WHITESPACE_TYPES

Public: Check the raw manifest string for lines containing hard tab characters and record an error for each instance found.

puppet.com/docs/puppet/latest/style_guide.html#spacing-indentation-and-whitespace

Public Instance Methods

check() click to toggle source
# File lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb, line 6
def check
  tokens.select { |r| Set[:IN_EDGE, :IN_EDGE_SUB].include?(r.type) }.each do |token|
    next if token.next_code_token.line == token.line

    notify(
      :warning,
      message: "arrow should be on the right operand's line",
      line: token.line,
      column: token.column,
      token: token,
      description: 'Test the manifest tokens for chaining arrow that is on the line of the left operand when the right operand is on another line.',
      help_uri: 'https://puppet.com/docs/puppet/latest/style_guide.html#chaining-arrow-syntax',
    )
  end
end
extract_hash_or_array_ref(token) click to toggle source
# File lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb, line 41
def extract_hash_or_array_ref(token)
  scanner = StringScanner.new(token.value)

  brack_depth = 0
  result = { ref: '' }

  until scanner.eos?
    result[:ref] += scanner.getch

    # Pass a length of 1 when slicing the last character from the string
    # to prevent Ruby 1.8 returning a Fixnum instead of a String.
    case result[:ref][-1, 1]
    when '['
      brack_depth += 1
    when ']'
      brack_depth -= 1
    end

    break if brack_depth.zero? && scanner.peek(1) != '['
  end

  result[:remainder] = scanner.rest
  result
end
find_comment_token(start_token) click to toggle source
# File lib/puppet-lint/plugins/check_documentation/documentation.rb, line 36
def find_comment_token(start_token)
  newlines = 0

  prev_token = start_token.prev_token
  while !prev_token.nil? && WHITESPACE_TOKENS.include?(prev_token.type)
    newlines += 1 if prev_token.type == :NEWLINE
    break if newlines > 1

    prev_token = prev_token.prev_token
  end

  return if prev_token.nil?

  prev_token if COMMENT_TOKENS.include?(prev_token.type)
end
fix(problem) click to toggle source
# File lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb, line 22
def fix(problem)
  return if problem[:token].nil?

  arrow_token = problem[:token]
  left_operand_token = arrow_token.prev_code_token
  right_operand_token = arrow_token.next_code_token

  # Move arrow token to just before the right operand
  remove_token(arrow_token)
  right_operand_index = tokens.index(right_operand_token)
  add_token(right_operand_index, arrow_token)
  whitespace_token = PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', right_operand_token.line, 3)
  add_token(right_operand_index + 1, whitespace_token)

  # Remove trailing whitespace after left operand (if it exists)
  return unless left_operand_token.next_token.type == :WHITESPACE

  trailing_whitespace_token = left_operand_token.next_token
  remove_token(trailing_whitespace_token) if [:NEWLINE, :WHITESPACE].include?(trailing_whitespace_token.next_token.type)
end
handle_variable_containing_dash(var_token) click to toggle source
# File lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb, line 70
def handle_variable_containing_dash(var_token)
  str_token = var_token.next_token

  var_name, text = var_token.value.split('-', 2)
  var_token.value = var_name

  return if str_token.nil?

  str_token.value = "-#{text}#{str_token.value}"
end
hash_key_for(token) click to toggle source

If the variable is using the $facts hash represented internally by multiple tokens, this helper simplifies accessing the hash key.

# File lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb, line 148
def hash_key_for(token)
  lbrack_token = token.next_code_token
  return '' unless lbrack_token && lbrack_token.type == :LBRACK

  key_token = lbrack_token.next_code_token
  return '' unless key_token && HASH_KEY_TYPES.include?(key_token.type)

  key_token.value
end
hash_or_array_ref?(token) click to toggle source
# File lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb, line 35
def hash_or_array_ref?(token)
  token.next_token &&
    STRING_TOKEN_TYPES.include?(token.next_token.type) &&
    token.next_token.value.start_with?('[')
end
parameter?(token) click to toggle source
# File lib/puppet-lint/plugins/check_classes/parameter_order.rb, line 46
def parameter?(token)
  return false unless token.type == :VARIABLE
  return false unless token.prev_code_token

  [
    :LPAREN,   # First parameter, no type specification
    :COMMA,    # Subsequent parameter, no type specification
    :TYPE,     # Parameter with simple type specification
    :RBRACK,   # Parameter with complex type specification
    :CLASSREF, # Parameter with custom type specification
  ].include?(token.prev_code_token.type)
end
required_parameter?(token) click to toggle source
# File lib/puppet-lint/plugins/check_classes/parameter_order.rb, line 59
def required_parameter?(token)
  return !(token.prev_code_token && token.prev_code_token.type == :EQUALS) if token.next_code_token.nil? || [:COMMA, :RPAREN].include?(token.next_code_token.type)

  false
end
run_cmd(message, *cmd) click to toggle source
# File lib/puppet-lint/tasks/release_test.rb, line 5
def run_cmd(message, *cmd)
  print("  #{message}... ")

  if Open3.respond_to?(:capture2e)
    output, status = Open3.capture2e(*cmd)
  else
    output = ''

    Open3.popen3(*cmd) do |stdin, stdout, stderr|
      stdin.close
      output += stdout.read
      output += stderr.read
    end
    status = $CHILD_STATUS.dup
  end

  if status.success?
    puts 'Done'
  else
    puts 'FAILED'
  end

  [output.strip, status.success?]
end
variable_contains_dash?(token) click to toggle source
# File lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb, line 66
def variable_contains_dash?(token)
  token.value.include?('-')
end
with_puppet_lint_head(&block) click to toggle source
# File lib/puppet-lint/tasks/release_test.rb, line 30
def with_puppet_lint_head(&block)
  print('  Updating Gemfile to use puppet-lint HEAD... ')

  buffer = Parser::Source::Buffer.new('Gemfile')
  buffer.source = File.read('Gemfile')
  parser = Parser::CurrentRuby.new
  ast = parser.parse(buffer)

  modified_gemfile = GemfileRewrite.new.rewrite(buffer, ast)
  if modified_gemfile == buffer.source
    puppet_lint_root = File.expand_path(File.join(__FILE__, '..', '..', '..', '..'))
    File.open('Gemfile', 'a') do |f|
      f.puts "gem 'puppet-lint', :path => '#{puppet_lint_root}'"
    end
  else
    File.open('Gemfile', 'w') do |f|
      f.puts modified_gemfile
    end
  end

  puts 'Done'

  Bundler.with_clean_env(&block)

  run_cmd('Restoring Gemfile', 'git', 'checkout', '--', 'Gemfile')
end