rubocop-grape 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1ddcfa8ca8f07c8b82d051f1b962d106b7e9390f505bce166901deb3d9421c4
4
- data.tar.gz: c7de3f5ff9600b83a730d347278e52d0f3f4553389c184b2c04be425193079b3
3
+ metadata.gz: 590df7af3dec0374453d4f8ba4282b6d2981492c4e25d0ea537dd9e7b1de8eca
4
+ data.tar.gz: d52f772ffb08ba797df64f30298362ce04872477b36544614430e7dd157540d4
5
5
  SHA512:
6
- metadata.gz: 204e3c444825e10ab0c90bdfbb3dac71db90d5aeb56a718a6124ef1b19a3883e93e5815131aefbc8bcc8ba5c20d46c2bdf987f145914167299a33f7cbf95fd44
7
- data.tar.gz: 02ebbff3326f417c71983683ed98aa7a414e9a2d3456b671b4a5fe7f19ae4cb84ccb42723f47ee1ed5085f359d6fb70557b1703045f053430e4726c32417f80a
6
+ metadata.gz: 32c44eac0eee98cee4c97de9e3ff829c7f7d65ef532ee5066985804cc430aff642c93402712240023b430a5a02578806f9a2864e1550147dbc6e9a12e56dbf0c
7
+ data.tar.gz: 6692df01c1c324431ba0625d5f2042447afb7b882977dd36fa3f556a9bf448f79cf5e9884240a66f9af1263d04b31e70ec8a1fd212975b6e0758816e9d899b29
data/config/default.yml CHANGED
@@ -28,3 +28,9 @@ Grape/ParamsPosition:
28
28
  Enabled: true
29
29
  Safe: false
30
30
  VersionAdded: '0.1'
31
+
32
+ Grape/PresentWith:
33
+ Enabled: true
34
+ Safe: false
35
+ SafeAutoCorrect: false
36
+ VersionAdded: '0.4.2'
@@ -9,15 +9,12 @@ module RuboCop
9
9
  # and https://en.wikipedia.org/wiki/ABC_Software_Metric.
10
10
  class AbcSize < Base
11
11
  extend ExcludeLimit
12
+ include EndpointHelper
12
13
 
13
14
  exclude_limit 'Max'
14
15
 
15
16
  MSG = 'Assignment Branch Condition size is too high. [%<abc_vector>s %<complexity>d/%<max>d]'
16
17
 
17
- def_node_matcher :http_method_node?, <<~PATTERN
18
- (block (send _ {:get :post :put :patch :delete} ...) ...)
19
- PATTERN
20
-
21
18
  def on_block(node)
22
19
  return unless http_method_node?(node)
23
20
 
@@ -31,20 +31,16 @@ module RuboCop
31
31
  #
32
32
  class FieldName < Base
33
33
  include ConfigurableNaming
34
- include RangeHelp
34
+ include EndpointHelper
35
35
 
36
36
  MSG = 'Use %<style>s for field names.'
37
37
 
38
- def_node_matcher :params_block?, <<~PATTERN
39
- (block (send _ :params) _ $_)
40
- PATTERN
41
-
42
38
  def_node_matcher :field_name, <<~PATTERN
43
39
  (send _ {:requires :optional} (sym $_) ...)
44
40
  PATTERN
45
41
 
46
42
  def on_block(node)
47
- return unless (body = params_block?(node))
43
+ return unless (body = params_node?(node))
48
44
 
49
45
  find_field_node(body)
50
46
  end
@@ -20,15 +20,9 @@ module RuboCop
20
20
  # end
21
21
  #
22
22
  class ParamsPosition < Base
23
- MSG = "It's no sense to define params in HTTP method's scope"
24
-
25
- def_node_matcher :params_block?, <<~PATTERN
26
- (block (send _ :params) ...)
27
- PATTERN
23
+ include EndpointHelper
28
24
 
29
- def_node_matcher :http_method_node?, <<~PATTERN
30
- (block (send _ {:get :post :put :patch :delete} ...) ...)
31
- PATTERN
25
+ MSG = "It's no sense to define params in HTTP method's scope"
32
26
 
