puppet-lint 2.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +74 -0
  3. data/.rubocop_todo.yml +89 -0
  4. data/.travis.yml +11 -9
  5. data/CHANGELOG.md +54 -0
  6. data/Gemfile +3 -0
  7. data/Rakefile +14 -3
  8. data/appveyor.yml +1 -4
  9. data/bin/puppet-lint +1 -1
  10. data/lib/puppet-lint.rb +25 -21
  11. data/lib/puppet-lint/bin.rb +15 -19
  12. data/lib/puppet-lint/checkplugin.rb +24 -17
  13. data/lib/puppet-lint/checks.rb +42 -31
  14. data/lib/puppet-lint/configuration.rb +11 -8
  15. data/lib/puppet-lint/data.rb +236 -164
  16. data/lib/puppet-lint/lexer.rb +225 -203
  17. data/lib/puppet-lint/lexer/token.rb +34 -18
  18. data/lib/puppet-lint/optparser.rb +33 -30
  19. data/lib/puppet-lint/plugins.rb +42 -38
  20. data/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb +26 -28
  21. data/lib/puppet-lint/plugins/check_classes/autoloader_layout.rb +21 -20
  22. data/lib/puppet-lint/plugins/check_classes/class_inherits_from_params_class.rb +8 -9
  23. data/lib/puppet-lint/plugins/check_classes/code_on_top_scope.rb +9 -8
  24. data/lib/puppet-lint/plugins/check_classes/inherits_across_namespaces.rb +12 -11
  25. data/lib/puppet-lint/plugins/check_classes/names_containing_dash.rb +13 -12
  26. data/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb +14 -13
  27. data/lib/puppet-lint/plugins/check_classes/nested_classes_or_defines.rb +9 -10
  28. data/lib/puppet-lint/plugins/check_classes/parameter_order.rb +40 -31
  29. data/lib/puppet-lint/plugins/check_classes/right_to_left_relationship.rb +3 -2
  30. data/lib/puppet-lint/plugins/check_classes/variable_scope.rb +21 -24
  31. data/lib/puppet-lint/plugins/check_comments/slash_comments.rb +3 -2
  32. data/lib/puppet-lint/plugins/check_comments/star_comments.rb +6 -5
  33. data/lib/puppet-lint/plugins/check_conditionals/case_without_default.rb +21 -20
  34. data/lib/puppet-lint/plugins/check_conditionals/selector_inside_resource.rb +10 -13
  35. data/lib/puppet-lint/plugins/check_documentation/documentation.rb +26 -17
  36. data/lib/puppet-lint/plugins/check_nodes/unquoted_node_name.rb +11 -11
  37. data/lib/puppet-lint/plugins/check_resources/duplicate_params.rb +4 -3
  38. data/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb +17 -18
  39. data/lib/puppet-lint/plugins/check_resources/ensure_not_symlink_target.rb +17 -16
  40. data/lib/puppet-lint/plugins/check_resources/file_mode.rb +22 -23
  41. data/lib/puppet-lint/plugins/check_resources/unquoted_file_mode.rb +14 -13
  42. data/lib/puppet-lint/plugins/check_resources/unquoted_resource_title.rb +9 -8
  43. data/lib/puppet-lint/plugins/check_strings/double_quoted_strings.rb +8 -8
  44. data/lib/puppet-lint/plugins/check_strings/only_variable_string.rb +29 -42
  45. data/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb +5 -4
  46. data/lib/puppet-lint/plugins/check_strings/quoted_booleans.rb +3 -2
  47. data/lib/puppet-lint/plugins/check_strings/single_quote_string_with_variables.rb +4 -3
  48. data/lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb +3 -2
  49. data/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb +9 -8
  50. data/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb +9 -8
  51. data/lib/puppet-lint/plugins/check_whitespace/140chars.rb +9 -9
  52. data/lib/puppet-lint/plugins/check_whitespace/2sp_soft_tabs.rb +4 -3
  53. data/lib/puppet-lint/plugins/check_whitespace/80chars.rb +10 -10
  54. data/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb +23 -22
  55. data/lib/puppet-lint/plugins/check_whitespace/hard_tabs.rb +3 -2
  56. data/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb +3 -2
  57. data/lib/puppet-lint/tasks/puppet-lint.rb +3 -3
  58. data/lib/puppet-lint/version.rb +1 -1
  59. data/puppet-lint.gemspec +4 -4
  60. data/spec/puppet-lint/bin_spec.rb +268 -140
  61. data/spec/puppet-lint/checks_spec.rb +229 -0
  62. data/spec/puppet-lint/configuration_spec.rb +10 -9
  63. data/spec/puppet-lint/data_spec.rb +84 -0
  64. data/spec/puppet-lint/ignore_overrides_spec.rb +71 -40
  65. data/spec/puppet-lint/lexer/token_spec.rb +1 -1
  66. data/spec/puppet-lint/lexer_spec.rb +306 -73
  67. data/spec/puppet-lint/plugins/check_classes/arrow_on_right_operand_line_spec.rb +12 -6
  68. data/spec/puppet-lint/plugins/check_classes/autoloader_layout_spec.rb +10 -10
  69. data/spec/puppet-lint/plugins/check_classes/class_inherits_from_params_class_spec.rb +15 -11
  70. data/spec/puppet-lint/plugins/check_classes/code_on_top_scope_spec.rb +26 -21
  71. data/spec/puppet-lint/plugins/check_classes/inherits_across_namespaces_spec.rb +3 -3
  72. data/spec/puppet-lint/plugins/check_classes/name_contains_uppercase_spec.rb +4 -5
  73. data/spec/puppet-lint/plugins/check_classes/names_containing_dash_spec.rb +13 -0
  74. data/spec/puppet-lint/plugins/check_classes/nested_classes_or_defines_spec.rb +33 -25
  75. data/spec/puppet-lint/plugins/check_classes/parameter_order_spec.rb +80 -55
  76. data/spec/puppet-lint/plugins/check_classes/right_to_left_relationship_spec.rb +2 -2
  77. data/spec/puppet-lint/plugins/check_classes/variable_scope_spec.rb +165 -130
  78. data/spec/puppet-lint/plugins/check_comments/slash_comments_spec.rb +2 -2
  79. data/spec/puppet-lint/plugins/check_comments/star_comments_spec.rb +53 -35
  80. data/spec/puppet-lint/plugins/check_conditionals/case_without_default_spec.rb +59 -49
  81. data/spec/puppet-lint/plugins/check_conditionals/selector_inside_resource_spec.rb +18 -14
  82. data/spec/puppet-lint/plugins/check_documentation/documentation_spec.rb +14 -10
  83. data/spec/puppet-lint/plugins/check_nodes/unquoted_node_name_spec.rb +7 -7
  84. data/spec/puppet-lint/plugins/check_resources/duplicate_params_spec.rb +54 -44
  85. data/spec/puppet-lint/plugins/check_resources/ensure_first_param_spec.rb +114 -89
  86. data/spec/puppet-lint/plugins/check_resources/ensure_not_symlink_target_spec.rb +41 -30
  87. data/spec/puppet-lint/plugins/check_resources/file_mode_spec.rb +46 -40
  88. data/spec/puppet-lint/plugins/check_resources/unquoted_file_mode_spec.rb +46 -40
  89. data/spec/puppet-lint/plugins/check_resources/unquoted_resource_title_spec.rb +95 -71
  90. data/spec/puppet-lint/plugins/check_strings/double_quoted_strings_spec.rb +28 -24
  91. data/spec/puppet-lint/plugins/check_strings/only_variable_string_spec.rb +11 -9
  92. data/spec/puppet-lint/plugins/check_strings/puppet_url_without_modules_spec.rb +1 -2
  93. data/spec/puppet-lint/plugins/check_strings/single_quote_string_with_variables_spec.rb +18 -14
  94. data/spec/puppet-lint/plugins/check_variables/variable_contains_dash_spec.rb +1 -1
  95. data/spec/puppet-lint/plugins/check_variables/variable_is_lowercase_spec.rb +1 -1
  96. data/spec/puppet-lint/plugins/check_whitespace/140chars_spec.rb +22 -15
  97. data/spec/puppet-lint/plugins/check_whitespace/2sp_soft_tabs_spec.rb +8 -6
  98. data/spec/puppet-lint/plugins/check_whitespace/80chars_spec.rb +23 -29
  99. data/spec/puppet-lint/plugins/check_whitespace/arrow_alignment_spec.rb +588 -494
  100. data/spec/puppet-lint/plugins/check_whitespace/hard_tabs_spec.rb +1 -1
  101. data/spec/puppet-lint/plugins/check_whitespace/trailing_whitespace_spec.rb +27 -19
  102. data/spec/puppet-lint_spec.rb +2 -12
  103. data/spec/spec_helper.rb +35 -30
  104. metadata +6 -5
  105. data/lib/puppet-lint/monkeypatches.rb +0 -2
  106. data/lib/puppet-lint/monkeypatches/string_percent.rb +0 -52
  107. data/lib/puppet-lint/monkeypatches/string_prepend.rb +0 -13
