rubocop-grape 0.2.2 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92c8005ffcb25101074781a0e8ef2dde2eec522fe78d8c8a56c2b4bc19f0ecec
4
- data.tar.gz: 91e7e2fa44a245223f72235c9655a9d124232f09ecb27eb32bae8a7d01725294
3
+ metadata.gz: d1ddcfa8ca8f07c8b82d051f1b962d106b7e9390f505bce166901deb3d9421c4
4
+ data.tar.gz: c7de3f5ff9600b83a730d347278e52d0f3f4553389c184b2c04be425193079b3
5
5
  SHA512:
6
- metadata.gz: 6becb4f74dcfaa3c922a5c4aab58f4b540286f412c2d4c697aea3bfe69ce16d32ce795b0944b7e1401fd8ed98aa0f779a95d54ce7a1e51b053084cf4bafcd8d9
7
- data.tar.gz: 507a763177ce9a01c1b50fbe067a37c40673c673a469daea3d3c6180dedeac16671e87bbbb3c2b64c625be599dcc020ebacd964804dad6d26f6206915038a864
6
+ metadata.gz: 204e3c444825e10ab0c90bdfbb3dac71db90d5aeb56a718a6124ef1b19a3883e93e5815131aefbc8bcc8ba5c20d46c2bdf987f145914167299a33f7cbf95fd44
7
+ data.tar.gz: 02ebbff3326f417c71983683ed98aa7a414e9a2d3456b671b4a5fe7f19ae4cb84ccb42723f47ee1ed5085f359d6fb70557b1703045f053430e4726c32417f80a
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:
@@ -0,0 +1,47 @@
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
+
13
+ exclude_limit 'Max'
14
+
15
+ MSG = 'Assignment Branch Condition size is too high. [%<abc_vector>s %<complexity>d/%<max>d]'
16
+
17
+ def_node_matcher :http_method_node?, <<~PATTERN
18
+ (block (send _ {:get :post :put :patch :delete} ...) ...)
19
+ PATTERN
20
+
21
+ def on_block(node)
22
+ return unless http_method_node?(node)
23
+
24
+ check_complexity(node)
25
+ end
26
+
27
+ def check_complexity(node)
28
+ return unless node.body
29
+
30
+ max = cop_config['Max']
31
+ complexity, abc_vector = complexity(node.body)
32
+ return unless complexity > max
33
+
34
+ msg = format(self.class::MSG, abc_vector: abc_vector, complexity: complexity, max: max)
35
+ add_offense(node, message: msg) { self.max = complexity.ceil }
36
+ end
37
+
38
+ def complexity(node)
39
+ Metrics::Utils::AbcSizeCalculator.calculate(
40
+ node,
41
+ discount_repeated_attributes: !cop_config['CountRepeatedAttributes']
42
+ )
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -46,18 +46,27 @@ module RuboCop
46
46
  def on_block(node)
47
47
  return unless (body = params_block?(node))
48
48
 
49
- if body.type == :begin
50
- body.children.each(&method(:check_field_name))
51
- else
52
- check_field_name(body)
53
- end
49
+ find_field_node(body)
54
50
  end
55
51
 
56
52
  private
57
53
 
54
+ def find_field_node(node)
55
+ case node.type
56
+ when :begin
57
+ node.each_child_node(&method(:find_field_node))
58
+ when :block
59
+ find_field_node(node.send_node)
60
+ find_field_node(node.body)
61
+ else
62
+ check_field_name(node)
63
+ end
64
+ end
65
+
58
66
  def check_field_name(node)
59
- field = node.children[2]
60
- check_name(node, field_name(node), field.source_range)
67
+ return unless (field_name = field_name(node))
68
+
69
+ check_name(node, field_name, node.first_argument.source_range)
61
70
  end
62
71
 
63
72
  def message(style)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'grape/abc_size'
3
4
  require_relative 'grape/field_name'
4
5
  require_relative 'grape/ivar'
5
6
  require_relative 'grape/params_position'
@@ -26,11 +26,11 @@ module RuboCop
26
26
  @config.for_all_cops['GrapeDir']
27
27
  end
28
28
 
29
- def roundup_relevant_cops(filename)
29
+ def roundup_relevant_cops(processed_source)
30
30
  super.select do |cop|
31
31
  next true unless cop.class.name.match?(/RuboCop::Cop::Grape::.+$/)
32
32
 
33
- grape_files.include?(filename)
33
+ grape_files.include?(processed_source.path)
34
34
  end
35
35
  end
36
36
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Grape
5
- VERSION = '0.2.2'
5
+ VERSION = '0.4.0'
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.2.2
4
+ version: 0.4.0
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-14 00:00:00.000000000 Z
11
+ date: 2023-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -44,6 +44,7 @@ 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