rubocop-betterment 1.15.0 → 1.16.0

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: 9b851af1c0666fffea3f63f36504e7acf1c6d6324e58caba76bcea50ac752f50
4
- data.tar.gz: 6dcbd29396beb9c7310f6ff9082ae9494219741fb17b6306ea976747445e337e
3
+ metadata.gz: bfc81280ce15b5ff44ba5f45345bf8ec461c34d4058135b2d86b2bb9a5f48600
4
+ data.tar.gz: fc53b65409351b9604cefff382add3a95c09ae5d98d93d3979ac935fee31c4b1
5
5
  SHA512:
6
- metadata.gz: 2067d92a8d52c91b76f3f42a470f852567f858ccc34b6ba0d50b0aa2c275ef8ba7b32287bf8592efbea39e18d1a7356ef375e51ffbddd6153d1809173ff5385c
7
- data.tar.gz: 8d18894011ce90246f1cdaf5a8c42043df0c6dcd7a92bf9af6407c300c9c01cdd1d2c4ebe941ed26b4eb09500b0abb45f4d59d1286ee1eb622f3a75b2003dff0
6
+ metadata.gz: cd1bb04b824fbc3386b0ecbe1755820aad68748aa91ad5339a92ceaac2adf08cb4d5cf4cefb7ed94fd972430b359fbb71b924bec0cad14e5d9ec87b7f1d1a96f
7
+ data.tar.gz: 96453a9b868b7c492712fdd733f1cf39a11e9c5a42136d663edec81405581eca70f019afca25b3c8a5801a06396379ee204bc8649286608a78322690dae28c3c
@@ -1,4 +1,5 @@
1
1
  require 'rubocop'
2
+ require 'rubocop/cop/betterment/utils/parser'
2
3
  require 'rubocop/cop/betterment/authorization_in_controller'
3
4
  require 'rubocop/cop/betterment/dynamic_params'
4
5
  require 'rubocop/cop/betterment/unscoped_find'
@@ -79,7 +79,7 @@ module RuboCop
79
79
  return if !node.send_type? && !node.variable?
80
80
  return if node.send_type? && (node.method_name == :[])
81
81
 
82
- name = get_root_token(node)
82
+ name = Utils::Parser.get_root_token(node)
83
83
  add_offense(node, message: MSG_UNSAFE_CREATE) if @wrapper_names.include?(name)
84
84
  end
85
85
 
@@ -90,7 +90,7 @@ module RuboCop
90
90
  methods = get_all_methods(node)
91
91
 
92
92
  methods.map do |method|
93
- method_returns = get_return_values(method)
93
+ method_returns = Utils::Parser.get_return_values(method)
94
94
 
95
95
  unless get_param_wrappers(method_returns).empty?
96
96
  @wrapper_methods[method.method_name] = method
@@ -114,7 +114,7 @@ module RuboCop
114
114
 
115
115
  # if rhs is a call to a parameter wrapper, eg
116
116
  # @var = parameter_wrapper
117
- root_token = get_root_token(value)
117
+ root_token = Utils::Parser.get_root_token(value)
118
118
  if root_token && @wrapper_names.include?(root_token)
119
119
  @wrapper_methods[variable_name] = assignment
120
120
  @wrapper_names << variable_name
@@ -129,40 +129,6 @@ module RuboCop
129
129
  end
130
130
  end
131
131
 
132
- def explicit_returns(node)
133
- node.descendants.select(&:return_type?).map { |x|
134
- x&.children&.first
135
- }.compact
136
- end
137
-
138
- def get_return_values(node) # rubocop:disable Metrics/AbcSize
139
- return [] unless node
140
- return explicit_returns(node) + get_return_values(node.body) if node.def_type?
141
- return [node] if node.literal? || node.variable?
142
-
143
- case node.type
144
- when :begin
145
- get_return_values(node.children.last)
146
- when :block
147
- get_return_values(node.body)
148
- when :if
149
- if_rets = get_return_values(node.if_branch)
150
- else_rets = get_return_values(node.else_branch)
151
- if_rets + else_rets
152
- when :case
153
- cases = []
154
- node.each_when do |block|
155
- cases += get_return_values(block.body)
156
- end
157
-
158
- cases + get_return_values(node.else_branch)
159
- when :send
160
- [node]
161
- else
162
- []
163
- end
164
- end
165
-
166
132
  def get_all_assignments(node)