@@ -23,8 +23,8 @@ class PuppetLint::Bin
23
23
 
24
24
  begin
25
25
  opts.parse!(@args)
26
- rescue OptionParser::InvalidOption
27
- puts "puppet-lint: #{$!.message}"
26
+ rescue OptionParser::InvalidOption => e
27
+ puts "puppet-lint: #{e.message}"
28
28
  puts "puppet-lint: try 'puppet-lint --help' for more information"
29
29
  return 1
30
30
  end
@@ -35,22 +35,20 @@ class PuppetLint::Bin
35
35
  end
36
36
 
37
37
  if @args[0].nil?
38
- puts "puppet-lint: no file specified"
38
+ puts 'puppet-lint: no file specified'
39
39
  puts "puppet-lint: try 'puppet-lint --help' for more information"
40
40
  return 1
41
41
  end
42
42
 
43
43
  begin
44
44
  path = @args[0]
45
- if File.directory?(path)
46
- path = Dir.glob("#{path}/**/*.pp")
47
- else
48
- path = @args
49
- end
45
+ path = if File.directory?(path)
46
+ Dir.glob("#{path}/**/*.pp")
47
+ else
48
+ @args
49
+ end
50
50
 
51
- if path.length > 1
52
- PuppetLint.configuration.with_filename = true
53
- end
51
+ PuppetLint.configuration.with_filename = true if path.length > 1
54
52
 
55
53
  return_val = 0
56
54
 
@@ -60,24 +58,22 @@ class PuppetLint::Bin
60
58
  l.file = f
