rubocop-grape 0.3.0 → 0.4.2
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 +4 -4
- data/config/default.yml +18 -0
- data/lib/rubocop/cop/grape/abc_size.rb +44 -0
- data/lib/rubocop/cop/grape/field_name.rb +2 -6
- data/lib/rubocop/cop/grape/params_position.rb +3 -9
- data/lib/rubocop/cop/grape/present_with.rb +49 -0
- data/lib/rubocop/cop/grape_cops.rb +4 -0
- data/lib/rubocop/cop/mixin/endpoint_helper.rb +18 -0
- data/lib/rubocop/grape/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 590df7af3dec0374453d4f8ba4282b6d2981492c4e25d0ea537dd9e7b1de8eca
|
4
|
+
data.tar.gz: d52f772ffb08ba797df64f30298362ce04872477b36544614430e7dd157540d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32c44eac0eee98cee4c97de9e3ff829c7f7d65ef532ee5066985804cc430aff642c93402712240023b430a5a02578806f9a2864e1550147dbc6e9a12e56dbf0c
|
7
|
+
data.tar.gz: 6692df01c1c324431ba0625d5f2042447afb7b882977dd36fa3f556a9bf448f79cf5e9884240a66f9af1263d04b31e70ec8a1fd212975b6e0758816e9d899b29
|
data/config/default.yml
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
AllCops:
|
2
2
|
GrapeDir: 'app/api'
|
3
3
|
|
4
|
+
Grape/AbcSize:
|
5
|
+
Description: >-
|
6
|
+
A calculated magnitude based on number of assignments,
|
7
|
+
branches, and conditions.
|
8
|
+
Reference:
|
9
|
+
- http://c2.com/cgi/wiki?AbcMetric
|
10
|
+
- https://en.wikipedia.org/wiki/ABC_Software_Metric
|
11
|
+
- https://www.rubydoc.info/gems/rubocop/0.27.0/RuboCop/Cop/Metrics/AbcSize
|
12
|
+
Enabled: true
|
13
|
+
Max: 20
|
14
|
+
CountRepeatedAttributes: true
|
15
|
+
|
4
16
|
Grape/FieldName:
|
5
17
|
EnforcedStyle: snake_case
|
6
18
|
SupportedStyles:
|
@@ -16,3 +28,9 @@ Grape/ParamsPosition:
|
|
16
28
|
Enabled: true
|
17
29
|
Safe: false
|
18
30
|
VersionAdded: '0.1'
|
31
|
+
|
32
|
+
Grape/PresentWith:
|
33
|
+
Enabled: true
|
34
|
+
Safe: false
|
35
|
+
SafeAutoCorrect: false
|
36
|
+
VersionAdded: '0.4.2'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Grape
|
6
|
+
# Checks that the ABC size of endpoints is not higher than the
|
7
|
+
# configured maximum. The ABC size is based on assignments, branches
|
8
|
+
# (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
|
9
|
+
# and https://en.wikipedia.org/wiki/ABC_Software_Metric.
|
10
|
+
class AbcSize < Base
|
11
|
+
extend ExcludeLimit
|
12
|
+
include EndpointHelper
|
13
|
+
|
14
|
+
exclude_limit 'Max'
|
15
|
+
|
16
|
+
MSG = 'Assignment Branch Condition size is too high. [%<abc_vector>s %<complexity>d/%<max>d]'
|
17
|
+
|
18
|
+
def on_block(node)
|
19
|
+
return unless http_method_node?(node)
|
20
|
+
|
21
|
+
check_complexity(node)
|
22
|
+
end
|
23
|
+
|
24
|
+
def check_complexity(node)
|
25
|
+
return unless node.body
|
26
|
+
|
27
|
+
max = cop_config['Max']
|
28
|
+
complexity, abc_vector = complexity(node.body)
|
29
|
+
return unless complexity > max
|
30
|
+
|
31
|
+
msg = format(self.class::MSG, abc_vector: abc_vector, complexity: complexity, max: max)
|
32
|
+
add_offense(node, message: msg) { self.max = complexity.ceil }
|
33
|
+
end
|
34
|
+
|
35
|
+
def complexity(node)
|
36
|
+
Metrics::Utils::AbcSizeCalculator.calculate(
|
37
|
+
node,
|
38
|
+
discount_repeated_attributes: !cop_config['CountRepeatedAttributes']
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -31,20 +31,16 @@ module RuboCop
|
|
31
31
|
#
|
32
32
|
class FieldName < Base
|
33
33
|
include ConfigurableNaming
|
34
|
-
include
|
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 =
|
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
|
-
|
24
|
-
|
25
|
-
def_node_matcher :params_block?, <<~PATTERN
|
26
|
-
(block (send _ :params) ...)
|
27
|
-
PATTERN
|
23
|
+
include EndpointHelper
|
28
24
|
|
29
|
-
|
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 &&
|
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,6 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'mixin/endpoint_helper'
|
4
|
+
|
5
|
+
require_relative 'grape/abc_size'
|
3
6
|
require_relative 'grape/field_name'
|
4
7
|
require_relative 'grape/ivar'
|
5
8
|
require_relative 'grape/params_position'
|
9
|
+
require_relative 'grape/present_with'
|
6
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
|
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
|
+
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:
|
11
|
+
date: 2024-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -44,11 +44,14 @@ files:
|
|
44
44
|
- bin/console
|
45
45
|
- config/default.yml
|
46
46
|
- lib/rubocop-grape.rb
|
47
|
+
- lib/rubocop/cop/grape/abc_size.rb
|
47
48
|
- lib/rubocop/cop/grape/field_name.rb
|
48
49
|
- lib/rubocop/cop/grape/ivar.rb
|
49
50
|
- lib/rubocop/cop/grape/params_position.rb
|
51
|
+
- lib/rubocop/cop/grape/present_with.rb
|
50
52
|
- lib/rubocop/cop/grape/route_param_type.rb
|
51
53
|
- lib/rubocop/cop/grape_cops.rb
|
54
|
+
- lib/rubocop/cop/mixin/endpoint_helper.rb
|
52
55
|
- lib/rubocop/grape.rb
|
53
56
|
- lib/rubocop/grape/grape_files.rb
|
54
57
|
- lib/rubocop/grape/inject.rb
|
@@ -76,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
79
|
- !ruby/object:Gem::Version
|
77
80
|
version: '0'
|
78
81
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
82
|
+
rubygems_version: 3.4.19
|
80
83
|
signing_key:
|
81
84
|
specification_version: 4
|
82
85
|
summary: Automatice Grape code style checking tool.
|