class PuppetLint::CheckPlugin

Public: A class that contains and provides information for the puppet-lint checks.

This class should not be used directly, but instead should be inherited.

Examples

class PuppetLint::Plugin::CheckFoo < PuppetLint::CheckPlugin
end

Public Class Methods

new() click to toggle source

Internal: Initialise a new PuppetLint::CheckPlugin.

# File lib/puppet-lint/checkplugin.rb, line 12
def initialize
  @problems = []
end

Public Instance Methods

fix_problems() click to toggle source

Internal: Fix any problems the check plugin has detected.

Returns an Array of problem Hashes.

# File lib/puppet-lint/checkplugin.rb, line 37
def fix_problems
  @problems.reject { |problem| problem[:kind] == :ignored || problem[:check] == :syntax }.each do |problem|
    next unless respond_to?(:fix)

    begin
      fix(problem)
      problem[:kind] = :fixed
    rescue PuppetLint::NoFix # rubocop:disable Lint/HandleExceptions
      # noop
    end
  end

  @problems
end
run() click to toggle source

Internal: Check the manifest for problems and filter out any problems that should be ignored.

Returns an Array of problem Hashes.

# File lib/puppet-lint/checkplugin.rb, line 20
def run
  check

  @problems.each do |problem|
    next if problem[:check] == :syntax
    next unless PuppetLint::Data.ignore_overrides[problem[:check]].key?(problem[:line])

    problem[:kind] = :ignored
    problem[:reason] = PuppetLint::Data.ignore_overrides[problem[:check]][problem[:line]]
  end

  @problems
end

Private Instance Methods

add_token(index, token) click to toggle source
# File lib/puppet-lint/checkplugin.rb, line 61
def add_token(index, token)
  PuppetLint::Data.insert(index, token)
end
class_indexes() click to toggle source

Public: Provides positional information for any class definitions in the tokens array to the check plugins.

Returns an Array of Hashes containing the position information.

# File lib/puppet-lint/checkplugin.rb, line 88
def class_indexes
  PuppetLint::Data.class_indexes
end
default_info() click to toggle source

Internal: Prepare default problem report information.

Returns a Hash of default problem information.

# File lib/puppet-lint/checkplugin.rb, line 148
def default_info
  @default_info ||= {
    :check      => self.class.const_get('NAME'),
    :fullpath   => fullpath,
    :path       => path,
    :filename   => filename,
  }
end
defined_type_indexes() click to toggle source

Public: Provides positional information for any defined type definitions in the tokens array to the check plugins.

Returns an Array of Hashes containing the position information.

# File lib/puppet-lint/checkplugin.rb, line 96
def defined_type_indexes
  PuppetLint::Data.defined_type_indexes
end
filename() click to toggle source

Public: Provides the name of the file being analysed to the check plugins.

Returns the String file name.

# File lib/puppet-lint/checkplugin.rb, line 127
def filename
  PuppetLint::Data.filename
end
formatting_tokens() click to toggle source

Public: Provides a list of formatting tokens to the check plugins.

Returns an Array of Symbol token types.

# File lib/puppet-lint/checkplugin.rb, line 134
def formatting_tokens
  PuppetLint::Data.formatting_tokens
end
fullpath() click to toggle source

Public: Provides the expanded path of the file being analysed to check plugins.

Returns the String path.

# File lib/puppet-lint/checkplugin.rb, line 112
def fullpath
  PuppetLint::Data.fullpath
end
manifest_lines() click to toggle source

Public: Provides a list of manifest lines to the check plugins.

Returns an Array of manifest lines.

# File lib/puppet-lint/checkplugin.rb, line 141
def manifest_lines
  PuppetLint::Data.manifest_lines
end
node_indexes() click to toggle source

Public: Provides positional information for any node definitions in the tokens array to the check plugins.

Returns an Array of Hashes containing the position information.

# File lib/puppet-lint/checkplugin.rb, line 104
def node_indexes
  PuppetLint::Data.node_indexes
end
notify(kind, problem) click to toggle source

Public: Report a problem with the manifest being checked.

kind - The Symbol problem type (:warning or :error). problem - A Hash containing the attributes of the problem

:message - The String message describing the problem.
:line    - The Integer line number of the location of the problem.
:column  - The Integer column number of the location of the problem.
:check   - The Symbol name of the check that detected the problem.

Returns nothing.

# File lib/puppet-lint/checkplugin.rb, line 167
def notify(kind, problem)
  problem[:kind] = kind
  problem.merge!(default_info) { |_key, v1, _v2| v1 }

  unless [:warning, :error, :fixed].include?(kind)
    raise ArgumentError, 'unknown value passed for kind'
  end

  [:message, :line, :column, :check].each do |attr|
    unless problem.key?(attr)
      raise ArgumentError, "problem hash must contain #{attr.inspect}"
    end
  end

  @problems << problem
end
path() click to toggle source

Public: Provides the path of the file being analysed as it was provided to puppet-lint to the check plugins.

Returns the String path.

# File lib/puppet-lint/checkplugin.rb, line 120
def path
  PuppetLint::Data.path
end
remove_token(token) click to toggle source
# File lib/puppet-lint/checkplugin.rb, line 65
def remove_token(token)
  PuppetLint::Data.delete(token)
end
resource_indexes() click to toggle source

Public: Provides positional information for any resource declarations in the tokens array to the check plugins.

Returns an Array of Hashes containing the position information.

# File lib/puppet-lint/checkplugin.rb, line 80
def resource_indexes
  PuppetLint::Data.resource_indexes
end
title_tokens() click to toggle source

Public: Provides the resource titles to the check plugins.

Returns an Array of PuppetLint::Lexer::Token objects.

# File lib/puppet-lint/checkplugin.rb, line 72
def title_tokens
  PuppetLint::Data.title_tokens
end
tokens() click to toggle source

Public: Provides the tokenised manifest to the check plugins.

Returns an Array of PuppetLint::Lexer::Token objects.

# File lib/puppet-lint/checkplugin.rb, line 57
def tokens
  PuppetLint::Data.tokens
end