puppet-lint 3.4.0 → 4.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/puppet-lint/bin.rb +7 -11
  3. data/lib/puppet-lint/checkplugin.rb +4 -8
  4. data/lib/puppet-lint/checks.rb +7 -9
  5. data/lib/puppet-lint/configuration.rb +2 -2
  6. data/lib/puppet-lint/data.rb +35 -39
  7. data/lib/puppet-lint/lexer/string_slurper.rb +2 -3
  8. data/lib/puppet-lint/lexer/token.rb +6 -5
  9. data/lib/puppet-lint/lexer.rb +35 -38
  10. data/lib/puppet-lint/monkeypatches.rb +4 -4
  11. data/lib/puppet-lint/optparser.rb +1 -1
  12. data/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb +1 -0
  13. data/lib/puppet-lint/plugins/check_classes/autoloader_layout.rb +2 -4
  14. data/lib/puppet-lint/plugins/check_classes/class_inherits_from_params_class.rb +2 -2
  15. data/lib/puppet-lint/plugins/check_classes/code_on_top_scope.rb +1 -1
  16. data/lib/puppet-lint/plugins/check_classes/nested_classes_or_defines.rb +5 -5
  17. data/lib/puppet-lint/plugins/check_classes/parameter_order.rb +8 -9
  18. data/lib/puppet-lint/plugins/check_classes/variable_scope.rb +32 -34
  19. data/lib/puppet-lint/plugins/check_conditionals/case_without_default.rb +2 -2
  20. data/lib/puppet-lint/plugins/check_documentation/documentation.rb +7 -6
  21. data/lib/puppet-lint/plugins/check_nodes/unquoted_node_name.rb +1 -1
  22. data/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb +3 -5
  23. data/lib/puppet-lint/plugins/check_resources/ensure_not_symlink_target.rb +2 -2
  24. data/lib/puppet-lint/plugins/check_resources/file_mode.rb +6 -6
  25. data/lib/puppet-lint/plugins/check_resources/unquoted_file_mode.rb +4 -4
  26. data/lib/puppet-lint/plugins/check_strings/double_quoted_strings.rb +4 -4
  27. data/lib/puppet-lint/plugins/check_strings/only_variable_string.rb +4 -5
  28. data/lib/puppet-lint/plugins/check_strings/quoted_booleans.rb +4 -4
  29. data/lib/puppet-lint/plugins/check_strings/single_quote_string_with_variables.rb +2 -2
  30. data/lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb +8 -7
  31. data/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb +3 -3
  32. data/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb +2 -2
  33. data/lib/puppet-lint/plugins/check_whitespace/140chars.rb +1 -0
  34. data/lib/puppet-lint/plugins/check_whitespace/80chars.rb +2 -1
  35. data/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb +9 -9
  36. data/lib/puppet-lint/plugins/check_whitespace/hard_tabs.rb +2 -2
  37. data/lib/puppet-lint/plugins/check_whitespace/line_length.rb +6 -6
  38. data/lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb +106 -105
  39. data/lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb +3 -2
  40. data/lib/puppet-lint/plugins.rb +3 -4
  41. data/lib/puppet-lint/report/codeclimate.rb +3 -2
  42. data/lib/puppet-lint/report/github.rb +1 -0
  43. data/lib/puppet-lint/tasks/puppet-lint.rb +7 -26
  44. data/lib/puppet-lint/tasks/release_test.rb +4 -4
  45. data/lib/puppet-lint/version.rb +1 -1
  46. data/lib/puppet-lint.rb +5 -3
  47. data/rubocop_baseline.yml +3 -440
  48. data/spec/spec_helper.rb +3 -2
  49. data/spec/spec_helper_acceptance_local.rb +1 -1
  50. data/spec/unit/puppet-lint/bin_spec.rb +12 -2
  51. data/spec/unit/puppet-lint/configuration_spec.rb +14 -14
  52. data/spec/unit/puppet-lint/lexer/string_slurper_spec.rb +3 -5
  53. data/spec/unit/puppet-lint/lexer_spec.rb +10 -11
  54. data/spec/unit/puppet-lint/plugins/check_classes/parameter_order_spec.rb +2 -2
  55. data/spec/unit/puppet-lint/plugins/check_conditionals/case_without_default_spec.rb +5 -5
  56. data/spec/unit/puppet-lint/plugins/check_strings/double_quoted_strings_spec.rb +1 -1
  57. data/spec/unit/puppet-lint/plugins/check_strings/only_variable_string_spec.rb +1 -1
  58. data/spec/unit/puppet-lint/plugins/check_whitespace/140chars_spec.rb +0 -2
  59. data/spec/unit/puppet-lint/plugins/check_whitespace/80chars_spec.rb +1 -3
  60. data/spec/unit/puppet-lint/plugins/legacy_facts/legacy_facts_spec.rb +16 -0
  61. data/spec/unit/puppet-lint/plugins/top_scope_facts/top_scope_facts_spec.rb +1 -0
  62. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9a0c2f244210e801942850dca7d3286d28fa16aeff0d68c43101f00e3c6a549