61
59
  l.run
62
60
  l.print_problems
63
- puts ',' if f != path.last and PuppetLint.configuration.json
61
+ puts ',' if f != path.last && PuppetLint.configuration.json
64
62
 
65
- if l.errors? or (l.warnings? and PuppetLint.configuration.fail_on_warnings)
63
+ if l.errors? || (l.warnings? && PuppetLint.configuration.fail_on_warnings)
66
64
  return_val = 1
67
65
  end
68
66
 
69
- if PuppetLint.configuration.fix && !l.problems.any? { |e| e[:check] == :syntax }
70
- File.open(f, 'w') do |fd|
71
- fd.write l.manifest
72
- end
67
+ next unless PuppetLint.configuration.fix && l.problems.none? { |r| r[:check] == :syntax }
68
+ File.open(f, 'wb') do |fd|
69
+ fd.write(l.manifest)
73
70
  end
74
71
  end
75
72
  puts ']' if PuppetLint.configuration.json
76
73
 
77
74
  return return_val
78
-
79
75
  rescue PuppetLint::NoCodeError
80
- puts "puppet-lint: no file specified or specified file does not exist"
76
+ puts 'puppet-lint: no file specified or specified file does not exist'
81
77
  puts "puppet-lint: try 'puppet-lint --help' for more information"
82
78
  return 1
83
79
  end
@@ -21,11 +21,11 @@ class PuppetLint::CheckPlugin
21
21
  check
22
22
 
23
23
  @problems.each do |problem|
24
- if problem[:check] != :syntax && PuppetLint::Data.ignore_overrides[problem[:check]].has_key?(problem[:line])
25
- problem[:kind] = :ignored
26
- problem[:reason] = PuppetLint::Data.ignore_overrides[problem[:check]][problem[:line]]
27
- next
28
- end
24
+ next if problem[:check] == :syntax
25
+ next unless PuppetLint::Data.ignore_overrides[problem[:check]].key?(problem[:line])
26
+
27
+ problem[:kind] = :ignored
28
+ problem[:reason] = PuppetLint::Data.ignore_overrides[problem[:check]][problem[:line]]
29
29
  end
30
30
 
31
31
  @problems
@@ -36,14 +36,13 @@ class PuppetLint::CheckPlugin
36
36
  # Returns an Array of problem Hashes.
37
37
  def fix_problems
38
38
  @problems.reject { |problem| problem[:kind] == :ignored || problem[:check] == :syntax }.each do |problem|
39
- if self.respond_to?(:fix)
40
- begin
41
- fix(problem)
42
- rescue PuppetLint::NoFix
43
- # noop
44
- else
45
- problem[:kind] = :fixed
46
- end
39
+ next unless respond_to?(:fix)
40
+
41
+ begin
42
+ fix(problem)
43
+ problem[:kind] = :fixed
44
+ rescue PuppetLint::NoFix # rubocop:disable Lint/HandleExceptions
45
+ # noop
47
46
  end
48
47
  end
49
48
 
@@ -59,6 +58,14 @@ class PuppetLint::CheckPlugin
59
58
  PuppetLint::Data.tokens
60
59
  end
61
60
 
61
+ def add_token(index, token)
62
+ PuppetLint::Data.insert(index, token)
63
+ end
64
+
65
+ def remove_token(token)
66
+ PuppetLint::Data.delete(token)
67
+ end
68
+
62
69
  # Public: Provides the resource titles to the check plugins.
63
70
  #
64
71
  # Returns an Array of PuppetLint::Lexer::Token objects.
@@ -159,14 +166,14 @@ class PuppetLint::CheckPlugin
159
166
  # Returns nothing.
160
167
  def notify(kind, problem)
161
168
  problem[:kind] = kind
162
- problem.merge!(default_info) { |key, v1, v2| v1 }
169
+ problem.merge!(default_info) { |_key, v1, _v2| v1 }
163
170
 
164
- unless [:warning, :error, :fixed].include? kind
165
- raise ArgumentError, "unknown value passed for kind"
171
+ unless [:warning, :error, :fixed].include?(kind)
172
+ raise ArgumentError, 'unknown value passed for kind'
166
173
  end
167
174
 
168
175
  [:message, :line, :column, :check].each do |attr|
169
- unless problem.has_key? attr
176
+ unless problem.key?(attr)
170
177
  raise ArgumentError, "problem hash must contain #{attr.inspect}"
171
178
  end
172
179
  end
@@ -25,10 +25,16 @@ class PuppetLint::Checks
25
25
  PuppetLint::Data.tokens = lexer.tokenise(content)
26
26
  PuppetLint::Data.parse_control_comments
27
27
  rescue PuppetLint::LexerError => e
