Skip to content

Commit dde7c49

Browse files
committed
(CONT-666) Skip CLASSREF types
This commit fixes voxpupuli/puppet-lint-trailing_comma-check#31. It appears that the `resource_indexes` method cannot differentiate between a resource declaration and a defaults declaration. If we compare the two, you can see that a defaults declaration type starts with a capital letter whereas a resource declaration does not. ```puppet Service { 'foo': ensure => running, } ``` ```puppet service { 'foo': ensure => running, } ``` When the `resource_indexes` method runs, it initially selects tokens by the presence of a colon then works to the right to decide if there are any violations. That means we are actually only starting to collect information about the declaration after the name.. so we never know the actual type of the declaration. ```puppet Service { 'foo': ↑ (here) ensure => running, } ``` https://github.com/puppetlabs/puppet-lint/blob/main/lib/puppet-lint/data.rb#L167 In contrast, `defaults_indexes` will skip any tokens that do not have a type of `:CLASSREF` because we know that a defaults declaration will always start with a capital letter (See [puppet-lint.com](http://puppet-lint.com/developer/tokens/) for more information on token types). This commit fixes the above by ensuring that `resource_indexes` checks the type of resource that it is analysing and skips the itteration if it detects a `:CLASSREF`.
1 parent 8a6442d commit dde7c49

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/puppet-lint/data.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ def title_tokens
155155
end
156156
end
157157

158+
# Internal: Determine if the given token contains a CLASSREF in
159+
# the token chain..
160+
#
161+
# Returns a Boolean.
162+
def classref?(token)
163+
current_token = token
164+
while (current_token = current_token.prev_code_token)
165+
return true if current_token.type == :CLASSREF
166+
return false if current_token.type == :NAME
167+
end
168+
end
169+
158170
# Internal: Calculate the positions of all resource declarations within the
159171
# tokenised manifest. These positions only point to the content of the
160172
# resource declarations, they do not include resource types or titles.
@@ -170,6 +182,7 @@ def resource_indexes
170182
result = []
171183
tokens.select { |t| t.type == :COLON }.each do |colon_token|
172184
next unless colon_token.next_code_token && colon_token.next_code_token.type != :LBRACE
185+
next if classref?(colon_token)
173186

174187
rel_start_idx = tokens[marker..-1].index(colon_token)
175188
break if rel_start_idx.nil?

0 commit comments

Comments
 (0)