4
- data.tar.gz: f6e1fdee24709edc0a690a7470c665c0952cba65f904ac8cfbe01e968578a886
3
+ metadata.gz: 434d2ed87adc1be458686f91bd8463abb7e677437f120944c47db58cfb56fa3c
4
+ data.tar.gz: f37a50155b77bac0570244abdbc6e2c02650b62a0adfe38d0b3e5f23bd44aeeb
5
5
  SHA512:
6
- metadata.gz: c1d1c760b0be0758b1c788c8f322b90f6cc3ea5ceabac3cf5052b00728178ef94fb99be0da4d49060edbbbe3dc18a8d243144ab112ae69e0a1351d832a42dddb
7
- data.tar.gz: 597dbe95bb35aa5379df4cf2a3117bb6880228079b5e6fcf884680d057d1ce4c544267333da5d39fea85137afdd0cf6de6502df584aebf9175be6854e4e2f79a
6
+ metadata.gz: ca94a402ba348e31914b04d9412d2ecf799be67f9d7c9646d0e4d1b748600ee8251c27166b4e4cb11540bb55c0dfcbfcfa27087e9105160a7e38f8f8b90f7237
7
+ data.tar.gz: 42eb871e6adf2d611139a1e907eae815d8a55a4e8d7a6a62e200da8fed4f12b4347ecaeb19475d1766bab09fcd7f721d3a6c2a50ec5510a14bb39735d875cb5a
@@ -49,7 +49,7 @@ class PuppetLint::Bin
49
49
 
50
50
  begin
51
51
  path = @args[0]
52
- full_path = File.expand_path(path, ENV['PWD'])
52
+ full_path = File.expand_path(path, ENV.fetch('PWD', nil))
53
53
  full_base_path = if File.directory?(full_path)
54
54
  full_path
55
55
  else
@@ -79,19 +79,17 @@ class PuppetLint::Bin
79
79
 
80
80
  path.each do |f|
81
81
  next if ignore_paths.any? { |p| File.fnmatch(p, f) }
82
+
82
83
  l = PuppetLint.new
83
84
  l.file = f
84
85
  l.run
85
86
  all_problems << l.print_problems
86
87
 
87
- if l.errors? || (l.warnings? && PuppetLint.configuration.fail_on_warnings)
88
- return_val = 1
89
- end
88
+ return_val = 1 if l.errors? || (l.warnings? && PuppetLint.configuration.fail_on_warnings)
90
89
 
91
90
  next unless PuppetLint.configuration.fix && l.problems.none? { |r| r[:check] == :syntax }
92
- File.open(f, 'wb') do |fd|
93
- fd.write(l.manifest)
94
- end
91
+
92
+ File.binwrite(f, l.manifest)
95
93
  end
96
94
 
97
95
  if PuppetLint.configuration.sarif
@@ -105,9 +103,7 @@ class PuppetLint::Bin
105
103
  puts JSON.pretty_generate(all_problems)
106
104
  end
107
105
 
108
- if PuppetLint.configuration.codeclimate_report_file
109
- PuppetLint::Report::CodeClimateReporter.write_report_file(all_problems, PuppetLint.configuration.codeclimate_report_file)
110
- end
106
+ PuppetLint::Report::CodeClimateReporter.write_report_file(all_problems, PuppetLint.configuration.codeclimate_report_file) if PuppetLint.configuration.codeclimate_report_file
111
107
 
112
108
  return_val
113
109
  rescue PuppetLint::NoCodeError
@@ -148,7 +144,7 @@ class PuppetLint::Bin
148
144
  'ruleIndex' => rule_index,
149
145
  'message' => { 'text' => message[:message] },
150
146
  'locations' => [{ 'physicalLocation' => { 'artifactLocation' => { 'uri' => relative_path, 'uriBaseId' => 'ROOTPATH' },
151
- 'region' => { 'startLine' => message[:line], 'startColumn' => message[:column] } } }],
147
+ 'region' => { 'startLine' => message[:line], 'startColumn' => message[:column] } } }]
152
148
  }
153
149
  results << result
154
150
  end
@@ -179,10 +179,10 @@ class PuppetLint::CheckPlugin
179
179
  # Returns a Hash of default problem information.
180
180
  def default_info