28
+ message = if e.reason.nil?
29
+ 'Syntax error'
30
+ else
31
+ "Syntax error (#{e.reason})"
32
+ end
33
+
28
34
  problems << {
29
35
  :kind => :error,
30
36
  :check => :syntax,
31
- :message => 'Syntax error (try running `puppet parser validate <file>`)',
37
+ :message => message,
32
38
  :line => e.line_no,
33
39
  :column => e.column,
34
40
  :fullpath => PuppetLint::Data.fullpath,
@@ -41,20 +47,16 @@ class PuppetLint::Checks
41
47
 
42
48
  # Internal: Run the lint checks over the manifest code.
43
49
  #
44
- # fileinfo - A Hash containing the following:
45
- # :fullpath - The expanded path to the file as a String.
46
- # :filename - The name of the file as a String.
47
- # :path - The original path to the file as passed to puppet-lint as
48
- # a String.
50
+ # fileinfo - The path to the file as passed to puppet-lint as a String.
49
51
  # data - The String manifest code to be checked.
50
52
  #
51
53
  # Returns an Array of problem Hashes.
52
54
  def run(fileinfo, data)
53
55
  load_data(fileinfo, data)
54
56
 
55
- checks_run = []
56
57
  enabled_checks.each do |check|
57
58
  klass = PuppetLint.configuration.check_object[check].new
59
+ # FIXME: shadowing #problems
58
60
  problems = klass.run
59
61
 
60
62
  if PuppetLint.configuration.fix
@@ -66,25 +68,34 @@ class PuppetLint::Checks
66
68
 
67
69
  @problems
68
70
  rescue => e
69
- puts <<-END
70
- Whoops! It looks like puppet-lint has encountered an error that it doesn't
71
- know how to handle. Please open an issue at https://github.com/rodjek/puppet-lint
72
- and paste the following output into the issue description.
73
- ---
74
- puppet-lint version: #{PuppetLint::VERSION}
75
- ruby version: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}
76
- platform: #{RUBY_PLATFORM}
77
- file path: #{fileinfo}
78
- file contents:
79
- ```
80
- #{File.read(fileinfo) if File.readable?(fileinfo)}
81
- ```
82
- error:
83
- ```
84
- #{e.class}: #{e.message}
85
- #{e.backtrace.join("\n")}
86
- ```
87
- END
71
+ puts <<-END.gsub(%r{^ {6}}, '')
72
+ Whoops! It looks like puppet-lint has encountered an error that it doesn't
73
+ know how to handle. Please open an issue at https://github.com/rodjek/puppet-lint
74
+ and paste the following output into the issue description.
75
+ ---
76
+ puppet-lint version: #{PuppetLint::VERSION}
77
+ ruby version: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}
78
+ platform: #{RUBY_PLATFORM}
79
+ file path: #{fileinfo}
80
+ END
81
+
82
+ if File.readable?(fileinfo)
83
+ puts [
84
+ 'file contents:',
85
+ '```',
86
+ File.read(fileinfo),
87
+ '```',
88
+ ].join("\n")
89
+ end
90
+
91
+ puts [
92
+ 'error:',
93
+ '```',
94
+ "#{e.class}: #{e.message}",
95
+ e.backtrace.join("\n"),
96
+ '```',
97
+ ].join("\n")
98
+
88
99
  exit 1
89
100
  end
90
101
 
@@ -92,17 +103,17 @@ END
92
103
  #
93
104
  # Returns an Array of String check names.
94
105
  def enabled_checks
95
- @enabled_checks ||= Proc.new do
96
- PuppetLint.configuration.checks.select { |check|
106
+ @enabled_checks ||= begin
107
+ PuppetLint.configuration.checks.select do |check|
97
108
  PuppetLint.configuration.send("#{check}_enabled?")
98
- }
99
- end.call
109
+ end
110
+ end
100
111
  end
101
112
 
102
113
  # Internal: Render the fixed manifest.
103
114
  #
104
115
  # Returns the manifest as a String.
105
116
  def manifest
106
- PuppetLint::Data.tokens.map { |t| t.to_manifest }.join('')
117
+ PuppetLint::Data.tokens.map(&:to_manifest).join('')
107
118
  end
108
119
  end
@@ -49,14 +49,16 @@ class PuppetLint
49
49
  # Signature
50
50
  #
51
51
  # <option>=(value)
52
- def method_missing(method, *args, &block)
53
- if method.to_s =~ /^(\w+)=$/
54
- option = $1
55
- add_option(option.to_s) if settings[option].nil?
56
- settings[option] = args[0]
57
- else
58
- nil
59
- end
52
+ def method_missing(method, *args, &_block)
53
+ super unless method.to_s =~ %r{^(?<option>\w+)=?$}
54
+
55
+ option = Regexp.last_match(:option)
56
+ add_option(option.to_s) if settings[option].nil?
57
+ settings[option] = args[0] if args.length > 1
58
+ end
59
+
60
+ def respond_to_missing?(method, *)
61
+ method.to_s =~ %r{^\w+=?$} || super
60
62
  end
61
63
 
62
64
  # Internal: Add options to the PuppetLint::Configuration object from inside
@@ -149,6 +151,7 @@ class PuppetLint
149
151
  self.fix = false
150
152
  self.json = false
151
153
  self.show_ignored = false
154
+ self.ignore_paths = ['vendor/**/*.pp']
152
155
  end
153
156
  end
154
157
  end
@@ -32,15 +32,28 @@ class PuppetLint::Data
32
32
  @defaults_indexes = nil
33
33
  end
34
34
 
35
+ def ruby1?
36
+ @ruby1 = RbConfig::CONFIG['MAJOR'] == '1' if @ruby1.nil?
37
+ @ruby1
38
+ end
39
+
35
40
  # Public: Get the tokenised manifest.
36
41
  #
37
42
  # Returns an Array of PuppetLint::Lexer::Token objects.
38
43
  def tokens
