mecha_style-rails 0.4.0 → 0.5.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: eee579d74bb4eac4e676219674a5b000ee52efc1169971e8a9346e771141d25e
4
- data.tar.gz: 4cddbcb02dac92362db1d90189865adb4ae369532621627dc930c5cd0602357b
3
+ metadata.gz: f924c7ce9232377de703a523f78ec3647aa6ae1332756f646d8c5f5f77f5b2e0
4
+ data.tar.gz: 385c2f779128fafa8e2eb6aacd9f824b7f5d711574a1b9d14db87c530eaed7de
5
5
  SHA512:
6
- metadata.gz: 0b10386387327d5ea6c7c182b7799530291475fcccdce92c0337c365bd547046b8754c4196206bcba9c6d536e74a5b76d50d9f16a56f8b87180d8464f3cb7b02
7
- data.tar.gz: 55910b5552131b95636bef3225b9884bdbf12526c850b2c2826c3d45cf7527757e26d213156a07daed62d141f9b3dc4b4a365ab0628c8af8923c905e02840056
6
+ metadata.gz: 556621d239ef24eb59efd5c0268b2a9668babf768cbce5f723bd3c629400ab6038f5bd064913f41676fe5d71171708f91fdda499ad974d339c7b328cf7a9fd1e
7
+ data.tar.gz: 8156cd2cc78e865d57e5725211ce534a63726cb2912ad6e546ac36ba95aa779caa6aff5826a8d510b9fb14f62b94a36c8ed31f3f6bb770a8d2f2f06b97e105fc
data/config/default.yml CHANGED
@@ -3,6 +3,9 @@
3
3
  Layout/LineLength:
4
4
  Max: 256
5
5
 
6
+ Layout/ExtraSpacing:
7
+ Enabled: false
8
+
6
9
  Style/GuardClause:
7
10
  Enabled: true
8
11
  MinBodyLength: 1
@@ -55,3 +58,8 @@ MechaStyleRails/SingleLineGuardClause:
55
58
  Description: "Enforce single-line modifier form for single-statement guard clauses."
56
59
  Enabled: true
57
60
  VersionAdded: "0.4.0"