181
181
  @default_info ||= {
182
- check: self.class.const_get('NAME'),
182
+ check: self.class.const_get(:NAME),
183
183
  fullpath: fullpath,
184
184
  path: path,
185
- filename: filename,
185
+ filename: filename
186
186
  }
187
187
  end
188
188
 
@@ -202,14 +202,10 @@ class PuppetLint::CheckPlugin
202
202
  problem[:kind] = kind
203
203
  problem.merge!(default_info) { |_key, v1, _v2| v1 }
204
204
 
205
- unless [:warning, :error, :fixed].include?(kind)
206
- raise ArgumentError, 'unknown value passed for kind'
207
- end
205
+ raise ArgumentError, 'unknown value passed for kind' unless [:warning, :error, :fixed].include?(kind)
208
206
 
209
207
  [:message, :line, :column, :check].each do |attr|
210
- unless problem.key?(attr)
211
- raise ArgumentError, "problem hash must contain #{attr.inspect}"
212
- end
208
+ raise ArgumentError, "problem hash must contain #{attr.inspect}" unless problem.key?(attr)
213
209
  end
214
210
 
215
211
  @problems << problem
@@ -39,7 +39,7 @@ class PuppetLint::Checks
39
39
  column: e.column,
40
40
  fullpath: PuppetLint::Data.fullpath,
41
41
  path: PuppetLint::Data.path,
42
- filename: PuppetLint::Data.filename,
42
+ filename: PuppetLint::Data.filename
43
43
  }
44
44
  PuppetLint::Data.tokens = []
45
45
  end
@@ -76,15 +76,15 @@ class PuppetLint::Checks
76
76
  kind: :error,
77
77
  check: :syntax,
78
78
  message: 'Syntax error',
79
- fullpath: File.expand_path(fileinfo, ENV['PWD']),
79
+ fullpath: File.expand_path(fileinfo, ENV.fetch('PWD', nil)),
80
80
  filename: File.basename(fileinfo),
81
81
  path: fileinfo,
82
82
  line: e.token.line,
83
- column: e.token.column,
83
+ column: e.token.column
84
84
  }
85
85
 
86
86
  @problems
87
- rescue => e
87
+ rescue StandardError => e
88
88
  $stdout.puts <<-END.gsub(%r{^ {6}}, '')
89
89
  Whoops! It looks like puppet-lint has encountered an error that it doesn't
90
90
  know how to handle. Please open an issue at https://github.com/puppetlabs/puppet-lint
@@ -120,10 +120,8 @@ class PuppetLint::Checks
120
120
  #
121
121
  # Returns an Array of String check names.
122
122
  def enabled_checks
123
- @enabled_checks ||= begin
124
- PuppetLint.configuration.checks.select do |check|
125
- PuppetLint.configuration.send("#{check}_enabled?")
126
- end
123
+ @enabled_checks ||= PuppetLint.configuration.checks.select do |check|
124
+ PuppetLint.configuration.send("#{check}_enabled?")
127
125
  end
128
126
  end
129
127
 
@@ -131,6 +129,6 @@ class PuppetLint::Checks
131
129
  #
132
130
  # Returns the manifest as a String.
133
131
  def manifest
134
- PuppetLint::Data.tokens.map(&:to_manifest).join('')
132
+ PuppetLint::Data.tokens.map(&:to_manifest).join
135
133
  end
136
134
  end
@@ -18,7 +18,7 @@ class PuppetLint::Configuration
18
18
  #
19
19
  # Returns true if the check is enabled, otherwise return false.
20
20
  define_method("#{check}_enabled?") do
21
- settings["#{check}_disabled"] == true ? false : true
21
+ settings["#{check}_disabled"] != true
22
22
  end
23
23
 
24
24
  # Public: Disable the named check.
@@ -153,6 +153,6 @@ class PuppetLint::Configuration
153
153
  self.show_ignored = false
154
154
  self.ignore_paths = ['vendor/**/*.pp']
155
155
  self.github_actions = ENV.key?('GITHUB_ACTION')
156
- self.codeclimate_report_file = ENV['CODECLIMATE_REPORT_FILE']
156
+ self.codeclimate_report_file = ENV.fetch('CODECLIMATE_REPORT_FILE', nil)
157
157
  end
158
158
  end
@@ -72,7 +72,7 @@ class PuppetLint::Data
72
72
 
73
73
  unless formatting_tokens.include?(token.type)
74
74
  current_token.next_token.prev_code_token = token unless current_token.next_token.nil?
75
- next_nf_idx = tokens[index..-1].index { |r| !formatting_tokens.include?(r.type) }
75
+ next_nf_idx = tokens[index..].index { |r| !formatting_tokens.include?(r.type) }
76
76
  unless next_nf_idx.nil?
