Skip to content

Commit

Permalink
* Update RuboCop config & code with Rubocop violations
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed Apr 21, 2021
1 parent 8e7bdc9 commit 028de0d
Show file tree
Hide file tree
Showing 24 changed files with 217 additions and 86 deletions.
40 changes: 33 additions & 7 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 1.9
TargetRubyVersion: 3.0
DisplayCopNames: true
NewCops: disable
Exclude:
- "benchmarks/**/*"
- "tmp/**/*"
- "vendor/**/*"
- "script/**/*.rb"
- "spec/fixtures/*.rb"
- "spec/ruby_version_specific/*.rb"
- "spec/*.rb"

# forces method defs to have params in parens
Style/MethodDefParentheses:
Expand Down Expand Up @@ -38,7 +43,7 @@ Lint/UnusedMethodArgument:
Enabled: false

# changes x ** 2 to x**2
Style/SpaceAroundOperators:
Layout/SpaceAroundOperators:
Enabled: false

# doesn't allow vars starting with _
Expand All @@ -56,7 +61,7 @@ Style/Documentation:

# enforces line length of 80
# TODO enable
Metrics/LineLength:
Layout/LineLength:
Enabled: false

# triggered by Contract ({ :name => String, :age => Fixnum }) => nil
Expand Down Expand Up @@ -101,7 +106,7 @@ Lint/DuplicateMethods:
Style/TrivialAccessors:
Enabled: false

Style/MultilineOperationIndentation:
Layout/MultilineOperationIndentation:
EnforcedStyle: indented

# Asks you to use %w{array of words} if possible.
Expand All @@ -111,12 +116,12 @@ Style/WordArray:

# conflicts with contracts
# we define contracts like `Baz = 1`
Style/ConstantName:
Naming/ConstantName:
Enabled: false

# `Contract` violates this, otherwise a good cop (enforces snake_case method names)
# TODO possible to get this enabled but ignore `Contract`?
Style/MethodName:
Naming/MethodName:
Enabled: false

# checks for !!
Expand All @@ -129,9 +134,30 @@ Metrics/ParameterLists:
Enabled: false

# Checks that braces used for hash literals have or don't have surrounding space depending on configuration.
Style/SpaceInsideHashLiteralBraces:
Layout/SpaceInsideHashLiteralBraces:
Enabled: false

# TODO enable
Style/SpecialGlobalVars:
Enabled: false

Style/IfUnlessModifier:
Enabled: false

Naming/MemoizedInstanceVariableName:
Enabled: false

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Layout/HashAlignment:
EnforcedColonStyle: table

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma
11 changes: 7 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

require:
- rubocop-performance

# Offense count: 2
Lint/NonLocalExitFromIterator:
Exclude:
Expand All @@ -32,14 +35,14 @@ Style/Alias:
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowAdjacentOneLineDefs.
Style/EmptyLineBetweenDefs:
Layout/EmptyLineBetweenDefs:
Exclude:
- 'benchmarks/wrap_test.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
Style/ExtraSpacing:
Layout/ExtraSpacing:
Exclude:
- 'spec/builtin_contracts_spec.rb'

Expand All @@ -58,7 +61,7 @@ Style/IfInsideElse:
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: symmetrical, new_line, same_line
Style/MultilineHashBraceLayout:
Layout/MultilineHashBraceLayout:
Exclude:
- 'spec/contracts_spec.rb'
- 'spec/fixtures/fixtures.rb'
Expand Down Expand Up @@ -130,6 +133,6 @@ Style/TrailingUnderscoreVariable:

# Offense count: 1
# Cop supports --auto-correct.
Style/UnneededInterpolation:
Style/RedundantInterpolation:
Exclude:
- 'lib/contracts/formatters.rb'
12 changes: 8 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec

group :test do
gem "rspec"
gem "aruba"
gem "cucumber", "~> 1.3.20"
gem "rubocop", "~> 0.41.2" if RUBY_VERSION >= "2"
gem "rspec"

gem "rubocop", ">= 1.0.0"
gem "rubocop-performance", ">= 1.0.0"
end

group :development do
gem "relish"
gem "method_profiler"
gem "ruby-prof"
gem "rake"
gem "relish"
gem "ruby-prof"
end
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