167
133
  return [] unless node.children && node.type == :class
168
134
 
@@ -184,36 +150,6 @@ module RuboCop
184
150
  end
185
151
  end
186
152
 
187
- # fetches the name of the leftmost ("root") token
188
- # @vars.merge(this: that).merge(etc: etc) => @vars
189
- # params.permit(:this) => params
190
- # params[:field] => params
191
- def get_root_token(node) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize
192
- return nil unless node
193
-
194
- return get_root_token(node.receiver) if node.receiver
195
-
196
- if node.send_type?
197
- name = node.method_name
198
- elsif node.variable?
199
- name, = *node
200
- elsif node.literal?
201
- _, name = *node
202
- elsif node.const_type?
203
- name = node.const_name
204
- elsif node.sym_type?
205
- name = node.value
206
- elsif node.variable?
207
- name = node.children[0]
208
- elsif node.parenthesized_call?
209
- name = nil
210
- else
211
- raise "Unknown node type: #{node.type.inspect}"
212
- end
213
-
214
- name
215
- end
216
-
217
153
  # this finds all calls to any method on a params like object
218
154
  # then walks up to find calls to permit
219
155
  # if the arguments to permit are "suspicious", then we add
@@ -266,7 +202,7 @@ module RuboCop
266
202
  _receiver_node, method_name, *arg_nodes = *argument
267
203
  return unless argument.send_type? && method_name == :[]
268
204
 
269
- argument_name = get_root_token(argument)
205
+ argument_name = Utils::Parser.get_root_token(argument)
270
206
 
271
207
  if param_symbol?(argument_name) || @wrapper_names.include?(argument_name)
272
208
  arg_nodes.find do |arg|
@@ -15,7 +15,7 @@ module RuboCop
15
15
 
16
16
  def on_send(node)
17
17
  _, _, *arg_nodes = *node
18
- return unless permit_or_hash?(node) && get_root_token(node) == :params
18
+ return unless permit_or_hash?(node) && Utils::Parser.get_root_token(node) == :params
19
19
 
20
20
  dynamic_param = find_dynamic_param(arg_nodes)
21
21
  add_offense(dynamic_param, message: MSG_DYNAMIC_PARAMS) if dynamic_param
@@ -30,32 +30,6 @@ module RuboCop
30
30
  arg.type == :array && find_dynamic_param(arg.values) || !arg.literal? && !arg.const_type?
31
31
  end
32
32
  end
33
-
34
- def get_root_token(node) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize
35
- return nil unless node
36
-
37
- return get_root_token(node.receiver) if node.receiver
38
-
39
- if node.send_type?
40
- name = node.method_name
41
- elsif node.variable?
42
- name, = *node
43
- elsif node.literal?
44
- _, name = *node
45
- elsif node.const_type?
46
- name = node.const_name
47
- elsif node.sym_type?
48
- name = node.value
49
- elsif node.variable?
50
- name = node.children[0]
51
- elsif node.parenthesized_call?
52
- name = nil
53
- else
54
- raise "Unknown node type: #{node.type.inspect}"
55
- end
56
-
57
- name
58
- end
59
33
  end
60
34
  end
61
35
  end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  def initialize(config = nil, options = nil)
32
32
  super(config, options)
33
33
  config = @config.for_cop(self)
34
- @unauthenticated_models = config.fetch("unauthenticated_models", [])
34
+ @unauthenticated_models = config.fetch("unauthenticated_models", []).map(&:to_sym)
35
35
  end
36
36
 
37
37
  def on_send(node)
@@ -41,7 +41,7 @@ module RuboCop
41
41
  find?(node) ||
42
42
  custom_scope_find?(node) ||
43
43
  static_method_name(node.method_name)
44
- ) && !@unauthenticated_models.include?(get_root_token(node))
44
+ ) && !@unauthenticated_models.include?(Utils::Parser.get_root_token(node))
45
45
 
46
46
  add_offense(node, message: MSG) if find_param_arg(arg_nodes)
