Skip to content

Commit 8311bd6

Browse files
Fix rubocop issues
1 parent 44ed6f6 commit 8311bd6

19 files changed

+91
-39
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ jobs:
88
steps:
99
- checkout
1010
- run: bundle install
11-
# - ruby/rubocop-check
11+
- ruby/rubocop-check
1212
- ruby/rspec-test

.rubocop.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
AllCops:
2+
TargetRubyVersion: 2.4
3+
NewCops: enable
4+
5+
Style/BlockDelimiters:
6+
EnforcedStyle: semantic
7+
8+
Style/Documentation:
9+
Enabled: false
10+
11+
Layout/LineLength:
12+
Max: 120
13+
14+
Metrics/BlockLength:
15+
Exclude:
16+
- spec/**/*

Gemfile

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
source "https://rubygems.org"
4-
5-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3+
source 'https://rubygems.org'
64

75
gemspec

Guardfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# A sample Guardfile
24
# More info at https://github.com/guard/guard#readme
35

@@ -24,8 +26,8 @@
2426
# * zeus: 'zeus rspec' (requires the server to be started separately)
2527
# * 'just' rspec: 'rspec'
2628

27-
guard :rspec, cmd: "bundle exec rspec" do
28-
require "guard/rspec/dsl"
29+
guard :rspec, cmd: 'bundle exec rspec' do
30+
require 'guard/rspec/dsl'
2931
dsl = Guard::RSpec::Dsl.new(self)
3032

3133
# RSpec files

bin/oblivion

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'oblivion'
45

5-
open(ARGV[0]) do |f|
6+
File.open(ARGV[0]) do |f|
67
puts Oblivion.uglify(f.read)
78
end

lib/oblivion.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'unparser'
24

35
require_relative 'oblivion/nodes'

lib/oblivion/base_processor.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'ast'
24

35
module Oblivion

lib/oblivion/class_uglifier.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
# frozen_string_literal: true
2+
13
require 'securerandom'
24

35
module Oblivion
46
class ClassUglifier < BaseProcessor
57
def initialize(methods)
8+
super()
69
@method_names = {}
710
methods.each do |name|
811
@method_names[name] = random_method_name(name)
@@ -14,9 +17,7 @@ def initialize(methods)
1417
def on_def(node)
1518
result = node
1619
result = result.with_name @method_names[node.name] if @method_names.key?(node.name)
17-
result = result.with_body MethodUglifier.new(@method_names).process(node.body)
18-
19-
result
20+
result.with_body MethodUglifier.new(@method_names).process(node.body)
2021
end
2122

2223
# TODO: Uglify instance_variables
@@ -35,8 +36,8 @@ def on_def(node)
3536
# node.updated(nil, new_children)
3637
# end
3738

38-
alias :on_class :uglify_class
39-
alias :on_sclass :uglify_class
39+
alias on_class uglify_class
40+
alias on_sclass uglify_class
4041

4142
private
4243

lib/oblivion/method_finder.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'set'
24

35
module Oblivion
@@ -30,19 +32,20 @@ def on_send(node)
3032
case called_method
3133
when :public, :protected, :private
3234
@access_modifier = called_method
33-
# TODO: Uglify instance_variables
34-
# when :attr_reader, :attr_writer, :attr_accessor
35-
# method_names = node.children[2..-1].map { |n| n.children[0] }
36-
# method_names.each do |method_name|
37-
# add_to_result method_name
38-
# end
35+
# TODO: Uglify instance_variables
36+
# when :attr_reader, :attr_writer, :attr_accessor
37+
# method_names = node.children[2..-1].map { |n| n.children[0] }
38+
# method_names.each do |method_name|
39+
# add_to_result method_name
40+
# end
3941
end
4042
node
4143
end
4244

4345
private
4446

4547
def initialize
48+
super
4649
@access_modifier = :public
4750
end
4851

lib/oblivion/method_uglifier.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
# frozen_string_literal: true
2+
13
module Oblivion
24
class MethodUglifier < BaseProcessor
35
def initialize(method_names)
6+
super()
47
@method_names = method_names
58
end
69

710
def on_send(node)
811
receiver, name, args = node.children
912

10-
new_children = [*node.children]
13+
new_children = Array.new(node.children)
1114
if [nil, AST::Node.new(:self)].include? receiver
1215
new_children[1] = @method_names[name] || name
1316
else

lib/oblivion/nodes.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'strings-case'
24

35
module Oblivion
@@ -21,7 +23,7 @@ def define_getter(name, child_index)
2123

2224
def define_update_method(name, child_index)
2325
define_method :"with_#{name}" do |new_value|
24-
new_children = [*children]
26+
new_children = Array.new(children)
2527
new_children[child_index] = new_value
2628
updated(nil, new_children)
2729
end
@@ -37,10 +39,14 @@ class Def < Base
3739

3840
def parse(node)
3941
class_name = Strings::Case.pascalcase(node.type.to_s)
40-
return node unless Nodes.const_defined?(class_name, inherit = false)
42+
return node unless own_constant_defined?(class_name)
4143

4244
node_class = Nodes.const_get(class_name)
4345
node_class.new(node.type, node.children, location: node.location)
4446
end
47+
48+
def own_constant_defined?(name)
49+
const_defined?(name, false)
50+
end
4551
end
4652
end