61
+
62
+ MechaStyleRails/AlignedGuardClauses:
63
+ Description: "Align consecutive modifier-form guard clauses into two columns with no blank lines between them."
64
+ Enabled: true
65
+ VersionAdded: "0.5.0"
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Enforces two-column vertical alignment (both `if`/`unless` keywords and
7
+ # conditions) and no blank lines between consecutive modifier-form guard clauses.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # return head :unauthorized unless current_user
12
+ #
13
+ # return head :forbidden if current_user.suspended?
14
+ #
15
+ # # good
16
+ # return head :unauthorized unless current_user
17
+ # return head :forbidden if current_user.suspended?
18
+ class AlignedGuardClauses < Base
19
+ extend AutoCorrector
20
+ include RangeHelp
21
+
22
+ ALIGN_MSG = "Align consecutive guard clause keywords and conditions into two columns."
23
+ BLANK_LINE_MSG = "Remove blank lines between consecutive guard clauses."
24
+ GUARD_SEND_METHODS = %i[raise fail throw].freeze
25
+
26
+ def on_begin(node)
27
+ return if node.children.size < 2
28
+
29
+ __guard_groups(node.children).each do |group|
30
+ __check_blank_lines(group)
31
+ __check_alignment(group)
32
+ end
33
+ end
34
+ alias on_kwbegin on_begin
35
+
36
+ def __guard_groups(statements)
37
+ statements.chunk_while { |a, b| __modifier_guard?(a) && __modifier_guard?(b) }
38
+ .select { |group| group.size >= 2 && __modifier_guard?(group.first) }
39
+ end
40
+
41
+ def __modifier_guard?(node)
42
+ return false unless node.if_type? && node.modifier_form? && node.single_line?
43
+
44
+ __guard_body?(node.if_branch)
45
+ end
46
+
47
+ def __guard_body?(body)
48
+ return false unless body
49
+ return true if body.return_type? || body.next_type? || body.break_type?
50
+
51
+ body.send_type? && body.receiver.nil? && GUARD_SEND_METHODS.include?(body.method_name)
52
+ end
53
+
54
+ def __check_blank_lines(group)
55
+ group.each_cons(2) do |prev_node, curr_node|
56
+ next if curr_node.first_line - prev_node.last_line <= 1
57
+
58
+ gap = __gap_range(prev_node, curr_node)
59
+ next if processed_source.contains_comment?(gap)
60
+
61
+ add_offense(curr_node, message: BLANK_LINE_MSG) do |corrector|
62
+ corrector.replace(gap, "\n")
63
+ end
64
+ end
65
+ end
66
+
67
+ def __gap_range(prev_node, curr_node)
68
+ prev_end = prev_node.source_range.end_pos
69
+ curr_start = curr_node.source_range.begin_pos - curr_node.source_range.begin.column
70
+ range_between(prev_end, curr_start)
71
+ end
72
+
73
+ def __check_alignment(group)
74
+ kw_col, cond_col = __target_columns(group)
75
+
76
+ group.each do |node|
77
+ next if __aligned?(node, kw_col, cond_col)
78
+
79
+ add_offense(node, message: ALIGN_MSG) do |corrector|
80
+ __correct_alignment(corrector, node, kw_col, cond_col)
81
+ end
82
+ end
83
+ end
84
+
85
+ def __target_columns(group)
86
+ kw_col = group.map { |n| n.if_branch.source_range.end.column }.max + 1
87
+ max_kw_len = group.map { |n| n.keyword.length }.max
88
+ [kw_col, kw_col + max_kw_len + 1]
89
+ end
90
+
91
+ def __aligned?(node, kw_col, cond_col)
92
+ node.loc.keyword.column == kw_col &&
93
+ node.condition.source_range.begin.column == cond_col
94
+ end
95
+
96
+ def __correct_alignment(corrector, node, kw_col, cond_col)
97
+ body_end = node.if_branch.source_range.end
98
+ padding = "#{" " * (kw_col - body_end.column)}#{node.keyword.ljust(cond_col - kw_col)}"
99
+ corrector.replace(range_between(body_end.end_pos, node.condition.source_range.begin_pos), padding)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  def on_class(node)
32
32
  namespace = __controller_namespace
33
33
  return unless namespace
34
- return if node.identifier.short_name == :BaseController
34
+ return if node.identifier.short_name == :BaseController
35
35
 
36
36
  parent = node.parent_class
37
37
  return unless parent
@@ -33,7 +33,7 @@ module RuboCop
33
33
 
34
34
  def on_or(node)
35
35
  return unless __controller_file?
36
- return if node.parent&.or_type?
36
+ return if node.parent&.or_type?
37
37
  return unless __contains_param_access?(node.lhs) && __contains_param_access?(node.rhs)
38
38
 
39
39
  add_offense(node, message: MSG_FALLBACK)
@@ -27,7 +27,7 @@ module RuboCop
27
27
 
28
28
  render_json_node(node) do |pair_node, value_node|
29
29
  next unless __inline_formatting?(value_node)
30
- next if __allow_error_hash? && __error_hash?(value_node)
30
+ next if __allow_error_hash? && __error_hash?(value_node)
31
31
 
32
32
  add_offense(pair_node)
33
33
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module MechaStyleRails
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -61,6 +61,7 @@ files:
61
61
  - README.md
62
62
  - config/default.yml
63
63
  - lib/mecha_style-rails.rb
64
+ - lib/rubocop/cop/mecha_style_rails/aligned_guard_clauses.rb
64
65
  - lib/rubocop/cop/mecha_style_rails/example.rb
65
66
  - lib/rubocop/cop/mecha_style_rails/inline_redirect_flash.rb
66
67
  - lib/rubocop/cop/mecha_style_rails/namespace_base_controller.rb