77
77
  next_nf_token = tokens[index + next_nf_idx]
78
78
  token.next_code_token = next_nf_token
@@ -92,9 +92,7 @@ class PuppetLint::Data
92
92
  end
93
93
 
94
94
  current_token.next_token = token
95
- unless formatting_tokens.include?(token.type)
96
- current_token.next_code_token = token
97
- end
95
+ current_token.next_code_token = token unless formatting_tokens.include?(token.type)
98
96
 
99
97
  tokens.insert(index, token)
100
98
  end
@@ -123,7 +121,7 @@ class PuppetLint::Data
123
121
  @fullpath = nil
124
122
  @filename = nil
125
123
  else
126
- @fullpath = File.expand_path(val, ENV['PWD'])
124
+ @fullpath = File.expand_path(val, ENV.fetch('PWD', nil))
127
125
  @filename = File.basename(val)
128
126
  end
129
127
  end
@@ -184,12 +182,14 @@ class PuppetLint::Data
184
182
  next unless colon_token.next_code_token && colon_token.next_code_token.type != :LBRACE
185
183
  next if classref?(colon_token)
186
184
 
187
- rel_start_idx = tokens[marker..-1].index(colon_token)
185
+ rel_start_idx = tokens[marker..].index(colon_token)
188
186
  break if rel_start_idx.nil?
187
+
189
188
  start_idx = rel_start_idx + marker
190
189
  end_token = colon_token.next_token_of([:SEMIC, :RBRACE])
191
- rel_end_idx = tokens[start_idx..-1].index(end_token)
190
+ rel_end_idx = tokens[start_idx..].index(end_token)
192
191
  raise PuppetLint::SyntaxError, colon_token if rel_end_idx.nil?
192
+
193
193
  marker = rel_end_idx + start_idx
194
194
 
195
195
  result << {
@@ -197,7 +197,7 @@ class PuppetLint::Data
197
197
  end: marker,
198
198
  tokens: tokens[start_idx..marker],
199
199
  type: find_resource_type_token(start_idx),
200
- param_tokens: find_resource_param_tokens(tokens[start_idx..marker]),
200
+ param_tokens: find_resource_param_tokens(tokens[start_idx..marker])
201
201
  }
202
202
  end
203
203
  result
@@ -237,9 +237,7 @@ class PuppetLint::Data
237
237
 
238
238
  break unless resource_tokens.include?(iter_token)
239
239
 
240
- if iter_token && iter_token.next_code_token.type == :FARROW
241
- param_tokens << iter_token
242
- end
240
+ param_tokens << iter_token if iter_token && iter_token.next_code_token.type == :FARROW
243
241
  end
244
242
 
245
243
  param_tokens
@@ -306,7 +304,7 @@ class PuppetLint::Data
306
304
  paren_depth = 0
307
305
  in_params = false
308
306
  inherited_class = nil
309
- tokens[i + 1..-1].each_with_index do |definition_token, j|
307
+ tokens[i + 1..].each_with_index do |definition_token, j|
310
308
  case definition_token.type
311
309
  when :INHERITS
312
310
  inherited_class = definition_token.next_code_token
@@ -320,19 +318,17 @@ class PuppetLint::Data
320
318
  brace_depth += 1
321
319
  when :RBRACE
322
320
  brace_depth -= 1
323
- if brace_depth.zero? && !in_params
324
- if token.next_code_token.type != :LBRACE
325
- result << {
326
- start: i,
327
- end: i + j + 1,
328
- tokens: tokens[i..(i + j + 1)],
329
- param_tokens: param_tokens(tokens[i..(i + j + 1)]),
330
- type: type,
331
- name_token: token.next_code_token,
332
- inherited_token: inherited_class,
333
- }
334
- break
335
- end
321
+ if brace_depth.zero? && !in_params && (token.next_code_token.type != :LBRACE)
322
+ result << {
323
+ start: i,
324
+ end: i + j + 1,
325
+ tokens: tokens[i..(i + j + 1)],
326
+ param_tokens: param_tokens(tokens[i..(i + j + 1)]),
327
+ type: type,
328
+ name_token: token.next_code_token,
329
+ inherited_token: inherited_class
330
+ }
331
+ break
336
332
  end
337
333
  end
338
334
  end
@@ -367,7 +363,7 @@ class PuppetLint::Data
367
363
  level = 0
368
364
  real_idx = 0
369
365
  in_paren = false
370
- tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
366
+ tokens[token_idx + 1..].each_with_index do |cur_token, cur_token_idx|
371
367
  break if level.zero? && in_paren
372
368
  break if level.zero? && cur_token.type == :NEWLINE
373
369
 
