sirop 0.6 → 0.8

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: a44ab3d216adc8e5bda83ca86fef998d8c059ac7f62b3ab4c4b1688be65a24a6
4
+ data.tar.gz: 8c4b7b1aa49f6e293ca2f161ade3f8c1b14ad386644e7abc713eefc7798bcbd7
5
5
  SHA512:
6
- metadata.gz: 9d92a9e43cfa127c5d2a8e1ccb963ea38e66b54670c0a33558a874a95e5b3ae11fc9092d5b69c0486828a8a759bb6a20c4596280e90e3f1285928badc1da9d55
7
- data.tar.gz: e5f76b7f45f7fd5a6ddb94613aa381b7f36ecc737f1c5c62a19c3257f48398509e61df2439b6b3d2716cbc80d8605cafe349174e6c61c4b21173e6ab7f21dbd6
6
+ metadata.gz: 5cfbd57a4e23be3fb959462f6c76aaf1af3e2810c80b81560772965d39654d7975a6aeb7f2441f0c33b9811c898f005ac89eb163ed4215099df7b47abc60e2ca
7
+ data.tar.gz: 8c778dee6a5a416438e37972684ee6cd12d8b8332c4422b099c913b14425d3acb6c63f092f7e8433277f409807724f70fe267e6b7e08d5f21b9367d682d69b3e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # 0.8 2025-08-06
2
+
3
+ - Add source map generation
4
+ - Require Ruby 3.4 or higher
5
+
6
+ # 0.7 2025-07-24
7
+
8
+ - Add minimize_whitespace option to `Sourcifier#initialize`
9
+ - Fix `Sirop.to_ast` for wrapped procs
10
+
1
11
  # 0.6 2025-07-23
2
12
 
3
13
  - Update to current version of Prism
@@ -29,4 +39,3 @@
29
39
  # 2024-02-20 0.1
30
40
 
31
41
  - 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,28 +91,47 @@ 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
101
+ @source_map = nil
100
102
  visit(node)
101
103
  @buffer
102
104
  end
103
-
105
+
106
+ def to_source_with_source_map(obj, node, line_ofs)
107
+ @buffer.clear
108
+ @source_map_line_ofs = line_ofs
109
+ @source_map = { source_fn: obj.source_location.first }
110
+ visit(node)
111
+ [@buffer, @source_map]
112
+ end
113
+
104
114
  def loc_start(loc)
105
115
  [loc.start_line, loc.start_column]
106
116
  end
107
-
117
+
108
118
  def loc_end(loc)
109
119
  [loc.end_line, loc.end_column]
110
120
  end
111
121
 
112
122
  def emit(str)
123
+ update_source_map
113
124
  @buffer << str
114
125
  end
115
126
 
127
+ def update_source_map
128
+ return if !@source_map
129
+
130
+ buffer_cur_line = @buffer.count("\n") + 1
131
+ orig_source_cur_line = @last_loc_start ? @last_loc_start.first : 1
132
+ @source_map[buffer_cur_line] ||= orig_source_cur_line
133
+ end
134
+
116
135
  def adjust_whitespace(loc)
117
136
  return if loc.is_a?(Sirop::Injection)
118
137
 
@@ -120,16 +139,16 @@ module Sirop
120
139
  line_diff = loc.start_line - @last_loc_end.first
121
140
  if line_diff > 0
122
141
  @buffer << "\n" * line_diff
123
- @buffer << ' ' * loc.start_column
142
+ @buffer << ' ' * loc.start_column if !@minimize_whitespace
124
143
  elsif line_diff == 0
125
144
  ofs = loc.start_column - @last_loc_end.last
126
145
  if ofs > 0
127
- @buffer << ' ' * ofs
146
+ @buffer << (@minimize_whitespace ? ' ' : (' ' * ofs))
128
147
  end
129
148
  end
130
149
  else
131
150
  # empty buffer
132
- @buffer << ' ' * loc.start_column
151
+ @buffer << ' ' * loc.start_column if !@minimize_whitespace
133
152
  end
134
153
  @last_loc = loc
135
154
  @last_loc_start = loc_start(loc)
@@ -141,7 +160,7 @@ module Sirop
141
160
 
