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

(MAINT) Rubocop updates #27

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ env:
BUNDLE_WITHOUT: release

jobs:
lint:
runs-on: ubuntu-latest
name: Rubocop
steps:
- uses: actions/checkout@v2
- name: Install Ruby ${{ matrix.ruby }}
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
bundler-cache: true
- name: Rubocop
run: |
bundle exec rubocop

test:
needs: lint
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- ruby: "2.4"
- ruby: "2.5"
- ruby: "2.6"
- ruby: "2.7"
- ruby: "3.0"
- ruby: "3.1"
Expand Down
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
inherit_gem:
puppet-lint:
- .rubocop.yml
AllCops:
Exclude:
- "vendor/**/*"
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ gemspec

group :release do
gem 'github_changelog_generator', require: false
gem 'faraday-retry', '~> 2.0.0', require: false
end

group :coverage, optional: ENV['COVERAGE']!='yes' do
gem 'simplecov-console', :require => false
gem 'codecov', :require => false
end

group :rubocop do
gem 'rubocop', '~> 1.6.1', require: false
gem 'rubocop-rspec', '~> 2.0.1', require: false
gem 'rubocop-performance', '~> 1.9.1', require: false
end
139 changes: 67 additions & 72 deletions lib/puppet-lint/plugins/check_trailing_comma.rb
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
PuppetLint.new_check(:trailing_comma) do
def array_indexes
chelnak marked this conversation as resolved.
Show resolved Hide resolved
@array_indexes ||= Proc.new do
@array_indexes ||= proc {
chelnak marked this conversation as resolved.
Show resolved Hide resolved
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

# 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],
}
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
end.call
}.call
end

def hash_indexes
@hash_indexes ||= Proc.new do
@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
if 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],
}
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
end.call
}.call
end

def defaults_indexes
@defaults_indexes ||= Proc.new do
@defaults_indexes ||= proc {
defaults = []
tokens.each_with_index do |token, token_idx|
if 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],
}
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
end.call
}.call
end

def check_elem(elem, except_type)
Expand All @@ -93,20 +90,18 @@ def check_elem(elem, except_type)
# until we find HEREDOC_OPEN. That is the line which should
# be examined for the comma.
if lbo_token && [:HEREDOC, :HEREDOC_POST].include?(lbo_token.type)
while lbo_token && lbo_token.type != :HEREDOC_OPEN do
lbo_token = lbo_token.prev_code_token
end
lbo_token = lbo_token.prev_code_token while lbo_token && lbo_token.type != :HEREDOC_OPEN
end

if lbo_token && lbo_token.type != except_type && \
elem[:tokens][-1].type != :SEMIC && \
lbo_token.type != :COMMA && \
lbo_token.next_token.type == :NEWLINE
elem[:tokens][-1].type != :SEMIC && \
lbo_token.type != :COMMA && \
lbo_token.next_token.type == :NEWLINE
notify :warning, {
:message => 'missing trailing comma after last element',
:line => lbo_token.next_token.line,
:column => lbo_token.next_token.column,
:token => lbo_token.next_token,
message: 'missing trailing comma after last element',
line: lbo_token.next_token.line,
column: lbo_token.next_token.column,
token: lbo_token.next_token,
}
end
end
Expand Down Expand Up @@ -138,7 +133,7 @@ def fix(problem)
:COMMA,
',',
problem[:token].line,
problem[:token].column
problem[:token].column,
)

idx = tokens.index(problem[:token])
Expand Down
4 changes: 2 additions & 2 deletions puppet-lint-trailing_comma-check.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
A puppet-lint plugin to check for missing trailing commas.
EOF

spec.required_ruby_version = Gem::Requirement.new(">= 2.0".freeze)
spec.required_ruby_version = Gem::Requirement.new(">= 2.7".freeze)

spec.add_dependency 'puppet-lint', '>= 1.0', '< 3.0'
spec.add_dependency 'puppet-lint', '~> 3.0.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rspec-its', '~> 1.0'
spec.add_development_dependency 'rspec-collection_matchers', '~> 1.0'
Expand Down
Loading