fasterer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/.fasterer.yml +17 -0
  3. data/.gitignore +15 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +8 -0
  6. data/.travis.yml +6 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +93 -0
  10. data/Rakefile +7 -0
  11. data/bin/fasterer +5 -0
  12. data/fasterer.gemspec +28 -0
  13. data/lib/fasterer.rb +7 -0
  14. data/lib/fasterer/analyzer.rb +99 -0
  15. data/lib/fasterer/binary_call.rb +4 -0
  16. data/lib/fasterer/cli.rb +10 -0
  17. data/lib/fasterer/file_traverser.rb +81 -0
  18. data/lib/fasterer/method_call.rb +138 -0
  19. data/lib/fasterer/method_definition.rb +51 -0
  20. data/lib/fasterer/offense.rb +69 -0
  21. data/lib/fasterer/offense_collector.rb +17 -0
  22. data/lib/fasterer/parse_error.rb +10 -0
  23. data/lib/fasterer/parser.rb +11 -0
  24. data/lib/fasterer/rescue_call.rb +36 -0
  25. data/lib/fasterer/scanners/method_call_scanner.rb +134 -0
  26. data/lib/fasterer/scanners/method_definition_scanner.rb +52 -0
  27. data/lib/fasterer/scanners/offensive.rb +25 -0
  28. data/lib/fasterer/scanners/rescue_call_scanner.rb +28 -0
  29. data/lib/fasterer/token.rb +27 -0
  30. data/lib/fasterer/version.rb +3 -0
  31. data/spec/lib/fasterer/analyzer/01_parallel_assignment_spec.rb +12 -0
  32. data/spec/lib/fasterer/analyzer/02_rescue_vs_respond_to_spec.rb +12 -0
  33. data/spec/lib/fasterer/analyzer/03_module_eval_spec.rb +12 -0
  34. data/spec/lib/fasterer/analyzer/04_find_vs_bsearch_spec.rb +12 -0
  35. data/spec/lib/fasterer/analyzer/06_shuffle_first_vs_sample_spec.rb +12 -0
  36. data/spec/lib/fasterer/analyzer/08_for_loop_vs_each_spec.rb +12 -0
  37. data/spec/lib/fasterer/analyzer/09_each_with_index_vs_while_spec.rb +12 -0
  38. data/spec/lib/fasterer/analyzer/10_map_flatten_vs_flat_map_spec.rb +12 -0
  39. data/spec/lib/fasterer/analyzer/11_reverse_each_vs_reverse_each_spec.rb +12 -0
  40. data/spec/lib/fasterer/analyzer/12_select_first_vs_detect_spec.rb +12 -0
  41. data/spec/lib/fasterer/analyzer/13_sort_vs_sort_by_spec.rb +12 -0
  42. data/spec/lib/fasterer/analyzer/14_fetch_with_argument_vs_block_spec.rb +12 -0
  43. data/spec/lib/fasterer/analyzer/15_keys_each_vs_each_key_spec.rb +12 -0
  44. data/spec/lib/fasterer/analyzer/16_hash_merge_bang_vs_hash_brackets_spec.rb +12 -0
  45. data/spec/lib/fasterer/analyzer/18_block_vs_symbol_to_proc_spec.rb +12 -0
  46. data/spec/lib/fasterer/analyzer/19_proc_call_vs_yield_spec.rb +12 -0
  47. data/spec/lib/fasterer/analyzer/24_gsub_vs_tr_spec.rb +12 -0
  48. data/spec/lib/fasterer/analyzer/98_misc_spec.rb +11 -0
  49. data/spec/lib/fasterer/analyzer/99_exceptional_files_spec.rb +11 -0
  50. data/spec/lib/fasterer/method_call_spec.rb +483 -0
  51. data/spec/lib/fasterer/method_definition_spec.rb +93 -0
  52. data/spec/lib/fasterer/rescue_call_spec.rb +76 -0
  53. data/spec/spec_helper.rb +99 -0
  54. data/spec/support/analyzer/01_parallel_assignment.rb +10 -0
  55. data/spec/support/analyzer/02_rescue_vs_respond_to.rb +36 -0
  56. data/spec/support/analyzer/03_module_eval.rb +11 -0
  57. data/spec/support/analyzer/04_find_vs_bsearch.rb +5 -0
  58. data/spec/support/analyzer/06_shuffle_first_vs_sample.rb +5 -0
  59. data/spec/support/analyzer/08_for_loop_vs_each.rb +8 -0
  60. data/spec/support/analyzer/09_each_with_index_vs_while.rb +3 -0
  61. data/spec/support/analyzer/10_map_flatten_vs_flat_map.rb +15 -0
  62. data/spec/support/analyzer/11_reverse_each_vs_reverse_each.rb +9 -0
  63. data/spec/support/analyzer/12_select_first_vs_detect.rb +7 -0
  64. data/spec/support/analyzer/13_sort_vs_sort_by.rb +9 -0
  65. data/spec/support/analyzer/14_fetch_with_argument_vs_block.rb +11 -0
  66. data/spec/support/analyzer/15_keys_each_vs_each_key.rb +15 -0
  67. data/spec/support/analyzer/16_hash_merge_bang_vs_hash_brackets.rb +21 -0
  68. data/spec/support/analyzer/18_block_vs_symbol_to_proc.rb +30 -0
  69. data/spec/support/analyzer/19_proc_call_vs_yield.rb +24 -0
  70. data/spec/support/analyzer/24_gsub_vs_tr.rb +14 -0
  71. data/spec/support/analyzer/98_misc.rb +15 -0
  72. data/spec/support/analyzer/99_exceptional_files.rb +7 -0
  73. data/spec/support/binary_call/simple_comparison.rb +0 -0
  74. data/spec/support/method_call/method_call_on_constant.rb +1 -0
  75. data/spec/support/method_call/method_call_on_integer.rb +1 -0
  76. data/spec/support/method_call/method_call_on_method_call.rb +1 -0
  77. data/spec/support/method_call/method_call_on_string.rb +1 -0
  78. data/spec/support/method_call/method_call_on_variable.rb +2 -0
  79. data/spec/support/method_call/method_call_with_a_block.rb +5 -0
  80. data/spec/support/method_call/method_call_with_a_integer_argument.rb +1 -0
  81. data/spec/support/method_call/method_call_with_a_regex_argument.rb +1 -0
  82. data/spec/support/method_call/method_call_with_an_argument_and_a_block.rb +2 -0
  83. data/spec/support/method_call/method_call_with_an_implicit_receiver.rb +1 -0
  84. data/spec/support/method_call/method_call_with_an_implicit_receiver_and_no_brackets.rb +1 -0
  85. data/spec/support/method_call/method_call_with_an_implicit_receiver_and_no_brackets_and_do_end.rb +3 -0
  86. data/spec/support/method_call/method_call_with_equals.rb +1 -0
  87. data/spec/support/method_call/method_call_with_one_argument.rb +1 -0
  88. data/spec/support/method_call/method_call_with_two_arguments.rb +2 -0
  89. data/spec/support/method_call/method_call_without_brackets.rb +1 -0
  90. data/spec/support/method_definition/method_with_argument_and_block.rb +3 -0
  91. data/spec/support/method_definition/method_with_block.rb +3 -0
  92. data/spec/support/method_definition/method_with_default_argument.rb +3 -0
  93. data/spec/support/method_definition/method_with_splat_and_block.rb +3 -0
  94. data/spec/support/method_definition/simple_method.rb +2 -0
  95. data/spec/support/method_definition/simple_method_omitted_parenthesis.rb +2 -0
  96. data/spec/support/method_definition/simple_method_with_argument.rb +3 -0
  97. data/spec/support/rescue_call/plain_rescue.rb +7 -0
  98. data/spec/support/rescue_call/rescue_with_class.rb +5 -0
  99. data/spec/support/rescue_call/rescue_with_class_and_variable.rb +5 -0
  100. data/spec/support/rescue_call/rescue_with_multiple_classes.rb +4 -0
  101. data/spec/support/rescue_call/rescue_with_multiple_classes_and_variable.rb +5 -0
  102. data/spec/support/rescue_call/rescue_with_variable.rb +6 -0
  103. 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
