class PuppetLint::CheckPlugin

Attributes

checks[R]
problems[R]

Public Class Methods

check(name, &b) click to toggle source
# File lib/puppet-lint/plugin.rb, line 157
def self.check(name, &b)
  PuppetLint.configuration.add_check name
  define_method("lint_check_#{name}", b)
end
new() click to toggle source
# File lib/puppet-lint/plugin.rb, line 24
def initialize
  @problems = []
  @checks = []
  @default_info = {:check => 'unknown', :linenumber => 0}
end

Public Instance Methods

class_indexes() click to toggle source
# File lib/puppet-lint/plugin.rb, line 143
def class_indexes
  filter_tokens if @class_indexes.nil?
  @class_indexes
end
data() click to toggle source
# File lib/puppet-lint/plugin.rb, line 129
def data
  @data
end
defined_type_indexes() click to toggle source
# File lib/puppet-lint/plugin.rb, line 148
def defined_type_indexes
  filter_tokens if @defined_type_indexes.nil?
  @defined_type_indexes
end
filter_tokens() click to toggle source
# File lib/puppet-lint/plugin.rb, line 74
def filter_tokens
  @title_tokens = []
  @resource_indexes = []
  @class_indexes = []
  @defined_type_indexes = []

  @tokens.each_index do |token_idx|
    if @tokens[token_idx].first == :COLON
      # gather a list of tokens that are resource titles
      if @tokens[token_idx-1].first == :RBRACK
        title_array_tokens = @tokens[@tokens.rindex { |r| r.first == :LBRACK }+1..token_idx-2]
        @title_tokens += title_array_tokens.select { |token| [:STRING, :NAME].include? token.first }
      else
        if @tokens[token_idx + 1].first != :LBRACE
          @title_tokens << @tokens[token_idx-1]
        end
      end

      # gather a list of start and end indexes for resource attribute blocks
      if @tokens[token_idx+1].first != :LBRACE
        @resource_indexes << {:start => token_idx+1, :end => @tokens[token_idx+1..-1].index { |r| [:SEMIC, :RBRACE].include? r.first }+token_idx}
      end
    elsif [:CLASS, :DEFINE].include? @tokens[token_idx].first
      lbrace_count = 0
      @tokens[token_idx+1..-1].each_index do |class_token_idx|
        idx = class_token_idx + token_idx
        if @tokens[idx].first == :LBRACE
          lbrace_count += 1
        elsif @tokens[idx].first == :RBRACE
          lbrace_count -= 1
          if lbrace_count == 0
            if @tokens[token_idx].first == :CLASS and @tokens[token_idx + 1].first != :LBRACE
              @class_indexes << {:start => token_idx, :end => idx}
            end
            @defined_type_indexes << {:start => token_idx, :end => idx} if @tokens[token_idx].first == :DEFINE
            break
          end
        end
      end
    end
  end
end
fullpath() click to toggle source
# File lib/puppet-lint/plugin.rb, line 125
def fullpath
  @fileinfo[:fullpath]
end
manifest_lines() click to toggle source
# File lib/puppet-lint/plugin.rb, line 153
def manifest_lines
  @manifest_lines ||= @data.split("\n")
end
notify(kind, message_hash) click to toggle source
notify(kind, message_hash)    #=> nil

Adds the message to the problems array. The kind gets added to the message_hash by setting the key :kind. Typically, the message_hash should contain following keys:

message

which contains a string value describing the problem

linenumber

which contains the line number on which the problem occurs.

Besides the :kind value that is being set, some other key/values are also added. Typically, this is

check

which contains the name of the check that is being executed.

linenumber

which defaults to 0 if the message does not already contain one.

notify :warning, :message => "Something happened", :linenumber => 4
=> {:kind=>:warning, :message=>"Something happened", :linenumber=>4, :check=>'unknown'}
# File lib/puppet-lint/plugin.rb, line 49
def notify(kind, message_hash)
  message_hash[:kind] = kind
  message_hash.merge!(@default_info) {|key, v1, v2| v1 }
  @problems << message_hash
  message_hash
end
path() click to toggle source
# File lib/puppet-lint/plugin.rb, line 121
def path
  @fileinfo[:path]
end
register_check(check) click to toggle source
# File lib/puppet-lint/plugin.rb, line 30
def register_check(check)
  @checks << check
end
resource_indexes() click to toggle source
# File lib/puppet-lint/plugin.rb, line 138
def resource_indexes
  filter_tokens if @resource_indexes.nil?
  @resource_indexes
end
run(fileinfo, data) click to toggle source
# File lib/puppet-lint/plugin.rb, line 56
def run(fileinfo, data)
  lexer = Puppet::Parser::Lexer.new
  lexer.string = data
  @tokens = lexer.fullscan
  @fileinfo = fileinfo
  @data = data

  self.public_methods.select { |method|
    method.to_s.start_with? 'lint_check_'
  }.each { |method|
    name = method.to_s[11..-1]
    @default_info[:check] = name
    self.send(method) if PuppetLint.configuration.send("#{name}_enabled?")
  }

  @problems
end
title_tokens() click to toggle source
# File lib/puppet-lint/plugin.rb, line 133
def title_tokens
  filter_tokens if @title_tokens.nil?
  @title_tokens
end
tokens() click to toggle source
# File lib/puppet-lint/plugin.rb, line 117
def tokens
  @tokens
end