mecha_style-rails 0.3.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: 7971a7613f3461d84d433f06b5e8ec9a2e4871ef93eb52ae416d4f4f58d0b013
4
- data.tar.gz: '08a2355f5d3583bd936f0385249a67e99eea5aa62a1f177d82692c753ea88181'
3
+ metadata.gz: f924c7ce9232377de703a523f78ec3647aa6ae1332756f646d8c5f5f77f5b2e0
4
+ data.tar.gz: 385c2f779128fafa8e2eb6aacd9f824b7f5d711574a1b9d14db87c530eaed7de
5
5
  SHA512:
6
- metadata.gz: 6b2f879e7af4b44db89294ba787aa454e2550308875366e898cffce450af13ff01d46d1e4b600984d08aad6dc7d878c5443a703781af1ab4ca221e60806ef23e
7
- data.tar.gz: 34f4089cdf7c516a3a7759117178cc254a1e320aad50b274f1be9473a16fd26e172ce5f1013e5dd0d250c261d4b2f54a663d710a1aba952f08c3f387c6178f07
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
@@ -50,3 +53,13 @@ MechaStyleRails/InlineRedirectFlash:
50
53
  Description: "Pass alert:/notice: directly to redirect_to instead of assigning flash[:alert]/flash[:notice] beforehand."
51
54
  Enabled: true
52
55
  VersionAdded: "0.3.0"
56
+
57
+ MechaStyleRails/SingleLineGuardClause:
58
+ Description: "Enforce single-line modifier form for single-statement guard clauses."
59
+ Enabled: true
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
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module MechaStyleRails
5
- VERSION = "0.3.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.3.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
@@ -69,6 +70,7 @@ files:
69
70
  - lib/rubocop/cop/mecha_style_rails/no_inline_json_render.rb
70
71
  - lib/rubocop/cop/mecha_style_rails/no_presence_ternary.rb
71
72
  - lib/rubocop/cop/mecha_style_rails/no_visibility_modifiers.rb
73
+ - lib/rubocop/cop/mecha_style_rails/single_line_guard_clause.rb
72
74
  - lib/rubocop/mecha_style_rails.rb
73
75
  - lib/rubocop/mecha_style_rails/plugin.rb
74
76
  - lib/rubocop/mecha_style_rails/version.rb