fasterer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.fasterer.yml +17 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +7 -0
- data/bin/fasterer +5 -0
- data/fasterer.gemspec +28 -0
- data/lib/fasterer.rb +7 -0
- data/lib/fasterer/analyzer.rb +99 -0
- data/lib/fasterer/binary_call.rb +4 -0
- data/lib/fasterer/cli.rb +10 -0
- data/lib/fasterer/file_traverser.rb +81 -0
- data/lib/fasterer/method_call.rb +138 -0
- data/lib/fasterer/method_definition.rb +51 -0
- data/lib/fasterer/offense.rb +69 -0
- data/lib/fasterer/offense_collector.rb +17 -0
- data/lib/fasterer/parse_error.rb +10 -0
- data/lib/fasterer/parser.rb +11 -0
- data/lib/fasterer/rescue_call.rb +36 -0
- data/lib/fasterer/scanners/method_call_scanner.rb +134 -0
- data/lib/fasterer/scanners/method_definition_scanner.rb +52 -0
- data/lib/fasterer/scanners/offensive.rb +25 -0
- data/lib/fasterer/scanners/rescue_call_scanner.rb +28 -0
- data/lib/fasterer/token.rb +27 -0
- data/lib/fasterer/version.rb +3 -0
- data/spec/lib/fasterer/analyzer/01_parallel_assignment_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/02_rescue_vs_respond_to_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/03_module_eval_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/04_find_vs_bsearch_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/06_shuffle_first_vs_sample_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/08_for_loop_vs_each_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/09_each_with_index_vs_while_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/10_map_flatten_vs_flat_map_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/11_reverse_each_vs_reverse_each_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/12_select_first_vs_detect_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/13_sort_vs_sort_by_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/14_fetch_with_argument_vs_block_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/15_keys_each_vs_each_key_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/16_hash_merge_bang_vs_hash_brackets_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/18_block_vs_symbol_to_proc_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/19_proc_call_vs_yield_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/24_gsub_vs_tr_spec.rb +12 -0
- data/spec/lib/fasterer/analyzer/98_misc_spec.rb +11 -0
- data/spec/lib/fasterer/analyzer/99_exceptional_files_spec.rb +11 -0
- data/spec/lib/fasterer/method_call_spec.rb +483 -0
- data/spec/lib/fasterer/method_definition_spec.rb +93 -0
- data/spec/lib/fasterer/rescue_call_spec.rb +76 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/support/analyzer/01_parallel_assignment.rb +10 -0
- data/spec/support/analyzer/02_rescue_vs_respond_to.rb +36 -0
- data/spec/support/analyzer/03_module_eval.rb +11 -0
- data/spec/support/analyzer/04_find_vs_bsearch.rb +5 -0
- data/spec/support/analyzer/06_shuffle_first_vs_sample.rb +5 -0
- data/spec/support/analyzer/08_for_loop_vs_each.rb +8 -0
- data/spec/support/analyzer/09_each_with_index_vs_while.rb +3 -0
- data/spec/support/analyzer/10_map_flatten_vs_flat_map.rb +15 -0
- data/spec/support/analyzer/11_reverse_each_vs_reverse_each.rb +9 -0
- data/spec/support/analyzer/12_select_first_vs_detect.rb +7 -0
- data/spec/support/analyzer/13_sort_vs_sort_by.rb +9 -0
- data/spec/support/analyzer/14_fetch_with_argument_vs_block.rb +11 -0
- data/spec/support/analyzer/15_keys_each_vs_each_key.rb +15 -0
- data/spec/support/analyzer/16_hash_merge_bang_vs_hash_brackets.rb +21 -0
- data/spec/support/analyzer/18_block_vs_symbol_to_proc.rb +30 -0
- data/spec/support/analyzer/19_proc_call_vs_yield.rb +24 -0
- data/spec/support/analyzer/24_gsub_vs_tr.rb +14 -0
- data/spec/support/analyzer/98_misc.rb +15 -0
- data/spec/support/analyzer/99_exceptional_files.rb +7 -0
- data/spec/support/binary_call/simple_comparison.rb +0 -0
- data/spec/support/method_call/method_call_on_constant.rb +1 -0
- data/spec/support/method_call/method_call_on_integer.rb +1 -0
- data/spec/support/method_call/method_call_on_method_call.rb +1 -0
- data/spec/support/method_call/method_call_on_string.rb +1 -0
- data/spec/support/method_call/method_call_on_variable.rb +2 -0
- data/spec/support/method_call/method_call_with_a_block.rb +5 -0
- data/spec/support/method_call/method_call_with_a_integer_argument.rb +1 -0
- data/spec/support/method_call/method_call_with_a_regex_argument.rb +1 -0
- data/spec/support/method_call/method_call_with_an_argument_and_a_block.rb +2 -0
- data/spec/support/method_call/method_call_with_an_implicit_receiver.rb +1 -0
- data/spec/support/method_call/method_call_with_an_implicit_receiver_and_no_brackets.rb +1 -0
- data/spec/support/method_call/method_call_with_an_implicit_receiver_and_no_brackets_and_do_end.rb +3 -0
- data/spec/support/method_call/method_call_with_equals.rb +1 -0
- data/spec/support/method_call/method_call_with_one_argument.rb +1 -0
- data/spec/support/method_call/method_call_with_two_arguments.rb +2 -0
- data/spec/support/method_call/method_call_without_brackets.rb +1 -0
- data/spec/support/method_definition/method_with_argument_and_block.rb +3 -0
- data/spec/support/method_definition/method_with_block.rb +3 -0
- data/spec/support/method_definition/method_with_default_argument.rb +3 -0
- data/spec/support/method_definition/method_with_splat_and_block.rb +3 -0
- data/spec/support/method_definition/simple_method.rb +2 -0
- data/spec/support/method_definition/simple_method_omitted_parenthesis.rb +2 -0
- data/spec/support/method_definition/simple_method_with_argument.rb +3 -0
- data/spec/support/rescue_call/plain_rescue.rb +7 -0
- data/spec/support/rescue_call/rescue_with_class.rb +5 -0
- data/spec/support/rescue_call/rescue_with_class_and_variable.rb +5 -0
- data/spec/support/rescue_call/rescue_with_multiple_classes.rb +4 -0
- data/spec/support/rescue_call/rescue_with_multiple_classes_and_variable.rb +5 -0
- data/spec/support/rescue_call/rescue_with_variable.rb +6 -0
- metadata +303 -0
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Fasterer::MethodDefinition do
|
4
|
+
|
5
|
+
let(:def_element) do
|
6
|
+
Fasterer::Parser.parse(File.read(RSpec.root.join('support', 'method_definition', file_name)))
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:method_definition) do
|
10
|
+
Fasterer::MethodDefinition.new(def_element)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'method with no arguments' do
|
14
|
+
|
15
|
+
let(:file_name) { 'simple_method.rb' }
|
16
|
+
|
17
|
+
it 'should not detect block' do
|
18
|
+
expect(method_definition.method_name).to eq(:hello)
|
19
|
+
expect(method_definition.has_block?).to eq(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'method with no arguments and omitted parenthesis' do
|
25
|
+
|
26
|
+
let(:file_name) { 'simple_method_omitted_parenthesis.rb' }
|
27
|
+
|
28
|
+
it 'should not detect block' do
|
29
|
+
expect(method_definition.method_name).to eq(:hello)
|
30
|
+
expect(method_definition.has_block?).to eq(false)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'method with one argument' do
|
36
|
+
|
37
|
+
let(:file_name) { 'simple_method_with_argument.rb' }
|
38
|
+
|
39
|
+
it 'should not detect block' do
|
40
|
+
expect(method_definition.method_name).to eq(:hello)
|
41
|
+
expect(method_definition.has_block?).to eq(false)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'method with a block' do
|
47
|
+
|
48
|
+
let(:file_name) { 'method_with_block.rb' }
|
49
|
+
|
50
|
+
it 'should detect block' do
|
51
|
+
expect(method_definition.method_name).to eq(:hello)
|
52
|
+
expect(method_definition.has_block?).to eq(true)
|
53
|
+
expect(method_definition.block_argument_name).to eq(:block)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'method with an argument and a block' do
|
59
|
+
|
60
|
+
let(:file_name) { 'method_with_argument_and_block.rb' }
|
61
|
+
|
62
|
+
it 'should detect block' do
|
63
|
+
expect(method_definition.method_name).to eq(:hello)
|
64
|
+
expect(method_definition.has_block?).to eq(true)
|
65
|
+
expect(method_definition.block_argument_name).to eq(:block)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'method with an splat argument and a block' do
|
71
|
+
|
72
|
+
let(:file_name) { 'method_with_splat_and_block.rb' }
|
73
|
+
|
74
|
+
it 'should detect block' do
|
75
|
+
expect(method_definition.method_name).to eq(:hello)
|
76
|
+
expect(method_definition.has_block?).to eq(true)
|
77
|
+
expect(method_definition.block_argument_name).to eq(:block)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'method with an default argument' do
|
83
|
+
|
84
|
+
let(:file_name) { 'method_with_default_argument.rb' }
|
85
|
+
|
86
|
+
it 'should not detect block' do
|
87
|
+
expect(method_definition.method_name).to eq(:hello)
|
88
|
+
expect(method_definition.has_block?).to eq(false)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Fasterer::RescueCall do
|
4
|
+
|
5
|
+
let(:file_path) { RSpec.root.join('support', 'rescue_call', file_name) }
|
6
|
+
|
7
|
+
let(:rescue_element) do
|
8
|
+
sexpd_file = Fasterer::Parser.parse(File.read(file_path))
|
9
|
+
sexpd_file[2]
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:rescue_call) do
|
13
|
+
Fasterer::RescueCall.new(rescue_element)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'plain rescue call' do
|
17
|
+
|
18
|
+
let(:file_name) { 'plain_rescue.rb' }
|
19
|
+
|
20
|
+
it 'should detect constant' do
|
21
|
+
expect(rescue_call.rescue_classes).to eq([])
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'rescue call with class' do
|
27
|
+
|
28
|
+
let(:file_name) { 'rescue_with_class.rb' }
|
29
|
+
|
30
|
+
it 'should detect integer' do
|
31
|
+
expect(rescue_call.rescue_classes).to eq([:NoMethodError])
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'rescue call with class and variable' do
|
37
|
+
|
38
|
+
let(:file_name) { 'rescue_with_class_and_variable.rb' }
|
39
|
+
|
40
|
+
it 'should detect string' do
|
41
|
+
expect(rescue_call.rescue_classes).to eq([:NoMethodError])
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'rescue call with variable' do
|
47
|
+
|
48
|
+
let(:file_name) { 'rescue_with_variable.rb' }
|
49
|
+
|
50
|
+
it 'should detect variable' do
|
51
|
+
expect(rescue_call.rescue_classes).to eq([])
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'rescue call with multiple classes' do
|
57
|
+
|
58
|
+
let(:file_name) { 'rescue_with_multiple_classes.rb' }
|
59
|
+
|
60
|
+
it 'should detect method' do
|
61
|
+
expect(rescue_call.rescue_classes).to eq([:NoMethodError, :StandardError])
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'rescue call with multiple classes and variable' do
|
67
|
+
|
68
|
+
let(:file_name) { 'rescue_with_multiple_classes_and_variable.rb' }
|
69
|
+
|
70
|
+
it 'should detect method' do
|
71
|
+
expect(rescue_call.rescue_classes).to eq([:NoMethodError, :StandardError])
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'fasterer'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
def RSpec.root
|
8
|
+
@root_path = Pathname.new(File.dirname(__FILE__))
|
9
|
+
end
|
10
|
+
|
11
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
12
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
13
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
14
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
15
|
+
#
|
16
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
17
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
18
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
19
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
20
|
+
# a separate helper file that requires the additional dependencies and performs
|
21
|
+
# the additional setup, and require it from the spec files that actually need it.
|
22
|
+
#
|
23
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
24
|
+
# users commonly want.
|
25
|
+
#
|
26
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
27
|
+
RSpec.configure do |config|
|
28
|
+
# rspec-expectations config goes here. You can use an alternate
|
29
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
30
|
+
# assertions if you prefer.
|
31
|
+
config.expect_with :rspec do |expectations|
|
32
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
33
|
+
# and `failure_message` of custom matchers include text for helper methods
|
34
|
+
# defined using `chain`, e.g.:
|
35
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
36
|
+
# # => "be bigger than 2 and smaller than 4"
|
37
|
+
# ...rather than:
|
38
|
+
# # => "be bigger than 2"
|
39
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
40
|
+
end
|
41
|
+
|
42
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
43
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
44
|
+
config.mock_with :rspec do |mocks|
|
45
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
46
|
+
# a real object. This is generally recommended, and will default to
|
47
|
+
# `true` in RSpec 4.
|
48
|
+
mocks.verify_partial_doubles = true
|
49
|
+
end
|
50
|
+
|
51
|
+
# The settings below are suggested to provide a good initial experience
|
52
|
+
# with RSpec, but feel free to customize to your heart's content.
|
53
|
+
=begin
|
54
|
+
# These two settings work together to allow you to limit a spec run
|
55
|
+
# to individual examples or groups you care about by tagging them with
|
56
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
57
|
+
# get run.
|
58
|
+
config.filter_run :focus
|
59
|
+
config.run_all_when_everything_filtered = true
|
60
|
+
|
61
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
62
|
+
# For more details, see:
|
63
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
64
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
65
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
66
|
+
config.disable_monkey_patching!
|
67
|
+
|
68
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
69
|
+
# be too noisy due to issues in dependencies.
|
70
|
+
config.warnings = true
|
71
|
+
|
72
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
73
|
+
# file, and it's useful to allow more verbose output when running an
|
74
|
+
# individual spec file.
|
75
|
+
if config.files_to_run.one?
|
76
|
+
# Use the documentation formatter for detailed output,
|
77
|
+
# unless a formatter has already been configured
|
78
|
+
# (e.g. via a command-line flag).
|
79
|
+
config.default_formatter = 'doc'
|
80
|
+
end
|
81
|
+
|
82
|
+
# Print the 10 slowest examples and example groups at the
|
83
|
+
# end of the spec run, to help surface which specs are running
|
84
|
+
# particularly slow.
|
85
|
+
config.profile_examples = 10
|
86
|
+
|
87
|
+
# Run specs in random order to surface order dependencies. If you find an
|
88
|
+
# order dependency and want to debug it, you can fix the order by providing
|
89
|
+
# the seed, which is printed after each run.
|
90
|
+
# --seed 1234
|
91
|
+
config.order = :random
|
92
|
+
|
93
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
94
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
95
|
+
# test failures related to randomization by passing the same `--seed` value
|
96
|
+
# as the one that triggered the failure.
|
97
|
+
Kernel.srand config.seed
|
98
|
+
=end
|
99
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
begin
|
2
|
+
'abakus'.to_a
|
3
|
+
rescue NoMethodError
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
begin
|
8
|
+
'abakus'.to_a
|
9
|
+
rescue NoMethodError => e
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
begin
|
14
|
+
'abakus'.to_a
|
15
|
+
rescue NoMethodError, StandardError => e
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
begin
|
20
|
+
'abakus'.to_a
|
21
|
+
rescue => e
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
'abakus'.to_a
|
27
|
+
rescue
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
'abakus'.to_a
|
33
|
+
rescue StandardError
|
34
|
+
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
h.merge!(item: 1, item2: 3)
|
2
|
+
|
3
|
+
h.merge!
|
4
|
+
|
5
|
+
h.merge!(item, item: 1)
|
6
|
+
|
7
|
+
h.merge(item: 1)
|
8
|
+
|
9
|
+
ENUM.each_with_object({}) do |e, h|
|
10
|
+
h.merge!(e => e)
|
11
|
+
end
|
12
|
+
|
13
|
+
ENUM.each_with_object({}) do |e, h|
|
14
|
+
h[e] = e
|
15
|
+
end
|
16
|
+
|
17
|
+
h.merge!(item: 1)
|
18
|
+
|
19
|
+
h.merge!({item: 1})
|
20
|
+
|
21
|
+
h.merge!({})
|