rubocop-grape 0.1.2 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd6f622b828feeb370985d1d3c2bb9fd5d5e366dca5ced9f4232dc00bd69541f
4
- data.tar.gz: 175d75a07c48e200de2417e4e951332a44c6bbd3f4cdeaef38cf025f6ab14c90
3
+ metadata.gz: '02479172353db2e2fea8b532d01cf2dde616c7751e0d81a4a550227789128fc7'
4
+ data.tar.gz: 83cc6fcfe0f01e021e4bedd257a5e2162f4a20c4f35aad28e5dcf04f2364706d
5
5
  SHA512:
6
- metadata.gz: 6961acacaa6a7304899b852294f651e6aba4e9528f674756cdd0536d7384739a9bda2d928e7ef4c9b23dda30776ce020fa8f78bf98e5d5c5e9d599502d99a3d1
7
- data.tar.gz: eb230658d3993d3c096a1e826096a4b7f6f00243a6b810a57c35d7b7a80d3ea6a3a5bc4f515e9d784e63d930710980d9e0ec2d6a5b7880bfd028d13599297a03
6
+ metadata.gz: 46c7d251b5e0882e72a76d1aa77271f04386141a172a5b4d054ff079957b4a43f2ffe1cfdba7d993ed83aa868bcaac6366db9cbfb2882c6b01acbe6f9631ba49
7
+ data.tar.gz: bd9bb8ca47a1c2cca8eaf6b08ec39ea9d80d69e59ecb32225909b1da83ee38a516c1cf8e65e7a14d2c8e322ea5a8b0a356a5bad2a1243623263b40c4381b070a
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # RuboCop::Grape
1
+ # RuboCop Grape
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubocop/grape`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Actions Status](https://github.com/kakubin/rubocop-grape/workflows/Ruby/badge.svg?branch=main)](https://github.com/kakubin/rubocop-grape/actions?query=workflow%3ARuby)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ [RuboCop](https://github.com/rubocop/rubocop) extension for [Grape](https://rubygems.org/gems/grape/versions/0.16.2).
6
6
 
7
7
  ## Installation
8
8
 
@@ -16,20 +16,30 @@ And then execute:
16
16
 
17
17
  $ bundle install
18
18
 
19
- Or install it yourself as:
19
+ ## Usage
20
20
 
21
- $ gem install rubocop-grape
21
+ You need to tell RuboCop to load the Grape extension. There are three
22
+ ways to do this:
22
23
 
23
- ## Usage
24
+ ### RuboCop configuration file
24
25
 
25
- TODO: Write usage instructions here
26
+ Put this into your `.rubocop.yml`.
26
27
 
27
- ## Development
28
+ ```yaml
29
+ require: rubocop-grape
30
+ ```
28
31
 
29
- 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.
32
+ Alternatively, use the following array notation when specifying multiple extensions.
33
+
34
+ ```yaml
35
+ require:
36
+ - rubocop-other-extension
37
+ - rubocop-grape
38
+ ```
30
39
 
31
- 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).
40
+ Now you can run `rubocop` and it will automatically load the RuboCop Grape
41
+ cops together with the standard cops.
32
42
 
33
43
  ## Contributing
34
44
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubocop-grape.
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kakubin/rubocop-grape.
data/config/default.yml CHANGED
@@ -1,3 +1,6 @@
1
+ AllCops:
2
+ GrapeDir: 'app/api'
3
+
1
4
  Grape/RouteParamType:
2
5
  Enabled: true
3
6
  Safe: false
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Grape
6
+ # @example
7
+ # # bad
8
+ # ActiveRecord.with_readonly do
9
+ # @ivar = User.find(params[:user_id])
10
+ # end
11
+ #
12
+ # # good
13
+ # var = ActiveRecord.with_readonly do
14
+ # User.find(params[:user_id]
15
+ # end
16
+ #
17
+ # # good
18
+ # office = nil
19
+ # user = nil
20
+ #
21
+ # ActiveRecord.with_readonly do
22
+ # user = User.find(params[:user_id])
23
+ # office = user.office
24
+ # end
25
+ #
26
+ class Ivar < Base
27
+ MSG = "Don't use instance_variable"
28
+
29
+ def on_ivasgn(node)
30
+ add_offense(node)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -27,7 +27,7 @@ module RuboCop
27
27
  PATTERN
28
28
 
29
29
  def_node_matcher :http_method_node?, <<~PATTERN
30
- (block (send _ {:get :post :put :patch :delete}) ...)
30
+ (block (send _ {:get :post :put :patch :delete} ...) ...)
31
31
  PATTERN
32
32
 
33
33
  def on_block(node)
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'grape/ivar'
3
4
  require_relative 'grape/params_position'
4
5
  require_relative 'grape/route_param_type'
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Grape
6
+ # This is a monkey patch for RuboCop
7
+ # which makes rubocop-grape cops adapted only for grape files.
8
+ module GrapeFile
9
+ DEFAULT_GRAPE_DIR = 'app/api'
10
+
11
+ def grape_dir
12
+ @grape_dir ||= load_grape_file_from_config || DEFAULT_GRAPE_DIR
13
+ end
14
+
15
+ def grape_files
16
+ @grape_files ||= Dir.glob('**/*.rb', base: grape_dir).map do |relative_path|
17
+ File.absolute_path(relative_path, grape_dir)
18
+ end
19
+ end
20
+
21
+ Team.prepend self
22
+
23
+ private
24
+
25
+ def load_grape_file_from_config
26
+ @config.for_all_cops['GrapeDir']
27
+ end
28
+
29
+ def roundup_relevant_cops(filename)
30
+ super.select do |cop|
31
+ next true unless cop.class.name.match?(/RuboCop::Cop::Grape::.+$/)
32
+
33
+ grape_files.include?(filename)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Grape
5
- VERSION = '0.1.2'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
data/lib/rubocop-grape.rb CHANGED
@@ -8,4 +8,6 @@ require_relative 'rubocop/grape/inject'
8
8
 
9
9
  RuboCop::Grape::Inject.defaults!
10
10
 
11
+ require_relative 'rubocop/grape/grape_files'
12
+
11
13
  require_relative 'rubocop/cop/grape_cops'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-grape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akito Hikasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-21 00:00:00.000000000 Z
11
+ date: 2023-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -44,10 +44,12 @@ files:
44
44
  - bin/console
45
45
  - config/default.yml
46
46
  - lib/rubocop-grape.rb
47
+ - lib/rubocop/cop/grape/ivar.rb
47
48
  - lib/rubocop/cop/grape/params_position.rb
48
49
  - lib/rubocop/cop/grape/route_param_type.rb
49
50
  - lib/rubocop/cop/grape_cops.rb
50
51
  - lib/rubocop/grape.rb
52
+ - lib/rubocop/grape/grape_files.rb
51
53
  - lib/rubocop/grape/inject.rb
52
54
  - lib/rubocop/grape/version.rb
53
55
  homepage: https://github.com/kakubin/rubocop-grape