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.
- checksums.yaml +4 -4
- data/.rubocop.yml +74 -0
- data/.rubocop_todo.yml +89 -0
- data/.travis.yml +11 -9
- data/CHANGELOG.md +54 -0
- data/Gemfile +3 -0
- data/Rakefile +14 -3
- data/appveyor.yml +1 -4
- data/bin/puppet-lint +1 -1
- data/lib/puppet-lint.rb +25 -21
- data/lib/puppet-lint/bin.rb +15 -19
- data/lib/puppet-lint/checkplugin.rb +24 -17
- data/lib/puppet-lint/checks.rb +42 -31
- data/lib/puppet-lint/configuration.rb +11 -8
- data/lib/puppet-lint/data.rb +236 -164
- data/lib/puppet-lint/lexer.rb +225 -203
- data/lib/puppet-lint/lexer/token.rb +34 -18
- data/lib/puppet-lint/optparser.rb +33 -30
- data/lib/puppet-lint/plugins.rb +42 -38
- data/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb +26 -28
- data/lib/puppet-lint/plugins/check_classes/autoloader_layout.rb +21 -20
- data/lib/puppet-lint/plugins/check_classes/class_inherits_from_params_class.rb +8 -9
- data/lib/puppet-lint/plugins/check_classes/code_on_top_scope.rb +9 -8
- data/lib/puppet-lint/plugins/check_classes/inherits_across_namespaces.rb +12 -11
- data/lib/puppet-lint/plugins/check_classes/names_containing_dash.rb +13 -12
- data/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb +14 -13
- data/lib/puppet-lint/plugins/check_classes/nested_classes_or_defines.rb +9 -10
- data/lib/puppet-lint/plugins/check_classes/parameter_order.rb +40 -31
- data/lib/puppet-lint/plugins/check_classes/right_to_left_relationship.rb +3 -2
- data/lib/puppet-lint/plugins/check_classes/variable_scope.rb +21 -24
- data/lib/puppet-lint/plugins/check_comments/slash_comments.rb +3 -2
- data/lib/puppet-lint/plugins/check_comments/star_comments.rb +6 -5
- data/lib/puppet-lint/plugins/check_conditionals/case_without_default.rb +21 -20
- data/lib/puppet-lint/plugins/check_conditionals/selector_inside_resource.rb +10 -13
- data/lib/puppet-lint/plugins/check_documentation/documentation.rb +26 -17
- data/lib/puppet-lint/plugins/check_nodes/unquoted_node_name.rb +11 -11
- data/lib/puppet-lint/plugins/check_resources/duplicate_params.rb +4 -3
- data/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb +17 -18
- data/lib/puppet-lint/plugins/check_resources/ensure_not_symlink_target.rb +17 -16
- data/lib/puppet-lint/plugins/check_resources/file_mode.rb +22 -23
- data/lib/puppet-lint/plugins/check_resources/unquoted_file_mode.rb +14 -13
- data/lib/puppet-lint/plugins/check_resources/unquoted_resource_title.rb +9 -8
- data/lib/puppet-lint/plugins/check_strings/double_quoted_strings.rb +8 -8
- data/lib/puppet-lint/plugins/check_strings/only_variable_string.rb +29 -42
- data/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb +5 -4
- data/lib/puppet-lint/plugins/check_strings/quoted_booleans.rb +3 -2
- data/lib/puppet-lint/plugins/check_strings/single_quote_string_with_variables.rb +4 -3
- data/lib/puppet-lint/plugins/check_strings/variables_not_enclosed.rb +3 -2
- data/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb +9 -8
- data/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb +9 -8
- data/lib/puppet-lint/plugins/check_whitespace/140chars.rb +9 -9
- data/lib/puppet-lint/plugins/check_whitespace/2sp_soft_tabs.rb +4 -3
- data/lib/puppet-lint/plugins/check_whitespace/80chars.rb +10 -10
- data/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb +23 -22
- data/lib/puppet-lint/plugins/check_whitespace/hard_tabs.rb +3 -2
- data/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb +3 -2
- data/lib/puppet-lint/tasks/puppet-lint.rb +3 -3
- data/lib/puppet-lint/version.rb +1 -1
- data/puppet-lint.gemspec +4 -4
- data/spec/puppet-lint/bin_spec.rb +268 -140
- data/spec/puppet-lint/checks_spec.rb +229 -0
- data/spec/puppet-lint/configuration_spec.rb +10 -9
- data/spec/puppet-lint/data_spec.rb +84 -0
- data/spec/puppet-lint/ignore_overrides_spec.rb +71 -40
- data/spec/puppet-lint/lexer/token_spec.rb +1 -1
- data/spec/puppet-lint/lexer_spec.rb +306 -73
- data/spec/puppet-lint/plugins/check_classes/arrow_on_right_operand_line_spec.rb +12 -6
- data/spec/puppet-lint/plugins/check_classes/autoloader_layout_spec.rb +10 -10
- data/spec/puppet-lint/plugins/check_classes/class_inherits_from_params_class_spec.rb +15 -11
- data/spec/puppet-lint/plugins/check_classes/code_on_top_scope_spec.rb +26 -21
- data/spec/puppet-lint/plugins/check_classes/inherits_across_namespaces_spec.rb +3 -3
- data/spec/puppet-lint/plugins/check_classes/name_contains_uppercase_spec.rb +4 -5
- data/spec/puppet-lint/plugins/check_classes/names_containing_dash_spec.rb +13 -0
- data/spec/puppet-lint/plugins/check_classes/nested_classes_or_defines_spec.rb +33 -25
- data/spec/puppet-lint/plugins/check_classes/parameter_order_spec.rb +80 -55
- data/spec/puppet-lint/plugins/check_classes/right_to_left_relationship_spec.rb +2 -2
- data/spec/puppet-lint/plugins/check_classes/variable_scope_spec.rb +165 -130
- data/spec/puppet-lint/plugins/check_comments/slash_comments_spec.rb +2 -2
- data/spec/puppet-lint/plugins/check_comments/star_comments_spec.rb +53 -35
- data/spec/puppet-lint/plugins/check_conditionals/case_without_default_spec.rb +59 -49
- data/spec/puppet-lint/plugins/check_conditionals/selector_inside_resource_spec.rb +18 -14
- data/spec/puppet-lint/plugins/check_documentation/documentation_spec.rb +14 -10
- data/spec/puppet-lint/plugins/check_nodes/unquoted_node_name_spec.rb +7 -7
- data/spec/puppet-lint/plugins/check_resources/duplicate_params_spec.rb +54 -44
- data/spec/puppet-lint/plugins/check_resources/ensure_first_param_spec.rb +114 -89
- data/spec/puppet-lint/plugins/check_resources/ensure_not_symlink_target_spec.rb +41 -30
- data/spec/puppet-lint/plugins/check_resources/file_mode_spec.rb +46 -40
- data/spec/puppet-lint/plugins/check_resources/unquoted_file_mode_spec.rb +46 -40
- data/spec/puppet-lint/plugins/check_resources/unquoted_resource_title_spec.rb +95 -71
- data/spec/puppet-lint/plugins/check_strings/double_quoted_strings_spec.rb +28 -24
- data/spec/puppet-lint/plugins/check_strings/only_variable_string_spec.rb +11 -9
- data/spec/puppet-lint/plugins/check_strings/puppet_url_without_modules_spec.rb +1 -2
- data/spec/puppet-lint/plugins/check_strings/single_quote_string_with_variables_spec.rb +18 -14
- data/spec/puppet-lint/plugins/check_variables/variable_contains_dash_spec.rb +1 -1
- data/spec/puppet-lint/plugins/check_variables/variable_is_lowercase_spec.rb +1 -1
- data/spec/puppet-lint/plugins/check_whitespace/140chars_spec.rb +22 -15
- data/spec/puppet-lint/plugins/check_whitespace/2sp_soft_tabs_spec.rb +8 -6
- data/spec/puppet-lint/plugins/check_whitespace/80chars_spec.rb +23 -29
- data/spec/puppet-lint/plugins/check_whitespace/arrow_alignment_spec.rb +588 -494
- data/spec/puppet-lint/plugins/check_whitespace/hard_tabs_spec.rb +1 -1
- data/spec/puppet-lint/plugins/check_whitespace/trailing_whitespace_spec.rb +27 -19
- data/spec/puppet-lint_spec.rb +2 -12
- data/spec/spec_helper.rb +35 -30
- metadata +6 -5
- data/lib/puppet-lint/monkeypatches.rb +0 -2
- data/lib/puppet-lint/monkeypatches/string_percent.rb +0 -52
- data/lib/puppet-lint/monkeypatches/string_prepend.rb +0 -13
data/lib/puppet-lint/bin.rb
CHANGED
@@ -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: #{
|
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
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
61
|
+
puts ',' if f != path.last && PuppetLint.configuration.json
|
64
62
|
|
65
|
-
if l.errors?
|
63
|
+
if l.errors? || (l.warnings? && PuppetLint.configuration.fail_on_warnings)
|
66
64
|
return_val = 1
|
67
65
|
end
|
68
66
|
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
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]
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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) { |
|
169
|
+
problem.merge!(default_info) { |_key, v1, _v2| v1 }
|
163
170
|
|
164
|
-
unless [:warning, :error, :fixed].include?
|
165
|
-
raise ArgumentError,
|
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.
|
176
|
+
unless problem.key?(attr)
|
170
177
|
raise ArgumentError, "problem hash must contain #{attr.inspect}"
|
171
178
|
end
|
172
179
|
end
|
data/lib/puppet-lint/checks.rb
CHANGED
@@ -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 =>
|
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 -
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
```
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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 ||=
|
96
|
-
PuppetLint.configuration.checks.select
|
106
|
+
@enabled_checks ||= begin
|
107
|
+
PuppetLint.configuration.checks.select do |check|
|
97
108
|
PuppetLint.configuration.send("#{check}_enabled?")
|
98
|
-
|
99
|
-
end
|
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
|
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, &
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
data/lib/puppet-lint/data.rb
CHANGED
@@ -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 =
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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 ||=
|
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
|
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
|
84
|
-
{:STRING => true, :NAME => true}.include?
|
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
|
-
|
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
|
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
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
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
|
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
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
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 ||=
|
324
|
+
@function_indexes ||= begin
|
265
325
|
functions = []
|
266
326
|
tokens.each_with_index do |token, token_idx|
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
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
|
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 ||=
|
373
|
+
@array_indexes ||= begin
|
313
374
|
arrays = []
|
314
375
|
tokens.each_with_index do |token, token_idx|
|
315
|
-
|
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
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
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
|
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 ||=
|
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?
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
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
|
-
|
364
|
-
|
365
|
-
|
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
|
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 ||=
|
447
|
+
@defaults_indexes ||= begin
|
386
448
|
defaults = []
|
387
449
|
tokens.each_with_index do |token, token_idx|
|
388
|
-
|
389
|
-
|
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
|
-
|
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
|
-
|
398
|
-
|
399
|
-
|
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
|
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
|
489
|
+
if depth.zero?
|
428
490
|
rparen_idx = i
|
429
491
|
break
|
430
492
|
end
|
431
|
-
elsif token.type == :LBRACE && depth
|
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?
|
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
|
533
|
+
comment_tokens = tokens.select do |token|
|
473
534
|
comment_token_types.include?(token.type)
|
474
|
-
|
475
|
-
control_comment_tokens = comment_tokens.select
|
476
|
-
token.value.strip =~
|
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 =
|
482
|
-
|
483
|
-
|
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
|
-
|
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
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
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
|