142
161
  if @last_loc
143
162
  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 &&
163
+ return if loc_loc.slice == @last_loc.slice && loc_loc.start_line == @last_loc.start_line &&
145
164
  loc_loc.start_column == @last_loc.start_column
146
165
  end
147
166
 
@@ -155,20 +174,20 @@ module Sirop
155
174
  str = str.chomp if chomp
156
175
  emit(str)
157
176
  end
158
-
177
+
159
178
  def emit_verbatim(node)
160
179
  emit_code(node.location)
161
180
  end
162
181
 
163
182
  def emit_nothing(node)
164
183
  # emit nothing
165
- end
184
+ end
166
185
 
167
186
  def emit_str(str)
168
187
  emit(str)
169
188
  @last_loc_end[1] += str.size
170
189
  end
171
-
190
+
172
191
  def emit_comma
173
192
  emit_str(',')
174
193
  end
@@ -193,7 +212,7 @@ module Sirop
193
212
  sym = :"visit_#{key}_node"
194
213
  define_method(sym) { |n| visit_plan(plan, n) }
195
214
  end
196
-
215
+
197
216
  def visit_plan(plan, node)
198
217
  return send(plan, node) if plan.is_a?(Symbol)
199
218
 
@@ -270,7 +289,7 @@ module Sirop
270
289
  def visit_arguments_node(node, subscript = 0..-1)
271
290
  visit_comma_separated_nodes(node.arguments[subscript])
272
291
  end
273
-
292
+
274
293
  def visit_keyword_hash_node(node)
275
294
  visit_comma_separated_nodes(node.elements)
276
295
  end
@@ -298,7 +317,7 @@ module Sirop
298
317
  end
299
318
 
300
319
  def visit_if_node_guard(node)
301
- emit_code(node.statements)
320
+ emit_code(node.statements)
302
321
  emit_code(node.if_keyword_loc)
303
322
  emit_code(node.predicate)
304
323
  end
@@ -318,7 +337,7 @@ module Sirop
318
337
  end
319
338
 
320
339
  def visit_unless_node_guard(node)
321
- emit_code(node.statements)
340
+ emit_code(node.statements)
322
341
  emit_code(node.keyword_loc)
323
342
  emit_code(node.predicate)
324
343
  end
@@ -385,7 +404,7 @@ module Sirop
385
404
  end
386
405
  emit_code(node.opening_loc)
387
406
  emit_code(node.arguments)
388
-
407
+
389
408
  if block.is_a?(Prism::BlockArgumentNode)
390
409
  emit_comma if node.arguments && node.arguments.arguments.size > 0
391
410
  emit_code(block)
@@ -461,7 +480,7 @@ module Sirop
461
480
  visit_comma_separated_nodes(node.elements)
462
481
  emit_code(node.closing_loc)
463
482
  end
464
-
483
+
465
484
  def visit_array_node(node)
466
485
  emit_code(node.opening_loc)
467
486
  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.8'
4
4
  end
data/lib/sirop.rb CHANGED
@@ -22,15 +22,20 @@ 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
+ end
29
+
30
+ def to_source_with_source_map(obj, line_ofs = 0, **)
31
+ ast = to_ast(obj)
32
+ Sourcifier.new(**).to_source_with_source_map(obj, ast, line_ofs)
28
33
  end
29
34
 
30
35
  private
31
36
 
32
37
  def proc_ast(proc)
33
- fn, lineno = proc.source_location
38
+ fn, lineno = proc.source_location
34
39
  pr = Prism.parse(IO.read(fn), filepath: fn)
35
40
  program = pr.value
36
41
 
@@ -39,6 +44,10 @@ module Sirop
39
44
  found!(node) if node.location.start_line == lineno
40
45
  super(node)
41
46
  end
47
+ on(:block) do |node|
48
+ found!(node) if node.location.start_line == lineno
49
+ super(node)
50
+ end
42
51
  on(:call) do |node|
43
52
  case node.name
44
53
  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.8'
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,14 +69,14 @@ 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
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.6.8
79
+ rubygems_version: 3.6.9
80
80
  specification_version: 4
81
81
  summary: 'Sirop: Ruby code rewriter'
82
82
  test_files: []