@@ -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,10 @@
1
+ a, b = 1, 2
2
+
3
+ class User
4
+ ROLES = ['mortal', 'admin', 'superadmin']
5
+
6
+ def initialize(first_name, last_name)
7
+ @first_name, @last_name = first_name, last_name
8
+ @full_name = [@first_name, @last_name].join(' ')
9
+ end
10
+ 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,11 @@
1
+ module Hihi
2
+ class << self
3
+ module_eval %{
4
+ def hello
5
+ puts "win"
6
+ end
7
+ }
8
+ end
9
+ end
10
+
11
+ Hihi.hello
@@ -0,0 +1,5 @@
1
+ data = [1,2,3]
2
+
3
+ data.bsearch { |number| number > 77_777_777 }
4
+ data.find { |number| number > 77_777_777 }
5
+
@@ -0,0 +1,5 @@
1
+ [].shuffle().first()
2
+ [].shuffle().first
3
+ [].shuffle.first()
4
+ [].shuffle.first
5
+ get_array().shuffle.first
@@ -0,0 +1,8 @@
1
+ for number in [*1..100] do
2
+ number
3
+ end
4
+
5
+ # don't catch methods named for
6
+ [].for do
7
+ end
8
+ 'ruby'.for('the world')
@@ -0,0 +1,3 @@
1
+ [1,2,3,4].each_with_index do |element, i|
2
+ puts "#{element}, #{i}"
3
+ end
@@ -0,0 +1,15 @@
1
+ ARRAY = (1..100).to_a
2
+
3
+ ARRAY.map { |e| [e, e] }.flatten(1)
4
+
5
+ ARRAY.map { |e| [e, e] }.flatten
6
+
7
+ ARRAY.map do |e|
8
+ [e, e]
9
+ end.flatten
10
+
11
+ ARRAY.map do |e|
12
+ [e, e]
13
+ end.flatten(1)
14
+
15
+ ARRAY.flat_map { |e| [e, e] }
@@ -0,0 +1,9 @@
1
+ ARRAY = (1..100).to_a
2
+
3
+ ARRAY.reverse.each{|x| x}
4
+
5
+ ARRAY.reverse_each{|x| x}
6
+
7
+ ARRAY.reverse.each do |x|
8
+ x
9
+ end
@@ -0,0 +1,7 @@
1
+ ARRAY = [*1..100]
2
+
3
+ ARRAY.select { |x| x.eql?(15) }.first
4
+
5
+ ARRAY.select do |x|
6
+ x.eql?(15)
7
+ end.first
@@ -0,0 +1,9 @@
1
+ User = Struct.new(:name)
2
+ ARRAY = Array.new(3) do
3
+ User.new(sprintf("%010d"), rand(1_000_000_000))
4
+ end
5
+
6
+ ARRAY.sort { |a, b| a.name <=> b.name }
7
+ ARRAY.sort_by(&:name)
8
+
9
+ ARRAY.sort
@@ -0,0 +1,11 @@
1
+ HASH = { :writing => :fast_ruby }
2
+
3
+ def slow
4
+ HASH.fetch(:writing, [*1..100])
5
+ end
6
+
7
+ def fast
8
+ HASH.fetch(:writing) { [*1..100] }
9
+ end
10
+
11
+ HASH.fetch(:writing)
@@ -0,0 +1,15 @@
1
+ HASH = {
2
+ red: 5,
3
+ black: 10,
4
+ grey: 15
5
+ }
6
+
7
+ HASH.keys.each(&:to_sym)
8
+
9
+ HASH.keys.each { |key| puts key.to_sym }
10
+
11
+ HASH.keys.each do |key|
12
+ puts key.to_sym
13
+ end
14
+
15
+ HASH.each_key(&:to_sym)
@@ -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!({})