rubocop-discourse 3.20.0 → 3.21.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: 336f075d60708f90b87bad9fe56b77e03b9d56032047301b7a1d35dd9b56f9c9
4
- data.tar.gz: 6b06003f421b68f9cced22eb72b8fc38428481766358de3ab81f5b4787dee859
3
+ metadata.gz: 72d00f798299cbb08dc4f79106e04369d3e21f68c79cbeef18a62b2fde1eeacd
4
+ data.tar.gz: b0995d4ac2faf20a491ad30dfcc0cf7b41cf13d37541472e494b9935b6df2dac
5
5
  SHA512:
6
- metadata.gz: 51cd73808b98db03713f0ffdb331dea65e27b817f3d0037ca3fb25a1e71fe3d0660f13aacdbdcadbdb318f7c0adc037372347a08a03ee24344872308fc5297e0
7
- data.tar.gz: f08e7f9c4348d2d80be7c0eee5d4d4b404be9e29391f0d76d3c1268d28fc139458bb2798f9c7a4d159a0036ddf1824525380f883bbc1146b7e0b4697544e54ce
6
+ metadata.gz: f1eb1425d35aba8f9eb63ce3a86b6d9c7935bcdce6c2b673b4da47bb0bdada3e6c36fa310395162ec5e1e1b6ef5bfd303967b354713a473c3008391d1ba54fb2
7
+ data.tar.gz: b2c19481fbb4585566da328abe877711c2bf64dee33026f01b6d02ec0368fae31de979a4315098db7fd27bddc447fd1771731328cd843b65f0dde147af503578
data/config/default.yml CHANGED
@@ -26,6 +26,12 @@ Discourse/NoResetColumnInformationInMigrations:
26
26
 
27
27
  # Specs
28
28
 
29
+ Discourse/NoIterationOverRSpecDefinitions:
30
+ Description: "Do not generate RSpec examples or groups with iterators."
31
+ Enabled: true
32
+ Include:
33
+ - "**/spec/**/*"
34
+
29
35
  Discourse/NoDirectMultisiteManipulation:
30
36
  Enabled: true
31
37
  Include:
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # Define RSpec examples and groups outside iterators so scenarios can be
7
+ # selected by file and line.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # %w[latest top].each do |filter|
12
+ # it "returns the #{filter} list" do
13
+ # get "/#{filter}"
14
+ # expect(response.status).to eq(200)
15
+ # end
16
+ # end
17
+ #
18
+ # # good
19
+ # it "returns the latest list" do
20
+ # get "/latest"
21
+ # expect(response.status).to eq(200)
22
+ # end
23
+ #
24
+ # it "returns the top list" do
25
+ # get "/top"
26
+ # expect(response.status).to eq(200)
27
+ # end
28
+ #
29
+ # Use shared examples for substantial repeated checks, with named contexts
30
+ # and keyword arguments.
31
+ #
32
+ # @example
33
+ # # good
34
+ # shared_examples "a public topic list" do |path:|
35
+ # before { get path }
36
+ #
37
+ # it "returns JSON successfully" do
38
+ # expect(response.status).to eq(200)
39
+ # expect(response.media_type).to eq("application/json")
40
+ # end
41
+ #
42
+ # it "includes topic data" do
43
+ # expect(response.parsed_body["topic_list"]).to include("topics" => be_an(Array))
44
+ # end
45
+ #
46
+ # it "includes user data" do
47
+ # expect(response.parsed_body["users"]).to be_an(Array)
48
+ # end
49
+ # end
50
+ #
51
+ # context "with latest topics" do
52
+ # include_examples "a public topic list", path: "/latest.json"
53
+ # end
54
+ #
55
+ # context "with top topics" do
56
+ # include_examples "a public topic list", path: "/top.json"
57
+ # end
58
+ class NoIterationOverRSpecDefinitions < RuboCop::Cop::RSpec::Base
59
+ MSG = "Do not generate RSpec examples or groups with iterators."
60
+
61
+ ITERATION_METHODS = %i[
62
+ combination
63
+ permutation
64
+ product
65
+ repeated_combination
66
+ repeated_permutation
67
+ step
68
+ with_index
69
+ with_object
70
+ ].freeze
71
+ private_constant :ITERATION_METHODS
72
+
73
+ def on_send(node)
74
+ return unless rspec_definition?(node)
75
+ return if inside_runtime_scope?(node)
76
+
77
+ node.each_ancestor do |ancestor|
78
+ if ancestor.def_type? || ancestor.defs_type? || ancestor.class_type? ||
79
+ ancestor.module_type?
80
+ break
81
+ end
82
+ if ancestor.any_block_type? && rspec_definition?(ancestor.send_node)
83
+ next if ancestor.send_node.equal?(node)
84
+
85
+ break
86
+ end
87
+
88
+ next unless iteration?(ancestor)
89
+
90
+ add_offense(node.loc.selector)
91
+ break
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def_node_matcher :rspec_definition?, <<~PATTERN
98
+ (send #rspec? {#ExampleGroups.all #Examples.all} ...)
99
+ PATTERN
100
+
101
+ def_node_matcher :runtime_scope?, <<~PATTERN
102
+ (any_block (send #rspec? {#Examples.all #Hooks.all #Helpers.all #Subjects.all} ...) ...)
103
+ PATTERN
104
+
105
+ def inside_runtime_scope?(node)
106
+ node
107
+ .each_ancestor(:block, :numblock, :itblock)
108
+ .any? { |ancestor| !ancestor.send_node.equal?(node) && runtime_scope?(ancestor) }
109
+ end
110
+
111
+ def iteration?(node)
112
+ return true if node.loop_keyword?
113
+ return false unless node.any_block_type?
114
+
115
+ method_call = node.send_node
116
+ method_call.enumerator_method? || method_call.enumerable_method? ||
117
+ ITERATION_METHODS.include?(node.method_name)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Discourse
5
- VERSION = "3.20.0"
5
+ VERSION = "3.21.0"
6
6
  end
7
7
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "rubocop"
4
4
  require "lint_roller"
5
+ require "rubocop/rspec/language"
6
+ require "rubocop/cop/rspec/base"
5
7
  require "active_support"
6
8
  require "active_support/core_ext/string/inflections"
7
9
  require "active_support/core_ext/object/blank"
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.20.0
4
+ version: 3.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Discourse Team
@@ -162,6 +162,7 @@ files:
162
162
  - lib/rubocop/cop/discourse/no_add_reference_active_record_migrations.rb
163
163
  - lib/rubocop/cop/discourse/no_chdir.rb
164
164
  - lib/rubocop/cop/discourse/no_direct_multisite_manipulation.rb
165
+ - lib/rubocop/cop/discourse/no_iteration_over_rspec_definitions.rb
165
166
  - lib/rubocop/cop/discourse/no_json_parse_response.rb
166
167
  - lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
167
168
  - lib/rubocop/cop/discourse/no_mocking_jobs.rb