Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CONT-666) Skip classref types #93

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
chelnak marked this conversation as resolved.
Show resolved Hide resolved
# the token chain..
#
# Returns a Boolean.
chelnak marked this conversation as resolved.
Show resolved Hide resolved
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
chelnak marked this conversation as resolved.
Show resolved Hide resolved
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)
chelnak marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

describe '.insert' do
Expand Down