rubocop-solidus 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 745ca1ca12310bb9de934135f03aae227d87d616005ce5e82aafe58d6554a88d
4
- data.tar.gz: 9723caf52452c8b1d4fa8bf88f644298bda3d01631af85df95a6aceb6b741462
3
+ metadata.gz: df0c66e0a5dddaec3401e5cfd7d4f2704d0d920d9883172e7d8551aee6663641
4
+ data.tar.gz: 929b18aba23c67ae33c3582d94e3758a6682668cfe8595e49107e72387672050
5
5
  SHA512:
6
- metadata.gz: 8683bf3a98d2483a25e1d18a078ca9a44ad39eb138a069364b664dbdc989f0199a7bcb86c68ce750268a310ca0c0342cc4cd55a60448edc65d3ff9b3a28e56f8
7
- data.tar.gz: 2c36b5f8362baf576f3eaecc05e63324d988f1fcf0a0d63ce27c606787437a8fd303e8ca1172269f7766de26378d6aaf2598831dcbf43eef5583551f2581177a
6
+ metadata.gz: de6813fbc0de1c6e24bc686622112ba91d71580a5d116f4eb33747ffeceef4c74fc437dc31c5b98a209f592b4bd5fb970be8495b7a04e98515fc262fd7497edf
7
+ data.tar.gz: 619bf635aa925403cdce3fcf65f9be39b45d5d301348eb2c6666f5b48e9f45b16cc9d22c7b641f5906f19991ca4ffe2a721b6643677cac458288af02c440fd18
data/.rubocop.yml CHANGED
@@ -2,6 +2,7 @@ inherit_from: .rubocop_todo.yml
2
2
 
3
3
  AllCops:
4
4
  NewCops: enable
5
+ SuggestExtensions: false
5
6
 
6
7
  Naming/FileName:
7
8
  Exclude:
@@ -14,6 +15,13 @@ Metrics/BlockLength:
14
15
  - 'spec/**/*.rb'
15
16
  - '*.gemspec'
16
17
 
18
+ Metrics/MethodLength:
19
+ Exclude:
20
+ - 'Rakefile'
21
+ - '**/*.rake'
22
+ - 'spec/**/*.rb'
23
+ - '*.gemspec'
24
+
17
25
  Metrics/ClassLength:
18
26
  Exclude:
19
27
  - 'tasks/**/*.rb'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.1.4 (2023-08-04)
