Skip to content

Commit

Permalink
Remove *_indexes functions
Browse files Browse the repository at this point in the history
Prior to this commit this check duplicated some of the indexing
functions from `PuppetLint::Data`. This was done because they had not been
exposed publicly.

As of v3.0.0 the duplicated indexe functions are exposed by
`PuppetLint::CheckPlugin` and therefore removed from this check.

The exception in this case is `defauls_indexes`. This remains duplicated
in the check as it has been modified and will need further attention.
  • Loading branch information
chelnak committed Oct 20, 2022
1 parent 9399d7c commit ebb94fc
Showing 1 changed file with 5 additions and 57 deletions.
62 changes: 5 additions & 57 deletions lib/puppet-lint/plugins/check_trailing_comma.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,4 @@
PuppetLint.new_check(:trailing_comma) do
def array_indexes
@array_indexes ||= proc {
arrays = []
tokens.each_with_index do |token, token_idx|
next unless token.type == :LBRACK
real_idx = 0
tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
real_idx = token_idx + 1 + cur_token_idx
break if cur_token.type == :RBRACK
end

# Ignore resource references
next if token.prev_code_token && \
token.prev_code_token.type == :CLASSREF

# Ignore data types
next if token.prev_code_token && \
token.prev_code_token.type == :TYPE

arrays << {
start: token_idx,
end: real_idx,
tokens: tokens[token_idx..real_idx],
}
end
arrays
}.call
end

def hash_indexes
@hash_indexes ||= proc {
hashes = []
tokens.each_with_index do |token, token_idx|
next unless token.prev_code_token
next unless [:EQUALS, :ISEQUAL, :FARROW, :LPAREN].include? token.prev_code_token.type
next unless token.type == :LBRACE
level = 0
real_idx = 0
tokens[token_idx + 1..-1].each_with_index do |cur_token, cur_token_idx|
real_idx = token_idx + 1 + cur_token_idx

level += 1 if cur_token.type == :LBRACE
level -= 1 if cur_token.type == :RBRACE
break if level < 0
end

hashes << {
start: token_idx,
end: real_idx,
tokens: tokens[token_idx..real_idx],
}
end
hashes
}.call
end

def defaults_indexes
@defaults_indexes ||= proc {
defaults = []
Expand Down Expand Up @@ -112,8 +56,12 @@ def check
check_elem(resource, :COLON)
end

# Arrays
array_indexes.each do |array|
next if array[:tokens].any? do |token|
token.prev_code_token && \
token.prev_code_token.type == :TYPE
end

check_elem(array, :LBRACK)
end

Expand Down

0 comments on commit ebb94fc

Please sign in to comment.