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.
  • Loading branch information
chelnak committed Oct 20, 2022
1 parent 9399d7c commit ea1b036
Showing 1 changed file with 12 additions and 85 deletions.
97 changes: 12 additions & 85 deletions lib/puppet-lint/plugins/check_trailing_comma.rb
Original file line number Diff line number Diff line change
@@ -1,88 +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 = []
tokens.each_with_index do |token, token_idx|
next unless token.type == :CLASSREF && token.next_code_token && \
token.next_code_token.type == :LBRACE && \
token.prev_code_token && \
# Ensure that we aren't matching a function return type:
token.prev_code_token.type != :RSHIFT && \
# Or a conditional matching a type:
![:MATCH, :NOMATCH].include?(token.prev_code_token.type)
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 == :RBRACE
end

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

def check_elem(elem, except_type)
lbo_token = elem[:tokens][-1].prev_code_token

Expand Down Expand Up @@ -112,8 +28,11 @@ 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 All @@ -124,6 +43,14 @@ def check

# Defaults
defaults_indexes.each do |defaults|
next unless defaults[:tokens].any? do |token|
token.type == :CLASSREF && \
token.next_code_token && \
token.next_code_token.type == :LBRACE && \
token.prev_code_token && \
token.prev_code_token.type != :RSHIFT && \
![:MATCH, :NOMATCH].include?(token.prev_code_token.type)
end
check_elem(defaults, :LBRACE)
end
end
Expand Down

0 comments on commit ea1b036

Please sign in to comment.