@@ -382,7 +378,7 @@ class PuppetLint::Data
382
378
  functions << {
383
379
  start: token_idx,
384
380
  end: real_idx,
385
- tokens: tokens[token_idx..real_idx],
381
+ tokens: tokens[token_idx..real_idx]
386
382
  }
387
383
  end
388
384
  functions
@@ -406,7 +402,7 @@ class PuppetLint::Data
406
402
  next unless token.type == :LBRACK
407
403
 
408
404
  real_idx = 0
409
- tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
405
+ tokens[token_idx + 1..].each_with_index do |cur_token, cur_token_idx|
410
406
  real_idx = token_idx + 1 + cur_token_idx
411
407
  break if cur_token.type == :RBRACK
412
408
  end
@@ -418,7 +414,7 @@ class PuppetLint::Data
418
414
  arrays << {
419
415
  start: token_idx,
420
416
  end: real_idx,
421
- tokens: tokens[token_idx..real_idx],
417
+ tokens: tokens[token_idx..real_idx]
422
418
  }
423
419
  end
424
420
  arrays
@@ -445,18 +441,18 @@ class PuppetLint::Data
445
441
 
446
442
  level = 0
447
443
  real_idx = 0
448
- tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
444
+ tokens[token_idx + 1..].each_with_index do |cur_token, cur_token_idx|
449
445
  real_idx = token_idx + 1 + cur_token_idx
450
446
 
451
447
  level += 1 if cur_token.type == :LBRACE
452
448
  level -= 1 if cur_token.type == :RBRACE
453
- break if level < 0
449
+ break if level.negative?
454
450
  end
455
451
 
456
452
  hashes << {
457
453
  start: token_idx,
458
454
  end: real_idx,
459
- tokens: tokens[token_idx..real_idx],
455
+ tokens: tokens[token_idx..real_idx]
460
456
  }
461
457
  end
462
458
  hashes
@@ -482,7 +478,7 @@ class PuppetLint::Data
482
478
 
483
479
  real_idx = 0
484
480
 
485
- tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
481
+ tokens[token_idx + 1..].each_with_index do |cur_token, cur_token_idx|
486
482
  real_idx = token_idx + 1 + cur_token_idx
487
483
  break if cur_token.type == :RBRACE
488
484
  end
@@ -490,7 +486,7 @@ class PuppetLint::Data
490
486
  defaults << {
491
487
  start: token_idx,
492
488
  end: real_idx,
493
- tokens: tokens[token_idx..real_idx],
489
+ tokens: tokens[token_idx..real_idx]
494
490
  }
495
491
  end
496
492
  defaults
@@ -564,7 +560,7 @@ class PuppetLint::Data
564
560
  comment_token_types.include?(token.type)
565
561
  end
566
562
  control_comment_tokens = comment_tokens.select do |token|
567
- token.value.strip =~ %r{\Alint\:(ignore\:[\w\d]+|endignore)}
563
+ token.value.strip =~ %r{\Alint:(ignore:[\w\d]+|endignore)}
568
564
  end
569
565
 
570
566
  stack = []
@@ -574,12 +570,12 @@ class PuppetLint::Data
574
570
 
575
571
  comment_words = token.value.strip.split(%r{\s+})
576
572
  comment_words.each_with_index do |word, i|
577
- if %r{\Alint\:(ignore|endignore)}.match?(word)
573
+ if %r{\Alint:(ignore|endignore)}.match?(word)
578
574
  comment_data << word
579
575
  else
580
576
  # Once we reach the first non-controlcomment word, assume the rest
581
577
  # of the words are the reason.
582
- reason = comment_words[i..-1]
578
+ reason = comment_words[i..]
583
579
  break
584
580
  end
585
581
  end
@@ -604,7 +600,7 @@ class PuppetLint::Data
604
600
  if top_override.nil?
605
601
  # TODO: refactor to provide a way to expose problems from
606
602
  # PuppetLint::Data via the normal problem reporting mechanism.
607
- $stderr.puts "WARNING: lint:endignore comment with no opening lint:ignore:<check> comment found on line #{token.line}"
603
+ warn "WARNING: lint:endignore comment with no opening lint:ignore:<check> comment found on line #{token.line}"
608
604
  else
609
605
  top_override.each do |start|
610
606
  next if start.nil?
@@ -620,7 +616,7 @@ class PuppetLint::Data
620
616
  end
621
617
 
622
618
  stack.each do |control|
623
- $stderr.puts "WARNING: lint:ignore:#{control[0][2]} comment on line #{control[0][0]} with no closing lint:endignore comment"
619
+ warn "WARNING: lint:ignore:#{control[0][2]} comment on line #{control[0][0]} with no closing lint:endignore comment"
624
620
  end
