diff --git a/.rubocop.yml b/.rubocop.yml index 5f1d0dc80..f7f418ced 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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 diff --git a/Rakefile b/Rakefile index c156c2bf9..a62bdfde2 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/puppet-lint/checkplugin.rb b/lib/puppet-lint/checkplugin.rb index a1fa65730..0497e512c 100644 --- a/lib/puppet-lint/checkplugin.rb +++ b/lib/puppet-lint/checkplugin.rb @@ -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 diff --git a/lib/puppet-lint/data.rb b/lib/puppet-lint/data.rb index 652408980..e122b1b30 100644 --- a/lib/puppet-lint/data.rb +++ b/lib/puppet-lint/data.rb @@ -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] @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 << { @@ -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 diff --git a/lib/puppet-lint/lexer.rb b/lib/puppet-lint/lexer.rb index e9d1696b3..9499fea8a 100644 --- a/lib/puppet-lint/lexer.rb +++ b/lib/puppet-lint/lexer.rb @@ -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]) diff --git a/lib/puppet-lint/lexer/token.rb b/lib/puppet-lint/lexer/token.rb index 5384932a4..df2fc6882 100644 --- a/lib/puppet-lint/lexer/token.rb +++ b/lib/puppet-lint/lexer/token.rb @@ -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 @@ -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 @@ -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] diff --git a/lib/puppet-lint/optparser.rb b/lib/puppet-lint/optparser.rb index bb1e1ef1b..dc3195fe6 100644 --- a/lib/puppet-lint/optparser.rb +++ b/lib/puppet-lint/optparser.rb @@ -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 diff --git a/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb b/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb index d2646f45f..ce23a44de 100644 --- a/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb +++ b/lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb @@ -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 diff --git a/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb b/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb index c4e53d81a..03a22fb0f 100644 --- a/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb +++ b/lib/puppet-lint/plugins/check_classes/names_containing_uppercase.rb @@ -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' diff --git a/lib/puppet-lint/plugins/check_classes/parameter_order.rb b/lib/puppet-lint/plugins/check_classes/parameter_order.rb index ddadd80e0..48f1a09ad 100644 --- a/lib/puppet-lint/plugins/check_classes/parameter_order.rb +++ b/lib/puppet-lint/plugins/check_classes/parameter_order.rb @@ -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 diff --git a/lib/puppet-lint/plugins/check_classes/variable_scope.rb b/lib/puppet-lint/plugins/check_classes/variable_scope.rb index d6960b542..76538a088 100644 --- a/lib/puppet-lint/plugins/check_classes/variable_scope.rb +++ b/lib/puppet-lint/plugins/check_classes/variable_scope.rb @@ -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 = {} @@ -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, diff --git a/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb b/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb index 019913b39..a0456736d 100644 --- a/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb +++ b/lib/puppet-lint/plugins/check_resources/ensure_first_param.rb @@ -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 diff --git a/lib/puppet-lint/plugins/check_resources/file_mode.rb b/lib/puppet-lint/plugins/check_resources/file_mode.rb index 20417cb9b..34177bc61 100644 --- a/lib/puppet-lint/plugins/check_resources/file_mode.rb +++ b/lib/puppet-lint/plugins/check_resources/file_mode.rb @@ -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, @@ -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}" diff --git a/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb b/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb index 7783682f9..1ca92f396 100644 --- a/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb +++ b/lib/puppet-lint/plugins/check_strings/puppet_url_without_modules.rb @@ -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, diff --git a/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb b/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb index 5e65e6e4a..7a71935b5 100644 --- a/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb +++ b/lib/puppet-lint/plugins/check_variables/variable_contains_dash.rb @@ -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, diff --git a/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb b/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb index 6d90c0f1c..2430eceb8 100644 --- a/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb +++ b/lib/puppet-lint/plugins/check_variables/variable_is_lowercase.rb @@ -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, diff --git a/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb b/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb index d687179b8..6f56d0d07 100644 --- a/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb +++ b/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb @@ -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 @@ -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 diff --git a/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb b/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb index b19996e10..07e810100 100644 --- a/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb +++ b/lib/puppet-lint/plugins/check_whitespace/trailing_whitespace.rb @@ -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| @@ -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 diff --git a/lib/puppet-lint/tasks/puppet-lint.rb b/lib/puppet-lint/tasks/puppet-lint.rb index 1b3efdb1b..c23569b80 100644 --- a/lib/puppet-lint/tasks/puppet-lint.rb +++ b/lib/puppet-lint/tasks/puppet-lint.rb @@ -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) diff --git a/spec/puppet-lint/checks_spec.rb b/spec/puppet-lint/checks_spec.rb index 758a483ee..57026c9f7 100644 --- a/spec/puppet-lint/checks_spec.rb +++ b/spec/puppet-lint/checks_spec.rb @@ -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] } } @@ -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|