puppet-lint 2.2.1 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -19
  3. data/README.md +181 -174
  4. data/Rakefile +1 -1
  5. data/lib/puppet-lint/bin.rb +6 -0
  6. data/lib/puppet-lint/lexer.rb +1 -1
  7. data/lib/puppet-lint/plugins.rb +16 -10
  8. data/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb +46 -0
  9. data/lib/puppet-lint/plugins/check_classes/autoloader_layout.rb +33 -0
  10. data/lib/puppet-lint/plugins/check_classes/class_inherits_from_params_class.rb +20 -0
  11. data/lib/puppet-lint/plugins/check_classes/code_on_top_scope.rb +20 -0
  12. data/lib/puppet-lint/plugins/check_classes/inherits_across_namespaces.rb +22 -0
  13. data/lib/puppet-lint/plugins/check_classes/names_containing_dash.rb +23 -0
  14. data/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb +28 -0
  15. data/lib/puppet-lint/plugins/check_classes/nested_classes_or_defines.rb +28 -0
  16. data/lib/puppet-lint/plugins/check_classes/parameter_order.rb +44 -0
  17. data/lib/puppet-lint/plugins/check_classes/right_to_left_relationship.rb +15 -0
  18. data/lib/puppet-lint/plugins/check_classes/variable_scope.rb +143 -0
  19. data/lib/puppet-lint/plugins/check_comments/slash_comments.rb +22 -0
  20. data/lib/puppet-lint/plugins/{check_comments.rb → check_comments/star_comments.rb} +1 -24
  21. data/lib/puppet-lint/plugins/{check_conditionals.rb → check_conditionals/case_without_default.rb} +1 -27
  22. data/lib/puppet-lint/plugins/check_conditionals/selector_inside_resource.rb +25 -0
  23. data/lib/puppet-lint/plugins/{check_documentation.rb → check_documentation/documentation.rb} +0 -0
  24. data/lib/puppet-lint/plugins/{check_nodes.rb → check_nodes/unquoted_node_name.rb} +0 -0
  25. data/lib/puppet-lint/plugins/check_resources/duplicate_params.rb +37 -0
  26. data/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb +70 -0
  27. data/lib/puppet-lint/plugins/check_resources/ensure_not_symlink_target.rb +44 -0
  28. data/lib/puppet-lint/plugins/check_resources/file_mode.rb +42 -0
  29. data/lib/puppet-lint/plugins/check_resources/unquoted_file_mode.rb +31 -0
  30. data/lib/puppet-lint/plugins/check_resources/unquoted_resource_title.rb +22 -0
  31. data/lib/puppet-lint/plugins/check_strings/double_quoted_strings.rb +27 -0
  32. data/lib/puppet-lint/plugins/check_strings/only_variable_string.rb +63 -0
  33. data/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb +25 -0
  34. data/lib/puppet-lint/plugins/check_strings/quoted_booleans.rb +26 -0
  35. data/lib/puppet-lint/plugins/check_strings/single_quote_string_with_variables.rb +17 -0
  36. data/lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb +23 -0
  37. data/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb +21 -0
  38. data/lib/puppet-lint/plugins/{check_variables.rb → check_variables/variable_is_lowercase.rb} +0 -22
  39. data/lib/puppet-lint/plugins/check_whitespace/140chars.rb +21 -0
  40. data/lib/puppet-lint/plugins/check_whitespace/2sp_soft_tabs.rb +19 -0
  41. data/lib/puppet-lint/plugins/check_whitespace/80chars.rb +21 -0
  42. data/lib/puppet-lint/plugins/{check_whitespace.rb → check_whitespace/arrow_alignment.rb} +0 -118
  43. data/lib/puppet-lint/plugins/check_whitespace/hard_tabs.rb +24 -0
  44. data/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb +28 -0
  45. data/lib/puppet-lint/tasks/puppet-lint.rb +4 -0
  46. data/lib/puppet-lint/version.rb +1 -1
  47. data/spec/puppet-lint/bin_spec.rb +18 -1
  48. data/spec/puppet-lint/lexer_spec.rb +19 -0
  49. metadata +41 -68
  50. data/lib/puppet-lint/plugins/check_classes.rb +0 -433
  51. data/lib/puppet-lint/plugins/check_resources.rb +0 -251
  52. data/lib/puppet-lint/plugins/check_strings.rb +0 -186
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Sharpe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-29 00:00:00.000000000 Z
11
+ date: 2017-07-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Checks your Puppet manifests against the Puppetlabs
@@ -42,15 +42,43 @@ files:
42
42
  - lib/puppet-lint/monkeypatches/string_prepend.rb
