rubocop-discourse 3.14.0 → 3.16.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: e722863ce072ee0a4d928e09b36a1e12e3b653618fdc0fe70a2f417d466e0ffa
4
- data.tar.gz: bd4a868a26bd7df10e3196c9d9c419f1b4268051d6302b335994eb539ae6e61e
3
+ metadata.gz: f4d1f21065c28440ac92a435fe615ba1aa71be866decb87a34569b5035b6ab28
4
+ data.tar.gz: 40738677dd38eb78850b649f223c5e70f86a487821d28ba66ab8e25fb7fe155c
5
5
  SHA512:
6
- metadata.gz: 0e266174ecceb19102b630bdb01419b0b22580fcadaf5cc5f69a3ba7c7c91a6274ff391a7cf43769c71cb035699e1867d96b0c72ba28cd0706335a5821ce3fe4
7
- data.tar.gz: 3e5d42d6aa34bf2eb23daaf91eef254623cd83fe812525c2bda429cd96e9f3ec0b0d0b19129d3faa5e64e749decf0164f012d99755787559d2e715ec3e16441c
6
+ metadata.gz: 161fab4a916b845fe201a36a7b5cdc6d329a25f4069bdaab7a62a9c0dcd9ed36e0ef3984698de67edf1aac6043351b2201a43550470fff3c86b3e531e132ab0e
7
+ data.tar.gz: 4a474ac36e50368a4a62fc6e1dba557f2294c9f498a53b28b589eed150a7abc2917a60ba71943d5821c32148876a309f3e4b3ca6eeee1dab9b40fa44d18c45f8
data/config/default.yml CHANGED
@@ -56,6 +56,11 @@ Discourse/NoMixingMultisiteAndStandardSpecs:
56
56
  Include:
57
57
  - "**/spec/**/*"
58
58
 
59
+ Discourse/NoSystemSpecMetadata:
60
+ Enabled: true
61
+ Include:
62
+ - "**/spec/**/*"
63
+
59
64
  Discourse/Plugins/CallRequiresPlugin:
60
65
  Enabled: true
61
66
  Include:
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # System spec metadata is inferred from the file path, so explicit
7
+ # `type: :system` and `system: true` metadata on `RSpec.describe` is redundant.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # RSpec.describe "login", system: true do
12
+ # end
13
+ #
14
+ # # good
15
+ # RSpec.describe "login" do
16
+ # end
17
+ class NoSystemSpecMetadata < Base
18
+ extend AutoCorrector
19
+
20
+ MSG = "Remove redundant `type: :system` and `system: true` metadata from `RSpec.describe`."
21
+ RESTRICT_ON_SEND = %i[describe].freeze
22
+
23
+ def_node_matcher :describe?, <<~PATTERN
24
+ (send
25
+ {nil? (const nil? :RSpec)}
26
+ :describe
27
+ ...
28
+ )
29
+ PATTERN
30
+
31
+ def_node_matcher :system_type_pair?, <<~PATTERN
32
+ (pair (sym :type) (sym :system))
33
+ PATTERN
34
+
35
+ def_node_matcher :system_true_pair?, <<~PATTERN
36
+ (pair (sym :system) true)
37
+ PATTERN
38
+
39
+ def on_send(node)
40
+ return unless describe?(node)
41
+ return unless node.last_argument&.hash_type?
42
+
43
+ hash = node.last_argument
44
+ offending_pairs =
45
+ hash.pairs.select { |pair| system_type_pair?(pair) || system_true_pair?(pair) }
46
+
47
+ return if offending_pairs.empty?
48
+
49
+ add_offense(offending_pairs.first) do |corrector|
50
+ corrector.replace(node, corrected_send_source(node, hash, offending_pairs))
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def corrected_send_source(node, hash, offending_pairs)
57
+ remaining_pairs = hash.pairs - offending_pairs
58
+ before_hash = source_for(node.source_range.begin_pos, hash.source_range.begin_pos)
59
+ after_hash = source_for(hash.source_range.end_pos, node.source_range.end_pos)
60
+
61
+ return "#{before_hash.sub(/,\s*\z/m, "")}#{after_hash}" if remaining_pairs.empty?
62
+
63
+ "#{before_hash}#{corrected_hash_source(hash, remaining_pairs)}#{after_hash}"
64
+ end
65
+
66
+ def corrected_hash_source(hash, remaining_pairs)
67
+ body =
68
+ remaining_pairs
69
+ .each_with_index
70
+ .map do |pair, index|
71
+ separator = index.zero? ? "" : pair_separator(hash, pair)
72
+ "#{separator}#{pair.source}"
73
+ end
74
+ .join
75
+
76
+ return body unless hash.braces?
77
+
78
+ return "{ #{body} }" unless hash.multiline?
79
+
80
+ indentation = " " * remaining_pairs.first.loc.expression.column
81
+ closing_indentation = " " * hash.loc.end.column
82
+
83
+ "{\n#{indentation}#{body}\n#{closing_indentation}}"
84
+ end
85
+
86
+ def pair_separator(hash, pair)
87
+ return ", " unless hash.multiline?
88
+
89
+ ",\n#{" " * pair.loc.expression.column}"
90
+ end
91
+
92
+ def source_for(begin_pos, end_pos)
93
+ processed_source.buffer.source[begin_pos...end_pos]
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Discourse
5
- VERSION = "3.14.0"
5
+ VERSION = "3.16.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.14.0
4
+ version: 3.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Discourse Team
@@ -167,6 +167,7 @@ files:
167
167
  - lib/rubocop/cop/discourse/no_mocking_jobs.rb
168
168
  - lib/rubocop/cop/discourse/no_nokogiri_html_fragment.rb
169
169
  - lib/rubocop/cop/discourse/no_reset_column_information_in_migrations.rb
170
+ - lib/rubocop/cop/discourse/no_system_spec_metadata.rb
170
171
  - lib/rubocop/cop/discourse/no_time_new_without_args.rb
171
172
  - lib/rubocop/cop/discourse/no_uri_escape_encode.rb
172
173
  - lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb