Skip to content

Commit

Permalink
Rubocop: Update syntax to Ruby 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Dec 16, 2021
1 parent a983e7f commit bdac0cd
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ Style/FileName:
- 'lib/puppet-lint.rb'
- 'lib/puppet-lint/tasks/puppet-lint.rb'
- 'spec/puppet-lint_spec.rb'
Style/FrozenStringLiteralComment:
Enabled: false
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ rescue LoadError
$stderr.puts 'Rubocop is not available for this version of Ruby.'
end

task :ci => [:test, :release_test]
task :ci => %i[test release_test]

# vim: syntax=ruby
4 changes: 2 additions & 2 deletions lib/puppet-lint/checkplugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ def notify(kind, problem)
problem[:kind] = kind
problem.merge!(default_info) { |_key, v1, _v2| v1 }

unless [:warning, :error, :fixed].include?(kind)
unless %i[warning error fixed].include?(kind)
raise ArgumentError, 'unknown value passed for kind'
end

[:message, :line, :column, :check].each do |attr|
%i[message line column check].each do |attr|
unless problem.key?(attr)
raise ArgumentError, "problem hash must contain #{attr.inspect}"
end
Expand Down
22 changes: 11 additions & 11 deletions lib/puppet-lint/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def insert(index, token)
token.next_token = current_token.next_token
token.prev_token = current_token

current_token.next_token.prev_token = token unless current_token.next_token.nil?
current_token.next_token&.prev_token = token

unless formatting_tokens.include?(token.type)
current_token.next_token.prev_code_token = token unless current_token.next_token.nil?
current_token.next_token&.prev_code_token = token
next_nf_idx = tokens[index..-1].index { |r| !formatting_tokens.include?(r.type) }
unless next_nf_idx.nil?
next_nf_token = tokens[index + next_nf_idx]
Expand Down Expand Up @@ -101,11 +101,11 @@ def insert(index, token)

# Public: Removes a token
def delete(token)
token.next_token.prev_token = token.prev_token unless token.next_token.nil?
token.prev_token.next_token = token.next_token unless token.prev_token.nil?
token.next_token&.prev_token = token.prev_token
token.prev_token&.next_token = token.next_token
unless formatting_tokens.include?(token.type)
token.prev_code_token.next_code_token = token.next_code_token unless token.prev_code_token.nil?
token.next_code_token.prev_code_token = token.prev_code_token unless token.next_code_token.nil?
token.prev_code_token&.next_code_token = token.next_code_token
token.next_code_token&.prev_code_token = token.prev_code_token
end

tokens.delete(token)
Expand Down Expand Up @@ -174,7 +174,7 @@ def resource_indexes
rel_start_idx = tokens[marker..-1].index(colon_token)
break if rel_start_idx.nil?
start_idx = rel_start_idx + marker
end_token = colon_token.next_token_of([:SEMIC, :RBRACE])
end_token = colon_token.next_token_of(%i[SEMIC RBRACE])
rel_end_idx = tokens[start_idx..-1].index(end_token)
raise PuppetLint::SyntaxError, colon_token if rel_end_idx.nil?
marker = rel_end_idx + start_idx
Expand Down Expand Up @@ -344,7 +344,7 @@ def function_indexes
next unless token.type == :NAME
next unless token_idx.zero? ||
(token_idx == 1 && tokens[0].type == :WHITESPACE) ||
[:NEWLINE, :INDENT].include?(token.prev_token.type) ||
%i[NEWLINE INDENT].include?(token.prev_token.type) ||
# function in a function
(token.prev_code_token && token.prev_code_token.type == :LPAREN)

Expand Down Expand Up @@ -428,7 +428,7 @@ def hash_indexes
tokens.each_with_index do |token, token_idx|
next unless token.type == :LBRACE
next unless token.prev_code_token
next unless [:EQUALS, :ISEQUAL, :FARROW, :LPAREN].include?(token.prev_code_token.type)
next unless %i[EQUALS ISEQUAL FARROW LPAREN].include?(token.prev_code_token.type)

level = 0
real_idx = 0
Expand All @@ -437,7 +437,7 @@ def hash_indexes

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

