decode 0.23.0 → 0.23.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: ddd2b15f3019877bc34174d6d75e65d435431d6b4642fe6806c01cb44a154d20
4
- data.tar.gz: 99d6be44ed79584ba17a8ed8479c976890aac2a621d4be6ac0f0ab3edef86c9f
3
+ metadata.gz: 0ae94375f9288ad2aa541ed744d7fb51b86da0bb83d77a019e17c6c31ac9f2cf
4
+ data.tar.gz: 74e3bfe6b181464eb8406ba684aee902b5519ae48d960f9e2bb6a79fffe1917d
5
5
  SHA512:
6
- metadata.gz: 6015f4796c39f6c187fdf42fc872da64d85f4472ed1a641c74255a6a39c5cf411b30e31d55eabb8211c09d0d04030eabc23e1cd850d6ecdb92c50e9c72e25388
7
- data.tar.gz: 32bb1229c0b4dca21f0334b13c2742dc3b5963f8252da77377b2687322ee94c60b8a2b313df410e113f1865bd48b65be05b3756f968de77de27d238d54001f27
6
+ metadata.gz: '089969a2909633681778781ef8047e262cc4f4405f04a2adb72dbeb73f818fa8a6a4f177573fa4077eee1a9892b4995dc24cbff2dc46aacd0343422af5a16df8'
7
+ data.tar.gz: 8d9133f1d02b98af77c6bbd3fbf26c93d01c9be4614eedefc6e4c731aa077103dda30a484a6ac0ddbbbc45b3fce76c3c5d49559a3563b09dfba29723fc38ace5
checksums.yaml.gz.sig CHANGED
Binary file
@@ -43,19 +43,22 @@ module Decode
43
43
  end
44
44
 
45
45
  # The source code associated with the definition.
46
- # @returns [String] The source code text for this definition.
46
+ # @returns [String]
47
47
  def text
48
- expression = @node.location
49
- lines = expression.slice.lines
48
+ location = @node.location
49
+ source_text = location.slice_lines
50
+ lines = source_text.split("\n")
51
+
50
52
  if lines.count == 1
51
53
  return lines.first
52
54
  else
53
- if indentation = expression.slice.lines.first[/\A\s+/]
54
- # Remove all the indentation:
55
+ # Get the indentation from the first line of the node in the original source
56
+ if indentation = source_text[/\A\s+/]
57
+ # Remove the base indentation from all lines
55
58
  lines.each{|line| line.sub!(indentation, "")}
56
59
  end
57
60
 
58
- return lines.join
61
+ return lines.join("\n")
59
62
  end
60
63
  end
61
64
 
@@ -49,7 +49,7 @@ module Decode
49
49
  def walk_definitions(node, parent = nil, source = nil, &block)
50
50
  # Check for scope definitions from comments
51
51
  if node.comments.any?
52
- parent = scope_for(node.comments.map(&:slice), parent, &block) || parent
52
+ parent = scope_for(comments_for(node), parent, &block) || parent
53
53
  end
54
54
 
55
55
  case node.type
@@ -72,7 +72,7 @@ module Decode
72
72
 
