Skip to content

Commit eba0ec2

Browse files
committed
Fixed bug while parsing standalone def structures
1 parent 26cca2d commit eba0ec2

File tree

6 files changed

+97
-12
lines changed

6 files changed

+97
-12
lines changed

.rubocop_todo.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2026-03-28 07:58:29 UTC using RuboCop version 1.84.0.
3+
# on 2026-04-12 12:42:12 UTC using RuboCop version 1.84.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -32,7 +32,7 @@ Metrics/BlockNesting:
3232
# Offense count: 4
3333
# Configuration parameters: CountComments, CountAsOne.
3434
Metrics/ClassLength:
35-
Max: 393
35+
Max: 407
3636

3737
# Offense count: 30
3838
# Configuration parameters: AllowedMethods, AllowedPatterns.
@@ -42,12 +42,12 @@ Metrics/CyclomaticComplexity:
4242
# Offense count: 65
4343
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
4444
Metrics/MethodLength:
45-
Max: 110
45+
Max: 105
4646

4747
# Offense count: 6
4848
# Configuration parameters: CountComments, CountAsOne.
4949
Metrics/ModuleLength:
50-
Max: 403
50+
Max: 398
5151

5252
# Offense count: 9
5353
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
@@ -77,7 +77,7 @@ Naming/PredicateMethod:
7777
Exclude:
7878
- 'lib/docscribe/inline_rewriter/collector.rb'
7979

80-
# Offense count: 60
80+
# Offense count: 61
8181
# Configuration parameters: IgnoredMetadata.
8282
RSpec/DescribeClass:
8383
Enabled: false
@@ -87,17 +87,16 @@ RSpec/DescribeClass:
8787
RSpec/ExampleLength:
8888
Max: 33
8989

90-
# Offense count: 125
90+
# Offense count: 128
9191
RSpec/MultipleExpectations:
9292
Max: 7
9393

94-
# Offense count: 14
94+
# Offense count: 13
9595
# Configuration parameters: AllowedConstants.
9696
Style/Documentation:
9797
Exclude:
9898
- 'spec/**/*'
9999
- 'test/**/*'
100-
- 'abc.rb'
101100
- 'lib/docscribe/cli.rb'
102101
- 'lib/docscribe/cli/config_builder.rb'
103102
- 'lib/docscribe/cli/init.rb'

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
docscribe (1.2.1)
4+
docscribe (1.3.0)
55
parser (>= 3.3)
66
prism (~> 1.8)
77

@@ -85,7 +85,7 @@ DEPENDENCIES
8585
CHECKSUMS
8686
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
8787
diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
88-
docscribe (1.2.1)
88+
docscribe (1.3.0)
8989
json (2.18.0) sha256=b10506aee4183f5cf49e0efc48073d7b75843ce3782c68dbeb763351c08fd505
9090
language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc
9191
lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87

Gemfile_2_7.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
docscribe (1.2.1)
4+
docscribe (1.3.0)
55
parser (>= 3.3)
66
prism (~> 1.8)
77

@@ -65,6 +65,7 @@ GEM
6565
yard (0.9.38)
6666

6767
PLATFORMS
68+
arm64-darwin-24
6869
arm64-darwin-25
6970
x86_64-linux
7071

lib/docscribe/inline_rewriter/collector.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,39 @@ def on_casgn(node)
240240
node
241241
end
242242

243+
# Enter a top-level method definition and collect it as a documentation target.
244+
#
245+
# Top-level methods implicitly belong to +Object+. This handler ensures
246+
# that +def foo+ declared outside of any class or module is still picked
247+
# up by the collector.
248+
#
249+
# @param [Parser::AST::Node] node
250+
# @return [Parser::AST::Node]
251+
def on_def(node)
252+
return node unless @name_stack.empty?
253+
254+
ctx = VisibilityCtx.new
255+
ctx.container_is_module = false
256+
process_stmt(node, ctx)
257+
node
258+
end
259+
260+
# Enter a top-level singleton method definition and collect it as a documentation target.
261+
#
262+
# Handles the case of +def self.foo+ declared at the top level, outside
263+
# of any class or module body.
264+
#
265+
# @param [Parser::AST::Node] node
266+
# @return [Parser::AST::Node]
267+
def on_defs(node)
268+
return node unless @name_stack.empty?
269+
270+
ctx = VisibilityCtx.new
271+
ctx.container_is_module = false
272+
process_stmt(node, ctx)
273+
node
274+
end
275+
243276
private
244277

245278
# Method documentation.

lib/docscribe/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Docscribe
4-
VERSION = '1.2.1'
4+
VERSION = '1.3.0'
55
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'top-level methods' do
4+
subject(:out) { Docscribe::InlineRewriter.insert_comments(code, strategy: :safe, config: conf) }
5+
6+
let(:conf) { Docscribe::Config.new('emit' => { 'header' => true }) }
7+
8+
context 'when a method is defined outside any class or module' do
9+
let(:code) { <<~RUBY }
10+
def foo
11+
1
12+
end
13+
RUBY
14+
15+
it 'adds documentation' do
16+
expect(out).to include('Object#foo')
17+
expect(out).to include('# Method documentation.')
18+
end
19+
end
20+
21+
context 'when a singleton method is defined at top level' do
22+
let(:code) { <<~RUBY }
23+
def self.bar
24+
2
25+
end
26+
RUBY
27+
28+
it 'adds documentation' do
29+
expect(out).to include('Object.bar')
30+
expect(out).to include('# Method documentation.')
31+
end
32+
end
33+
34+
context 'when top-level and class methods coexist' do
35+
let(:code) { <<~RUBY }
36+
def foo
37+
1
38+
end
39+
40+
class Obj
41+
def abc(param: {})
42+
12
43+
end
44+
end
45+
RUBY
46+
47+
it 'documents both' do
48+
expect(out).to include('# +Object#foo+')
49+
expect(out).to include('# +Obj#abc+')
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)