625
621
  end
626
622
  end
@@ -1,11 +1,10 @@
1
1
  require 'strscan'
2
2
 
3
+ # rubocop:disable Style/ClassAndModuleChildren
3
4
  class PuppetLint::Lexer
4
5
  # Internal: A class for slurping strings from a Puppet manifest.
5
6
  class StringSlurper
6
- attr_accessor :scanner
7
- attr_accessor :results
8
- attr_accessor :interp_stack
7
+ attr_accessor :scanner, :results, :interp_stack
9
8
 
10
9
  START_INTERP_PATTERN = %r{\$\{}.freeze
11
10
  END_INTERP_PATTERN = %r{\}}.freeze
@@ -1,3 +1,4 @@
1
+ # rubocop:disable Style/ClassAndModuleChildren
1
2
  class PuppetLint::Lexer
2
3
  # Public: Stores a fragment of the manifest and the information about its
3
4
  # location in the manifest.
@@ -175,12 +176,10 @@ class PuppetLint::Lexer
175
176
 
176
177
  token_iter = send("#{direction}_token".to_sym)
177
178
  until token_iter.nil?
178
- if to_find.include?(token_iter.type)
179
- return token_iter if opts[:value].nil? || token_iter.value == opts[:value]
180
- end
179
+ return token_iter if to_find.include?(token_iter.type) && (opts[:value].nil? || token_iter.value == opts[:value])
181
180
 
182
- opening_token = direction == :next ? 'L' : 'R'
183
- closing_token = direction == :next ? 'R' : 'L'
181
+ opening_token = (direction == :next) ? 'L' : 'R'
182
+ closing_token = (direction == :next) ? 'R' : 'L'
184
183
 
185
184
  if opts[:skip_blocks]
186
185
  case token_iter.type
@@ -194,6 +193,7 @@ class PuppetLint::Lexer
194
193
  end
195
194
 
196
195
  return nil if token_iter.nil?
196
+
197
197
  token_iter = token_iter.send("#{direction}_token".to_sym)
198
198
  end
199
199
  nil
@@ -202,6 +202,7 @@ class PuppetLint::Lexer
202
202
  def interpolated_variable?
203
203
  return false if type == :TYPE && value != 'type'
204
204
  return true if type == :NAME
205
+
205
206
  PuppetLint::Lexer::KEYWORDS.include?(type.to_s.downcase)
206
207
  end
207
208
  end
@@ -1,6 +1,3 @@
1
- # encoding: utf-8
2
-
3
- require 'pp'
4
1
  require 'strscan'
5
2
  require 'set'
6
3
  require 'puppet-lint/lexer/token'
@@ -56,27 +53,27 @@ class PuppetLint::Lexer
56
53
  # From https://github.com/puppetlabs/puppet/blob/master/lib/puppet/pops/parser/lexer2.rb#L116-L137
57
54
  # or thereabouts
58
55
  KEYWORDS = {
59
- 'case' => true,
60
- 'class' => true,
61
- 'default' => true,
62
- 'define' => true,
63
- 'import' => true,
64
- 'if' => true,
65
- 'elsif' => true,
66
- 'else' => true,
56
+ 'case' => true,
57
+ 'class' => true,
58
+ 'default' => true,
59
+ 'define' => true,
60
+ 'import' => true,
61
+ 'if' => true,
62
+ 'elsif' => true,
63
+ 'else' => true,
67
64
  'inherits' => true,
68
- 'node' => true,
69
- 'and' => true,
70
- 'or' => true,
71
- 'undef' => true,
72
- 'false' => true,
73
- 'true' => true,
74
- 'in' => true,
75
- 'unless' => true,
65
+ 'node' => true,
66
+ 'and' => true,
67
+ 'or' => true,
68
+ 'undef' => true,
69
+ 'false' => true,
70
+ 'true' => true,
71
+ 'in' => true,
72
+ 'unless' => true,
76
73
  'function' => true,
77
- 'type' => true,
78
- 'attr' => true,
79
- 'private' => true,
74
+ 'type' => true,
75
+ 'attr' => true,
76
+ 'private' => true
80
77
  }.freeze
81
78
 
82
79
  # Internal: A Hash whose keys are Strings representing reserved keywords in
@@ -86,9 +83,9 @@ class PuppetLint::Lexer
86
83
  # Currently unused
87
84
  APP_MANAGEMENT_TOKENS = {
88
85
  'application' => true,
89
- 'consumes' => true,
90
- 'produces' => true,
91
- 'site' => true,
86
+ 'consumes' => true,
87
+ 'produces' => true,
88
+ 'site' => true
92
89
  }.freeze
93
90
 
94
91
  # Internal: A Hash whose keys are Symbols representing token types which
