sirop 0.6 → 0.7

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: 30ef982b49313b55ac8b76d8886f406ee315421080e728bc1d9cdf05bcce93e7
4
- data.tar.gz: 40c33682b5c44f10e242049041f40f20f58a2939340debb858307b5bc12b9319
3
+ metadata.gz: 0aa1bac2ed98532aeed671c846e23c49d64a0df0c838f908d345aff6b3eb20db
4
+ data.tar.gz: 0efe8bb07a5f691e60978b8a523891808dd849c2b05d62aa28fad1b5b46045ed
5
5
  SHA512:
6
- metadata.gz: 9d92a9e43cfa127c5d2a8e1ccb963ea38e66b54670c0a33558a874a95e5b3ae11fc9092d5b69c0486828a8a759bb6a20c4596280e90e3f1285928badc1da9d55
7
- data.tar.gz: e5f76b7f45f7fd5a6ddb94613aa381b7f36ecc737f1c5c62a19c3257f48398509e61df2439b6b3d2716cbc80d8605cafe349174e6c61c4b21173e6ab7f21dbd6
6
+ metadata.gz: c92e0f7c2cf5c4452e4af50b5b1e99786eb8d2453668020c5a9fe2d955f242562e96d068b23fee50a304bc02f446e903c43ec8bb720cff0a681a648e6528f7d4
7
+ data.tar.gz: daf7af4d543398a0e34d4e0521737d179ed120530921c5b560148bbe806e3f38d8b7c12b9d2e3c0665eed26560f46106e80e23751fd0258c354df9d4fa7b1354
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.7 2025-07-24
2
+
3
+ - Add minimize_whitespace option to `Sourcifier#initialize`
4
+ - Fix `Sirop.to_ast` for wrapped procs
5
+
1
6
  # 0.6 2025-07-23
2
7
 
3
8
  - Update to current version of Prism
@@ -29,4 +34,3 @@
29
34
  # 2024-02-20 0.1
30
35
 
31
36
  - Find node for a given `Proc` object
32
- -
data/lib/sirop/finder.rb CHANGED
@@ -5,8 +5,7 @@ require 'prism'
5
5
  module Sirop
6
6
  class Finder < Prism::BasicVisitor
7
7
  def self.find(*, &)
8
- finder = self.new
9
- finder.find(*, &)
8
+ new.find(*, &)
10
9
  end
11
10
 
12
11
  def find(root, key, &)
@@ -3,7 +3,7 @@
3
3
  require 'prism'
4
4
 
5
5
  module Sirop
6
- #
6
+ #
7
7
  class Sourcifier < Prism::BasicVisitor