4
+
5
+ ### New features
6
+
7
+ * [#51](https://github.com/solidusio/rubocop-solidus/pull/51): Create automatic documentation based on the cop's descriptions. ([@MassimilianoLattanzio][])
8
+
3
9
  ## 0.1.3 (2023-07-28)
4
10
 
5
11
  ### New features
data/Gemfile CHANGED
@@ -10,3 +10,4 @@ gem 'pry'
10
10
  gem 'rake'
11
11
  gem 'rspec'
12
12
  gem 'rubocop'
13
+ gem 'yard'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-solidus (0.1.3)
4
+ rubocop-solidus (0.1.4)
5
5
  rubocop
6
6
 
7
7
  GEM
@@ -54,6 +54,7 @@ GEM
54
54
  parser (>= 3.2.1.0)
55
55
  ruby-progressbar (1.13.0)
56
56
  unicode-display_width (2.4.2)
57
+ yard (0.9.34)
57
58
 
58
59
  PLATFORMS
59
60
  -darwin-22
@@ -69,6 +70,7 @@ DEPENDENCIES
69
70
  rspec
70
71
  rubocop
71
72
  rubocop-solidus!
73
+ yard
72
74
 
73
75
  BUNDLED WITH
74
76
  2.3.26
data/README.md CHANGED
@@ -3,12 +3,14 @@
3
3
  Automatic Solidus code style checking tool.
4
4
  A RuboCop extension focused on enforcing Solidus best practices and coding conventions.
5
5
 
6
- ## Installation
6
+ ## Getting started
7
+
8
+ ### Installation
7
9
 
8
10
  Add this line to your application's Gemfile:
9
11
 
10
12
  ```ruby
11
- gem 'rubocop-solidus' require: false
13
+ gem 'rubocop-solidus', require: false
12
14
  ```
13
15
 
14
16
  And then execute:
@@ -19,7 +21,7 @@ Or install it yourself as:
19
21
 
20
22
  $ gem install rubocop-solidus
21
23
 
22
- ## Usage
24
+ ### Usage
23
25
 
24
26
  To Start using this extension in your application add the following lines to your `.rubocop.yml` file
25
27
 
@@ -30,7 +32,17 @@ require:
30
32
 
31
33
  After this simply use the `rubocop` command to start linting.
32
34
 
33
- ## Creating new cops
35
+ ## Documentation
36
+
37
+ You can read about each cop supplied by RuboCop Solidus in [the docs](docs/cops.md).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/solidusio/rubocop-solidus.
42
+
43
+ To contribute a new cop, please see the sections below.
44
+
45
+ ### Creating new cops
34
46
 
35
47
  To create a new cop, run the following command:
36
48
 
@@ -40,7 +52,7 @@ $ bundle exec rake 'new_cop[Solidus/NameOfTheCop]'
40
52
 
41
53
  and then follow the instructions on the screen.
42
54
 
43
- ## Creating changelog entries
55
+ ### Creating changelog entries
44
56
 
45
57
  Changelog entries are just files under the `changelog/` folder that will be merged
46
58
  into `CHANGELOG.md` at release time. You can create new changelog entries like this:
@@ -51,7 +63,7 @@ $ bundle exec rake changelog:new|fix|change
51
63
 
52
64
  The type of the changelog correspond to "new feature", "bug-fix" and "changed" entries in the changelog.
53
65
 
54
- ### Changelog entry format
66
+ #### Changelog entry format
55
67
 
56
68
  Here are a few examples:
57
69
 
@@ -68,7 +80,7 @@ Here are a few examples:
68
80
  * If this is a breaking change, mark it with `**(Breaking)**`.
69
81
  * At the end of the entry, add an implicit link to your GitHub user page as `([@username][])`.
70
82
 
71
- ## Release a new version
83
+ ### Release a new version
72
84
 
73
85
  To release a new version, run the following command:
74
86
 
@@ -89,10 +101,6 @@ To deploy the new version to RubyGems, run the following command:
89
101
  $ bundle exec rake release
90
102
  ```
91
103
 
92
- ## Contributing
93
-
94
- Bug reports and pull requests are welcome on GitHub at https://github.com/solidusio/rubocop-solidus.
95
-
96
104
  ## License
97
105
 
98
106
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -6,7 +6,12 @@ require 'rspec/core/rake_task'
6
6
 
7
7
  Dir['tasks/**/*.rake'].each { |t| load t }
8
8
 
9
- task default: %i[spec rubocop]
9
+ task default: %i[
10
+ spec
11
+ rubocop
12
+ generate_cops_documentation
13
+ documentation_syntax_check
14
+ ]
10
15
 
11
16
  RuboCop::RakeTask.new
12
17
 
data/docs/cops.md ADDED
@@ -0,0 +1,19 @@
1
+ ## Available cops
2
+
3
+ In the following section you find all available cops:
4
+
5
+ <!-- START_COP_LIST -->
6
+ #### Department [Solidus](cops_solidus.md)
7
+
8
+ * [Solidus/ClassEvalDecorator](cops_solidus.md#solidusclassevaldecorator)
9
+ * [Solidus/ReimbursementHookDeprecated](cops_solidus.md#solidusreimbursementhookdeprecated)
10
+ * [Solidus/SpreeCalculatorFreeShippingDeprecated](cops_solidus.md#solidusspreecalculatorfreeshippingdeprecated)
11
+ * [Solidus/SpreeCalculatorPercentPerItemDeprecated](cops_solidus.md#solidusspreecalculatorpercentperitemdeprecated)
12
+ * [Solidus/SpreeCalculatorPriceSackDeprecated](cops_solidus.md#solidusspreecalculatorpricesackdeprecated)
13
+ * [Solidus/SpreeDefaultCreditCardDeprecated](cops_solidus.md#solidusspreedefaultcreditcarddeprecated)
14
+ * [Solidus/SpreeGatewayBogusDeprecated](cops_solidus.md#solidusspreegatewaybogusdeprecated)
15
+ * [Solidus/SpreeIconDeprecated](cops_solidus.md#solidusspreeicondeprecated)
16
+ * [Solidus/SpreeRefundCallPerform](cops_solidus.md#solidusspreerefundcallperform)
17
+ * [Solidus/SpreeTDeprecated](cops_solidus.md#solidusspreetdeprecated)
18
+
19
+ <!-- END_COP_LIST -->
@@ -0,0 +1,276 @@
1
+ # Solidus
2
+
3
+ ## Solidus/ClassEvalDecorator
4
+
5
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
6
+ --- | --- | --- | --- | --- | ---
7
+ Enabled | Yes | No | 0.1.0 | - | -
8
+
9
+ Solidus suggests a decorator module instead of `class_eval` when overriding some features.
10
+ This cop finds any `class_eval` and asks to use a decorator module instead.
11
+ More info: https://guides.solidus.io/customization/customizing-the-core.
12
+
13
+ ### Examples
14
+
15
+ ```ruby
16
+ # bad
17
+ SpreeClass.class_eval do
18
+ # your code here
19
+ end
20
+
21
+ # good
22
+ module Spree
23
+ module SpreeClassDecorator
24
+ # your code here
25
+ end
26
+
27
+ Spree::SpreeClass.prepend self
28
+ end
29
+ ```
30
+
31
+ ### References
32
+
33
+ * [https://github.com/solidusio/rubocop-solidus/issues/21](https://github.com/solidusio/rubocop-solidus/issues/21)
34
+
35
+ ## Solidus/ReimbursementHookDeprecated
36
+
37
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
38
+ --- | --- | --- | --- | --- | ---
39
+ Enabled | Yes | No | 0.1.0 | - | 2.11
40
+
41
+ This cop finds reimbursement_success_hooks and reimbursement_failed_hooks calls and
42
+ asks to remove them and subscribe to reimbursement_reimbursed event instead.
43
+
44
+ ### Examples
45
+
46
+ ```ruby
47
+ # bad
48
+ reimbursement_success_hooks.each { |h| h.call self }
49
+ reimbursement_failed_hooks.each { |h| h.call self }
50
+
51
+ # good
52
+ ```
53
+ ```ruby
54
+ # bad
55
+ reimbursement_success_hooks.any?
56
+ reimbursement_failed_hooks.any?
57
+
58
+ # good
59
+ ```
60
+
61
+ ### References
62
+
63
+ * [https://github.com/solidusio/rubocop-solidus/issues/27](https://github.com/solidusio/rubocop-solidus/issues/27)
64
+
65
+ ## Solidus/SpreeCalculatorFreeShippingDeprecated
66
+
67
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
68
+ --- | --- | --- | --- | --- | ---
69
+ Enabled | Yes | No | 0.1.0 | - | -
70
+
71
+ This cop finds Spree::Calculator::FreeShipping calls.
72
+ This cop is needed as they have been deprecated in future version.
73
+
74
+ ### Examples
75
+
76
+ ```ruby
77
+ # bad
78
+ Spree::Calculator::FreeShipping
79
+
80
+ # good
81
+ ```
82
+
83
+ ### References
84
+
85
+ * [https://github.com/solidusio/rubocop-solidus/issues/29](https://github.com/solidusio/rubocop-solidus/issues/29)
86
+
87
+ ## Solidus/SpreeCalculatorPercentPerItemDeprecated
88
+
89
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
90
+ --- | --- | --- | --- | --- | ---
91
+ Enabled | Yes | Yes | 0.1.0 | - | -
92
+
93
+ This cop finds Spree::Calculator::PercentPerItem calls.
94
+ This cop is needed as they have been deprecated in future version.
95
+
96
+ ### Examples
97
+
98
+ ```ruby
99
+ # bad
100
+ Spree::Calculator::PercentPerItem
101
+
102
+ # good
103
+ Spree::Calculator::PercentOnLineItem
104
+ ```
105
+
106
+ ### References
107
+
108
+ * [https://github.com/solidusio/rubocop-solidus/issues/29](https://github.com/solidusio/rubocop-solidus/issues/29)
109
+
110
+ ## Solidus/SpreeCalculatorPriceSackDeprecated
111
+
112
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
113
+ --- | --- | --- | --- | --- | ---
114
+ Enabled | Yes | No | 0.1.0 | - | -
115
+
116
+ This cop finds Spree::Calculator::PriceSack calls.
117
+ This cop is needed as they have been deprecated in future version.
118
+
119
+ ### Examples
120
+
121
+ ```ruby
122
+ # bad
123
+ Spree::Calculator::PriceSack
124
+
125
+ # good
126
+ ```
127
+
128
+ ### References
129
+
130
+ * [https://github.com/solidusio/rubocop-solidus/issues/29](https://github.com/solidusio/rubocop-solidus/issues/29)
131
+
132
+ ## Solidus/SpreeDefaultCreditCardDeprecated
133
+
134
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
135
+ --- | --- | --- | --- | --- | ---
136
+ Enabled | Yes | Yes | 0.1.0 | - | 2.2
137
+
138
+ This cop finds user.default_credit_card suggest using user.wallet.default_wallet_payment_source.
139
+
140
+ ### Examples
141
+
142
+ ```ruby
143
+ # bad
144
+ user.default_credit_card
145
+
146
+ # good
147
+ user.wallet.default_wallet_payment_source
148
+ ```
149
+
150
+ ### References
151
+
152
+ * [https://github.com/solidusio/rubocop-solidus/issues/33](https://github.com/solidusio/rubocop-solidus/issues/33)
153
+
154
+ ## Solidus/SpreeGatewayBogusDeprecated
155
+
156
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
157
+ --- | --- | --- | --- | --- | ---
158
+ Enabled | Yes | Yes | 0.1.0 | - | 2.1
159
+
160
+ This cop finds Spree::Gateway::Bogus calls and replaces them with the Spree::PaymentMethod::BogusCreditCard.
161
+ This cop is needed as the Spree::Gateway::Bogus has been deprecated in future version.
162
+
163
+ ### Examples
164
+
165
+ ```ruby
166
+ # bad
167
+ Spree::Gateway::Bogus.new
168
+ Spree::Gateway::Bogus.create
169
+ Spree::Gateway::Bogus.create!
170
+
171
+ # good
172
+ Spree::PaymentMethod::BogusCreditCard.new
173
+ Spree::PaymentMethod::BogusCreditCard.create
174
+ Spree::PaymentMethod::BogusCreditCard.create!
175
+ ```
176
+
177
+ ### References
178
+
179
+ * [https://github.com/solidusio/rubocop-solidus/issues/26](https://github.com/solidusio/rubocop-solidus/issues/26)
180
+
181
+ ## Solidus/SpreeIconDeprecated
182
+
183
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
184
+ --- | --- | --- | --- | --- | ---
185
+ Enabled | Yes | Yes | 0.1.0 | - | 2.3
186
+
187
+ This cop finds icon helper calls and suggest using solidus_icon.
188
+
189
+ ### Examples
190
+
191
+ ```ruby
192
+ # bad
193
+ helper.icon('example')
194
+
195
+ # good
196
+ helper.solidus_icon('example')
197
+ ```
198
+
199
+ ### References
200
+
201
+ * [https://github.com/solidusio/rubocop-solidus/issues/32](https://github.com/solidusio/rubocop-solidus/issues/32)
202
+
203
+ ## Solidus/SpreeRefundCallPerform
204
+
205
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
206
+ --- | --- | --- | --- | --- | ---
207
+ Enabled | Yes | No | 0.1.0 | - | 2.11
208
+
209
+ This cop finds Spree::Refund.create(your: attributes) calls and
210
+ replaces them with the Spree::Refund.create(your: attributes, perform_after_create: false).perform! call.
211
+
212
+ ### Examples
213
+
214
+ ```ruby
215
+ # bad
216
+ Spree::Refund.create(your: attributes)
217
+
218
+ # good
219
+ Spree::Refund.create(your: attributes, perform_after_create: false).perform!
220
+ ```
221
+
222
+ ### References
223
+
224
+ * [https://github.com/solidusio/rubocop-solidus/issues/28](https://github.com/solidusio/rubocop-solidus/issues/28)
225
+
226
+ ## Solidus/SpreeTDeprecated
227
+
228
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
229
+ --- | --- | --- | --- | --- | ---
230
+ Enabled | Yes | Yes | 0.1.0 | - | -
231
+
232
+ This cop finds Spree.t method calls and replaces them with the I18n,t method call.
233
+ This cop is needed as the Spree.t version has been deprecated in future version.
234
+
235
+ ### Examples
236
+
237
+ ```ruby
238
+ # Without any parameters
239
+
240
+ # bad
241
+ Spree.t(:bar)
242
+
243
+ # good
244
+ I18n.t(:bar, scope: :spree)
245
+ ```
246
+ ```ruby
247
+ # With the scope parameter
248
+
249
+ # bad
250
+ Spree.t(:bar, scope: [:city])
251
+
252
+ # good
253
+ I18n.t(:bar, scope: [:spree, :city])
254
+ ```
255
+ ```ruby
256
+ # With the scope and other parameters
257
+
258
+ # bad
259
+ Spree.t(:bar, scope: [:city], email: email)
260
+
261
+ # good
262
+ I18n.t(:bar, scope: [:spree, :city], email: email)
263
+ ```
264
+ ```ruby
265
+ # With the scope parameter as a string
266
+
267
+ # bad
268
+ Spree.t('bar', scope: 'admin.city')
269
+
270
+ # good
271
+ I18n.t('bar', scope: 'spree.admin.city')
272
+ ```
273
+
274
+ ### References
275
+
276
+ * [https://github.com/solidusio/rubocop-solidus/issues/22](https://github.com/solidusio/rubocop-solidus/issues/22)
@@ -24,6 +24,10 @@ module RuboCop
24
24
  def targeted_solidus_version?(version)
25
25
  Gem::Version.new(@minimum_solidus_version) <= Gem::Version.new(version)
26
26
  end
27
+
28
+ def required_minimum_solidus_version
29
+ @minimum_solidus_version
30
+ end
27
31
  end
28
32
 
29
33
  # This method overrides the one in RuboCop::Cop::Base.
@@ -3,35 +3,31 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Solidus
6
- # TODO: Write cop description and example of bad / good code. For every
7
- # `SupportedStyle` and unique configuration, there needs to be examples.
8
- # Examples must have valid Ruby syntax. Do not use upticks.
6
+ # Solidus suggests a decorator module instead of `class_eval` when overriding some features.
7
+ # This cop finds any `class_eval` and asks to use a decorator module instead.
8
+ # More info: https://guides.solidus.io/customization/customizing-the-core.
9
9
  #
10
- #
11
- # @example EnforcedStyle: SpreeClass
12
- # # Description of the `SpreeClass` style.
10
+ # @example
13
11
  #
14
12
  # # bad
15
13
  # SpreeClass.class_eval do
16
- # .
17
- # .
14
+ # # your code here
18
15
  # end
19
16
  #
20
- #
21
17
  # # good
22
- # module SpreeClassDecorator
23
- # .
24
- # .
18
+ # module Spree
19
+ # module SpreeClassDecorator
20
+ # # your code here
21
+ # end
22
+ #
23
+ # Spree::SpreeClass.prepend self
25
24
  # end
26
25
  #
27
26
  class ClassEvalDecorator < Base
28
- MSG = 'Do not use `class_eval` flag. Use a decorator module instead. Check this link for an example https://guides.solidus.io/cookbook/redefining-checkout-steps'
27
+ MSG = 'Do not use `class_eval` flag. Use a decorator module instead. More info: https://guides.solidus.io/customization/customizing-the-core.'
29
28
 
30
- # TODO: Don't call `on_send` unless the method name is in this list
31
- # If you don't need `on_send` in the cop you created, remove it.
32
29
  RESTRICT_ON_SEND = %i[class_eval].freeze
33
30
 
34
- # @!method on_class_eval?(node)
35
31
  def_node_matcher :on_class_eval?, <<~PATTERN
36
32
  (send ($...) :class_eval)
37
33
  PATTERN
@@ -5,6 +5,23 @@ module RuboCop
5
5
  module Solidus
6
6
  # This cop finds reimbursement_success_hooks and reimbursement_failed_hooks calls and
7
7
  # asks to remove them and subscribe to reimbursement_reimbursed event instead.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # reimbursement_success_hooks.each { |h| h.call self }
13
+ # reimbursement_failed_hooks.each { |h| h.call self }
14
+ #
15
+ # # good
16
+ #
17
+ # @example
18
+ #
19
+ # # bad
20
+ # reimbursement_success_hooks.any?
21
+ # reimbursement_failed_hooks.any?
22
+ #
23
+ # # good
24
+ #
8
25
  class ReimbursementHookDeprecated < Base
9
26
  include TargetSolidusVersion
10
27
  minimum_solidus_version 2.11
@@ -6,6 +6,13 @@ module RuboCop
6
6
  # This cop finds Spree::Calculator::FreeShipping calls.
7
7
  # This cop is needed as they have been deprecated in future version.
8
8
  #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Spree::Calculator::FreeShipping
13
+ #
14
+ # # good
15
+ #
9
16
  class SpreeCalculatorFreeShippingDeprecated < Base
10
17
  MSG = 'Spree::Calculator::FreeShipping is deprecated.'
11
18
 
@@ -6,10 +6,18 @@ module RuboCop
6
6
  # This cop finds Spree::Calculator::PercentPerItem calls.
7
7
  # This cop is needed as they have been deprecated in future version.
8
8
  #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Spree::Calculator::PercentPerItem
13
+ #
14
+ # # good
15
+ # Spree::Calculator::PercentOnLineItem
16
+ #
9
17
  class SpreeCalculatorPercentPerItemDeprecated < Base
10
18
  extend AutoCorrector
11
19
 
12
- MSG = 'Spree::Calculator::PercentPerItem is deprecated.'
20
+ MSG = 'Spree::Calculator::PercentPerItem is deprecated. Use Spree::Calculator::PercentOnLineItem instead.'
13
21
 
14
22
  def_node_matcher :percent_per_item?, <<~PATTERN
15
23
  (send (... (... :Calculator) :PercentPerItem) $_)
@@ -6,6 +6,13 @@ module RuboCop
6
6
  # This cop finds Spree::Calculator::PriceSack calls.
7
7
  # This cop is needed as they have been deprecated in future version.
8
8
  #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Spree::Calculator::PriceSack
13
+ #
14
+ # # good
15
+ #
9
16
  class SpreeCalculatorPriceSackDeprecated < Base
10
17
  MSG = 'Spree::Calculator::PriceSack is deprecated.'
11
18
 
@@ -3,16 +3,16 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Solidus
6
- # This cop finds user.default_credit_card suggest using user.wallet.default_wallet_payment_source
6
+ # This cop finds user.default_credit_card suggest using user.wallet.default_wallet_payment_source.
7
+ #
8
+ # @example
7
9
  #
8
- # @example EnforcedStyle:
9
10
  # # bad
10
11
  # user.default_credit_card
11
12
  #
12
13
  # # good
13
14
  # user.wallet.default_wallet_payment_source
14
15
  #
15
- #
16
16
  class SpreeDefaultCreditCardDeprecated < Base
17
17
  extend AutoCorrector
18
18
  include TargetSolidusVersion
@@ -20,7 +20,6 @@ module RuboCop
20
20
 
21
21
  MSG = 'user.default_credit_card is deprecated. Please use user.wallet.default_wallet_payment_source instead.'
22
22
 
23
- # @!method bad_method?(node)
24
23
  def_node_matcher :default_credit_card?, <<~PATTERN
25
24
  (send ... :default_credit_card)
26
25
  PATTERN
@@ -3,29 +3,20 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Solidus
6
- # This cop finds Spree::Gateway::Bogus calls and replaces them with the Spree::PaymentMethod::BogusCreditCard call
6
+ # This cop finds Spree::Gateway::Bogus calls and replaces them with the Spree::PaymentMethod::BogusCreditCard.
7
7
  # This cop is needed as the Spree::Gateway::Bogus has been deprecated in future version.
8
8
  #
9
- # @example EnforcedStyle:
9
+ # @example
10
10
  # # bad
11
11
  # Spree::Gateway::Bogus.new
12
- #
13
- # # good
14
- # Spree::PaymentMethod::BogusCreditCard.new
15
- #
16
- # # bad
17
12
  # Spree::Gateway::Bogus.create
18
- #
19
- # # good
20
- # Spree::PaymentMethod::BogusCreditCard.create
21
- #
22
- # # bad
23
13
  # Spree::Gateway::Bogus.create!
24
14
  #
25
15
  # # good
16
+ # Spree::PaymentMethod::BogusCreditCard.new
17
+ # Spree::PaymentMethod::BogusCreditCard.create
26
18
  # Spree::PaymentMethod::BogusCreditCard.create!
27
19
  #
28
- #
29
20
  class SpreeGatewayBogusDeprecated < Base
30
21
  extend AutoCorrector
31
22
  include TargetSolidusVersion
@@ -33,7 +24,6 @@ module RuboCop
33
24
 
34
25
  MSG = 'Spree::Gateway::Bogus is deprecated. Please use Spree::PaymentMethod::BogusCreditCard instead.'
35
26
 
36
- # @!method bad_method?(node)
37
27
  def_node_matcher :bad_class?, <<~PATTERN
38
28
  (send (... (... :Gateway) :Bogus) $_)
39
29
  PATTERN
@@ -3,16 +3,16 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Solidus
6
- # This cop finds icon helper calls and suggest using solidus_icon
6
+ # This cop finds icon helper calls and suggest using solidus_icon.
7
+ #
8
+ # @example
7
9
  #
8
- # @example EnforcedStyle:
9
10
  # # bad
10
11
  # helper.icon('example')
11
12
  #
12
13
  # # good
13
14
  # helper.solidus_icon('example')
14
15
  #
15
- #
16
16
  class SpreeIconDeprecated < Base
17
17
  extend AutoCorrector
18
18
  include TargetSolidusVersion
@@ -4,7 +4,15 @@ module RuboCop
4
4
  module Cop
5
5
  module Solidus
6
6
  # This cop finds Spree::Refund.create(your: attributes) calls and
7
- # replaces them with the Spree::Refund.create(your: attributes, perform_after_create: false).perform! call
7
+ # replaces them with the Spree::Refund.create(your: attributes, perform_after_create: false).perform! call.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Spree::Refund.create(your: attributes)
13
+ #
14
+ # # good
15
+ # Spree::Refund.create(your: attributes, perform_after_create: false).perform!
8
16
  #
9
17
  class SpreeRefundCallPerform < Base
10
18
  include TargetSolidusVersion
@@ -16,7 +24,6 @@ module RuboCop
16
24
 
17
25
  RESTRICT_ON_SEND = %i[create].freeze
18
26
 
19
- # @!method bad_method?(node)
20
27
  def_node_matcher :create_refund?, <<~PATTERN
21
28
  (send (const (const nil? :Spree) :Refund) :create ...)
22
29
  PATTERN
@@ -3,43 +3,51 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Solidus
6
- # This cop finds Spree.t method calls and replaces them with the I18n,t method call
6
+ # This cop finds Spree.t method calls and replaces them with the I18n,t method call.
7
7
  # This cop is needed as the Spree.t version has been deprecated in future version.
8
8
  #
9
+ # @example
10
+ # # Without any parameters
9
11
  #
10
- # @example EnforcedStyle: bar (default)
11
12
  # # bad
12
13
  # Spree.t(:bar)
13
14
  #
14
15
  # # good
15
16
  # I18n.t(:bar, scope: :spree)
16
17
  #
18
+ # @example
19
+ # # With the scope parameter
20
+ #
17
21
  # # bad
18
22
  # Spree.t(:bar, scope: [:city])
19
23
  #
20
24
  # # good
21
25
  # I18n.t(:bar, scope: [:spree, :city])
22
26
  #
27
+ # @example
28
+ # # With the scope and other parameters
29
+ #
23
30
  # # bad
24
31
  # Spree.t(:bar, scope: [:city], email: email)
25
32
  #
26
33
  # # good
27
34
  # I18n.t(:bar, scope: [:spree, :city], email: email)
28
35
  #
36
+ # @example
37
+ # # With the scope parameter as a string
38
+ #
29
39
  # # bad
30
40
  # Spree.t('bar', scope: 'admin.city')
31
41
  #
32
42
  # # good
33
43
  # I18n.t('bar', scope: 'spree.admin.city')
34
44
  #
35
- #
36
45
  class SpreeTDeprecated < Base
37
46
  extend AutoCorrector
38
47
  MSG = 'Use I18n.t instead of Spree.t which has been deprecated in future versions.'
39
48
 
40
49
  RESTRICT_ON_SEND = %i[t].freeze
41
50
 
42
- # @!method spree_t?(node)
43
51
  def_node_matcher :spree_t?, <<~PATTERN
44
52
  (send ($...) :t ...)
45
53
  PATTERN
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Solidus
5
- VERSION = '0.1.3'
5
+ VERSION = '0.1.4'
6
6
  end
7
7
  end
@@ -0,0 +1,5 @@
1
+ ### New features
2
+
3
+ * [#51](https://github.com/solidusio/rubocop-solidus/pull/51): Create automatic documentation based on the cop's descriptions. ([@MassimilianoLattanzio][])
4
+
5
+ [@MassimilianoLattanzio]: https://github.com/MassimilianoLattanzio
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.metadata['homepage_uri'] = spec.homepage
21
21
  spec.metadata['source_code_uri'] = 'https://www.github.com/solidusio/rubocop-solidus'
22
- spec.metadata['changelog_uri'] = 'https://www.github.com/solidusio/rubocop-solidus'
22
+ spec.metadata['changelog_uri'] = 'https://www.github.com/solidusio/rubocop-solidus/blob/master/CHANGELOG.md'
23
+ spec.metadata['documentation_uri'] = 'https://www.github.com/solidusio/rubocop-solidus/blob/master/docs/cops.md'
23
24
 
24
25
  # Specify which files should be added to the gem when it is released.
25
26
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -0,0 +1,322 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard'
4
+ require 'rubocop'
5
+ require 'rubocop-solidus'
6
+
7
+ YARD::Rake::YardocTask.new(:yard_for_generate_documentation) do |task|
8
+ task.files = ['lib/rubocop/cop/**/*.rb']
9
+ task.options = ['--no-output']
10
+ end
11
+
12
+ desc('Generate docs of all cops departments')
13
+ task generate_cops_documentation: :yard_for_generate_documentation do
14
+ def cops_of_department(cops, department)
15
+ cops.with_department(department).sort!
16
+ end
17
+
18
+ def cops_body(config, cop, description, examples_objects, pars)
19
+ content = h2(cop.cop_name)
20
+ content << required_ruby_version(cop)
21
+ content << properties(config, cop)
22
+ content << "#{description}\n"
23
+ content << examples(examples_objects) if examples_objects.count.positive?
24
+ content << configurations(pars)
25
+ content << references(config, cop)
26
+ content
27
+ end
28
+
29
+ def examples(examples_object)
30
+ examples_object.each_with_object(h3('Examples').dup) do |example, content|
31
+ content << h4(example.name) unless example.name == ''
32
+ content << code_example(example)
33
+ end
34
+ end
35
+
36
+ def required_ruby_version(cop)
37
+ return '' unless cop.respond_to?(:required_minimum_ruby_version)
38
+
39
+ <<~NOTE
40
+ !!! Note
41
+
42
+ Required Ruby version: #{cop.required_minimum_ruby_version}
43
+
44
+ NOTE
45
+ end
46
+
47
+ def properties(config, cop)
48
+ header = [
49
+ 'Enabled by default', 'Safe', 'Supports autocorrection', 'VersionAdded',
50
+ 'VersionChanged', 'Required Solidus Version'
51
+ ]
52
+ config = config.for_cop(cop)
53
+ safe_auto_correct = config.fetch('SafeAutoCorrect', true)
54
+ autocorrect = if cop.support_autocorrect?
55
+ "Yes #{'(Unsafe)' unless safe_auto_correct}"
56
+ else
57
+ 'No'
58
+ end
59
+ minimum_solidus_version = if cop.respond_to?(:required_minimum_solidus_version)
60
+ cop.required_minimum_solidus_version
61
+ else
62
+ '-'
63
+ end
64
+ content = [[
65
+ config.fetch('Enabled') ? 'Enabled' : 'Disabled',
66
+ config.fetch('Safe', true) ? 'Yes' : 'No',
67
+ autocorrect,
68
+ config.fetch('VersionAdded', '-'),
69
+ config.fetch('VersionChanged', '-'),
70
+ minimum_solidus_version
71
+ ]]
72
+ "#{to_table(header, content)}\n"
73
+ end
74
+
75
+ def h2(title)
76
+ content = +"\n"
77
+ content << "## #{title}\n"
78
+ content << "\n"
79
+ content
80
+ end
81
+
82
+ def h3(title)
83
+ content = +"\n"
84
+ content << "### #{title}\n"
85
+ content << "\n"
86
+ content
87
+ end
88
+
89
+ def h4(title)
90
+ content = +"#### #{title}\n"
91
+ content << "\n"
92
+ content
93
+ end
94
+
95
+ def code_example(ruby_code)
96
+ content = +"```ruby\n"
97
+ content << ruby_code.text
98
+ .gsub('@good', '# good').gsub('@bad', '# bad').strip
99
+ content << "\n```\n"
100
+ content
101
+ end
102
+
103
+ def configurations(pars)
104
+ return '' if pars.empty?
105
+
106
+ header = ['Name', 'Default value', 'Configurable values']
107
+ configs = pars.each_key.reject { |key| key.start_with?('Supported') }
108
+ content = configs.map do |name|
109
+ configurable = configurable_values(pars, name)
110
+ default = format_table_value(pars[name])
111
+ [name, default, configurable]
112
+ end
113
+
114
+ h3('Configurable attributes') + to_table(header, content)
115
+ end
116
+
117
+ # rubocop:disable Metrics/CyclomaticComplexity
118
+ def configurable_values(pars, name)
119
+ case name
120
+ when /^Enforced/
121
+ supported_style_name = RuboCop::Cop::Util.to_supported_styles(name)
122
+ format_table_value(pars[supported_style_name])
123
+ when 'IndentationWidth'
124
+ 'Integer'
125
+ when 'Database'
126
+ format_table_value(pars['SupportedDatabases'])
127
+ else
128
+ case pars[name]
129
+ when String
130
+ 'String'
131
+ when Integer
132
+ 'Integer'
133
+ when Float
134
+ 'Float'
135
+ when true, false
136
+ 'Boolean'
137
+ when Array
138
+ 'Array'
139
+ else
140
+ ''
141
+ end
142
+ end
143
+ end
144
+ # rubocop:enable Metrics/CyclomaticComplexity
145
+
146
+ def to_table(header, content)
147
+ table = [
148
+ header.join(' | '),
149
+ Array.new(header.size, '---').join(' | ')
150
+ ]
151
+ table.concat(content.map { |c| c.join(' | ') })
152
+ "#{table.join("\n")}\n"
153
+ end
154
+
155
+ def format_table_value(val)
156
+ value =
157
+ case val
158
+ when Array
159
+ if val.empty?
160
+ '`[]`'
161
+ else
162
+ val.map { |config| format_table_value(config) }.join(', ')
163
+ end
164
+ else
165
+ "`#{val.nil? ? '<none>' : val}`"
166
+ end
167
+ value.gsub("#{Dir.pwd}/", '').rstrip
168
+ end
169
+
170
+ def references(config, cop)
171
+ cop_config = config.for_cop(cop)
172
+ urls = RuboCop::Cop::MessageAnnotator.new(
173
+ config, cop.name, cop_config, {}
174
+ ).urls
175
+ return '' if urls.empty?
176
+
177
+ content = h3('References')
178
+ content << urls.map { |url| "* [#{url}](#{url})" }.join("\n")
179
+ content << "\n"
180
+ content
181
+ end
182
+
183
+ def print_cops_of_department(cops, department, config)
184
+ selected_cops = cops_of_department(cops, department).select do |cop|
185
+ cop.to_s.start_with?('RuboCop::Cop::Solidus')
186
+ end
187
+ return if selected_cops.empty?
188
+
189
+ content = +"# #{department}\n"
190
+ selected_cops.each do |cop|
191
+ content << print_cop_with_doc(cop, config)
192
+ end
193
+ file_name = "#{Dir.pwd}/docs/cops_#{department.downcase}.md"
194
+ File.open(file_name, 'w') do |file|
195
+ puts "* generated #{file_name}"
196
+ file.write("#{content.strip}\n")
197
+ end
198
+ end
199
+
200
+ def print_cop_with_doc(cop, config)
201
+ t = config.for_cop(cop)
202
+ non_display_keys = %w[Description Enabled StyleGuide Reference Safe SafeAutoCorrect VersionAdded
203
+ VersionChanged]
204
+ pars = t.reject { |k| non_display_keys.include?(k) }
205
+ description = 'No documentation'
206
+ examples_object = []
207
+ YARD::Registry.all(:class).detect do |code_object|
208
+ next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge
209
+
210
+ description = code_object.docstring unless code_object.docstring.blank?
211
+ examples_object = code_object.tags('example')
212
+ end
213
+ cops_body(config, cop, description, examples_object, pars)
214
+ end
215
+
216
+ # rubocop:disable Metrics/AbcSize
217
+ def table_of_content_for_department(cops, department)
218
+ selected_cops = cops_of_department(cops, department.to_sym).select do |cop|
219
+ cop.to_s.start_with?('RuboCop::Cop::Solidus')
220
+ end
221
+ return if selected_cops.empty?
222
+
223
+ type_title = department[0].upcase + department[1..]
224
+ filename = "cops_#{department.downcase}.md"
225
+ content = +"#### Department [#{type_title}](#{filename})\n\n"
226
+ selected_cops.each do |cop|
227
+ anchor = cop.cop_name.sub('/', '').downcase
228
+ content << "* [#{cop.cop_name}](#{filename}##{anchor})\n"
229
+ end
230
+
231
+ content
232
+ end
233
+ # rubocop:enable Metrics/AbcSize
234
+
235
+ def print_table_of_contents(cops)
236
+ path = "#{Dir.pwd}/docs/cops.md"
237
+ original = File.read(path)
238
+ content = +"<!-- START_COP_LIST -->\n"
239
+
240
+ content << table_contents(cops)
241
+
242
+ content << "\n<!-- END_COP_LIST -->"
243
+
244
+ unless original.empty?
245
+ content = original.sub(
246
+ /<!-- START_COP_LIST -->.+<!-- END_COP_LIST -->/m, content
247
+ )
248
+ end
249
+ File.write(path, content)
250
+ end
251
+
252
+ def table_contents(cops)
253
+ cops
254
+ .departments
255
+ .map(&:to_s)
256
+ .sort
257
+ .map { |department| table_of_content_for_department(cops, department) }
258
+ .compact
259
+ .join("\n")
260
+ end
261
+
262
+ def assert_docs_synchronized
263
+ # Do not print diff and yield whether exit code was zero
264
+ sh('git diff --quiet docs') do |outcome, _|
265
+ return if outcome
266
+
267
+ # Output diff before raising error
268
+ sh('GIT_PAGER=cat git diff docs')
269
+
270
+ warn('The docs directory is out of sync. ' \
271
+ 'Run `rake generate_cops_documentation` and commit the results.')
272
+ exit!
273
+ end
274
+ end
275
+
276
+ def main
277
+ cops = RuboCop::Cop::Cop.registry
278
+ config = RuboCop::ConfigLoader.load_file('config/default.yml')
279
+
280
+ YARD::Registry.load!
281
+ cops.departments.sort!.each do |department|
282
+ print_cops_of_department(cops, department, config)
283
+ end
284
+
285
+ print_table_of_contents(cops)
286
+
287
+ assert_docs_synchronized if ENV['CI'] == 'true'
288
+ ensure
289
+ RuboCop::ConfigLoader.default_configuration = nil
290
+ end
291
+
292
+ main
293
+ end
294
+
295
+ desc('Syntax check for the documentation comments')
296
+ task documentation_syntax_check: :yard_for_generate_documentation do
297
+ require 'parser/ruby27'
298
+
299
+ ok = true
300
+ YARD::Registry.load!
301
+ cops = RuboCop::Cop::Cop.registry
302
+ cops.each do |cop|
303
+ examples = YARD::Registry.all(:class).find do |code_object|
304
+ next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge
305
+
306
+ break code_object.tags('example')
307
+ end
308
+
309
+ examples.to_a.each do |example|
310
+ buffer = Parser::Source::Buffer.new('<code>', 1)
311
+ buffer.source = example.text
312
+ parser = Parser::Ruby27.new(RuboCop::AST::Builder.new)
313
+ parser.diagnostics.all_errors_are_fatal = true
314
+ parser.parse(buffer)
315
+ rescue Parser::SyntaxError => e
316
+ path = example.object.file
317
+ puts "#{path}: Syntax Error in an example. #{e}"
318
+ ok = false
319
+ end
320
+ end
321
+ abort unless ok
322
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-solidus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - piyushswain
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-28 00:00:00.000000000 Z
11
+ date: 2023-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -43,6 +43,8 @@ files:
43
43
  - README.md
44
44
  - Rakefile
45
45
  - config/default.yml
46
+ - docs/cops.md
47
+ - docs/cops_solidus.md
46
48
  - lib/rubocop-solidus.rb
47
49
  - lib/rubocop/cop/mixin/target_solidus_version.rb
48
50
  - lib/rubocop/cop/solidus/class_eval_decorator.rb
@@ -61,10 +63,12 @@ files:
61
63
  - lib/rubocop/solidus/version.rb
62
64
  - relnotes/v0.1.2.md
63
65
  - relnotes/v0.1.3.md
66
+ - relnotes/v0.1.4.md
64
67
  - rubocop-solidus.gemspec
65
68
  - sig/rubocop/solidus.rbs
66
69
  - tasks/changelog.rake
67
70
  - tasks/changelog.rb
71
+ - tasks/cops_documentation.rake
68
72
  - tasks/cut_release.rake
69
73
  homepage: https://www.github.com/solidusio/rubocop-solidus
70
74
  licenses:
@@ -72,7 +76,8 @@ licenses:
72
76
  metadata:
73
77
  homepage_uri: https://www.github.com/solidusio/rubocop-solidus
74
78
  source_code_uri: https://www.github.com/solidusio/rubocop-solidus
75
- changelog_uri: https://www.github.com/solidusio/rubocop-solidus
79
+ changelog_uri: https://www.github.com/solidusio/rubocop-solidus/blob/master/CHANGELOG.md
80
+ documentation_uri: https://www.github.com/solidusio/rubocop-solidus/blob/master/docs/cops.md
76
81
  post_install_message:
77
82
  rdoc_options: []
78
83
  require_paths:
@@ -88,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
93
  - !ruby/object:Gem::Version
89
94
  version: '0'
90
95
  requirements: []
91
- rubygems_version: 3.4.16
96
+ rubygems_version: 3.4.18
92
97
  signing_key:
93
98
  specification_version: 4
94
99
  summary: Automatic Solidus code style checking tool.