rubocop-socketry 0.6.1 → 0.8.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: 199a4f2c374f6e452459742724b7ba8ba9b0e9d9859ddf4ae12172a95e033bd9
4
- data.tar.gz: ddc2c5a20e4b7181b1cdc0d07c53f85dc7acabb9ea4681616e3000292b45496f
3
+ metadata.gz: 0dacbb6af420eedc76cec483085c0bac470416faf318a2ddaee52980c3a43f59
4
+ data.tar.gz: 4f128fdca62a037f273ee0ab534446e08df7b97bd578b6ca50f287f3f63e8d43
5
5
  SHA512:
6
- metadata.gz: f04815a61b6651a7c4325967a9d193ade2ecb85800cb3ef816afed0a0f51d5849ab8a3aeb758474b93a8927b7fd984826f983749a9ff4824a0cf5beba1d72d95
7
- data.tar.gz: 6b255a3e77c44782ea801bc3de112411896f9395ab0b78191998b8a064528a29cbca77bc50be56223f68c1b2df2e1155332f3985fd6e62bc935f5850a7b91553
6
+ metadata.gz: b87895050fdfd00301e6f3f33bd742f87f6ffcc46963f33f33df285e7f8d3e9079b24fb6be8aee2352c697df1d36d7fddf9cca90e9453da047b3fad2cd5208ab
7
+ data.tar.gz: 0b21c56b058e9a97716e37846c52238afb798d5b584f6920820706aa93e11b41a5f1e5258b5171d7842c943916d9b93b53b9674e4c4a0f1e9fd016f2d7ba9839
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2025, by Samuel Williams.
4
+ # Copyright, 2025-2026, by Samuel Williams.
5
5
 
6
6
  require "rubocop"
7
7
 
@@ -11,19 +11,21 @@ module RuboCop
11
11
  # A RuboCop cop that enforces consistent spacing before block delimiters.
12
12
  #
13
13
  # This cop enforces the following style:
14
- # - `foo {bar}` - space when method has no parentheses and is not chained
15
- # - `foo(1, 2) {bar}` - space after closing paren for standalone methods
16
- # - `array.each{|x| x*2}.reverse` - no space for method chains (even with parens)
17
- # - `->(foo){foo}` - no space for lambdas (stabby lambda syntax)
18
- # - `lambda{foo}` - no space for lambda keyword
19
- # - `proc{foo}` - no space for proc keyword
20
- # - `Proc.new{foo}` - no space for Proc.new
14
+ # - `foo {bar}` - space for top-level statements without parentheses.
15
+ # - `x = foo{bar}` - no space when part of an expression (assignment, argument, etc).
16
+ # - `foo(1, 2) {bar}` - space after closing paren for top-level statements.
17
+ # - `array.each{|x| x*2}.reverse` - no space for method chains.
18
+ # - `->(foo){foo}` - no space for lambdas (stabby lambda syntax).
19
+ # - `lambda{foo}` - no space for lambda keyword.
20
+ # - `proc{foo}` - no space for proc keyword.
21
+ # - `Proc.new{foo}` - no space for `Proc.new`.
21
22
  class BlockDelimiterSpacing < RuboCop::Cop::Base
22
23
  extend Cop::AutoCorrector
23
24
 
24
25
  MSG_ADD_SPACE = "Add a space before the opening brace."
25
26
  MSG_REMOVE_SPACE = "Remove space before the opening brace for method chains."
26
27
  MSG_REMOVE_SPACE_LAMBDA = "Remove space before the opening brace for lambdas/procs."
28
+ MSG_REMOVE_SPACE_EXPRESSION = "Remove space before the opening brace for expressions."
27
29
 
28
30
  def on_block(node)
29
31
  return unless node.braces?
@@ -44,6 +46,12 @@ module RuboCop
44
46
  # array.each{|x| x*2}.reverse - no space
45
47
  # obj.method(1, 2){|x| x}.other - also no space
46
48
  check_no_space_before_brace(node, send_node)
49
+ # Priority 3: Check if it's part of an expression (not top-level)
50
+ # Blocks within expressions should have no space
51
+ elsif part_of_expression?(node)
52
+ # x = Async{server.run} - no space (part of assignment)
53
+ # foo(bar{baz}) - no space (part of argument)
54
+ check_no_space_for_expression(node, send_node)
47
55
  elsif has_parentheses?(send_node)
48
56
  # foo(1, 2) {bar} - space after ) for standalone methods
49
57
  check_space_after_parentheses(node, send_node)
@@ -71,6 +79,32 @@ module RuboCop
71
79
  false
72
80
  end
73
81
 
82
+ # Check if the block is part of an expression (not a top-level statement)
83
+ # Top-level statements are directly inside a :begin node (file/method body)
84
+ # or inside do...end block bodies, and should have space.
85
+ # Everything else (expressions, nested arguments, etc.) should not have space.
86
+ def part_of_expression?(node)
87
+ parent = node.parent
88
+ return false unless parent
89
+
90
+ # If parent is a :begin node (sequence of statements), this is top-level
91
+ return false if parent.type == :begin
92
+
93
+ # If parent is a :block node, check if it's a do...end block
94
+ # do...end blocks contain statements (no space)
95
+ # {...} blocks contain expressions (space required)
96
+ if parent.type == :block
97
+ # do...end blocks use keywords, {...} blocks use braces
98
+ return false unless parent.braces?
99
+ end
100
+
101
+ # Check if we're in a :kwbegin node (begin...end block body)
102
+ return false if parent.type == :kwbegin
103
+
104
+ # Otherwise, it's part of an expression (assignment, argument, etc.)
105
+ true
106
+ end
107
+
74
108
  # Check that there's no space before the opening brace for lambdas
