rubocop-discourse 3.16.0 → 3.17.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: f4d1f21065c28440ac92a435fe615ba1aa71be866decb87a34569b5035b6ab28
4
- data.tar.gz: 40738677dd38eb78850b649f223c5e70f86a487821d28ba66ab8e25fb7fe155c
3
+ metadata.gz: 6c45b71e5356cb68292653738d05f27653a5e922d62442130751e8c341506680
4
+ data.tar.gz: 89e1f0b302c37bf300ceb97cea9a2035e49811c33e8d32dc50c22972f128f563
5
5
  SHA512:
6
- metadata.gz: 161fab4a916b845fe201a36a7b5cdc6d329a25f4069bdaab7a62a9c0dcd9ed36e0ef3984698de67edf1aac6043351b2201a43550470fff3c86b3e531e132ab0e
7
- data.tar.gz: 4a474ac36e50368a4a62fc6e1dba557f2294c9f498a53b28b589eed150a7abc2917a60ba71943d5821c32148876a309f3e4b3ca6eeee1dab9b40fa44d18c45f8
6
+ metadata.gz: fa562f62898674a35e4d7d12c513d648727fa20d982812ed4e7af1a30e08b65e6249fd1f20cd250f1a9e422dd15509b7409bec1ed1a9ac4897dedd5e56e1b0e7
7
+ data.tar.gz: 2cf1e4dd9cdaf5a57dfe7d3814c2624c46b3179b93ce5b9341be33e763270bb2f5ffd3ebb66ff74c86e269a4e23c27081b02232a40822d2689c1089e79378a0a
data/config/default.yml CHANGED
@@ -94,3 +94,6 @@ Discourse/Services/GroupKeywords:
94
94
 
95
95
  Discourse/Services/EmptyLinesAroundBlocks:
96
96
  Enabled: true
97
+
98
+ Discourse/Services/MutableAttributeDefault:
99
+ Enabled: true
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ module Services
7
+ # Wrap mutable `attribute` defaults in a proc — `ActiveModel::Attributes`
8
+ # shares the literal across instances, so mutations leak between calls.
9
+ # `:array`-typed attributes are exempt; the type dups on read.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # options do
14
+ # attribute :overrides, default: {}
15
+ # attribute :ids, default: [1, 2]
16
+ # end
17
+ #
18
+ # # good
19
+ # options do
20
+ # attribute :overrides, default: -> { {} }
21
+ # attribute :ids, default: -> { [1, 2] }
22
+ # attribute :tags, :array, default: [] # :array dups per read
23
+ # end
24
+ #
25
+ class MutableAttributeDefault < Base
26
+ extend AutoCorrector
27
+
28
+ MSG =
29
+ "Mutable `default: %<source>s` is shared across all instances; wrap it in a proc: `-> { %<source>s }`."
30
+ RESTRICT_ON_SEND = %i[attribute].freeze
31
+
32
+ def_node_matcher :service_include?, <<~MATCHER
33
+ (class _ _
34
+ {
35
+ (begin <(send nil? :include (const (const nil? :Service) :Base)) ...>)
36
+ <(send nil? :include (const (const nil? :Service) :Base)) ...>
37
+ }
38
+ )
39
+ MATCHER
40
+
41
+ def_node_matcher :mutable_default, <<~PATTERN
42
+ (send nil? :attribute _ ... (hash <(pair (sym :default) ${hash array}) ...>))
43
+ PATTERN
44
+
45
+ def_node_matcher :array_typed?, <<~PATTERN
46
+ (send nil? :attribute _ (sym :array) ...)
47
+ PATTERN
48
+
49
+ def on_class(node)
50
+ @service = true if service_include?(node)
51
+ end
52
+
53
+ def on_send(node)
54
+ return unless @service
55
+ return if array_typed?(node)
56
+ default = mutable_default(node)
57
+ return unless default
58
+
59
+ source = default.source
60
+ add_offense(default, message: format(MSG, source: source)) do |corrector|
61
+ corrector.replace(default, "-> { #{source} }")
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Discourse
5
- VERSION = "3.16.0"
5
+ VERSION = "3.17.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-discourse
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.16.0
4
+ version: 3.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Discourse Team
@@ -179,6 +179,7 @@ files:
179
179
  - lib/rubocop/cop/discourse/plugins/use_require_relative.rb
180
180
  - lib/rubocop/cop/discourse/services/empty_lines_around_blocks.rb
181
181
  - lib/rubocop/cop/discourse/services/group_keywords.rb
182
+ - lib/rubocop/cop/discourse/services/mutable_attribute_default.rb
182
183
  - lib/rubocop/cop/discourse/time_eq_matcher.rb
183
184
  - lib/rubocop/cop/discourse_cops.rb
184
185
  - lib/rubocop/discourse/plugin.rb