task :default => [:spec]

task :add_tag do
Expand Down
4 changes: 3 additions & 1 deletion contracts.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require File.expand_path(File.join(__FILE__, "../lib/contracts/version"))

Gem::Specification.new do |s|
Expand All @@ -10,7 +12,7 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.homepage = "https://github.com/egonSchiele/contracts.ruby"
s.license = "BSD-2-Clause"
s.required_ruby_version = ">= 3", "< 4"
s.required_ruby_version = [">= 3.0", "< 4"]
s.post_install_message = "
0.16.x will be the supporting Ruby 2.x and be feature frozen (only fixes will be released)
For Ruby 3.x use 0.17.x or later (might not be released yet)
Expand Down
2 changes: 2 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "aruba/cucumber"
require "aruba/jruby" if RUBY_PLATFORM == "java"

Expand Down
41 changes: 25 additions & 16 deletions lib/contracts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "contracts/attrs"
require "contracts/builtin_contracts"
require "contracts/decorators"
Expand Down Expand Up @@ -51,7 +53,9 @@ class Contract < Contracts::Decorator
end

attr_reader :args_contracts, :ret_contract, :klass, :method

def initialize(klass, method, *contracts)
super(klass, method)
unless contracts.last.is_a?(Hash)
unless contracts.one?
fail %{
Expand Down Expand Up @@ -102,8 +106,8 @@ def initialize(klass, method, *contracts)
@klass, @method = klass, method
end

def pretty_contract c
c.is_a?(Class) ? c.name : c.class.name
def pretty_contract contract
contract.is_a?(Class) ? contract.name : contract.class.name
end

def to_s
Expand All @@ -130,15 +134,15 @@ def self.failure_msg(data)
expected_prefix = "Expected: "
expected_value = Contracts::Support.indent_string(
Contracts::Formatters::Expected.new(data[:contract]).contract.pretty_inspect,
expected_prefix.length
expected_prefix.length,
).strip
expected_line = expected_prefix + expected_value + ","
expected_line = "#{expected_prefix}#{expected_value},"

# Actual
actual_prefix = "Actual: "
actual_value = Contracts::Support.indent_string(
data[:arg].pretty_inspect,
actual_prefix.length
actual_prefix.length,
).strip
actual_line = actual_prefix + actual_value

Expand All @@ -157,16 +161,19 @@ def self.failure_msg(data)
position_value = Contracts::Support.method_position(data[:method])
position_line = position_prefix + position_value

header +
"\n" +
[
header,
Contracts::Support.indent_string(
[expected_line,
actual_line,
value_line,
contract_line,
position_line].join("\n"),
indent_amount
)
[
expected_line,
actual_line,
value_line,
contract_line,
position_line,
].join("\n"),
indent_amount,
),
].join("\n")
end

# Callback for when a contract fails. By default it raises
Expand All @@ -182,7 +189,7 @@ def self.failure_msg(data)
# puts failure_msg(data)
# exit
# end
def self.failure_callback(data, use_pattern_matching = true)
def self.failure_callback(data, use_pattern_matching: true)
if data[:contracts].pattern_match? && use_pattern_matching
return DEFAULT_FAILURE_CALLBACK.call(data)
end
Expand Down Expand Up @@ -242,7 +249,8 @@ def call(*args, &blk)
# returns true if it appended nil
def maybe_append_block! args, blk
return false unless @has_proc_contract && !blk &&
(@args_contract_index || args.size < args_contracts.size)
(@args_contract_index || args.size < args_contracts.size)

args << nil
true
end
Expand All @@ -251,6 +259,7 @@ def maybe_append_block! args, blk
# returns true if it appended nil
def maybe_append_options! args, kargs, blk
return false unless @has_options_contract

if @has_proc_contract && args_contracts[-2].is_a?(Contracts::Builtin::KeywordArgs)
args.insert(-2, kargs)
elsif args_contracts[-1].is_a?(Contracts::Builtin::KeywordArgs)
Expand Down
2 changes: 2 additions & 0 deletions lib/contracts/attrs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Contracts
module Attrs
def attr_reader_with_contract(*names, contract)
Expand Down
Loading

0 comments on commit 028de0d

Please sign in to comment.