Skip to content

Commit

Permalink
Merge pull request #93 from puppetlabs/CONT-666-skip_classref_types
Browse files Browse the repository at this point in the history
(CONT-666) Skip classref types
  • Loading branch information
GSPatton authored Feb 28, 2023
2 parents 8a6442d + df978a2 commit 2fd501a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/puppet-lint/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ def title_tokens
end
end

# Internal: Determine if the given token contains a CLASSREF in
# the token chain..
#
# Returns a Boolean.
def classref?(token)
current_token = token
while (current_token = current_token.prev_code_token)
return true if current_token.type == :CLASSREF
return false if current_token.type == :NAME
end
end

# Internal: Calculate the positions of all resource declarations within the
# tokenised manifest. These positions only point to the content of the
# resource declarations, they do not include resource types or titles.
Expand All @@ -170,6 +182,7 @@ def resource_indexes
result = []
tokens.select { |t| t.type == :COLON }.each do |colon_token|
next unless colon_token.next_code_token && colon_token.next_code_token.type != :LBRACE
next if classref?(colon_token)

rel_start_idx = tokens[marker..-1].index(colon_token)
break if rel_start_idx.nil?
Expand Down
36 changes: 36 additions & 0 deletions spec/unit/puppet-lint/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@
}
end
end

context 'when given a defaults declaration' do
let(:manifest) { "Service { 'foo': }" }

it 'returns an empty array' do
expect(data.resource_indexes).to eq([])
end
end

context 'when given a set of resource declarations' do
let(:manifest) { <<-MANIFEST }
service {
'foo':
ensure => running,
}
service {
'bar':
ensure => running;
'foobar':
ensure => stopped;
}
service { ['first', 'second']:
ensure => running,
}
service { 'third':
ensure => running,
}
MANIFEST

it 'returns an array of resource indexes' do
expect(data.resource_indexes.length).to eq(5)
end
end
end

describe '.insert' do
Expand Down

0 comments on commit 2fd501a

Please sign in to comment.