rubocop-rspec-guide 0.2.0 → 0.2.1

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: dbcc5261e61c2e9d16ccfa146e62046779e7e48f782a8fdb5003203cd55a99a4
4
- data.tar.gz: 3b8c2dae7e97d1d892ba9077944c93de7370803ed3c92edfeb2e43e33a4f9c3f
3
+ metadata.gz: 0c6e4154c7d3d386bd161fa1405ac9e36eb2152fbf38f568a3fa15698a53e7ab
4
+ data.tar.gz: 86b32810a2ad0c401d6757297ca6880d454966d9355aa7de45f69026cf4426d3
5
5
  SHA512:
6
- metadata.gz: f599508c8ad0870461f04847d6b6c727647557c645231b46df9e8bc63737e2118f50d9590fe7cfbec29668941170fe138187bb939bb0d750938f696c5de4bb2c
7
- data.tar.gz: 8c9820bc4f21d0a7e82b1711f3abfc85a7131bc7b27b7b46b72c719f02c9426b4ad53f42512e6050677aa41ba903a1db1d45444fba35cd80fe609f0252b75b56
6
+ metadata.gz: 59676351d5629e7947ce2123819409a90657976b064b0386f1488f5593f7760b65f81d9a1911468e5ed4ebf85ce529f15dbaf3ce4249d51fb0f4ab4cbd122770
7
+ data.tar.gz: 43841a5b040e380c13f8210c55ac1297b7b7b6570a4438d0d4f30951b7a29313ae4af55295fe8d4069e8434a3f33b0b542d15de76b6d80524a40fd976dd9f820
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2025-10-29
4
+
5
+ ### Changed
6
+ - **RSpecGuide/HappyPathFirst**: Allow corner case contexts when examples (it/specify) appear before first context
7
+ - Examples before contexts are considered happy path
8
+ - No offense if at least one example exists before the first context
9
+
3
10
  ## [0.2.0] - 2025-10-28
4
11
 
5
12
  ### Changed
@@ -6,6 +6,10 @@ module RuboCop
6
6
  # Checks that corner cases are not the first context in a describe block.
7
7
  # Happy path should come first for better readability.
8
8
  #
9
+ # The cop allows corner case contexts to appear first if there are
10
+ # example blocks (it/specify) before the first context, as those examples
11
+ # represent the happy path.
12
+ #
9
13
  # @example
10
14
  # # bad
11
15
  # describe '#process' do
@@ -37,6 +41,17 @@ module RuboCop
37
41
  # end
38
42
  # end
39
43
  #
44
+ # # good - examples before first context represent happy path
45
+ # describe '#add_child' do
46
+ # it 'adds child to children collection' do
47
+ # # ...
48
+ # end
49
+ #
50
+ # context 'but child is already in collection' do
51
+ # # ...
52
+ # end
53
+ # end
54
+ #
40
55
  class HappyPathFirst < Base
41
56
  MSG = "Place happy path contexts before corner cases. " \
42
57
  "First context appears to be a corner case: %<description>s"
@@ -67,6 +82,10 @@ module RuboCop
67
82
  contexts = collect_direct_child_contexts(node)
68
83
  return if contexts.size < 2
69
84
 
85
+ # If there are any examples (it/specify) before the first context,
86
+ # this is a happy path, so no offense
87
+ return if has_examples_before_first_context?(node, contexts.first)
88
+
70
89
  # Check first context
71
90
  context_with_description?(contexts.first) do |description|
72
91
  if corner_case_context?(description)
@@ -80,6 +99,30 @@ module RuboCop
80
99
 
81
100
  private
82
101
 
102
+ def has_examples_before_first_context?(node, first_context)
103
+ body = node.body
104
+ return false unless body
105
+
106
+ children = body.begin_type? ? body.children : [body]
107
+
108
+ children.each do |child|
109
+ # Stop when we reach the first context
110
+ break if child == first_context
111
+
112
+ # Check if this is an example (it/specify)
113
+ return true if example?(child)
114
+ end
115
+
116
+ false
117
+ end
118
+
119
+ def example?(node)
120
+ return false unless node.block_type?
121
+
122
+ send_node = node.send_node
123
+ send_node.method?(:it) || send_node.method?(:specify)
124
+ end
125
+
83
126
  def collect_direct_child_contexts(node)
84
127
  body = node.body
85
128
  return [] unless body
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module RSpec
5
5
  module Guide
6
- VERSION = "0.2.0"
6
+ VERSION = "0.2.1"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rspec-guide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Matskevich