Class: PuppetLint::Checks

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-lint/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChecks

Returns a new instance of Checks.



5
6
7
8
9
10
11
12
13
# File 'lib/puppet-lint/plugin.rb', line 5

def initialize
  @problems = []
  @default_info = {:check => 'unknown', :linenumber => 0, :column => 0}

  PuppetLint.configuration.checks.each do |check|
    method = PuppetLint.configuration.check_method[check]
    self.class.send(:define_method, "lint_check_#{check}", &method)
  end
end

Instance Attribute Details

#manifest_linesObject (readonly)

Returns the value of attribute manifest_lines.



3
4
5
# File 'lib/puppet-lint/plugin.rb', line 3

def manifest_lines
  @manifest_lines
end

#problemsObject (readonly)

Returns the value of attribute problems.



2
3
4
# File 'lib/puppet-lint/plugin.rb', line 2

def problems
  @problems
end

Instance Method Details

#class_indexesObject

Internal: Calculate the positions of all class definitions within the tokenised manifest.

Returns an Array of Hashes, each containing:

:start - An Integer position in the `tokens` Array pointing to the first
         token of a class (type :CLASS).
:end   - An Integer position in the `tokens` Array pointing to the last
         token of a class (type :RBRACE).


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/puppet-lint/plugin.rb', line 157

def class_indexes
  @class_indexes ||= Proc.new do
    result = []
    tokens.each_index do |token_idx|
      if tokens[token_idx].type == :CLASS
        depth = 0
        in_params = false
        tokens[token_idx+1..-1].each_index do |class_token_idx|
          idx = class_token_idx + token_idx + 1
          if tokens[idx].type == :LPAREN
            in_params = true
          elsif tokens[idx].type == :RPAREN
            in_params = false
          elsif tokens[idx].type == :LBRACE
            depth += 1 unless in_params
          elsif tokens[idx].type == :RBRACE
            depth -= 1 unless in_params
            if depth == 0 && ! in_params
              if tokens[token_idx].next_code_token.type != :LBRACE
                result << {:start => token_idx, :end => idx}
              end
              break
            end
          end
        end
      end
    end
    result
  end.call
end

#defined_type_indexesObject

Internal: Calculate the positions of all defined type definitions within the tokenised manifest.

Returns an Array of Hashes, each containing:

:start - An Integer position in the `tokens` Array pointing to the first
         token of a defined type (type :DEFINE).
:end   - An Integer position in the `tokens` Array pointing to the last
         token of a defined type (type :RBRACE).


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/puppet-lint/plugin.rb', line 196

def defined_type_indexes
  @defined_type_indexes ||= Proc.new do
    result = []
    tokens.each_index do |token_idx|
      if tokens[token_idx].type == :DEFINE
        depth = 0
        in_params = false
        tokens[token_idx+1..-1].each_index do |define_token_idx|
          idx = define_token_idx + token_idx + 1
          if tokens[idx].type == :LPAREN
            in_params = true
          elsif tokens[idx].type == :RPAREN
            in_params = false
          elsif tokens[idx].type == :LBRACE
            depth += 1 unless in_params
          elsif tokens[idx].type == :RBRACE
            depth -= 1 unless in_params
            if depth == 0 && ! in_params
              result << {:start => token_idx, :end => idx}
              break
            end
          end
        end
      end
    end
    result
  end.call
end

#enabled_checksObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet-lint/plugin.rb', line 64

def enabled_checks
  @enabled_checks ||= Proc.new do
    self.public_methods.select { |method|
      method.to_s.start_with? 'lint_check_'
    }.map { |method|
      method.to_s[11..-1]
    }.select { |name|
      PuppetLint.configuration.send("#{name}_enabled?")
    }
  end.call
end

#formatting_tokensObject



225
226
227
# File 'lib/puppet-lint/plugin.rb', line 225

def formatting_tokens
  @formatting_tokens ||= PuppetLint::Lexer::FORMATTING_TOKENS
end

#fullpathObject



80
81
82
# File 'lib/puppet-lint/plugin.rb', line 80

def fullpath
  @fileinfo[:fullpath]
end

#load_data(fileinfo, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet-lint/plugin.rb', line 37

def load_data(fileinfo, data)
  lexer = PuppetLint::Lexer.new
  begin
    @tokens = lexer.tokenise(data)
  rescue PuppetLint::LexerError => e
    notify :error, {
      :message => 'Syntax error (try running `puppet parser validate <file>`)',
      :linenumber => e.line_no,
      :column => e.column,
    }
    @tokens = []
  end
  @fileinfo = fileinfo
  @data = data
end

#notify(kind, message_hash) ⇒ Object

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'}


30
31
32
33
34
35
# File 'lib/puppet-lint/plugin.rb', line 30

def notify(kind, message_hash)
  message_hash[:kind] = kind
  message_hash.merge!(@default_info) {|key, v1, v2| v1 }
  @problems << message_hash
  message_hash
end

#resource_indexesObject

Internal: Calculate the positions of all resource declarations within the tokenised manifest. These positions only point to the content of the resource declaration, they do not include resource types or titles/namevars.

Returns an Array of Hashes, each containing:

:start - An Integer position in the `tokens` Array pointing to the first
         Token of a resource declaration parameters (type :NAME).
:end   - An Integer position in the `tokens` Array pointing to the last
         Token of a resource declaration parameters (type :RBRACE).


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/puppet-lint/plugin.rb', line 120

def resource_indexes
  @resource_indexes ||= Proc.new do
    result = []
    tokens.each_index do |token_idx|
      if tokens[token_idx].type == :COLON
        next_token = tokens[token_idx].next_code_token
        depth = 1
        if next_token.type != :LBRACE
          tokens[(token_idx + 1)..-1].each_index do |idx|
            real_idx = token_idx + idx + 1
            if tokens[real_idx].type == :LBRACE
              depth += 1
            elsif {:SEMIC => true, :RBRACE => true}.include? tokens[real_idx].type
              unless tokens[real_idx].type == :SEMIC && depth > 1
                depth -= 1
                if depth == 0
                  result << {:start => token_idx + 1, :end => real_idx}
                  break
                end
              end
            end
          end
        end
      end
    end
    result
  end.call
end

#run(fileinfo, data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet-lint/plugin.rb', line 53

def run(fileinfo, data)
  load_data(fileinfo, data)

  enabled_checks.each do |check|
    @default_info[:check] = check
    self.send("lint_check_#{check}")
  end

  @problems
end

#title_tokensObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet-lint/plugin.rb', line 84

def title_tokens
  @title_tokens ||= Proc.new do
    result = []
    tokens.each_index do |token_idx|
      if tokens[token_idx].type == :COLON
        # gather a list of tokens that are resource titles
        if tokens[token_idx-1].type == :RBRACK
          array_start_idx = tokens.rindex { |r|
            r.type == :LBRACK
          }
          title_array_tokens = tokens[(array_start_idx + 1)..(token_idx - 2)]
          result += title_array_tokens.select { |token|
            {:STRING => true, :NAME => true}.include? token.type
          }
        else
          next_token = tokens[token_idx].next_code_token
          if next_token.type != :LBRACE
            result << tokens[token_idx - 1]
          end
        end
      end
    end
    result
  end.call
end

#tokensObject



76
77
78
# File 'lib/puppet-lint/plugin.rb', line 76

def tokens
  @tokens
end