hashes << {
Expand Down Expand Up @@ -561,7 +561,7 @@ def parse_control_comments

comment_words = token.value.strip.split(%r{\s+})
comment_words.each_with_index do |word, i|
if word =~ %r{\Alint\:(ignore|endignore)}
if word.match?(%r{\Alint\:(ignore|endignore)})
comment_data << word
else
# Once we reach the first non-controlcomment word, assume the rest
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def tokenise(code)
end
tokens << new_token(:VARIABLE, var_name, opts)

elsif chunk =~ %r{\A'.*?'}m
elsif chunk.match?(%r{\A'.*?'}m)
str_content = StringScanner.new(code[i + 1..-1]).scan_until(%r{(\A|[^\\])(\\\\)*'}m)
length = str_content.size + 1
tokens << new_token(:SSTRING, str_content[0..-2])
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet-lint/lexer/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def to_manifest
end

def string_suffix
no_enclose_tokens = Set.new([:UNENC_VARIABLE, :DQMID, :DQPOST, :HEREDOC_MID, :HEREDOC_POST])
no_enclose_tokens = Set.new(%i[UNENC_VARIABLE DQMID DQPOST HEREDOC_MID HEREDOC_POST])
if next_token && no_enclose_tokens.include?(next_token.type)
''
else
Expand All @@ -120,7 +120,7 @@ def string_suffix
end

def string_prefix
no_enclose_tokens = Set.new([:UNENC_VARIABLE, :DQPRE, :DQMID, :HEREDOC_PRE, :HEREDOC_MID])
no_enclose_tokens = Set.new(%i[UNENC_VARIABLE DQPRE DQMID HEREDOC_PRE HEREDOC_MID])
if prev_token && no_enclose_tokens.include?(prev_token.type)
''
else
Expand Down Expand Up @@ -169,7 +169,7 @@ def prev_token_of(type, opts = {})
# Returns a PuppetLint::Lexer::Token object if a matching token could be
# found, otherwise nil.
def find_token_of(direction, type, opts = {})
return nil unless [:next, :prev].include?(direction)
return nil unless %i[next prev].include?(direction)

opts[:skip_blocks] ||= true
to_find = Array[*type]
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/optparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.build(args = [])

opts.on(
'--error-level LEVEL',
[:all, :warning, :error],
%i[all warning error],
'The level of error to return (warning, error or all).'
) do |el|
PuppetLint.configuration.error_level = el
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def fix(problem)
# Remove trailing whitespace after left operand (if it exists)
return unless left_operand_token.next_token.type == :WHITESPACE
trailing_whitespace_token = left_operand_token.next_token
remove_token(trailing_whitespace_token) if [:NEWLINE, :WHITESPACE].include?(trailing_whitespace_token.next_token.type)
remove_token(trailing_whitespace_token) if %i[NEWLINE WHITESPACE].include?(trailing_whitespace_token.next_token.type)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PuppetLint.new_check(:names_containing_uppercase) do
def check
(class_indexes + defined_type_indexes).each do |class_idx|
next unless class_idx[:name_token].value =~ %r{[A-Z]}
next unless class_idx[:name_token].value.match?(%r{[A-Z]})

obj_type = if class_idx[:type] == :CLASS
'class'
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_classes/parameter_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def required_parameter?(token)
data_type = token.prev_token_of(:TYPE, :skip_blocks => true)
return false if data_type && data_type.value == 'Optional'

if token.next_code_token.nil? || [:COMMA, :RPAREN].include?(token.next_code_token.type)
if token.next_code_token.nil? || %i[COMMA RPAREN].include?(token.next_code_token.type)
return !(token.prev_code_token && token.prev_code_token.type == :EQUALS)
end

Expand Down
14 changes: 6 additions & 8 deletions lib/puppet-lint/plugins/check_classes/variable_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ def check
referenced_variables = Set[]
object_tokens = idx[:tokens]

unless idx[:param_tokens].nil?
idx[:param_tokens].each do |token|
next unless token.type == :VARIABLE
next unless POST_VAR_TOKENS.include?(token.next_code_token.type)
idx[:param_tokens]&.each do |token|
next unless token.type == :VARIABLE
next unless POST_VAR_TOKENS.include?(token.next_code_token.type)

variables_in_scope << token.value
end
variables_in_scope << token.value
end

future_parser_scopes = {}
Expand Down Expand Up @@ -124,9 +122,9 @@ def check
end

next if token.value.include?('::')
next if token.value =~ %r{^(facts|trusted)\[.+\]}
next if token.value.match?(%r{^(facts|trusted)\[.+\]})
next if variables_in_scope.include?(token.value.gsub(%r{\[.+\]\Z}, ''))
next if token.value =~ %r{\A\d+\Z}
next if token.value.match?(%r{\A\d+\Z})

notify(
:warning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fix(problem)

raise PuppetLint::NoFix if ensure_param_name_token.nil?

ensure_param_comma_token = ensure_param_name_token.next_token_of([:COMMA, :SEMIC])
ensure_param_comma_token = ensure_param_name_token.next_token_of(%i[COMMA SEMIC])

if first_param_name_token.nil? || first_param_comma_token.nil? || ensure_param_comma_token.nil?
raise PuppetLint::NoFix
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-lint/plugins/check_resources/file_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def check
value_token = param_token.next_code_token.next_code_token

break if IGNORE_TYPES.include?(value_token.type)
break if value_token.value =~ MODE_RE
break if value_token.value.match?(MODE_RE)

notify(
:warning,
Expand All @@ -33,7 +33,7 @@ def check
end

def fix(problem)
raise PuppetLint::NoFix unless problem[:token].value =~ %r{\A[0-7]{3}\Z}
raise PuppetLint::NoFix unless problem[:token].value.match?(%r{\A[0-7]{3}\Z})

problem[:token].type = :SSTRING
problem[:token].value = "0#{problem[:token].value}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def check
tokens.select { |token|
(token.type == :SSTRING || token.type == :STRING || token.type == :DQPRE) && token.value.start_with?('puppet://')
}.reject { |token|
token.value[%r{puppet://.*?/(.+)}, 1].start_with?('modules/') unless token.value[%r{puppet://.*?/(.+)}, 1].nil?
token.value[%r{puppet://.*?/(.+)}, 1]&.start_with?('modules/')
}.each do |token|
notify(
:warning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def check
tokens.select { |r|
VARIABLE_DASH_TYPES.include?(r.type)
}.each do |token|
next unless token.value.gsub(%r{\[.+?\]}, '') =~ %r{-}
next unless token.value.gsub(%r{\[.+?\]}, '').match?(%r{-})

notify(
:warning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def check
tokens.select { |r|
VARIABLE_LOWERCASE_TYPES.include?(r.type)
}.each do |token|
next unless token.value.gsub(%r{\[.+?\]}, '') =~ %r{[A-Z]}
next unless token.value.gsub(%r{\[.+?\]}, '').match?(%r{[A-Z]})

notify(
:warning,
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def fix(problem)
problem[:token].prev_code_token.prev_token.value = ' ' * problem[:newline_indent]

end_param_idx = tokens.index(problem[:token].prev_code_token)
start_param_idx = tokens.index(problem[:token].prev_token_of([:INDENT, :NEWLINE]))
start_param_idx = tokens.index(problem[:token].prev_token_of(%i[INDENT NEWLINE]))
param_length = tokens[start_param_idx..end_param_idx].map { |r| r.to_manifest.length }.reduce(0) { |sum, x| sum + x } + 1
new_ws_len = problem[:arrow_column] - param_length
else
Expand All @@ -115,7 +115,7 @@ def fix(problem)
new_ws_len += (problem[:arrow_column] - problem[:token].column)
end

raise PuppetLint::NoFix if new_ws_len < 0
raise PuppetLint::NoFix if new_ws_len.negative?
new_ws = ' ' * new_ws_len

if problem[:token].prev_token.type == :WHITESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PuppetLint.new_check(:trailing_whitespace) do
def check
tokens.select { |token|
[:WHITESPACE, :INDENT].include?(token.type)
%i[WHITESPACE INDENT].include?(token.type)
}.select { |token|
token.next_token.nil? || token.next_token.type == :NEWLINE
}.each do |token|
Expand All @@ -25,7 +25,7 @@ def fix(problem)
prev_token = problem[:token].prev_token
next_token = problem[:token].next_token
prev_token.next_token = next_token
next_token.prev_token = prev_token unless next_token.nil?
next_token&.prev_token = prev_token
tokens.delete(problem[:token])
end
end
2 changes: 1 addition & 1 deletion lib/puppet-lint/tasks/puppet-lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(*args, &task_block)
def define(args, &task_block)
desc 'Run puppet-lint'

task_block.call(*[self, args].slice(0, task_block.arity)) if task_block
task_block&.call(*[self, args].slice(0, task_block.arity))

# clear any (auto-)pre-existing task
Rake::Task[@name].clear if Rake::Task.task_defined?(@name)
Expand Down
4 changes: 2 additions & 2 deletions spec/puppet-lint/checks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
end

context 'when there are checks enabled' do
let(:enabled_checks) { [:arrow_alignment, :hard_tabs] }
let(:enabled_checks) { %i[arrow_alignment hard_tabs] }
let(:enabled_check_classes) { enabled_checks.map { |r| PuppetLint.configuration.check_object[r] } }
let(:disabled_checks) { PuppetLint.configuration.checks - enabled_checks }
let(:disabled_check_classes) { disabled_checks.map { |r| PuppetLint.configuration.check_object[r] } }
Expand Down Expand Up @@ -194,7 +194,7 @@
describe '#enabled_checks' do
subject(:enabled_checks) { instance.enabled_checks }

let(:expected_enabled_checks) { [:arrow_alignment, :trailing_whitespace] }
let(:expected_enabled_checks) { %i[arrow_alignment trailing_whitespace] }

before do
PuppetLint.configuration.checks.each do |check|
Expand Down

0 comments on commit bdac0cd

Please sign in to comment.