Skip to content

Commit

Permalink
rubocop added
Browse files Browse the repository at this point in the history
  • Loading branch information
olegantonyan committed Nov 9, 2018
1 parent 1412229 commit cc9b299
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
15 changes: 15 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
AllCops:
Exclude:
- 'bin/**/*'

Style/CommentedKeyword:
Enabled: false

Style/Documentation:
Enabled: false

Style/Semicolon:
Enabled: false

Metrics/LineLength:
Max: 170
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) { |repo_name| 'https://github.com/#{repo_name}' }
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in typerb.gemspec
gemspec
20 changes: 19 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
PATH
remote: .
specs:
typerb (0.1.2)
typerb (0.1.3)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.0)
awesome_print (1.8.0)
coderay (1.1.2)
diff-lcs (1.3)
jaro_winkler (1.5.1)
method_source (0.9.1)
parallel (1.12.1)
parser (2.5.3.0)
ast (~> 2.4.0)
powerpack (0.1.2)
pry (0.12.0)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rainbow (3.0.0)
rake (10.5.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
Expand All @@ -27,8 +34,18 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.60.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.4.0)
ruby-progressbar (1.10.0)
super_awesome_print (0.2.5)
awesome_print
unicode-display_width (1.4.0)

PLATFORMS
ruby
Expand All @@ -38,6 +55,7 @@ DEPENDENCIES
pry
rake (>= 10.0)
rspec (>= 3.0)
rubocop
super_awesome_print
typerb!

Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
6 changes: 3 additions & 3 deletions lib/typerb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

module Typerb
refine Object do
def type!(*klasses)
def type!(*klasses) # rubocop: disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength NOTE: temporary
raise ArgumentError, 'provide at least one class' if klasses.empty?
return if klasses.any? { |kls| is_a?(kls) }

kls_text = klasses.size > 1 ? klasses.map(&:name).join(' or ') : klasses.first.name

# error message when multiple calls on the same line, or in a console - can't extract variable name
exception_text = "expected #{kls_text}, got #{self.class} (#{to_s})"
exception_text = "expected #{kls_text}, got #{self.class} (#{self})"

where = caller_locations(1, 1)[0]
file = where.path
Expand All @@ -21,7 +21,7 @@ def type!(*klasses)
node = RubyVM::AST.parse(code)
if node.children.last.children.size == 3 && node.children.last.children[1] == :type!
var_name = node.children.last.children.first.children.first
exception_text = "`#{var_name}` should be #{kls_text}, not #{self.class} (#{to_s})"
exception_text = "`#{var_name}` should be #{kls_text}, not #{self.class} (#{self})"
end
end
exception = TypeError.new(exception_text)
Expand Down
8 changes: 4 additions & 4 deletions spec/typerb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe Typerb do
RSpec.describe Typerb do # rubocop: disable Metrics/BlockLength
it 'has a version number' do
expect(Typerb::VERSION).not_to be nil
end
Expand Down Expand Up @@ -35,15 +35,15 @@ def initialize(arg1, arg2, arg3)
expect { kls.new('hello', 1, {}) }.to raise_error(TypeError, '`arg1` should be Numeric, not String (hello)')
expect { kls.new(1, 123, {}) }.to raise_error(TypeError, '`arg2` should be String, not Integer (123)')
expect { kls.new(1, '123', nil) }.to raise_error(TypeError, '`arg3` should be Hash, not NilClass ()')
expect { kls.new(123, 'hello', { o: 1 }) }.not_to raise_error
expect { kls.new(123, 'hello', o: 1) }.not_to raise_error
end

it 'raises TypeError for wrong type and ugly syntax' do
kls = Class.new do
using Typerb

def initialize(arg)
arg. type!(Integer)
arg. type!(Integer) # rubocop: disable Layout/ExtraSpacing
# NOTE cannot split into multiline, i.e. this will not work
# arg.
# type!(Integer)
Expand All @@ -65,7 +65,7 @@ def initialize(arg)
end
expect { kls.new(123) }.not_to raise_error
expect { kls.new('123') }.not_to raise_error
expect { kls.new({hello: 123}) }.to raise_error(TypeError, '`arg` should be Integer or String, not Hash ({:hello=>123})')
expect { kls.new(hello: 123) }.to raise_error(TypeError, '`arg` should be Integer or String, not Hash ({:hello=>123})')
end

it '(kind of) works with multiple args on the same line 1', multiline: true do
Expand Down
11 changes: 7 additions & 4 deletions typerb.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'typerb/version'

Expand All @@ -8,8 +10,8 @@ Gem::Specification.new do |spec|
spec.authors = ['Oleg Antonyan']
spec.email = ['[email protected]']

spec.summary = %q(Typecheck sugar for Ruby.)
spec.description = %q(Typecheck sugar for Ruby.)
spec.summary = 'Typecheck sugar for Ruby.'
spec.description = 'Typecheck sugar for Ruby.'
spec.homepage = 'https://github.com/olegantonyan/typerb'
spec.license = 'MIT'

Expand All @@ -23,10 +25,11 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '>= 1.17'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'rake', '>= 10.0'
spec.add_development_dependency 'rspec', '>= 3.0'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'super_awesome_print'
spec.add_development_dependency 'pry'

spec.required_ruby_version = '>= 2.6.0-preview3'
end

0 comments on commit cc9b299

Please sign in to comment.