Skip to content

Commit

Permalink
Use Procs to add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
raphink committed Feb 10, 2015
1 parent e0b43b8 commit 18f5503
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions lib/puppet-lint/plugins/check_trailing_comma.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
PuppetLint.new_check(:trailing_comma) do
def array_indexes
results = []
tokens.each_with_index do |token, token_idx|
if 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
@array_indexes ||= Proc.new do
arrays = []
tokens.each_with_index do |token, token_idx|
if 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

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

def defaults_indexes
results = []
tokens.each_with_index do |token, token_idx|
if token.type == :CLASSREF && token.next_code_token && \
token.next_code_token.type == :LBRACE
real_idx = 0
@defaults_indexes ||= Proc.new do
defaults = []
tokens.each_with_index do |token, token_idx|
if token.type == :CLASSREF && token.next_code_token && \
token.next_code_token.type == :LBRACE
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
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

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

def check_elem(elem, except_type)
Expand Down

0 comments on commit 18f5503

Please sign in to comment.