@@ -104,7 +101,7 @@ class PuppetLint::Lexer
104
101
  IF: true,
105
102
  ELSIF: true,
106
103
  LPAREN: true,
107
- EQUALS: true,
104
+ EQUALS: true
108
105
  }.freeze
109
106
 
110
107
  # Internal: some commonly used regular expressions
@@ -112,7 +109,7 @@ class PuppetLint::Lexer
112
109
  # \v == vertical tab
113
110
  # \f == form feed
114
111
  # \p{Zs} == ASCII + Unicode non-linebreaking whitespace
115
- WHITESPACE_RE = RUBY_VERSION == '1.8.7' ? %r{[\t\v\f ]} : %r{[\t\v\f\p{Zs}]}
112
+ WHITESPACE_RE = (RUBY_VERSION == '1.8.7') ? %r{[\t\v\f ]} : %r{[\t\v\f\p{Zs}]}
116
113
 
117
114
  LINE_END_RE = %r{(?:\r\n|\r|\n)}.freeze
118
115
 
@@ -182,7 +179,7 @@ class PuppetLint::Lexer
182
179
  COMMENT: true,
183
180
  MLCOMMENT: true,
184
181
  SLASH_COMMENT: true,
185
- INDENT: true,
182
+ INDENT: true
186
183
  }.freeze
187
184
 
188
185
  # Internal: Access the internal token storage.
@@ -203,7 +200,7 @@ class PuppetLint::Lexer
203
200
  i = 0
204
201
 
205
202
  while i < code.size
206
- chunk = code[i..-1]
203
+ chunk = code[i..]
207
204
 
208
205
  found = false
209
206
 
@@ -233,12 +230,12 @@ class PuppetLint::Lexer
233
230
  tokens << new_token(:VARIABLE, var_name, opts)
234
231
 
235
232
  elsif %r{\A'.*?'}m.match?(chunk)