47
47
  end
@@ -55,10 +55,10 @@ module RuboCop
55
55
  if arg.type == :hash
56
56
  arg.children.each do |pair|
57
57
  _key, value = *pair.children
58
- return arg if get_root_token(value) == :params
58
+ return arg if Utils::Parser.get_root_token(value) == :params
59
59
  end
60
60
  end
61
- get_root_token(arg) == :params
61
+ Utils::Parser.get_root_token(arg) == :params
62
62
  end
63
63
  end
64
64
 
@@ -69,32 +69,6 @@ module RuboCop
69
69
 
70
70
  match[2] ? 'find_by!' : 'find_by'
71
71
  end
72
-
73
- def get_root_token(node) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize
74
- return nil unless node
75
-
76
- return get_root_token(node.receiver) if node.receiver
77
-
78
- if node.send_type?
79
- name = node.method_name
80
- elsif node.variable?
81
- name, = *node
82
- elsif node.literal?
83
- _, name = *node
84
- elsif node.const_type?
85
- name = node.const_name
86
- elsif node.sym_type?
87
- name = node.value
88
- elsif node.variable?
89
- name = node.children[0]
90
- elsif node.parenthesized_call?
91
- name = nil
92
- else
93
- raise "Unknown node type: #{node.type.inspect}"
94
- end
95
-
96
- name
97
- end
98
72
  end
99
73
  end
100
74
  end
@@ -0,0 +1,69 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Utils
4
+ module Parser
5
+ def self.get_root_token(node) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/CyclomaticComplexity
6
+ return nil unless node
7
+
8
+ return get_root_token(node.receiver) if node.receiver
9
+
10
+ if node.send_type?
11
+ name = node.method_name
12
+ elsif node.variable?
13
+ name, = *node
14
+ elsif node.literal?
15
+ _, name = *node
16
+ elsif node.const_type?
17
+ name = node.const_name.to_sym
18
+ elsif node.sym_type?
19
+ name = node.value
20
+ elsif node.variable?
21
+ name = node.children[0]
22
+ elsif node.self_type?
23
+ name = :self
24
+ elsif node.block_pass_type?
25
+ name, = *node.children
26
+ else
27
+ name = nil
28
+ end
29
+
30
+ name
31
+ end
32
+
33
+ def self.get_return_values(node) # rubocop:disable Metrics/AbcSize
34
+ return [] unless node
35
+ return explicit_returns(node) + get_return_values(node.body) if node.def_type?
36
+ return [node] if node.literal? || node.variable?
37
+
38
+ case node.type
39
+ when :begin
40
+ get_return_values(node.children.last)
41
+ when :block
42
+ get_return_values(node.body)
43
+ when :if
44
+ if_rets = get_return_values(node.if_branch)
45
+ else_rets = get_return_values(node.else_branch)
46
+ if_rets + else_rets
47
+ when :case
48
+ cases = []
49
+ node.each_when do |block|
50
+ cases += get_return_values(block.body)
51
+ end
52
+
53
+ cases + get_return_values(node.else_branch)
54
+ when :send
55
+ [node]
56
+ else
57
+ []
58
+ end
59
+ end
60
+
61
+ def self.explicit_returns(node)
62
+ node.descendants.select(&:return_type?).map { |x|
63
+ x&.children&.first
64
+ }.compact
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-betterment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.15.0
4
+ version: 1.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Development
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2020-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -100,6 +100,7 @@ files:
100
100
  - lib/rubocop/cop/betterment/spec_helper_required_outside_spec_dir.rb
101
101
  - lib/rubocop/cop/betterment/timeout.rb
102
102
  - lib/rubocop/cop/betterment/unscoped_find.rb
103
+ - lib/rubocop/cop/betterment/utils/parser.rb
103
104
  homepage:
104
105
  licenses:
105
106
  - MIT
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
120
  - !ruby/object:Gem::Version
120
121
  version: '0'
121
122
  requirements: []
122
- rubygems_version: 3.1.3
123
+ rubygems_version: 3.1.4
123
124
  signing_key:
124
125
  specification_version: 4
125
126
  summary: Betterment rubocop configuration