43
43
  - lib/puppet-lint/optparser.rb
44
44
  - lib/puppet-lint/plugins.rb
45
- - lib/puppet-lint/plugins/check_classes.rb
46
- - lib/puppet-lint/plugins/check_comments.rb
47
- - lib/puppet-lint/plugins/check_conditionals.rb
48
- - lib/puppet-lint/plugins/check_documentation.rb
49
- - lib/puppet-lint/plugins/check_nodes.rb
50
- - lib/puppet-lint/plugins/check_resources.rb
51
- - lib/puppet-lint/plugins/check_strings.rb
52
- - lib/puppet-lint/plugins/check_variables.rb
53
- - lib/puppet-lint/plugins/check_whitespace.rb
45
+ - lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb
46
+ - lib/puppet-lint/plugins/check_classes/autoloader_layout.rb
47
+ - lib/puppet-lint/plugins/check_classes/class_inherits_from_params_class.rb
48
+ - lib/puppet-lint/plugins/check_classes/code_on_top_scope.rb
49
+ - lib/puppet-lint/plugins/check_classes/inherits_across_namespaces.rb
50
+ - lib/puppet-lint/plugins/check_classes/names_containing_dash.rb
51
+ - lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb
52
+ - lib/puppet-lint/plugins/check_classes/nested_classes_or_defines.rb
53
+ - lib/puppet-lint/plugins/check_classes/parameter_order.rb
54
+ - lib/puppet-lint/plugins/check_classes/right_to_left_relationship.rb
55
+ - lib/puppet-lint/plugins/check_classes/variable_scope.rb
56
+ - lib/puppet-lint/plugins/check_comments/slash_comments.rb
57
+ - lib/puppet-lint/plugins/check_comments/star_comments.rb
58
+ - lib/puppet-lint/plugins/check_conditionals/case_without_default.rb
59
+ - lib/puppet-lint/plugins/check_conditionals/selector_inside_resource.rb
60
+ - lib/puppet-lint/plugins/check_documentation/documentation.rb
61
+ - lib/puppet-lint/plugins/check_nodes/unquoted_node_name.rb
62
+ - lib/puppet-lint/plugins/check_resources/duplicate_params.rb
63
+ - lib/puppet-lint/plugins/check_resources/ensure_first_param.rb
64
+ - lib/puppet-lint/plugins/check_resources/ensure_not_symlink_target.rb
65
+ - lib/puppet-lint/plugins/check_resources/file_mode.rb
66
+ - lib/puppet-lint/plugins/check_resources/unquoted_file_mode.rb
67
+ - lib/puppet-lint/plugins/check_resources/unquoted_resource_title.rb
68
+ - lib/puppet-lint/plugins/check_strings/double_quoted_strings.rb
69
+ - lib/puppet-lint/plugins/check_strings/only_variable_string.rb
70
+ - lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb
71
+ - lib/puppet-lint/plugins/check_strings/quoted_booleans.rb
72
+ - lib/puppet-lint/plugins/check_strings/single_quote_string_with_variables.rb
73
+ - lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb
74
+ - lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb
75
+ - lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb
76
+ - lib/puppet-lint/plugins/check_whitespace/140chars.rb
77
+ - lib/puppet-lint/plugins/check_whitespace/2sp_soft_tabs.rb
78
+ - lib/puppet-lint/plugins/check_whitespace/80chars.rb
79
+ - lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb
80
+ - lib/puppet-lint/plugins/check_whitespace/hard_tabs.rb
81
+ - lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb
54
82
  - lib/puppet-lint/tasks/puppet-lint.rb
