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

Drop support for Ruby 2.3 and older #31

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
inherit_from: './.rubocop_todo.yml'
AllCops:
TargetRubyVersion: 1.9
TargetRubyVersion: 2.4

Style/HashSyntax:
EnforcedStyle: hash_rockets
Expand Down 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
37 changes: 7 additions & 30 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,20 @@ source 'https://rubygems.org'
gemspec

group :test do
gem 'rake', '~> 10.0'
gem 'rake', '~> 13.0'
gem 'rspec-its', '~> 1.0'
gem 'rspec-collection_matchers', '~> 1.0'

if RUBY_VERSION < '2.0'
# json 2.x requires ruby 2.0. Lock to 1.8
gem 'json', '= 1.8'
# json_pure 2.0.2 requires ruby 2.0, and 2.0.1 requires ruby 1.9. Lock to 1.8.3.
gem 'json_pure', '= 1.8.3'
# addressable 2.4.0 requires ruby 1.9.0. Lock to 2.3.8.
gem 'addressable', '= 2.3.8'
gem 'diff-lcs', '< 1.3'
gem 'rspec', '<= 3.4'
else
gem 'rspec', '~> 3.0'
gem 'json'
end
gem 'rspec', '~> 3.0'
gem 'json'

if RUBY_VERSION > '1.8'
# requires ruby 1.9+, on 1.8 we'll fall back to the old regex parsing
gem 'rspec-json_expectations', '~> 1.4'
end
gem 'rspec-json_expectations', '>= 1.4'

gem 'rubocop', '0.49.1' if RUBY_VERSION > '2.0'
gem 'rubocop', '0.49.1'
gem 'simplecov', :require => false if ENV['COVERAGE'] == 'yes'
end

group :development do
if RUBY_VERSION > '1.9'
# For Changelog generation
if RUBY_VERSION >= '2.2.2'
gem 'github_changelog_generator', :require => false
else
gem 'github_changelog_generator', '~> 1.13.0', :require => false
gem 'rack', '~> 1.0', :require => false
end

gem 'pry'
end
gem 'github_changelog_generator', :require => false
gem 'pry'
end
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the safe navigator was Ruby 2.5. Am I messing up versions in my head?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the safe autofix with ruby 2.4 as target version


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
4 changes: 2 additions & 2 deletions lib/puppet-lint/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def heredoc_queue
# \v == vertical tab
# \f == form feed
# \p{Zs} == ASCII + Unicode non-linebreaking whitespace
WHITESPACE_RE = RUBY_VERSION == '1.8.7' ? %r{[\t\v\f ]} : %r{[\t\v\f\p{Zs}]}
WHITESPACE_RE = %r{[\t\v\f\p{Zs}]}

LINE_END_RE = %r{(?:\r\n|\r|\n)}

Expand Down 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is completely unrelated, but I'm just looking at this and this is a bit hard to read. I really wonder if this isn't the same as:

Suggested change
param_length = tokens[start_param_idx..end_param_idx].map { |r| r.to_manifest.length }.reduce(0) { |sum, x| sum + x } + 1
param_length = tokens[start_param_idx..end_param_idx].sum { |r| r.to_manifest.length } + 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok to me, but I don't know enough about ruby to ensure this works. could be handled with a test.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked and Enumable#sum was introduced in Ruby 2.4 so my suggestion is only valid after this PR's changes.

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
5 changes: 0 additions & 5 deletions lib/puppet-lint/tasks/release_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ def with_puppet_lint_head
exit
end

if RUBY_VERSION.start_with?('1')
puts 'Unable to run release_tests on Ruby < 2.0'
exit
end

require 'puppet-lint/tasks/gemfile_rewrite'

modules_to_test = [
Expand Down
1 change: 1 addition & 0 deletions puppet-lint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |s|
s.authors = ['Tim Sharpe', 'Puppet, Inc.', 'Community Contributors']
s.email = ['tim@sharpe.id.au', 'modules-team@puppet.com']
s.license = 'MIT'
s.required_ruby_version = '>= 2.4.0'
end
Loading