39
- calling_method = begin
40
- caller[0][/`.*'/][1..-2]
41
- rescue NoMethodError
42
- caller[1][/`.*'/][1..-2]
43
- end
44
+ calling_method = if ruby1?
45
+ begin
46
+ caller[0][%r{`.*'}][1..-2] # rubocop:disable Performance/Caller
47
+ rescue NoMethodError
48
+ caller[1][%r{`.*'}][1..-2] # rubocop:disable Performance/Caller
49
+ end
50
+ else
51
+ begin
52
+ caller(0..0).first[%r{`.*'}][1..-2]
53
+ rescue NoMethodError
54
+ caller(1..1).first[%r{`.*'}][1..-2]
55
+ end
56
+ end
44
57
 
45
58
  if calling_method == 'check'
46
59
  @tokens.dup
@@ -49,6 +62,55 @@ class PuppetLint::Data
49
62
  end
50
63
  end
51
64
 
65
+ # Public: Add new token.
66
+ def insert(index, token)
67
+ current_token = tokens[index - 1]
68
+ token.next_token = current_token.next_token
69
+ token.prev_token = current_token
70
+
71
+ current_token.next_token.prev_token = token unless current_token.next_token.nil?
72
+
73
+ unless formatting_tokens.include?(token.type)
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) }
76
+ unless next_nf_idx.nil?
77
+ next_nf_token = tokens[index + next_nf_idx]
78
+ token.next_code_token = next_nf_token
79
+ next_nf_token.prev_code_token = token
80
+ end
81
+ end
82
+
83
+ if formatting_tokens.include?(current_token.type)
84
+ prev_nf_idx = tokens[0..index - 1].rindex { |r| !formatting_tokens.include?(r.type) }
85
+ unless prev_nf_idx.nil?
86
+ prev_nf_token = tokens[prev_nf_idx]
87
+ token.prev_code_token = prev_nf_token
88
+ prev_nf_token.next_code_token = token
89
+ end
90
+ else
91
+ token.prev_code_token = current_token
92
+ end
93
+
94
+ current_token.next_token = token
95
+ unless formatting_tokens.include?(token.type)
96
+ current_token.next_code_token = token
97
+ end
98
+
99
+ tokens.insert(index, token)
100
+ end
101
+
102
+ # Public: Removes a token
103
+ def delete(token)
104
+ token.next_token.prev_token = token.prev_token unless token.next_token.nil?
105
+ token.prev_token.next_token = token.next_token unless token.prev_token.nil?
106
+ unless formatting_tokens.include?(token.type)
107
+ token.prev_code_token.next_code_token = token.next_code_token unless token.prev_code_token.nil?
108
+ token.next_code_token.prev_code_token = token.prev_code_token unless token.next_code_token.nil?
109
+ end
110
+
111
+ tokens.delete(token)
112
+ end
113
+
52
114
  # Internal: Store the path to the manifest file and populate fullpath and
53
115
  # filename.
54
116
  #
@@ -70,29 +132,27 @@ class PuppetLint::Data
70
132
  #
71
133
  # Returns an Array of PuppetLint::Lexer::Token objects.
72
134
  def title_tokens
73
- @title_tokens ||= Proc.new do
135
+ @title_tokens ||= begin
74
136
  result = []
75
137
  tokens.each_index do |token_idx|
76
138
  if tokens[token_idx].type == :COLON
77
139
  # gather a list of tokens that are resource titles
78
- if tokens[token_idx-1].type == :RBRACK
79
- array_start_idx = tokens.rindex { |r|
140
+ if tokens[token_idx - 1].type == :RBRACK
141
+ array_start_idx = tokens.rindex do |r|
80
142
  r.type == :LBRACK
81
- }
143
+ end
82
144
  title_array_tokens = tokens[(array_start_idx + 1)..(token_idx - 2)]
83
- result += title_array_tokens.select { |token|
84
- {:STRING => true, :NAME => true}.include? token.type
85
- }
145
+ result += title_array_tokens.select do |token|
146
+ { :STRING => true, :NAME => true }.include?(token.type)
147
+ end
86
148
  else
87
149
  next_token = tokens[token_idx].next_code_token
88
- if next_token.type != :LBRACE
89
- result << tokens[token_idx - 1]
90
- end
150
+ result << tokens[token_idx - 1] unless next_token.type == :LBRACE
91
151
  end
92
152
  end
93
153
  end
94
154
  result
95
- end.call
155
+ end
96
156
  end
97
157
 
98
158
  # Internal: Calculate the positions of all resource declarations within the
@@ -109,21 +169,21 @@ class PuppetLint::Data
109
169
  marker = 0
110
170
  result = []
111
171
  tokens.select { |t| t.type == :COLON }.each do |colon_token|
112
- if colon_token.next_code_token && colon_token.next_code_token.type != :LBRACE
113
- start_idx = tokens.index(colon_token)
114
- next if start_idx < marker
115
- end_token = colon_token.next_token_of([:SEMIC, :RBRACE])
116
- end_idx = tokens.index(end_token)
117
-
118
- result << {
119
- :start => start_idx + 1,
120
- :end => end_idx,
121
- :tokens => tokens[start_idx..end_idx],
122
- :type => find_resource_type_token(start_idx),
123
- :param_tokens => find_resource_param_tokens(tokens[start_idx..end_idx]),
124
- }
125
- marker = end_idx
126
- end
172
+ next unless colon_token.next_code_token && colon_token.next_code_token.type != :LBRACE
173
+
174
+ start_idx = tokens.index(colon_token)
175
+ next if start_idx < marker
176
+ end_token = colon_token.next_token_of([:SEMIC, :RBRACE])
177
+ end_idx = tokens.index(end_token)
178
+
179
+ result << {
180
+ :start => start_idx + 1,
181
+ :end => end_idx,
182
+ :tokens => tokens[start_idx..end_idx],
183
+ :type => find_resource_type_token(start_idx),
184
+ :param_tokens => find_resource_param_tokens(tokens[start_idx..end_idx]),
185
+ }
186
+ marker = end_idx
127
187
  end
128
188
  result
129
189
  end
@@ -136,9 +196,9 @@ class PuppetLint::Data
136
196
  #
137
197
  # Returns a Token object.
138
198
  def find_resource_type_token(index)
139
- lbrace_idx = tokens[0..index].rindex { |token|
199
+ lbrace_idx = tokens[0..index].rindex do |token|
140
200
  token.type == :LBRACE && token.prev_code_token.type != :QMARK
141
- }
201
+ end
142
202
  tokens[lbrace_idx].prev_code_token
143
203
  end
144
204
 
@@ -150,9 +210,9 @@ class PuppetLint::Data
150
210
  #
151
211
  # Returns an Array of Token objects.
152
212
  def find_resource_param_tokens(resource_tokens)
153
- resource_tokens.select { |token|
213
+ resource_tokens.select do |token|
154
214
  token.type == :NAME && token.next_code_token.type == :FARROW
155
- }
215
+ end
156
216
  end
157
217
 
158
218
  # Internal: Calculate the positions of all class definitions within the
@@ -210,38 +270,38 @@ class PuppetLint::Data
210
270
  def definition_indexes(type)
211
271
  result = []
212
272
  tokens.each_with_index do |token, i|
213
- if token.type == type
214
- brace_depth = 0
215
- paren_depth = 0
216
- in_params = false
217
- inherited_class = nil
218
- tokens[i+1..-1].each_with_index do |definition_token, j|
219
- case definition_token.type
220
- when :INHERITS
221
- inherited_class = definition_token.next_code_token
222
- when :LPAREN
223
- in_params = true if paren_depth == 0
224
- paren_depth += 1
225
- when :RPAREN
226
- in_params = false if paren_depth == 1
227
- paren_depth -= 1
228
- when :LBRACE
229
- brace_depth += 1
230
- when :RBRACE
231
- brace_depth -= 1
232
- if brace_depth == 0 && !in_params
233
- if token.next_code_token.type != :LBRACE
234
- result << {
235
- :start => i,
236
- :end => i + j + 1,
237
- :tokens => tokens[i..(i + j + 1)],
238
- :param_tokens => param_tokens(tokens[i..(i + j + 1)]),
239
- :type => type,
240
- :name_token => token.next_code_token,
241
- :inherited_token => inherited_class,
242
- }
243
- break
244
- end
273
+ next unless token.type == type
274
+
275
+ brace_depth = 0
276
+ paren_depth = 0
277
+ in_params = false
278
+ inherited_class = nil
279
+ tokens[i + 1..-1].each_with_index do |definition_token, j|
280
+ case definition_token.type
281
+ when :INHERITS
282
+ inherited_class = definition_token.next_code_token
283
+ when :LPAREN
284
+ in_params = true if paren_depth.zero?
285
+ paren_depth += 1
286
+ when :RPAREN
287
+ in_params = false if paren_depth == 1
288
+ paren_depth -= 1
289
+ when :LBRACE
290
+ brace_depth += 1
291
+ when :RBRACE
292
+ brace_depth -= 1
293
+ if brace_depth.zero? && !in_params
294
+ if token.next_code_token.type != :LBRACE
295
+ result << {
296
+ :start => i,
297
+ :end => i + j + 1,
298
+ :tokens => tokens[i..(i + j + 1)],
299
+ :param_tokens => param_tokens(tokens[i..(i + j + 1)]),
300
+ :type => type,
301
+ :name_token => token.next_code_token,
302
+ :inherited_token => inherited_class,
303
+ }
304
+ break
245
305
  end
246
306
  end
247
307
  end
@@ -261,41 +321,42 @@ class PuppetLint::Data
261
321
  # :tokens - An Array consisting of all the Token objects that make up the
262
322
  # function call.
263
323
  def function_indexes
264
- @function_indexes ||= Proc.new do
324
+ @function_indexes ||= begin
265
325
  functions = []
266
326
  tokens.each_with_index do |token, token_idx|
267
- if token.type == :NAME && \
268
- (token_idx == 0 || (token_idx == 1 && tokens[0].type == :WHITESPACE) || token.prev_token.type == :NEWLINE || token.prev_token.type == :INDENT || \
269
- # function in a function
270
- (token.prev_code_token && token.prev_code_token.type == :LPAREN))
271
-
272
- # Hash key
273
- next if token.next_code_token && token.next_code_token.type == :FARROW
274
-
275
- level = 0
276
- real_idx = 0
277
- in_paren = false
278
- tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
279
- break if level == 0 && in_paren
280
- break if level == 0 && cur_token.type == :NEWLINE
281
-
282
- if cur_token.type == :LPAREN
283
- level += 1
284
- in_paren = true
285
- end
286
- level -= 1 if cur_token.type == :RPAREN
287
- real_idx = token_idx + 1 + cur_token_idx
288
- end
289
-
290
- functions << {
291
- :start => token_idx,
292
- :end => real_idx,
293
- :tokens => tokens[token_idx..real_idx],
294
- }
327
+ next unless token.type == :NAME
328
+ next unless token_idx.zero? ||
329
+ (token_idx == 1 && tokens[0].type == :WHITESPACE) ||
330
+ [:NEWLINE, :INDENT].include?(token.prev_token.type) ||
331
+ # function in a function
332
+ (token.prev_code_token && token.prev_code_token.type == :LPAREN)
333
+
334
+ # Hash key
335
+ next if token.next_code_token && token.next_code_token.type == :FARROW
336
+
337
+ level = 0
338
+ real_idx = 0
339
+ in_paren = false
340
+ tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
341
+ break if level.zero? && in_paren
342
+ break if level.zero? && cur_token.type == :NEWLINE
343
+
344
+ if cur_token.type == :LPAREN
345
+ level += 1
346
+ in_paren = true
347
+ end
348
+ level -= 1 if cur_token.type == :RPAREN
349
+ real_idx = token_idx + 1 + cur_token_idx
295
350
  end
351
+
352
+ functions << {
353
+ :start => token_idx,
354
+ :end => real_idx,
355
+ :tokens => tokens[token_idx..real_idx],
356
+ }
296
357
  end
297
358
  functions
298
- end.call
359
+ end
299
360
  end
300
361
 
301
362
  # Internal: Calculate the positions of all array values within
@@ -309,28 +370,29 @@ class PuppetLint::Data
309
370
  # :tokens - An Array consisting of all the Token objects that make up the
310
371
  # array value.
311
372
  def array_indexes
312
- @array_indexes ||= Proc.new do
373
+ @array_indexes ||= begin
313
374
  arrays = []
314
375
  tokens.each_with_index do |token, token_idx|
315
- if token.type == :LBRACK
316
- real_idx = 0
317
- tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
318
- real_idx = token_idx + 1 + cur_token_idx
319
- break if cur_token.type == :RBRACK
320
- end
376
+ next unless token.type == :LBRACK
321
377
 
322
- # Ignore resource references
323
- next if token.prev_code_token && \
324
- token.prev_code_token.type == :CLASSREF
325
- arrays << {
326
- :start => token_idx,
327
- :end => real_idx,
328
- :tokens => tokens[token_idx..real_idx],
329
- }
378
+ real_idx = 0
379
+ tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
380
+ real_idx = token_idx + 1 + cur_token_idx
381
+ break if cur_token.type == :RBRACK
330
382
  end
383
+
384
+ # Ignore resource references
385
+ next if token.prev_code_token &&
386
+ token.prev_code_token.type == :CLASSREF
387
+
388
+ arrays << {
389
+ :start => token_idx,
390
+ :end => real_idx,
391
+ :tokens => tokens[token_idx..real_idx],
392
+ }
331
393
  end
332
394
  arrays
333
- end.call
395
+ end
334
396
  end
335
397
 
336
398
  # Internal: Calculate the positions of all hash values within
@@ -344,31 +406,31 @@ class PuppetLint::Data
344
406
  # :tokens - An Array consisting of all the Token objects that make up the
345
407
  # hash value.
346
408
  def hash_indexes
347
- @hash_indexes ||= Proc.new do
409
+ @hash_indexes ||= begin
348
410
  hashes = []
349
411
  tokens.each_with_index do |token, token_idx|
412
+ next unless token.type == :LBRACE
350
413
  next unless token.prev_code_token
351
- next unless [:EQUALS, :ISEQUAL, :FARROW, :LPAREN].include? token.prev_code_token.type
352
- if token.type == :LBRACE
353
- level = 0
354
- real_idx = 0
355
- tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
356
- real_idx = token_idx + 1 + cur_token_idx
357
-
358
- level += 1 if cur_token.type == :LBRACE
359
- level -= 1 if cur_token.type == :RBRACE
360
- break if level < 0
361
- end
414
+ next unless [:EQUALS, :ISEQUAL, :FARROW, :LPAREN].include?(token.prev_code_token.type)
415
+
416
+ level = 0
417
+ real_idx = 0
418
+ tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
419
+ real_idx = token_idx + 1 + cur_token_idx
362
420
 
363
- hashes << {
364
- :start => token_idx,
365
- :end => real_idx,
366
- :tokens => tokens[token_idx..real_idx],
367
- }
421
+ level += 1 if cur_token.type == :LBRACE
422
+ level -= 1 if cur_token.type == :RBRACE
423
+ break if level < 0
368
424
  end
425
+
426
+ hashes << {
427
+ :start => token_idx,
428
+ :end => real_idx,
429
+ :tokens => tokens[token_idx..real_idx],
430
+ }
369
431
  end
370
432
  hashes
371
- end.call
433
+ end
372
434
  end
373
435
 
374
436
  # Internal: Calculate the positions of all defaults declarations within
@@ -382,27 +444,27 @@ class PuppetLint::Data
382
444
  # :tokens - An Array consisting of all the Token objects that make up the
383
445
  # defaults declaration.
384
446
  def defaults_indexes
385
- @defaults_indexes ||= Proc.new do
447
+ @defaults_indexes ||= begin
386
448
  defaults = []
387
449
  tokens.each_with_index do |token, token_idx|
388
- if token.type == :CLASSREF && token.next_code_token && \
389
- token.next_code_token.type == :LBRACE
390
- real_idx = 0
450
+ next unless token.type == :CLASSREF
451
+ next unless token.next_code_token && token.next_code_token.type == :LBRACE
391
452
 
392
- tokens[token_idx+1..-1].each_with_index do |cur_token, cur_token_idx|
393
- real_idx = token_idx + 1 + cur_token_idx
394
- break if cur_token.type == :RBRACE
395
- end
453
+ real_idx = 0
396
454
 
397
- defaults << {
398
- :start => token_idx,
399
- :end => real_idx,
400
- :tokens => tokens[token_idx..real_idx],
401
- }
455
+ tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
456
+ real_idx = token_idx + 1 + cur_token_idx
457
+ break if cur_token.type == :RBRACE
402
458
  end
459
+
460
+ defaults << {
461
+ :start => token_idx,
462
+ :end => real_idx,
463
+ :tokens => tokens[token_idx..real_idx],
464
+ }
403
465
  end
404
466
  defaults
405
- end.call
467
+ end
406
468
  end
407
469
 
408
470
  # Internal: Finds all the tokens that make up the defined type or class
@@ -424,17 +486,17 @@ class PuppetLint::Data
424
486
  lparen_idx = i if depth == 1
425
487
  elsif token.type == :RPAREN
426
488
  depth -= 1
427
- if depth == 0
489
+ if depth.zero?
428
490
  rparen_idx = i
429
491
  break
430
492
  end
431
- elsif token.type == :LBRACE && depth == 0
493
+ elsif token.type == :LBRACE && depth.zero?
432
494
  # no parameters
433
495
  break
434
496
  end
435
497
  end
436
498
 
437
- if lparen_idx.nil? or rparen_idx.nil?
499
+ if lparen_idx.nil? || rparen_idx.nil?
438
500
  nil
439
501
  else
440
502
  these_tokens[(lparen_idx + 1)..(rparen_idx - 1)]
@@ -465,25 +527,35 @@ class PuppetLint::Data
465
527
  # Returns nothing.
466
528
  def parse_control_comments
467
529
  @ignore_overrides.each_key { |check| @ignore_overrides[check].clear }
468
- control_re = /\A(lint:\S+)(\s+lint:\S+)*(.*)/
469
530
 
470
531
  comment_token_types = Set[:COMMENT, :MLCOMMENT, :SLASH_COMMENT]
471
532
 
472
- comment_tokens = tokens.select { |token|
533
+ comment_tokens = tokens.select do |token|
473
534
  comment_token_types.include?(token.type)
474
- }
475
- control_comment_tokens = comment_tokens.select { |token|
476
- token.value.strip =~ /\Alint:(ignore:[\w\d]+|endignore)/
477
- }
535
+ end
536
+ control_comment_tokens = comment_tokens.select do |token|
537
+ token.value.strip =~ %r{\Alint\:(ignore\:[\w\d]+|endignore)}
538
+ end
478
539
 
479
540
  stack = []
480
541
  control_comment_tokens.each do |token|
481
- comment_data = control_re.match(token.value.strip).to_a[1..-1].compact.map(&:strip)
482
- if comment_data.last =~ /\Alint:(ignore|endignore)/
483
- comment_data << ''
542
+ comment_data = []
543
+ reason = []
544
+
545
+ comment_words = token.value.strip.split(%r{\s+})
546
+ comment_words.each_with_index do |word, i|
547
+ if word =~ %r{\Alint\:(ignore|endignore)}
548
+ comment_data << word
549
+ else
550
+ # Once we reach the first non-controlcomment word, assume the rest
551
+ # of the words are the reason.
552
+ reason = comment_words[i..-1]
553
+ break
554
+ end
484
555
  end
485
- reason = comment_data.pop
556
+
486
557
  stack_add = []
558
+
487
559
  comment_data.each do |control|
488
560
  split_control = control.split(':')
489
561
  command = split_control[1]
@@ -493,9 +565,9 @@ class PuppetLint::Data
493
565
  if token.prev_token && !Set[:NEWLINE, :INDENT].include?(token.prev_token.type)
494
566
  # control comment at the end of the line, override applies to
495
567
  # a single line only
496
- (ignore_overrides[check] ||= {})[token.line] = reason
568
+ (ignore_overrides[check] ||= {})[token.line] = reason.join(' ')
497
569
  else
498
- stack_add << [token.line, reason, check]
570
+ stack_add << [token.line, reason.join(' '), check]
499
571
  end
500
572
  else
501
573
  top_override = stack.pop
@@ -505,10 +577,10 @@ class PuppetLint::Data
505
577
  puts "WARNING: lint:endignore comment with no opening lint:ignore:<check> comment found on line #{token.line}"
506
578
  else
507
579
  top_override.each do |start|
508
- unless start.nil?
509
- (start[0]..token.line).each do |i|
510
- (ignore_overrides[start[2]] ||= {})[i] = start[1]
511
- end
580
+ next if start.nil?
581
+
582
+ (start[0]..token.line).each do |i|
583
+ (ignore_overrides[start[2]] ||= {})[i] = start[1]
512
584
  end
513
585
  end
514
586
  end