rubocop-each_return 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 444edce9436ad5a8d1bd42df45bc3db1b6c6a41c1d6ee062d5b4e66b4d000994
4
+ data.tar.gz: 17fbed82c763588ccfe03d74762f7822a842d396fe6b35ae79d390fc5582bd86
5
+ SHA512:
6
+ metadata.gz: 2efa4a0166cc7f8f6dc6162e5d8c4c31908997c29e1f7065c3933acf90c3f5592a8199646562d1a4881f787e01e4743ba41d0df26e281690ee0a4c513c6d86d2
7
+ data.tar.gz: da9858b6c0fde414230af990a14451cc232d941f58d4f1019ce9728d1f069743af6659ce0fa5370fd2d666276835d5a7ecf35b6bbccc3f45e053d1c4c3084199
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ Naming/FileName:
2
+ Exclude:
3
+ - lib/rubocop-each_return.rb
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-01-22
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rubocop-each_return.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-each_return (0.1.0)
5
+ rubocop
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ method_source (1.0.0)
14
+ parallel (1.21.0)
15
+ parser (3.1.0.0)
16
+ ast (~> 2.4.1)
17
+ pry (0.14.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.2.0)
23
+ rexml (3.2.5)
24
+ rspec (3.10.0)
25
+ rspec-core (~> 3.10.0)
26
+ rspec-expectations (~> 3.10.0)
27
+ rspec-mocks (~> 3.10.0)
28
+ rspec-core (3.10.1)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-expectations (3.10.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-mocks (3.10.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-support (3.10.3)
37
+ rubocop (1.25.0)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.1.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml
43
+ rubocop-ast (>= 1.15.1, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 3.0)
46
+ rubocop-ast (1.15.1)
47
+ parser (>= 3.0.1.1)
48
+ ruby-progressbar (1.11.0)
49
+ unicode-display_width (2.1.0)
50
+
51
+ PLATFORMS
52
+ x86_64-linux
53
+
54
+ DEPENDENCIES
55
+ pry
56
+ rake (~> 13.0)
57
+ rspec (~> 3.0)
58
+ rubocop (~> 1.21)
59
+ rubocop-each_return!
60
+
61
+ BUNDLED WITH
62
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Javier Jimenez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Rubocop::EachReturn
2
+
3
+ This cop checks for the use of return values when calling `each` method.
4
+
5
+ `each` method just returns the same object that was originally being called, so it makes no sense
6
+ to store it in another variable.
7
+
8
+ Most probably the author thought she was calling `map` and wanted to use the result, this
9
+ cop helps the developer identify those cases.
10
+
11
+ It can autocorrect this offense by removing the variable and the operator of a lvasgn. Autocorrect in this case means removing the assigment since it has no effect but the author could also want to change `each` for `map`, this gem can not help with that and the author should identify that potential problem.
12
+
13
+ ```
14
+ # incorrect
15
+ value = object.each { |x| x*2 }
16
+
17
+ # correct
18
+ object.each { |x| operation! }
19
+ value = object.map { |x| x*2 }
20
+ ```
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'rubocop-each_return', require: false
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle install
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install rubocop-each_return
37
+
38
+ ## Usage
39
+
40
+ TODO: Write usage instructions here
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/javiyu/rubocop-each_return.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ require 'rspec/core/rake_task'
15
+
16
+ RSpec::Core::RakeTask.new(:spec) do |spec|
17
+ spec.pattern = FileList['spec/**/*_spec.rb']
18
+ end
19
+
20
+ desc 'Generate a new cop with a template'
21
+ task :new_cop, [:cop] do |_task, args|
22
+ require 'rubocop'
23
+
24
+ cop_name = args.fetch(:cop) do
25
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
26
+ exit!
27
+ end
28
+
29
+ generator = RuboCop::Cop::Generator.new(cop_name)
30
+
31
+ generator.write_source
32
+ generator.write_spec
33
+ generator.inject_require(root_file_path: 'lib/rubocop/cop/each_return_cops.rb')
34
+ generator.inject_config(config_file_path: 'config/default.yml')
35
+
36
+ puts generator.todo
37
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rubocop/each_return"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ # Write it!
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ # This cop checks for the use of return values when calling `each` method.
8
+ #
9
+ # `each` method just returns the same object that was originally being called, so it makes no sense
10
+ # to store it in another variable.
11
+ # Most probably the author thought she was calling `map` and wanted to use the result, this
12
+ # cop helps the developer identify those cases.
13
+ # It can autocorrect this offense by removing the variable and the operator of a lvasgn.
14
+ #
15
+ # @example
16
+ # # incorrect
17
+ # value = object.each { |x| x*2 }
18
+ #
19
+ # # correct
20
+ # object.each { |x| operation! }
21
+ # value = object.map { |x| x*2 }
22
+ #
23
+ class EachReturnValue < RuboCop::Cop::Base
24
+ extend AutoCorrector
25
+ include RangeHelp
26
+
27
+ def_node_matcher(:array_each?, '(lvasgn _ (block (send _ :each) ...))')
28
+
29
+ def on_lvasgn(node)
30
+ if array_each?(node)
31
+ add_offense(node, message: 'Do not use the return value of .each') do |corrector|
32
+ corrector.remove(node.loc.name)
33
+ corrector.remove(range_with_surrounding_space(range: node.loc.operator))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
4
+ # See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md
5
+ module RuboCop
6
+ module EachReturn
7
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
8
+ # bit of our configuration.
9
+ module Inject
10
+ def self.defaults!
11
+ path = CONFIG_DEFAULT.to_s
12
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
13
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
14
+ puts "configuration from #{path}" if ConfigLoader.debug?
15
+ config = ConfigLoader.merge_with_default(config, path)
16
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module EachReturn
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "each_return/version"
4
+
5
+ module RuboCop
6
+ module EachReturn
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
10
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
11
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
12
+
13
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
14
+ end
15
+ end
16
+
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ require_relative 'rubocop/each_return'
6
+ require_relative 'rubocop/each_return/version'
7
+ require_relative 'rubocop/each_return/inject'
8
+
9
+ RuboCop::EachReturn::Inject.defaults!
10
+
11
+ require_relative 'rubocop/cop/each_return_cops'
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rubocop/each_return/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rubocop-each_return"
7
+ spec.version = RuboCop::EachReturn::VERSION
8
+ spec.authors = ["Javier Jimenez"]
9
+ spec.email = ["javier@spacebay.net"]
10
+
11
+ spec.summary = "Rubocop extension to avoid assigning of the result of calling array.each to a variable"
12
+ spec.homepage = "https://github.com/javiyu/rubocop-each_return"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency 'rubocop'
31
+
32
+ spec.add_development_dependency 'pry'
33
+ end
34
+
@@ -0,0 +1,6 @@
1
+ module Rubocop
2
+ module EachReturn
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-each_return
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Javier Jimenez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - javier@spacebay.net
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - CHANGELOG.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - config/default.yml
59
+ - lib/rubocop-each_return.rb
60
+ - lib/rubocop/cop/each_return_cops.rb
61
+ - lib/rubocop/each_return.rb
62
+ - lib/rubocop/each_return/inject.rb
63
+ - lib/rubocop/each_return/version.rb
64
+ - rubocop-each_return.gemspec
65
+ - sig/rubocop/each_return.rbs
66
+ homepage: https://github.com/javiyu/rubocop-each_return
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/javiyu/rubocop-each_return
71
+ source_code_uri: https://github.com/javiyu/rubocop-each_return
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.6.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.3.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Rubocop extension to avoid assigning of the result of calling array.each
91
+ to a variable
92
+ test_files: []