mecha_style-rails 0.2.0 → 0.3.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: bc2b28cfcdd1692dbea50a2d57bfcb17e33b74ee7e919b67805ccb101e81485a
4
- data.tar.gz: ef04df650c42dbbb6630b81a7098df4b09a1afea0e2d9cc805f6aa1eab209126
3
+ metadata.gz: 7971a7613f3461d84d433f06b5e8ec9a2e4871ef93eb52ae416d4f4f58d0b013
4
+ data.tar.gz: '08a2355f5d3583bd936f0385249a67e99eea5aa62a1f177d82692c753ea88181'
5
5
  SHA512:
6
- metadata.gz: 1bb457bd841e99817c87654d16fa1625be8cfc7c4176ebb86a50f194a1198598cc14f449de39e2c9d048771955ad2712dbe42318a2b89df5d7c27077c052b66e
7
- data.tar.gz: f114860fa2efded37ffeabd77fe1ca03da064954656229b5b535d6d6df887fc0d6df53e376ae0290b4ed9aa4a5314c8abe8b8cfdf6887c13d3027ce05dc1a697
6
+ metadata.gz: 6b2f879e7af4b44db89294ba787aa454e2550308875366e898cffce450af13ff01d46d1e4b600984d08aad6dc7d878c5443a703781af1ab4ca221e60806ef23e
7
+ data.tar.gz: 34f4089cdf7c516a3a7759117178cc254a1e320aad50b274f1be9473a16fd26e172ce5f1013e5dd0d250c261d4b2f54a663d710a1aba952f08c3f387c6178f07
data/config/default.yml CHANGED
@@ -45,3 +45,8 @@ 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"
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module MechaStyleRails
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -62,6 +62,7 @@ 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