33
27
  def on_block(node)
34
28
  return unless http_method_node?(node)
@@ -37,7 +31,7 @@ module RuboCop
37
31
  end
38
32
 
39
33
  def collect_violating_nodes(node, collector = [])
40
- collector.push(node) if node.type == :block && params_block?(node)
34
+ collector.push(node) if node.type == :block && params_node?(node)
41
35
 
42
36
  node.children.each do |descendant|
43
37
  collect_violating_nodes(descendant, collector) if descendant.is_a?(Parser::AST::Node)
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Grape
6
+ # @example
7
+ #
8
+ # # bad
9
+ # get do
10
+ # present users: users, with: API::Entities::User
11
+ # end
12
+ #
13
+ # # good
14
+ # get do
15
+ # present :users, users, with: API::Entities::User
16
+ # end
17
+ #
18
+ class PresentWith < Base
19
+ extend AutoCorrector
20
+ include EndpointHelper
21
+
22
+ MSG_TEMPLATE = 'Maybe you mistyped "`%<current>s`", it should be "`%<correct>s`"'
23
+
24
+ def_node_matcher :one_hash_arg_with_key_name_with?, <<~PATTERN
25
+ (send _ :present
26
+ (hash
27
+ (pair (sym $_key)
28
+ {(send _ $_val) (lvar $_val)})
29
+ (pair (sym :with) _)))
30
+ PATTERN
31
+
32
+ def on_send(present_node)
33
+ one_hash_arg_with_key_name_with?(present_node) do |key, val|
34
+ kwarg = present_node.children.last
35
+
36
+ add_offense(kwarg.children.first, message: message(key, val)) do |corrector|
37
+ corrector.remove(kwarg.children.first)
38
+ corrector.insert_before(kwarg, ":#{key}, #{val}")
39
+ end
40
+ end
41
+ end
42
+
43
+ def message(key, val)
44
+ format(MSG_TEMPLATE, current: "#{key}: #{val}", correct: ":#{key}, #{val}")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'mixin/endpoint_helper'
4
+
3
5
  require_relative 'grape/abc_size'
4
6
  require_relative 'grape/field_name'
5
7
  require_relative 'grape/ivar'
6
8
  require_relative 'grape/params_position'
9
+ require_relative 'grape/present_with'
7
10
  require_relative 'grape/route_param_type'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # EndpointHelper is a set of AST definitions for endpoint definition by Grape DSL
6
+ module EndpointHelper
7
+ extend NodePattern::Macros
8
+
9
+ def_node_matcher :http_method_node?, <<~PATTERN
10
+ (block (send _ {:get :post :put :patch :delete} ...) ...)
11
+ PATTERN
12
+
13
+ def_node_matcher :params_node?, <<~PATTERN
14
+ (block (send _ :params) _ $_)
15
+ PATTERN
16
+ end
17
+ end
18
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Grape
5
- VERSION = '0.4.0'
5
+ VERSION = '0.4.2'
6
6
  end
7
7
  end
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.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akito Hikasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-29 00:00:00.000000000 Z
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -48,8 +48,10 @@ files:
48
48
  - lib/rubocop/cop/grape/field_name.rb
49
49
  - lib/rubocop/cop/grape/ivar.rb
50
50
  - lib/rubocop/cop/grape/params_position.rb
51
+ - lib/rubocop/cop/grape/present_with.rb
51
52
  - lib/rubocop/cop/grape/route_param_type.rb
52
53
  - lib/rubocop/cop/grape_cops.rb
54
+ - lib/rubocop/cop/mixin/endpoint_helper.rb
53
55
  - lib/rubocop/grape.rb
54
56
  - lib/rubocop/grape/grape_files.rb
55
57
  - lib/rubocop/grape/inject.rb
@@ -77,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
79
  - !ruby/object:Gem::Version
78
80
  version: '0'
79
81
  requirements: []
80
- rubygems_version: 3.2.33
82
+ rubygems_version: 3.4.19
81
83
  signing_key:
82
84
  specification_version: 4
83
85
  summary: Automatice Grape code style checking tool.