236
- str_content = StringScanner.new(code[i + 1..-1]).scan_until(%r{(\A|[^\\])(\\\\)*'}m)
233
+ str_content = StringScanner.new(code[i + 1..]).scan_until(%r{(\A|[^\\])(\\\\)*'}m)
237
234
  length = str_content.size + 1
238
235
  tokens << new_token(:SSTRING, str_content[0..-2])
239
236
 
240
237
  elsif chunk.start_with?('"')
241
- slurper = PuppetLint::Lexer::StringSlurper.new(code[i + 1..-1])
238
+ slurper = PuppetLint::Lexer::StringSlurper.new(code[i + 1..])
242
239
  begin
243
240
  string_segments = slurper.parse
244
241
  process_string_segments(string_segments)
@@ -271,7 +268,7 @@ class PuppetLint::Lexer
271
268
  tokens << new_token(:MLCOMMENT, mlcomment, raw: mlcomment_raw)
272
269
 
273
270
  elsif chunk.match(%r{\A/.*?/}m) && possible_regex?
274
- str_content = StringScanner.new(code[i + 1..-1]).scan_until(%r{(\A|[^\\])(\\\\)*/}m)
271
+ str_content = StringScanner.new(code[i + 1..]).scan_until(%r{(\A|[^\\])(\\\\)*/}m)
275
272
  length = str_content.size + 1
276
273
  tokens << new_token(:REGEX, str_content[0..-2])
277
274
 
@@ -286,7 +283,7 @@ class PuppetLint::Lexer
286
283
  length += indent.size
287
284
  else
288
285
  heredoc_tag = heredoc_queue.shift
289
- slurper = PuppetLint::Lexer::StringSlurper.new(code[i + length..-1])
286
+ slurper = PuppetLint::Lexer::StringSlurper.new(code[i + length..])
290
287
  heredoc_segments = slurper.parse_heredoc(heredoc_tag)
291
288
  process_heredoc_segments(heredoc_segments)
292
289
  length += slurper.consumed_chars
@@ -298,7 +295,7 @@ class PuppetLint::Lexer
298
295
 
299
296
  unless heredoc_queue.empty?
300
297
  heredoc_tag = heredoc_queue.shift
301
- slurper = PuppetLint::Lexer::StringSlurper.new(code[i + length..-1])
298
+ slurper = PuppetLint::Lexer::StringSlurper.new(code[i + length..])
302
299
  heredoc_segments = slurper.parse_heredoc(heredoc_tag)
303
300
  process_heredoc_segments(heredoc_segments)
304
301
  length += slurper.consumed_chars
@@ -419,11 +416,11 @@ class PuppetLint::Lexer
419
416
  lexer = PuppetLint::Lexer.new
420
417
  lexer.tokenise(segment[1])
421
418
  lexer.tokens.each_with_index do |t, i|
422
- type = i.zero? && t.interpolated_variable? ? :VARIABLE : t.type
419
+ type = (i.zero? && t.interpolated_variable?) ? :VARIABLE : t.type
423
420
  tokens << new_token(type, t.value, raw: t.raw)
424
421
  end
425
422
  when :UNENC_VAR
426
- tokens << new_token(:UNENC_VARIABLE, segment[1].gsub(%r{\A\$}, ''))
423
+ tokens << new_token(:UNENC_VARIABLE, segment[1].delete_prefix('$'))
427
424
  else
428
425
  tokens << new_token(:DQMID, segment[1])
429
426
  end
@@ -451,11 +448,11 @@ class PuppetLint::Lexer
451
448
  lexer = PuppetLint::Lexer.new
452
449
  lexer.tokenise(segment[1])
453
450
  lexer.tokens.each_with_index do |t, i|
454
- type = i.zero? && t.interpolated_variable? ? :VARIABLE : t.type
451
+ type = (i.zero? && t.interpolated_variable?) ? :VARIABLE : t.type
455
452
  tokens << new_token(type, t.value, raw: t.raw)
456
453
  end
457
454
  when :UNENC_VAR
458
- tokens << new_token(:UNENC_VARIABLE, segment[1].gsub(%r{\A\$}, ''))
455
+ tokens << new_token(:UNENC_VARIABLE, segment[1].delete_prefix('$'))
459
456
  else
460
457
  tokens << new_token(:HEREDOC_MID, segment[1])
461
458
  end
@@ -1,9 +1,9 @@
1
1
  begin
2
2
  '%{test}' % { test: 'replaced' } == 'replaced'
3
- rescue
3
+ rescue StandardError
4
4
  # monkeypatch String#% into Ruby 1.8.7
5
5
  class String
6
- Percent = instance_method('%') unless defined?(Percent)
6
+ Percent = instance_method(:%) unless defined?(Percent)
7
7
 
8
8
  def %(*a, &b)
9
9
  a.flatten!
@@ -18,7 +18,7 @@ rescue
18
18
  if a.empty?
19
19
  string
20
20
  else
21
- Percent.bind(string).call(a, &b)
21
+ Percent.bind_call(string, a, &b)
22
22
  end
23
23
  end
24
24
 
@@ -28,7 +28,7 @@ rescue
28
28
  vars.each do |var, value|
29
29
  var = var.to_s
30
30
  var.gsub!(%r{[^a-zA-Z0-9_]}, '')
31
- changed = gsub!(%r{\%\{#{var}\}}, value.to_s)
31
+ changed = gsub!(%r{%\{#{var}\}}, value.to_s)
32
32
  end
33
33
  break unless changed
34
34
  end
@@ -143,7 +143,7 @@ class PuppetLint::OptParser
143
143
 
144
144
  unless args.include?('--no-config')
145
145
  opt_parser.load('/etc/puppet-lint.rc')
146
- if ENV.key?('HOME') && File.readable?(ENV['HOME'])
146
+ if File.readable?(Dir.home)
147
147
  home_dotfile_path = File.expand_path('~/.puppet-lint.rc')
148
148
  opt_parser.load(home_dotfile_path) if File.readable?(home_dotfile_path)
149
149
  end
@@ -35,6 +35,7 @@ PuppetLint.new_check(:arrow_on_right_operand_line) do
35
35
 
36
36
  # Remove trailing whitespace after left operand (if it exists)
37
37
  return unless left_operand_token.next_token.type == :WHITESPACE
38
+
38
39
  trailing_whitespace_token = left_operand_token.next_token
39
40
  remove_token(trailing_whitespace_token) if [:NEWLINE, :WHITESPACE].include?(trailing_whitespace_token.next_token.type)
40
41
  end
@@ -12,14 +12,12 @@ PuppetLint.new_check(:autoloader_layout) do
12
12
  split_title = title_token.value.split('::')
13
13
  mod = split_title.first
14
14
  expected_path = if split_title.length > 1
15
- "/#{mod}/manifests/#{split_title[1..-1].join('/')}.pp"
15
+ "/#{mod}/manifests/#{split_title[1..].join('/')}.pp"
16
16
  else
17
17
  "/#{title_token.value}/manifests/init.pp"
18
18
  end
19
19
 
20
- if PuppetLint.configuration.relative
21
- expected_path = expected_path.gsub(%r{^/}, '').split('/')[1..-1].join('/')
22
- end
20
+ expected_path = expected_path.gsub(%r{^/}, '').split('/')[1..].join('/') if PuppetLint.configuration.relative
23
21
 
24
22
  next if fullpath.end_with?(expected_path)
25
23