lib/oblivion/uglifier.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
# frozen_string_literal: true
2+
13
module Oblivion
24
class Uglifier < BaseProcessor
35
def self.process(ast)
46
new.process(ast)
57
end
68

7-
alias :on_class :uglify_class
9+
alias on_class uglify_class
810

911
private
1012

1113
def initialize
14+
super
1215
@method_names_by_class = {}
1316
end
1417
end

lib/oblivion/version.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Oblivion
2-
VERSION = '0.1.0'.freeze
4+
VERSION = '0.1.0'
35
end

oblivion.gemspec

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ Gem::Specification.new do |spec|
1111
spec.homepage = 'https://github.com/kfischer-okarin/oblivion'
1212
spec.license = 'MIT'
1313

14-
15-
spec.required_ruby_version = '>= 2.0.0'
14+
spec.required_ruby_version = '>= 2.4.0'
1615

1716
spec.add_dependency 'parser', '~> 2.7'
18-
spec.add_dependency 'unparser', '~> 0.4.7'
1917
spec.add_dependency 'strings-case', '~> 0.3.0'
18+
spec.add_dependency 'unparser', '~> 0.4.7'
2019

21-
spec.add_development_dependency 'rspec', '~> 3.9'
20+
spec.add_development_dependency 'codecov', '~> 0.2.6'
2221
spec.add_development_dependency 'guard-rspec', '~> 4.7'
22+
spec.add_development_dependency 'rspec', '~> 3.9'
2323
spec.add_development_dependency 'rspec_junit_formatter', '~> 0.4.1'
2424
spec.add_development_dependency 'simplecov', '~> 0.19.0'
25-
spec.add_development_dependency 'codecov', '~> 0.2.6'
2625

2726
spec.add_development_dependency 'rubocop', '~> 0.89.1'
2827
spec.add_development_dependency 'rubocop-rspec', '~> 1.43'

spec/oblivion/uglifier_spec.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative '../spec_helper'
24

35
RSpec.describe Oblivion::Uglifier do
@@ -122,7 +124,11 @@ def method; end
122124
# RUBY
123125
# }
124126
# let(:expected_body) {
125-
# a_node(:send, nil, method_definer, an_object_not_eq_to(s(:sym, :method)), an_object_not_eq_to(s(:sym, :method2)))
127+
# a_node(:send,
128+
# nil,
129+
# method_definer,
130+
# an_object_not_eq_to(s(:sym, :method)),
131+
# an_object_not_eq_to(s(:sym, :method2)))
126132
# }
127133

128134
# include_examples 'expected class body'
@@ -145,8 +151,8 @@ def method; end
145151

146152
let(:expected_body) {
147153
a_node(:class,
148-
s(:const, nil, :Inline), nil,
149-
including(a_method_definition(an_object_not_eq_to(:method))))
154+
s(:const, nil, :Inline), nil,
155+
including(a_method_definition(an_object_not_eq_to(:method))))
150156
}
151157

152158
include_examples 'expected class body'
@@ -164,7 +170,7 @@ def method; end
164170

165171
let(:expected_body) {
166172
a_node(:sclass, s(:self),
167-
including(a_method_definition(an_object_not_eq_to(:method))))
173+
including(a_method_definition(an_object_not_eq_to(:method))))
168174
}
169175

170176
include_examples 'expected class body'
@@ -254,8 +260,7 @@ def method; end
254260
s(:block,
255261
s(:send, s(:begin, s(:irange, s(:int, 1), s(:int, 10))), :each),
256262
s(:args, s(:procarg0, s(:arg, :i))),
257-
method_call_with_new_name
258-
)
263+
method_call_with_new_name)
259264
}
260265

261266
include_examples 'expected method body'

spec/oblivion_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative 'spec_helper'
24

35
RSpec.describe Oblivion do

spec/spec_helper.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# frozen_string_literal: true
2+
13
require 'ast'
24
require 'unparser'
35

4-
require_relative 'support/matchers.rb'
5-
require_relative 'support/coverage.rb'
6-
require_relative '../lib/oblivion.rb'
6+
require_relative 'support/matchers'
7+
require_relative 'support/coverage'
8+
require_relative '../lib/oblivion'
79

810
RSpec.configure do |config|
911
config.expect_with :rspec do |expectations|
@@ -30,6 +32,7 @@ def self_and_descendants
3032

3133
children.each do |c|
3234
next unless c.is_a? AST::Node
35+
3336
c.self_and_descendants.each do |cc|
3437
y << cc
3538
end

spec/support/coverage.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'simplecov'
24
SimpleCov.start do
35
add_filter 'spec'

spec/support/matchers.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
RSpec::Matchers.define_negated_matcher :an_object_not_eq_to, :an_object_eq_to
24

35
RSpec::Matchers.define :a_node do |type, *children|
@@ -23,7 +25,7 @@ def initialize(name)
2325

2426
def matches?(value)
2527
result = value.type == :def && values_match?(@name, value.children[0])
26-
result = result && values_match?(@body_matcher, value.children[2]) if @body_matcher
28+
result &&= values_match?(@body_matcher, value.children[2]) if @body_matcher
2729
result
2830
end
2931

@@ -44,6 +46,6 @@ def a_method_definition(name)
4446
end
4547
end
4648

47-
RSpec.configure do |config|
49+
RSpec.configure do |_config|
4850
include CustomMatchers
4951
end

0 commit comments

Comments
 (0)