diff --git a/.rubocop.yml b/.rubocop.yml index b0ad999..636ab39 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,6 +9,3 @@ inherit_gem: Naming/FileName: Exclude: - "*.gemspec" - -AllCops: - TargetRubyVersion: 3.2 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ba6565b..957e112 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config --no-auto-gen-timestamp` -# using RuboCop version 1.79.2. +# using RuboCop version 1.81.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -38,70 +38,21 @@ RSpec/DescribeClass: - '**/spec/views/**/*' - 'spec/puppet-lint/plugins/check_strict_indent_spec.rb' -# Offense count: 17 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: always, conditionals -Style/AndOr: - Exclude: - - 'lib/puppet-lint/plugins/check_strict_indent.rb' - # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AutoCorrect, EnforcedStyle, AllowComments. +# Configuration parameters: EnforcedStyle, AllowComments. # SupportedStyles: empty, nil, both Style/EmptyElse: Exclude: - 'lib/puppet-lint/plugins/check_strict_indent.rb' -# Offense count: 3 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: always, always_true, never -Style/FrozenStringLiteralComment: - Exclude: - - '**/*.arb' - - 'lib/puppet-lint/plugins/check_strict_indent.rb' - - 'puppet-lint-strict_indent-check.gemspec' - - 'spec/puppet-lint/plugins/check_strict_indent_spec.rb' - # Offense count: 4 Style/MultilineBlockChain: Exclude: - 'lib/puppet-lint/plugins/check_strict_indent.rb' - 'spec/puppet-lint/plugins/check_strict_indent_spec.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowMethodComparison, ComparisonsThreshold. -Style/MultipleComparison: - Exclude: - - 'lib/puppet-lint/plugins/check_strict_indent.rb' - # Offense count: 3 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns. -# SupportedStyles: predicate, comparison -Style/NumericPredicate: - Exclude: - - 'spec/**/*' - - 'lib/puppet-lint/plugins/check_strict_indent.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength. -# AllowedMethods: present?, blank?, presence, try, try! -Style/SafeNavigation: - Exclude: - - 'lib/puppet-lint/plugins/check_strict_indent.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/WhileUntilModifier: - Exclude: - - 'lib/puppet-lint/plugins/check_strict_indent.rb' - -# Offense count: 2 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings. # URISchemes: http, https diff --git a/Gemfile b/Gemfile index 549e640..2a28e9b 100644 --- a/Gemfile +++ b/Gemfile @@ -14,5 +14,5 @@ group :development do gem 'rspec', '~> 3.12' gem 'rspec-collection_matchers', '~> 1.2' gem 'rspec-its', '>= 1.3', '< 3' - gem 'voxpupuli-rubocop', '~> 4.2.0' + gem 'voxpupuli-rubocop', '~> 5.0.0' end diff --git a/lib/puppet-lint/plugins/check_strict_indent.rb b/lib/puppet-lint/plugins/check_strict_indent.rb index 0990c8c..e6a4185 100644 --- a/lib/puppet-lint/plugins/check_strict_indent.rb +++ b/lib/puppet-lint/plugins/check_strict_indent.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Public: Check the manifest tokens for correct indent levels and # record a warning for each instance found. @@ -50,12 +52,12 @@ def check # indent for open groups in the previous line open_groups = 0 prev_token = token.prev_token - while !prev_token.nil? and prev_token.type != :NEWLINE + while !prev_token.nil? && (prev_token.type != :NEWLINE) temp_indent += 1 if prev_token.type == :HEREDOC_OPEN if %i[LBRACE LBRACK LPAREN].include?(prev_token.type) if matches[prev_token].nil? open_groups += 1 - elsif matches[prev_token].line > prev_token.line and !matched_open_lines.include?(matches[prev_token].line) + elsif (matches[prev_token].line > prev_token.line) && !matched_open_lines.include?(matches[prev_token].line) # increase indent for left braces not matched in the same line or in a line we have already matched a brace to open_groups += 1 matched_open_lines << matches[prev_token].line @@ -67,17 +69,15 @@ def check # reset prev_token to last non-whitespace token on previous line prev_token = token.prev_token - while !prev_token.nil? and (prev_token.type == :WHITESPACE or prev_token.type == :COMMENT) - prev_token = prev_token.prev_token - end + prev_token = prev_token.prev_token while !prev_token.nil? && ((prev_token.type == :WHITESPACE) || (prev_token.type == :COMMENT)) # get type if available - prev_type = prev_token.nil? ? nil : prev_token.type + prev_type = prev_token&.type # handle change in indent based on last token case prev_type when :COLON - if open_groups == 0 + if open_groups.zero? if colon_indent.nil? # only indent for a colon when you haven't indented yet colon_indent = prev_token.line @@ -99,23 +99,23 @@ def check # unindent for closing brackets in the current line next_token = token.next_token matched_close_lines = [] - while !next_token.nil? and next_token.type != :NEWLINE + while !next_token.nil? && (next_token.type != :NEWLINE) if %i[RBRACE RBRACK RPAREN].include?(next_token.type) - if !matches[next_token].nil? and - matches[next_token].line < next_token.line and + if !matches[next_token].nil? && + (matches[next_token].line < next_token.line) && !matched_close_lines.include?(matches[next_token].line) # right braces matched in a previous line decrease indent unless we already matched a brace in the same line indent -= 1 matched_close_lines << matches[next_token].line end - if next_token.type == :RBRACE and !colon_indent.nil? && (!matches[next_token].nil? and matches[next_token].line < colon_indent) + if (next_token.type == :RBRACE) && !colon_indent.nil? && !matches[next_token].nil? && (matches[next_token].line < colon_indent) # unindent at the end of resources if needed indent -= 1 colon_indent = nil end - elsif next_token.type == :SEMIC and !colon_indent.nil? and - %i[INDENT NEWLINE].include?(next_token.prev_token.type) and - (next_token.next_token.nil? or next_token.next_token.type == :NEWLINE) + elsif (next_token.type == :SEMIC) && !colon_indent.nil? && + %i[INDENT NEWLINE].include?(next_token.prev_token.type) && + (next_token.next_token.nil? || (next_token.next_token.type == :NEWLINE)) # For a lone semicolon within a block decrement immediately. Use temp_indent because # indent will be decremented in the next line by the prev_token logic above. temp_indent -= 1 @@ -124,7 +124,7 @@ def check end # obviously we have a problem - if indent < 0 + if indent.negative? notify :error, { message: 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues', line: token.next_token.line, @@ -151,7 +151,7 @@ def check # For interpolated heredocs the pipe whitespace is in the HEREDOC_POST token so we need scan forward # to this and get its length. next_token = token.next_token - while !next_token.nil? and next_token.type != :NEWLINE and next_token.type != :HEREDOC_POST + while !next_token.nil? && (next_token.type != :NEWLINE) && (next_token.type != :HEREDOC_POST) next_token = next_token.next_token end if next_token.type == :HEREDOC_POST @@ -195,7 +195,7 @@ def fix(problem) change = problem[:indent] - problem[:actual] indent_heredoc(problem[:token], change) next_token = problem[:token].next_token - while !next_token.nil? and next_token.type != :HEREDOC_POST + while !next_token.nil? && (next_token.type != :HEREDOC_POST) indent_heredoc(next_token, change) if next_token.type == :HEREDOC_MID next_token = next_token.next_token end @@ -211,10 +211,10 @@ def fix(problem) def map_heredoc_lines(value, change, skip) char_for_indent = ' ' value.split("\n").map! do |line| - if skip or line.empty? + if skip || line.empty? skip = false line - elsif change < 0 + elsif change.negative? line[-change..] else (char_for_indent * change) + line diff --git a/puppet-lint-strict_indent-check.gemspec b/puppet-lint-strict_indent-check.gemspec index 286b528..ce1de62 100644 --- a/puppet-lint-strict_indent-check.gemspec +++ b/puppet-lint-strict_indent-check.gemspec @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Gem::Specification.new do |spec| spec.name = 'puppet-lint-strict_indent-check' spec.version = '5.0.0' diff --git a/spec/puppet-lint/plugins/check_strict_indent_spec.rb b/spec/puppet-lint/plugins/check_strict_indent_spec.rb index 5dabbbf..8d9aec1 100644 --- a/spec/puppet-lint/plugins/check_strict_indent_spec.rb +++ b/spec/puppet-lint/plugins/check_strict_indent_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'strict_indent' do