rubocop-nueca 2.0.1 → 2.2.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 +4 -4
- data/README.md +1 -1
- data/config/default.yml +4 -0
- data/lib/rubocop/cop/rails/route_unnecessary_array_notation.rb +72 -0
- data/lib/rubocop/cop/rswag/scattered_setup.rb +35 -0
- data/lib/rubocop/nueca/version.rb +1 -1
- data/lib/rubocop-nueca.rb +5 -3
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e2e8263941b4c5f0934ad7fb9f8df991e90583476f10fc3290a3c7dd2f917ff
|
4
|
+
data.tar.gz: 0e503986f5bfd5003b40beee3844cfeb9ae5cdc179b2d4576d139e1d244ba18f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc56766f7ecaa64cb93001e8ee3cd4eca0250bbf4b509d41f2fb6bcbe36d3d30e1d34dba4bdcdbcd26a4f8cd143600000364c5b9ad184793b99bc24f3992373e
|
7
|
+
data.tar.gz: 1e89c75c3f19fea9dc29979dd38af0695f4ea4e77f126e7d3d0bc407853f2a3e61baef1f9862016101ee280775bf4ca0c1a24f5cac2f1a48c4418d8cb4c03f05
|
data/README.md
CHANGED
data/config/default.yml
CHANGED
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../shared/route_helper'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module Rails
|
8
|
+
class RouteUnnecessaryArrayNotation < RuboCop::Cop::Base
|
9
|
+
include RouteHelper
|
10
|
+
extend AutoCorrector
|
11
|
+
|
12
|
+
MSG = 'Unnecessary array notation for single element. ' \
|
13
|
+
'Use `%<key>s: %<value>s` instead of `%<key>s: [%<value>s]`.'
|
14
|
+
|
15
|
+
def on_send(node)
|
16
|
+
return unless route_file?
|
17
|
+
return unless route_method?(node)
|
18
|
+
|
19
|
+
node.arguments.each do |arg|
|
20
|
+
next unless arg.hash_type?
|
21
|
+
|
22
|
+
check_hash_pairs(arg)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def route_method?(node)
|
29
|
+
return false unless node.send_type?
|
30
|
+
|
31
|
+
route_methods = [
|
32
|
+
:get, :post, :put, :patch, :delete, :head, :options, :match, :root,
|
33
|
+
:resource, :resources,
|
34
|
+
:namespace, :scope, :concern, :member, :collection,
|
35
|
+
:draw
|
36
|
+
]
|
37
|
+
|
38
|
+
route_methods.include?(node.method_name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def check_hash_pairs(hash_node)
|
42
|
+
hash_node.pairs.each do |pair|
|
43
|
+
check_single_element_array(pair)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def check_single_element_array(pair)
|
48
|
+
value = pair.value
|
49
|
+
return unless value.array_type?
|
50
|
+
return unless value.children.size == 1
|
51
|
+
|
52
|
+
element = value.children.first
|
53
|
+
return unless element.sym_type? || element.str_type?
|
54
|
+
|
55
|
+
key = pair.key
|
56
|
+
key_source = key.source
|
57
|
+
element_source = element.source
|
58
|
+
|
59
|
+
message = format(
|
60
|
+
MSG,
|
61
|
+
key: key_source,
|
62
|
+
value: element_source
|
63
|
+
)
|
64
|
+
|
65
|
+
add_offense(value, message: message) do |corrector|
|
66
|
+
corrector.replace(value, element_source)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSwag
|
6
|
+
class ScatteredSetup < RuboCop::Cop::Base
|
7
|
+
MSG = 'Do not define multiple hooks of the same type in the same response group.'
|
8
|
+
|
9
|
+
def on_block(node)
|
10
|
+
return unless rswag_test?(node)
|
11
|
+
|
12
|
+
hooks = analyzable_hooks(node)
|
13
|
+
hooks.group_by { |hook| hook.send_node.method_name }.each_value do |group|
|
14
|
+
next if group.size <= 1
|
15
|
+
|
16
|
+
group[1..].each { |hook| add_offense(hook) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def rswag_test?(_node)
|
23
|
+
processed_source.file_path&.include?('spec/requests/api')
|
24
|
+
end
|
25
|
+
|
26
|
+
def analyzable_hooks(node)
|
27
|
+
hook_methods = [:before, :after, :around]
|
28
|
+
node.body&.each_child_node(:block)&.select do |blk|
|
29
|
+
hook_methods.include?(blk.send_node&.method_name)
|
30
|
+
end || []
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/rubocop-nueca.rb
CHANGED
@@ -6,7 +6,9 @@ require 'rubocop'
|
|
6
6
|
# Load the plugin
|
7
7
|
require_relative 'rubocop/nueca/plugin'
|
8
8
|
|
9
|
-
# Load all custom
|
10
|
-
|
11
|
-
|
9
|
+
# Load all custom cops
|
10
|
+
['rails', 'rswag'].each do |subdir|
|
11
|
+
Dir[File.join(__dir__, 'rubocop', 'cop', subdir, '*.rb')].each do |file|
|
12
|
+
require file
|
13
|
+
end
|
12
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-nueca
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tien
|
@@ -152,7 +152,9 @@ files:
|
|
152
152
|
- lib/rubocop/cop/rails/route_root_position.rb
|
153
153
|
- lib/rubocop/cop/rails/route_separation.rb
|
154
154
|
- lib/rubocop/cop/rails/route_sorting.rb
|
155
|
+
- lib/rubocop/cop/rails/route_unnecessary_array_notation.rb
|
155
156
|
- lib/rubocop/cop/rails/time_zone_today.rb
|
157
|
+
- lib/rubocop/cop/rswag/scattered_setup.rb
|
156
158
|
- lib/rubocop/cop/shared/collection_context.rb
|
157
159
|
- lib/rubocop/cop/shared/route_collector.rb
|
158
160
|
- lib/rubocop/cop/shared/route_helper.rb
|