73
73
  definition = Module.new(path,
74
74
  visibility: :public,
75
- comments: node.comments.map(&:slice),
75
+ comments: comments_for(node),
76
76
  parent: parent,
77
77
  node: node,
78
78
  language: @language,
@@ -94,7 +94,7 @@ module Decode
94
94
  definition = Class.new(path,
95
95
  super_class: super_class,
96
96
  visibility: :public,
97
- comments: node.comments.map(&:slice),
97
+ comments: comments_for(node),
98
98
  parent: parent,
99
99
  node: node,
100
100
  language: @language,
@@ -112,7 +112,7 @@ module Decode
112
112
  when :singleton_class_node
113
113
  if name = singleton_name_for(node)
114
114
  definition = Singleton.new(name,
115
- comments: node.comments.map(&:slice),
115
+ comments: comments_for(node),
116
116
  parent: parent, language: @language, visibility: :public, source: source
117
117
  )
118
118
 
@@ -127,7 +127,7 @@ module Decode
127
127
 
128
128
  definition = Method.new(node.name,
129
129
  visibility: @visibility,
130
- comments: node.comments.map(&:slice),
130
+ comments: comments_for(node),
131
131
  parent: parent,
132
132
  node: node,
133
133
  language: @language,
@@ -138,7 +138,7 @@ module Decode
138
138
  yield definition
139
139
  when :constant_write_node
140
140
  definition = Constant.new(node.name,
141
- comments: node.comments.map(&:slice),
141
+ comments: comments_for(node),
142
142
  parent: parent,
143
143
  node: node,
144
144
  language: @language,
@@ -162,7 +162,7 @@ module Decode
162
162
 
163
163
  definition = Method.new(arg_node.name,
164
164
  visibility: name,
165
- comments: arg_node.comments.map(&:slice),
165
+ comments: comments_for(arg_node),
166
166
  parent: parent,
167
167
  node: arg_node,
168
168
  language: @language,
@@ -191,7 +191,7 @@ module Decode
191
191
  end
192
192
  when :attr, :attr_reader, :attr_writer, :attr_accessor
193
193
  definition = Attribute.new(attribute_name_for(node),
194
- comments: node.comments.map(&:slice),
194
+ comments: comments_for(node),
195
195
  parent: parent, language: @language, node: node
196
196
  )
197
197
 
@@ -207,7 +207,7 @@ module Decode
207
207
  old_name = symbol_name_for(old_name_arg)
208
208
 
209
209
  definition = Alias.new(new_name.to_sym, old_name.to_sym,
210
- comments: node.comments.map(&:slice),
210
+ comments: comments_for(node),
211
211
  parent: parent,
212
212
  node: node,
213
213
  language: @language,
@@ -220,14 +220,14 @@ module Decode
220
220
  else
221
221
  # Check if this call should be treated as a definition
222
222
  # either because it has a @name comment, @attribute comment, or a block
223
- has_name_comment = node.comments.any? { |comment| comment.slice.match(NAME_ATTRIBUTE) }
224
- has_attribute_comment = kind_for(node, node.comments.map(&:slice))
223
+ has_name_comment = comments_for(node).any? { |comment| comment.match(NAME_ATTRIBUTE) }
224
+ has_attribute_comment = kind_for(node, comments_for(node))
225
225
  has_block = node.block
226
226
 
227
227
  if has_name_comment || has_attribute_comment || has_block
228
228
  definition = Call.new(
229
229
  attribute_name_for(node),
230
- comments: node.comments.map(&:slice),
230
+ comments: comments_for(node),
231
231
  parent: parent, language: @language, node: node
232
232
  )
233
233
 
@@ -245,7 +245,7 @@ module Decode
245
245
  old_name = node.old_name.unescaped
246
246
 
247
247
  definition = Alias.new(new_name.to_sym, old_name.to_sym,
248
- comments: node.comments.map(&:slice),
248
+ comments: comments_for(node),
249
249
  parent: parent,
250
250
  node: node,
251
251
  language: @language,
@@ -279,6 +279,42 @@ module Decode
279
279
 
280
280
  private
281
281
 
282
+ # Extract clean comment text from a node by removing leading # symbols and whitespace.
283
+ # Only returns comments that directly precede the node (i.e., are adjacent to it).
284
+ # @parameter node [Node] The AST node with comments.
285
+ # @returns [Array] Array of cleaned comment strings.
286
+ def comments_for(node)
287
+ # Find the node's starting line
288
+ node_start_line = node.location.start_line
289
+
290
+ # Filter comments to only include those that directly precede the node
291
+ # We work backwards from the line before the node to find consecutive comments
292
+ adjacent_comments = []
293
+ expected_line = node_start_line - 1
294
+
295
+ # Process comments in reverse order to work backwards from the node
296
+ node.comments.reverse_each do |comment|
297
+ comment_line = comment.location.start_line
298
+
299
+ # If this comment is on the expected line, it's adjacent
300
+ if comment_line == expected_line
301
+ adjacent_comments.unshift(comment)
302
+ expected_line = comment_line - 1
303
+ elsif comment_line < expected_line
304
+ # If we hit a comment that's too far back, stop
305
+ break
306
+ end
307
+ # If comment_line > expected_line, skip it (it's not adjacent)
308
+ end
309
+
310
+ # Clean and return the adjacent comments
311
+ adjacent_comments.map do |comment|
312
+ text = comment.slice
313
+ # Remove leading # and optional whitespace
314
+ text.sub(/\A\#\s?/, "")
315
+ end
316
+ end
317
+
282
318
  def assign_definition(parent, definition)
283
319
  (@definitions[parent] ||= {})[definition.name] = definition
284
320
  end
@@ -301,32 +337,6 @@ module Decode
301
337
  end
302
338
  end
303
339
 
304
- def extract_comments_for(node, comments)
305
- prefix = []
306
-
307
- while comment = comments.first
308
- break if comment.location.line >= node.location.line
309
-
310
- if last_comment = prefix.last
311
- if last_comment.location.line != (comment.location.line - 1)
312
- prefix.clear
313
- end
314
- end
315
-
316
- prefix << comments.shift
317
- end
318
-
319
- # The last comment must butt up against the node:
320
- if comment = prefix.last
321
- if comment.location.line == (node.location.line - 1)
322
- return prefix.map do |comment|
323
- # Remove # and at most one space/tab to preserve indentation
324
- comment.slice.sub(/\A\#[\s\t]?/, "")
325
- end
326
- end
327
- end
328
- end
329
-
330
340
  def with_visibility(visibility = :public, &block)
331
341
  saved_visibility = @visibility
332
342
  @visibility = visibility
@@ -335,12 +345,11 @@ module Decode
335
345
  @visibility = saved_visibility
336
346
  end
337
347
 
338
- NAME_ATTRIBUTE = /\A\#\s*@name\s+(?<value>.*?)\Z/
348
+ NAME_ATTRIBUTE = /\A@name\s+(?<value>.*?)\Z/
339
349
 
340
350
  def attribute_name_for(node)
341
- node.comments.each do |comment|
342
- text = comment.slice
343
- if match = text.match(NAME_ATTRIBUTE)
351
+ comments_for(node).each do |comment|
352
+ if match = comment.match(NAME_ATTRIBUTE)
344
353
  return match[:value].to_sym
345
354
  end
346
355
  end
@@ -410,8 +419,8 @@ module Decode
410
419
  end
411
420
 
412
421
  KIND_ATTRIBUTE = /\A
413
- (\#\s*@(?<kind>attribute)\s+(?<value>.*?))|
414
- (\#\s*@define\s+(?<kind>)\s+(?<value>.*?))
422
+ (@(?<kind>attribute)\s+(?<value>.*?))|
423
+ (@define\s+(?<kind>)\s+(?<value>.*?))
415
424
  \Z/x
416
425
 
417
426
  def kind_for(node, comments = nil)
@@ -425,7 +434,7 @@ module Decode
425
434
  end
426
435
 
427
436
  SCOPE_ATTRIBUTE = /\A
428
- \#\s*@scope\s+(?<names>.*?)
437
+ @scope\s+(?<names>.*?)
429
438
  \Z/x
430
439
 
431
440
  def scope_for(comments, parent = nil, &block)
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
6
  module Decode
7
- VERSION = "0.23.0"
7
+ VERSION = "0.23.1"
8
8
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.23.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file