75
109
  def check_no_space_for_lambda(block_node, send_node)
76
110
  brace_begin = block_node.loc.begin
@@ -105,6 +139,40 @@ module RuboCop
105
139
  end
106
140
  end
107
141
 
142
+ # Check that there's no space before the opening brace for expressions
143
+ def check_no_space_for_expression(block_node, send_node)
144
+ brace_begin = block_node.loc.begin
145
+
146
+ # Find the position just before the brace
147
+ char_before_pos = brace_begin.begin_pos - 1
148
+
149
+ return if char_before_pos < 0
150
+
151
+ char_before = processed_source.buffer.source[char_before_pos]
152
+
153
+ # If there's no space before the brace, we're good
154
+ return unless char_before == " "
155
+
156
+ # Find the extent of whitespace before the brace
157
+ start_pos = char_before_pos
158
+ while start_pos > 0 && processed_source.buffer.source[start_pos - 1] =~ /\s/
159
+ start_pos -= 1
160
+ end
161
+
162
+ space_range = Parser::Source::Range.new(
163
+ processed_source.buffer,
164
+ start_pos,
165
+ brace_begin.begin_pos
166
+ )
167
+
168
+ add_offense(
169
+ space_range,
170
+ message: MSG_REMOVE_SPACE_EXPRESSION
171
+ ) do |corrector|
172
+ corrector.remove(space_range)
173
+ end
174
+ end
175
+
108
176
  # Check if the block is part of a method chain (e.g., foo{}.bar or foo.bar{}.baz)
109
177
  def part_of_method_chain?(block_node)
110
178
  send_node = block_node.send_node
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2025, by Samuel Williams.
4
+ # Copyright, 2025-2026, by Samuel Williams.
5
5
 
6
6
  module RuboCop
7
7
  module Socketry
8
- VERSION = "0.6.1"
8
+ VERSION = "0.8.0"
9
9
  end
10
10
  end
data/license.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MIT License
2
2
 
3
- Copyright, 2025, by Samuel Williams.
3
+ Copyright, 2025-2026, by Samuel Williams.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/readme.md CHANGED
@@ -27,6 +27,26 @@ Layout/ConsistentBlankLineIndentation:
27
27
 
28
28
  Please see the [project releases](https://socketry.github.io/rubocop-socketry/releases/index) for all releases.
29
29
 
30
+ ### v0.8.0
31
+
32
+ - Fixed `Layout/BlockDelimiterSpacing` to correctly distinguish between statement and expression contexts for blocks inside `do...end` blocks.
33
+
34
+ ### v0.7.0
35
+
36
+ - Fixed `Layout/BlockDelimiterSpacing` to correctly handle blocks inside `do...end` blocks (statements should have space before braces).
37
+
38
+ ### v0.6.1
39
+
40
+ - Refined `Style/GlobalExceptionVariables` to allow global exception variables in safe contexts:
41
+ - Inside rescue blocks (well-defined scope).
42
+ - In rescue modifiers (`expression rescue $!`).
43
+ - In method parameter defaults (`def foo(error = $!)`).
44
+ - Added specific warning for using global exception variables in ensure blocks (extremely unsafe).
45
+
46
+ ### v0.6.0
47
+
48
+ - Extended `Layout/BlockDelimiterSpacing` to handle lambda and proc constructs (`->`, `lambda`, `proc`, `Proc.new`).
49
+
30
50
  ### v0.5.0
31
51
 
32
52
  - Added `Style/GlobalExceptionVariables` cop to warn against using global exception variables (`$!`, `$@`, `$ERROR_INFO`, `$ERROR_POSITION`).
data/releases.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Releases
2
2
 
3
+ ## v0.8.0
4
+
5
+ - Fixed `Layout/BlockDelimiterSpacing` to correctly distinguish between statement and expression contexts for blocks inside `do...end` blocks.
6
+
7
+ ## v0.7.0
8
+
9
+ - Fixed `Layout/BlockDelimiterSpacing` to correctly handle blocks inside `do...end` blocks (statements should have space before braces).
10
+
11
+ ## v0.6.1
12
+
13
+ - Refined `Style/GlobalExceptionVariables` to allow global exception variables in safe contexts:
14
+ - Inside rescue blocks (well-defined scope).
15
+ - In rescue modifiers (`expression rescue $!`).
16
+ - In method parameter defaults (`def foo(error = $!)`).
17
+ - Added specific warning for using global exception variables in ensure blocks (extremely unsafe).
18
+
19
+ ## v0.6.0
20
+
21
+ - Extended `Layout/BlockDelimiterSpacing` to handle lambda and proc constructs (`->`, `lambda`, `proc`, `Proc.new`).
22
+
3
23
  ## v0.5.0
4
24
 
5
25
  - Added `Style/GlobalExceptionVariables` cop to warn against using global exception variables (`$!`, `$@`, `$ERROR_INFO`, `$ERROR_POSITION`).
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-socketry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubygems_version: 3.6.9
106
+ rubygems_version: 4.0.3
107
107
  specification_version: 4
108
108
  summary: RuboCop rules for Socketry projects
109
109
  test_files: []
metadata.gz.sig CHANGED
Binary file