mecha_style-rails 0.3.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
|
@@ -50,3 +50,8 @@ MechaStyleRails/InlineRedirectFlash:
|
|
|
50
50
|
Description: "Pass alert:/notice: directly to redirect_to instead of assigning flash[:alert]/flash[:notice] beforehand."
|
|
51
51
|
Enabled: true
|
|
52
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,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
|
|
@@ -69,6 +69,7 @@ files:
|
|
|
69
69
|
- lib/rubocop/cop/mecha_style_rails/no_inline_json_render.rb
|
|
70
70
|
- lib/rubocop/cop/mecha_style_rails/no_presence_ternary.rb
|
|
71
71
|
- lib/rubocop/cop/mecha_style_rails/no_visibility_modifiers.rb
|
|
72
|
+
- lib/rubocop/cop/mecha_style_rails/single_line_guard_clause.rb
|
|
72
73
|
- lib/rubocop/mecha_style_rails.rb
|
|
73
74
|
- lib/rubocop/mecha_style_rails/plugin.rb
|
|
74
75
|
- lib/rubocop/mecha_style_rails/version.rb
|