55
83
  - lib/puppet-lint/version.rb
56
84
  - puppet-lint.gemspec
@@ -128,63 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
156
  version: '0'
129
157
  requirements: []
130
158
  rubyforge_project:
131
- rubygems_version: 2.6.8
159
+ rubygems_version: 2.6.11
132
160
  signing_key:
133
161
  specification_version: 4
134
162
  summary: Ensure your Puppet manifests conform with the Puppetlabs style guide
135
- test_files:
136
- - spec/fixtures/test/manifests/fail.pp
137
- - spec/fixtures/test/manifests/ignore.pp
138
- - spec/fixtures/test/manifests/ignore_multiple_block.pp
139
- - spec/fixtures/test/manifests/ignore_multiple_line.pp
140
- - spec/fixtures/test/manifests/ignore_reason.pp
141
- - spec/fixtures/test/manifests/init.pp
142
- - spec/fixtures/test/manifests/malformed.pp
143
- - spec/fixtures/test/manifests/mismatched_control_comment.pp
144
- - spec/fixtures/test/manifests/unterminated_control_comment.pp
145
- - spec/fixtures/test/manifests/url_interpolation.pp
146
- - spec/fixtures/test/manifests/warning.pp
147
- - spec/puppet-lint/bin_spec.rb
148
- - spec/puppet-lint/configuration_spec.rb
149
- - spec/puppet-lint/ignore_overrides_spec.rb
150
- - spec/puppet-lint/lexer/token_spec.rb
151
- - spec/puppet-lint/lexer_spec.rb
152
- - spec/puppet-lint/plugins/check_classes/arrow_on_right_operand_line_spec.rb
153
- - spec/puppet-lint/plugins/check_classes/autoloader_layout_spec.rb
154
- - spec/puppet-lint/plugins/check_classes/class_inherits_from_params_class_spec.rb
155
- - spec/puppet-lint/plugins/check_classes/code_on_top_scope_spec.rb
156
- - spec/puppet-lint/plugins/check_classes/inherits_across_namespaces_spec.rb
157
- - spec/puppet-lint/plugins/check_classes/name_contains_uppercase_spec.rb
158
- - spec/puppet-lint/plugins/check_classes/names_containing_dash_spec.rb
159
- - spec/puppet-lint/plugins/check_classes/nested_classes_or_defines_spec.rb
160
- - spec/puppet-lint/plugins/check_classes/parameter_order_spec.rb
161
- - spec/puppet-lint/plugins/check_classes/right_to_left_relationship_spec.rb
162
- - spec/puppet-lint/plugins/check_classes/variable_scope_spec.rb
163
- - spec/puppet-lint/plugins/check_comments/slash_comments_spec.rb
164
- - spec/puppet-lint/plugins/check_comments/star_comments_spec.rb
165
- - spec/puppet-lint/plugins/check_conditionals/case_without_default_spec.rb
166
- - spec/puppet-lint/plugins/check_conditionals/selector_inside_resource_spec.rb
167
- - spec/puppet-lint/plugins/check_documentation/documentation_spec.rb
168
- - spec/puppet-lint/plugins/check_nodes/unquoted_node_name_spec.rb
169
- - spec/puppet-lint/plugins/check_resources/duplicate_params_spec.rb
170
- - spec/puppet-lint/plugins/check_resources/ensure_first_param_spec.rb
171
- - spec/puppet-lint/plugins/check_resources/ensure_not_symlink_target_spec.rb
172
- - spec/puppet-lint/plugins/check_resources/file_mode_spec.rb
173
- - spec/puppet-lint/plugins/check_resources/unquoted_file_mode_spec.rb
174
- - spec/puppet-lint/plugins/check_resources/unquoted_resource_title_spec.rb
175
- - spec/puppet-lint/plugins/check_strings/double_quoted_strings_spec.rb
176
- - spec/puppet-lint/plugins/check_strings/only_variable_string_spec.rb
177
- - spec/puppet-lint/plugins/check_strings/puppet_url_without_modules_spec.rb
178
- - spec/puppet-lint/plugins/check_strings/quoted_booleans_spec.rb
179
- - spec/puppet-lint/plugins/check_strings/single_quote_string_with_variables_spec.rb
180
- - spec/puppet-lint/plugins/check_strings/variables_not_enclosed_spec.rb
181
- - spec/puppet-lint/plugins/check_variables/variable_contains_dash_spec.rb
182
- - spec/puppet-lint/plugins/check_variables/variable_is_lowercase_spec.rb
183
- - spec/puppet-lint/plugins/check_whitespace/140chars_spec.rb
184
- - spec/puppet-lint/plugins/check_whitespace/2sp_soft_tabs_spec.rb
185
- - spec/puppet-lint/plugins/check_whitespace/80chars_spec.rb
186
- - spec/puppet-lint/plugins/check_whitespace/arrow_alignment_spec.rb
187
- - spec/puppet-lint/plugins/check_whitespace/hard_tabs_spec.rb
188
- - spec/puppet-lint/plugins/check_whitespace/trailing_whitespace_spec.rb
189
- - spec/puppet-lint_spec.rb
190
- - spec/spec_helper.rb
163
+ test_files: []
@@ -1,433 +0,0 @@
1
- # Public: Test the manifest tokens for any right-to-left (<-) chaining
2
- # operators and record a warning for each instance found.
3
- #
4
- # https://docs.puppet.com/guides/style_guide.html#chaining-arrow-syntax
5
- PuppetLint.new_check(:right_to_left_relationship) do
6
- def check
7
- tokens.select { |r| r.type == :OUT_EDGE }.each do |token|
8
- notify :warning, {
9
- :message => 'right-to-left (<-) relationship',
10
- :line => token.line,
11
- :column => token.column,
12
- }
13
- end
14
- end
15
- end
16
-
17
- # Public: Test the manifest tokens for chaining arrow that is
18
- # on the line of the left operand when the right operand is on another line.
19
- #
20
- # https://docs.puppet.com/guides/style_guide.html#chaining-arrow-syntax
21
- PuppetLint.new_check(:arrow_on_right_operand_line) do
22
- def check
23
- tokens.select { |r| Set[:IN_EDGE, :IN_EDGE_SUB].include?(r.type) }.each do |token|
24
- if token.next_code_token.line != token.line
25
- notify :warning, {
26
- :message => 'arrow should be on the right operand\'s line',
27
- :line => token.line,
28
- :column => token.column,
29
- :token => token,
30
- }
31
- end
32
- end
33
- end
34
-
35
- def fix(problem)
36
- token = problem[:token]
37
- tokens.delete(token)
38
-
39
- # remove any excessive whitespace on the line
40
- temp_token = token.prev_code_token
41
- while (temp_token = temp_token.next_token)
42
- tokens.delete(temp_token) if whitespace?(temp_token)
43
- break if temp_token.type == :NEWLINE
44
- end
45
-
46
- temp_token.next_token = token
47
- token.prev_token = temp_token
48
- index = tokens.index(token.next_code_token)
49
- tokens.insert(index, token)
50
-
51
- whitespace_token = PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', temp_token.line + 1, 3)
52
- whitespace_token.prev_token = token
53
- token.next_token = whitespace_token
54
- whitespace_token.next_token = tokens[index + 1]
55
- tokens[index + 1].prev_token = whitespace_token
56
- tokens.insert(index + 1, whitespace_token)
57
- end
58
-
59
- def whitespace?(token)
60
- Set[:INDENT, :WHITESPACE].include?(token.type)
61
- end
62
- end
63
-
64
- # Public: Test the manifest tokens for any classes or defined types that are
65
- # not in an appropriately named file for the autoloader to detect and record
66
- # an error of each instance found.
67
- #
68
- # https://docs.puppet.com/guides/style_guide.html#separate-files
69
- PuppetLint.new_check(:autoloader_layout) do
70
- def check
71
- unless fullpath.nil? || fullpath == ''
72
- (class_indexes + defined_type_indexes).each do |class_idx|
73
- title_token = class_idx[:name_token]
74
- split_title = title_token.value.split('::')
75
- mod = split_title.first
76
- if split_title.length > 1
77
- expected_path = "/#{mod}/manifests/#{split_title[1..-1].join('/')}.pp"
78
- else
79
- expected_path = "/#{title_token.value}/manifests/init.pp"
80
- end
81
-
82
- if PuppetLint.configuration.relative
83
- expected_path = expected_path.gsub(/^\//,'').split('/')[1..-1].join('/')
84
- end
85
-
86
- unless fullpath.end_with? expected_path
87
- notify :error, {
88
- :message => "#{title_token.value} not in autoload module layout",
89
- :line => title_token.line,
90
- :column => title_token.column,
91
- }
92
- end
93
- end
94
- end
95
- end
96
- end
97
-
98
- # Public: Check the manifest tokens for any classes or defined types that
99
- # have a dash in their name and record an error for each instance found.
100
- #
101
- # No style guide reference
102
- PuppetLint.new_check(:names_containing_dash) do
103
- def check
104
- (class_indexes + defined_type_indexes).each do |class_idx|
105
- if class_idx[:name_token].value.include? '-'
106
- if class_idx[:type] == :CLASS
107
- obj_type = 'class'
108
- else
109
- obj_type = 'defined type'
110
- end
111
-
112
- notify :error, {
113
- :message => "#{obj_type} name containing a dash",
114
- :line => class_idx[:name_token].line,
115
- :column => class_idx[:name_token].column,
116
- }
117
- end
118
- end
119
- end
120
- end
121
-
122
- # Public: Check the manifest tokens for any classes that inherit a params
123
- # subclass and record a warning for each instance found.
124
- #
125
- # No style guide reference
126
- PuppetLint.new_check(:class_inherits_from_params_class) do
127
- def check
128
- class_indexes.each do |class_idx|
129
- unless class_idx[:inherited_token].nil?
130
- if class_idx[:inherited_token].value.end_with? '::params'
131
- notify :warning, {
132
- :message => 'class inheriting from params class',
133
- :line => class_idx[:inherited_token].line,
134
- :column => class_idx[:inherited_token].column,
135
- }
136
- end
137
- end
138
- end
139
- end
140
- end
141
- PuppetLint.configuration.send('disable_class_inherits_from_params_class')
142
-
143
- # Public: Test the manifest tokens for any parameterised classes or defined
144
- # types that take parameters and record a warning if there are any optional
145
- # parameters listed before required parameters.
146
- #
147
- # https://docs.puppet.com/guides/style_guide.html#display-order-of-parameters
148
- PuppetLint.new_check(:parameter_order) do
149
- def check
150
- (class_indexes + defined_type_indexes).each do |class_idx|
151
- unless class_idx[:param_tokens].nil?
152
- paren_stack = []
153
- hash_or_array_stack = []
154
- class_idx[:param_tokens].each_with_index do |token, i|
155
- if token.type == :LPAREN
156
- paren_stack.push(true)
157
- elsif token.type == :RPAREN
158
- paren_stack.pop
159
- elsif token.type == :LBRACE || token.type == :LBRACK
160
- hash_or_array_stack.push(true)
161
- elsif token.type == :RBRACE || token.type == :RBRACK
162
- hash_or_array_stack.pop
163
- end
164
- next if (! hash_or_array_stack.empty?)
165
- next unless paren_stack.empty?
166
-
167
- if token.type == :VARIABLE
168
- if token.next_code_token.nil? || [:COMMA, :RPAREN].include?(token.next_code_token.type)
169
- prev_tokens = class_idx[:param_tokens][0..i]
170
- unless prev_tokens.rindex { |r| r.type == :EQUALS }.nil?
171
- unless token.prev_code_token.nil? or token.prev_code_token.type == :EQUALS
172
- msg = 'optional parameter listed before required parameter'
173
- notify :warning, {
174
- :message => msg,
175
- :line => token.line,
176
- :column => token.column,
177
- }
178
- end
179
- end
180
- end
181
- end
182
- end
183
- end
184
- end
185
- end
186
- end
187
-
188
- # Public: Test the manifest tokens for any classes that inherit across
189
- # namespaces and record a warning for each instance found.
190
- #
191
- # https://docs.puppet.com/guides/style_guide.html#class-inheritance
192
- PuppetLint.new_check(:inherits_across_namespaces) do
193
- def check
194
- class_indexes.each do |class_idx|
195
- unless class_idx[:inherited_token].nil?
196
- inherited_module_name = class_idx[:inherited_token].value.split('::').reject { |r| r.empty? }.first
197
- class_module_name = class_idx[:name_token].value.split('::').reject { |r| r.empty? }.first
198
-
199
- unless class_module_name == inherited_module_name
200
- notify :warning, {
201
- :message => "class inherits across module namespaces",
202
- :line => class_idx[:inherited_token].line,
203
- :column => class_idx[:inherited_token].column,
204
- }
205
- end
206
- end
207
- end
208
- end
209
- end
210
-
211
- # Public: Test the manifest tokens for any classes or defined types that are
212
- # defined inside another class.
213
- #
214
- # https://docs.puppet.com/guides/style_guide.html#nested-classes-or-defined-types
215
- PuppetLint.new_check(:nested_classes_or_defines) do
216
- TOKENS = Set[:CLASS, :DEFINE]
217
-
218
- def check
219
- class_indexes.each do |class_idx|
220
- # Skip the first token so that we don't pick up the first :CLASS
221
- class_tokens = class_idx[:tokens][1..-1]
222
-
223
- class_tokens.each do |token|
224
- if TOKENS.include?(token.type)
225
- if token.next_code_token.type != :LBRACE
226
- type = token.type == :CLASS ? 'class' : 'defined type'
227
-
228
- notify :warning, {
229
- :message => "#{type} defined inside a class",
230
- :line => token.line,
231
- :column => token.column,
232
- }
233
- end
234
- end
235
- end
236
- end
237
- end
238
- end
239
-
240
- # Public: Find and warn about module names with illegal uppercase characters.
241
- #
242
- # https://docs.puppet.com/puppet/latest/reference/modules_fundamentals.html#allowed-module-names
243
- # Provides a fix. [puppet-lint #554]
244
- PuppetLint.new_check(:names_containing_uppercase) do
245
- def check
246
- (class_indexes + defined_type_indexes).each do |class_idx|
247
- if class_idx[:name_token].value =~ /[A-Z]/
248
- if class_idx[:type] == :CLASS
249
- obj_type = 'class'
250
- else
251
- obj_type = 'defined type'
252
- end
253
-
254
- notify :error, {
255
- :message => "#{obj_type} '#{class_idx[:name_token].value}' contains illegal uppercase",
256
- :line => class_idx[:name_token].line,
257
- :column => class_idx[:name_token].column,
258
- :token => class_idx[:name_token],
259
- }
260
- end
261
- end
262
- end
263
-
264
- def fix(problem)
265
- problem[:token].value.downcase!
266
- end
267
- end
268
-
269
- # Public: Test that no code is outside of a class or define scope.
270
- #
271
- # No style guide reference
272
- PuppetLint.new_check(:code_on_top_scope) do
273
- def check
274
- class_scope = (class_indexes + defined_type_indexes).map { |e| tokens[e[:start]..e[:end]] }.flatten
275
- top_scope = tokens - class_scope
276
-
277
- top_scope.each do |token|
278
- unless formatting_tokens.include? token.type
279
- notify :warning, {
280
- :message => "code outside of class or define block - #{token.value}",
281
- :line => token.line,
282
- :column => token.column
283
- }
284
- end
285
- end
286
- end
287
- end
288
- PuppetLint.configuration.send("disable_code_on_top_scope")
289
-
290
- # Public: Test the manifest tokens for any variables that are referenced in
291
- # the manifest. If the variables are not fully qualified or one of the
292
- # variables automatically created in the scope, check that they have been
293
- # defined in the local scope and record a warning for each variable that has
294
- # not.
295
- #
296
- # https://docs.puppet.com/guides/style_guide.html#namespacing-variables
297
- PuppetLint.new_check(:variable_scope) do
298
- DEFAULT_SCOPE_VARS = Set[
299
- 'name',
300
- 'title',
301
- 'module_name',
302
- 'environment',
303
- 'clientcert',
304
- 'clientversion',
305
- 'servername',
306
- 'serverip',
307
- 'serverversion',
308
- 'caller_module_name',
309
- 'alias',
310
- 'audit',
311
- 'before',
312
- 'loglevel',
313
- 'noop',
314
- 'notify',
315
- 'require',
316
- 'schedule',
317
- 'stage',
318
- 'subscribe',
319
- 'tag',
320
- 'facts',
321
- 'trusted',
322
- 'server_facts',
323
- ]
324
- POST_VAR_TOKENS = Set[:COMMA, :EQUALS, :RPAREN]
325
-
326
- def check
327
- variables_in_scope = DEFAULT_SCOPE_VARS.clone
328
-
329
- (class_indexes + defined_type_indexes).each do |idx|
330
- referenced_variables = Set[]
331
- object_tokens = idx[:tokens]
332
-
333
- unless idx[:param_tokens].nil?
334
- idx[:param_tokens].each do |token|
335
- if token.type == :VARIABLE
336
- if POST_VAR_TOKENS.include? token.next_code_token.type
337
- variables_in_scope << token.value
338
- end
339
- end
340
- end
341
- end
342
-
343
- future_parser_scopes = {}
344
- in_pipe = false
345
- block_params_stack = []
346
-
347
- object_tokens.each do |token|
348
- case token.type
349
- when :EQUALS
350
- if token.prev_code_token.type == :VARIABLE
351
- variables_in_scope << token.prev_code_token.value
352
- elsif token.prev_code_token.type == :RBRACK
353
- temp_token = token
354
-
355
- brack_depth = 0
356
- while temp_token = temp_token.prev_code_token
357
- case temp_token.type
358
- when :VARIABLE
359
- variables_in_scope << temp_token.value
360
- when :RBRACK
361
- brack_depth += 1
362
- when :LBRACK
363
- brack_depth -= 1
364
- break if brack_depth == 0
365
- when :COMMA
366
- # ignore
367
- else # unexpected
368
- break
369
- end
370
- end
371
- end
372
- when :VARIABLE
373
- if in_pipe
374
- block_params_stack[-1] << token.value
375
- else
376
- referenced_variables << token
377
- end
378
- when :PIPE
379
- in_pipe = !in_pipe
380
-
381
- if in_pipe
382
- block_params_stack << []
383
- else
384
- start_idx = tokens.find_index(token)
385
- end_token = nil
386
- brace_depth = 0
387
-
388
- tokens[start_idx..-1].each do |sub_token|
389
- case sub_token.type
390
- when :LBRACE
391
- brace_depth += 1
392
- when :RBRACE
393
- brace_depth -= 1
394
- if brace_depth == 0
395
- end_token = sub_token
396
- break
397
- end
398
- end
399
- end
400
-
401
- params = block_params_stack.pop
402
- (token.line..end_token.line).each do |line|
403
- future_parser_scopes[line] ||= []
404
- future_parser_scopes[line].concat(params)
405
- end
406
- end
407
- end
408
- end
409
-
410
- msg = "top-scope variable being used without an explicit namespace"
411
- referenced_variables.each do |token|
412
- unless future_parser_scopes[token.line].nil?
413
- next if future_parser_scopes[token.line].include?(token.value.gsub(/\[.+\]\Z/, ''))
414
- end
415
-
416
- unless token.value.include? '::'
417
- unless token.value =~ /^(facts|trusted)\[.+\]/
418
- unless variables_in_scope.include? token.value.gsub(/\[.+\]\Z/, '')
419
- unless token.value =~ /\A\d+\Z/
420
- notify :warning, {
421
- :message => msg,
422
- :line => token.line,
423
- :column => token.column,
424
- }
425
- end
426
- end
427
- end
428
- end
429
- end
430
- end
431
- end
432
- end
433
-