8
8
  VISIT_PLANS = {
9
9
  alias_global_variable: [:keyword_loc, :new_name, :old_name],
@@ -91,20 +91,21 @@ module Sirop
91
91
 
92
92
  attr_reader :buffer
93
93
 
94
- def initialize
94
+ def initialize(minimize_whitespace: false)
95
95
  @buffer = +''
96
+ @minimize_whitespace = minimize_whitespace
96
97
  end
97
-
98
+
98
99
  def to_source(node)
99
100
  @buffer.clear
100
101
  visit(node)
101
102
  @buffer
102
103
  end
103
-
104
+
104
105
  def loc_start(loc)
105
106
  [loc.start_line, loc.start_column]
106
107
  end
107
-
108
+
108
109
  def loc_end(loc)
109
110
  [loc.end_line, loc.end_column]
110
111
  end
@@ -120,16 +121,16 @@ module Sirop
120
121
  line_diff = loc.start_line - @last_loc_end.first
121
122
  if line_diff > 0
122
123
  @buffer << "\n" * line_diff
123
- @buffer << ' ' * loc.start_column
124
+ @buffer << ' ' * loc.start_column if !@minimize_whitespace
124
125
  elsif line_diff == 0
125
126
  ofs = loc.start_column - @last_loc_end.last
126
127
  if ofs > 0
127
- @buffer << ' ' * ofs
128
+ @buffer << (@minimize_whitespace ? ' ' : (' ' * ofs))
128
129
  end
129
130
  end
130
131
  else
131
132
  # empty buffer
132
- @buffer << ' ' * loc.start_column
133
+ @buffer << ' ' * loc.start_column if !@minimize_whitespace
133
134
  end
134
135
  @last_loc = loc
135
136
  @last_loc_start = loc_start(loc)
@@ -141,7 +142,7 @@ module Sirop
141
142
 
142
143
  if @last_loc
143
144
  loc_loc = loc.is_a?(Prism::Node) ? loc.location : loc
144
- return if loc_loc.slice == @last_loc.slice && loc_loc.start_line == @last_loc.start_line &&
145
+ return if loc_loc.slice == @last_loc.slice && loc_loc.start_line == @last_loc.start_line &&
145
146
  loc_loc.start_column == @last_loc.start_column
146
147
  end
147
148
 
@@ -155,20 +156,20 @@ module Sirop
155
156
  str = str.chomp if chomp
156
157
  emit(str)
157
158
  end
158
-
159
+
159
160
  def emit_verbatim(node)
160
161
  emit_code(node.location)
161
162
  end
162
163
 
163
164
  def emit_nothing(node)
164
165
  # emit nothing
165
- end
166
+ end
166
167
 
167
168
  def emit_str(str)
168
169
  emit(str)
169
170
  @last_loc_end[1] += str.size
170
171
  end
171
-
172
+
172
173
  def emit_comma
173
174
  emit_str(',')
174
175
  end
@@ -193,7 +194,7 @@ module Sirop
193
194
  sym = :"visit_#{key}_node"
194
195
  define_method(sym) { |n| visit_plan(plan, n) }
195
196
  end
196
-
197
+
197
198
  def visit_plan(plan, node)
198
199
  return send(plan, node) if plan.is_a?(Symbol)
199
200
 
@@ -270,7 +271,7 @@ module Sirop
270
271
  def visit_arguments_node(node, subscript = 0..-1)
271
272
  visit_comma_separated_nodes(node.arguments[subscript])
272
273
  end
273
-
274
+
274
275
  def visit_keyword_hash_node(node)
275
276
  visit_comma_separated_nodes(node.elements)
276
277
  end
@@ -298,7 +299,7 @@ module Sirop
298
299
  end
299
300
 
300
301
  def visit_if_node_guard(node)
301
- emit_code(node.statements)
302
+ emit_code(node.statements)
302
303
  emit_code(node.if_keyword_loc)
303
304
  emit_code(node.predicate)
304
305
  end
@@ -318,7 +319,7 @@ module Sirop
318
319
  end
319
320
 
320
321
  def visit_unless_node_guard(node)
321
- emit_code(node.statements)
322
+ emit_code(node.statements)
322
323
  emit_code(node.keyword_loc)
323
324
  emit_code(node.predicate)
324
325
  end
@@ -385,7 +386,7 @@ module Sirop
385
386
  end
386
387
  emit_code(node.opening_loc)
387
388
  emit_code(node.arguments)
388
-
389
+
389
390
  if block.is_a?(Prism::BlockArgumentNode)
390
391
  emit_comma if node.arguments && node.arguments.arguments.size > 0
391
392
  emit_code(block)
@@ -461,7 +462,7 @@ module Sirop
461
462
  visit_comma_separated_nodes(node.elements)
462
463
  emit_code(node.closing_loc)
463
464
  end
464
-
465
+
465
466
  def visit_array_node(node)
466
467
  emit_code(node.opening_loc)
467
468
  if node.opening_loc && node.opening_loc.slice =~ /^%/
data/lib/sirop/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Sirop
2
2
  # Sirop version
3
- VERSION = '0.6'
3
+ VERSION = '0.7'
4
4
  end
data/lib/sirop.rb CHANGED
@@ -22,15 +22,15 @@ module Sirop
22
22
  end
23
23
  end
24
24
 
25
- def to_source(obj)
25
+ def to_source(obj, **)
26
26
  obj = to_ast(obj) if !obj.is_a?(Prism::Node)
27
- Sourcifier.new.to_source(obj)
27
+ Sourcifier.new(**).to_source(obj)
28
28
  end
29
29
 
30
30
  private
31
31
 
32
32
  def proc_ast(proc)
33
- fn, lineno = proc.source_location
33
+ fn, lineno = proc.source_location
34
34
  pr = Prism.parse(IO.read(fn), filepath: fn)
35
35
  program = pr.value
36
36
 
@@ -39,6 +39,10 @@ module Sirop
39
39
  found!(node) if node.location.start_line == lineno
40
40
  super(node)
41
41
  end
42
+ on(:block) do |node|
43
+ found!(node) if node.location.start_line == lineno
44
+ super(node)
45
+ end
42
46
  on(:call) do |node|
43
47
  case node.name
44
48
  when :proc, :lambda
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sirop
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: 5.22.0
32
+ version: 5.25.5
33
33
  type: :development
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 5.22.0
39
+ version: 5.25.5
40
40
  email: sharon@noteflakes.com
41
41
  executables: []
42
42
  extensions: []
@@ -69,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
- version: '3.2'
72
+ version: '3.4'
73
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="