mecha_style-rails 0.2.0 → 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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: eee579d74bb4eac4e676219674a5b000ee52efc1169971e8a9346e771141d25e
|
|
4
|
+
data.tar.gz: 4cddbcb02dac92362db1d90189865adb4ae369532621627dc930c5cd0602357b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0b10386387327d5ea6c7c182b7799530291475fcccdce92c0337c365bd547046b8754c4196206bcba9c6d536e74a5b76d50d9f16a56f8b87180d8464f3cb7b02
|
|
7
|
+
data.tar.gz: 55910b5552131b95636bef3225b9884bdbf12526c850b2c2826c3d45cf7527757e26d213156a07daed62d141f9b3dc4b4a365ab0628c8af8923c905e02840056
|
data/config/default.yml
CHANGED
|
@@ -45,3 +45,13 @@ MechaStyleRails/NoPresenceTernary:
|
|
|
45
45
|
Description: "Disallow noisy params[:x].present? ? ... : ... ternaries in controllers."
|
|
46
46
|
Enabled: true
|
|
47
47
|
VersionAdded: "0.2.0"
|
|
48
|
+
|
|
49
|
+
MechaStyleRails/InlineRedirectFlash:
|
|
50
|
+
Description: "Pass alert:/notice: directly to redirect_to instead of assigning flash[:alert]/flash[:notice] beforehand."
|
|
51
|
+
Enabled: true
|
|
52
|
+
VersionAdded: "0.3.0"
|
|
53
|
+
|
|
54
|
+
MechaStyleRails/SingleLineGuardClause:
|
|
55
|
+
Description: "Enforce single-line modifier form for single-statement guard clauses."
|
|
56
|
+
Enabled: true
|
|
57
|
+
VersionAdded: "0.4.0"
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module MechaStyleRails
|
|
6
|
+
# Enforces passing `alert:` or `notice:` directly to `redirect_to` instead of
|
|
7
|
+
# assigning `flash[:alert]` / `flash[:notice]` immediately before `redirect_to`.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# # bad
|
|
11
|
+
# flash[:alert] = "Invalid state."
|
|
12
|
+
# return redirect_to root_path
|
|
13
|
+
#
|
|
14
|
+
# # good
|
|
15
|
+
# return redirect_to root_path, alert: "Invalid state."
|
|
16
|
+
class InlineRedirectFlash < Base
|
|
17
|
+
MSG = "Pass `%<key>s:` directly to `redirect_to` instead of assigning `flash[:%<key>s]` on the preceding line."
|
|
18
|
+
|
|
19
|
+
def_node_matcher :flash_assignment?, <<~PATTERN
|
|
20
|
+
(send (send nil? :flash) :[]= (sym ${:alert :notice}) _)
|
|
21
|
+
PATTERN
|
|
22
|
+
|
|
23
|
+
def_node_matcher :redirect_call, <<~PATTERN
|
|
24
|
+
{
|
|
25
|
+
$(send nil? :redirect_to ...)
|
|
26
|
+
(return $(send nil? :redirect_to ...))
|
|
27
|
+
}
|
|
28
|
+
PATTERN
|
|
29
|
+
|
|
30
|
+
def on_begin(node)
|
|
31
|
+
return unless __controller_file? && node.children.size >= 2
|
|
32
|
+
|
|
33
|
+
node.children.each_cons(2) do |first_stmt, second_stmt|
|
|
34
|
+
flash_key = flash_assignment?(first_stmt)
|
|
35
|
+
redirect_node = flash_key && redirect_call(second_stmt)
|
|
36
|
+
next unless redirect_node && !__has_flash_option?(redirect_node)
|
|
37
|
+
|
|
38
|
+
add_offense(first_stmt, message: format(MSG, key: flash_key))
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
alias on_kwbegin on_begin
|
|
42
|
+
|
|
43
|
+
def __has_flash_option?(redirect_node)
|
|
44
|
+
last_arg = redirect_node.last_argument
|
|
45
|
+
return false unless last_arg&.hash_type?
|
|
46
|
+
|
|
47
|
+
last_arg.keys.any? { |k| k.sym_type? && %i[alert notice flash].include?(k.value) }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def __controller_file?
|
|
51
|
+
processed_source.file_path.tr("\\", "/").include?("app/controllers/")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module MechaStyleRails
|
|
6
|
+
# Enforces single-line modifier form for single-statement guard clauses
|
|
7
|
+
# (`return`, `next`, `break`, `raise`, `fail`, `throw`) when the resulting
|
|
8
|
+
# line fits within `Layout/LineLength`.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# # bad
|
|
12
|
+
# unless valid_state?
|
|
13
|
+
# return redirect_to root_path, alert: "Invalid state."
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# # good
|
|
17
|
+
# return redirect_to root_path, alert: "Invalid state." unless valid_state?
|
|
18
|
+
class SingleLineGuardClause < Base
|
|
19
|
+
extend AutoCorrector
|
|
20
|
+
|
|
21
|
+
MSG = "Write single-statement guard clauses on a single line."
|
|
22
|
+
GUARD_SEND_METHODS = %i[raise fail throw].freeze
|
|
23
|
+
|
|
24
|
+
def on_if(node)
|
|
25
|
+
return unless __single_line_guard_candidate?(node)
|
|
26
|
+
|
|
27
|
+
replacement = __single_line_source(node)
|
|
28
|
+
|
|
29
|
+
add_offense(node) do |corrector|
|
|
30
|
+
next if __unsafe_autocorrect?(node, replacement)
|
|
31
|
+
|
|
32
|
+
corrector.replace(node, replacement)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def __single_line_guard_candidate?(node)
|
|
37
|
+
return false unless __block_if?(node)
|
|
38
|
+
|
|
39
|
+
body = node.if_branch
|
|
40
|
+
return false unless body && __guard_statement?(body)
|
|
41
|
+
return false unless node.condition.single_line? && body.single_line?
|
|
42
|
+
|
|
43
|
+
!__assigns_local_variable?(node.condition)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def __block_if?(node)
|
|
47
|
+
!(node.ternary? || node.modifier_form? || node.elsif? || node.else?)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def __single_line_source(node)
|
|
51
|
+
"#{node.if_branch.source} #{node.keyword} #{node.condition.source}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def __guard_statement?(node)
|
|
55
|
+
return true if node.return_type? || node.next_type? || node.break_type?
|
|
56
|
+
|
|
57
|
+
node.send_type? && node.receiver.nil? && GUARD_SEND_METHODS.include?(node.method_name)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def __assigns_local_variable?(condition)
|
|
61
|
+
condition.lvasgn_type? || condition.each_descendant(:lvasgn).any?
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def __exceeds_line_length?(node, replacement)
|
|
65
|
+
max = __max_line_length
|
|
66
|
+
return false unless max
|
|
67
|
+
|
|
68
|
+
node.loc.keyword.column + replacement.length > max
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def __max_line_length
|
|
72
|
+
line_length_config = config.for_cop("Layout/LineLength")
|
|
73
|
+
return nil unless line_length_config["Enabled"] != false
|
|
74
|
+
|
|
75
|
+
line_length_config["Max"]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def __unsafe_autocorrect?(node, replacement)
|
|
79
|
+
return true if processed_source.contains_comment?(node.source_range)
|
|
80
|
+
return true if __exceeds_line_length?(node, replacement)
|
|
81
|
+
|
|
82
|
+
[node.if_branch, node.condition].any? do |part|
|
|
83
|
+
__contains_keyword_logic?(part) || __contains_do_end_block?(part)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def __contains_keyword_logic?(node)
|
|
88
|
+
nodes = [node, *node.descendants]
|
|
89
|
+
nodes.any? { |n| (n.and_type? || n.or_type?) && n.keyword? }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def __contains_do_end_block?(node)
|
|
93
|
+
nodes = [node, *node.descendants]
|
|
94
|
+
nodes.any? { |n| n.block_type? && n.keywords? }
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mecha_style-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jeff Lunt
|
|
@@ -62,12 +62,14 @@ files:
|
|
|
62
62
|
- config/default.yml
|
|
63
63
|
- lib/mecha_style-rails.rb
|
|
64
64
|
- lib/rubocop/cop/mecha_style_rails/example.rb
|
|
65
|
+
- lib/rubocop/cop/mecha_style_rails/inline_redirect_flash.rb
|
|
65
66
|
- lib/rubocop/cop/mecha_style_rails/namespace_base_controller.rb
|
|
66
67
|
- lib/rubocop/cop/mecha_style_rails/no_fallback_param_lookup.rb
|
|
67
68
|
- lib/rubocop/cop/mecha_style_rails/no_inline_hash_mapping.rb
|
|
68
69
|
- lib/rubocop/cop/mecha_style_rails/no_inline_json_render.rb
|
|
69
70
|
- lib/rubocop/cop/mecha_style_rails/no_presence_ternary.rb
|
|
70
71
|
- lib/rubocop/cop/mecha_style_rails/no_visibility_modifiers.rb
|
|
72
|
+
- lib/rubocop/cop/mecha_style_rails/single_line_guard_clause.rb
|
|
71
73
|
- lib/rubocop/mecha_style_rails.rb
|
|
72
74
|
- lib/rubocop/mecha_style_rails/plugin.rb
|
|
73
75
|
- lib/rubocop/mecha_style_rails/version.rb
|