synvert-core 1.9.2 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 737e801df5a1d686045b99ff8038cd64bd2d39e96813fec4abdedb4473b6f943
4
- data.tar.gz: 4adcef7e1eaff7297f35ce5b4df40a3d3edbe447c1354c2a0e7c5e279ec2b60f
3
+ metadata.gz: 16de1af456b15373297aebe409711e1725bb329e3da7d4fa8d84023348ceb236
4
+ data.tar.gz: e034ccf44cfce96335f10a38d5aa36793faa08c84e8d75509fc658824cf64676
5
5
  SHA512:
6
- metadata.gz: 1ad2b3a8e7baa9d158e9a7bdbb0dbc9f46b5924ccd7c18867c41ba40192b2e3b57ae500e25edfdf7d0735161abb953bdb833aacbb0e2403321d692c1ae070dd1
7
- data.tar.gz: aafdeaa9f19a732c6386bec43c3fafbd56398d9f543e316875d3fa7038a91b1fdc153c1d2c530259a2b8fb137fcecd1ff9ab936723d5bfbdd0cf71d7fd175d28
6
+ metadata.gz: 3f745e87c308b417c5938c2731af00254abe5beba70472bca1ebfdf534127b7df8f6ab33261b8eb5d31d33deaa471927a780a14720f0ee406eea44f5a7e06495
7
+ data.tar.gz: ac2c35d21a4e6558b14c861966c6326446926bc27cc9e2d278757df31cf68a33142e62d47f9d40e5ec72c1d3fd8efde6574046e00506c9a12d0a800580a00023
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.10.0 (2022-10-09)
4
+
5
+ * Eval snippet by http url, file path or snippet name
6
+ * Remove `execute_command` option
7
+ * Remove `Rewriter.call`
8
+ * Remove `Rewriter.execute`
9
+
3
10
  ## 1.9.2 (2022-10-03)
4
11
 
5
12
  * Fix `test` in sub_snippets
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.9.2)
4
+ synvert-core (1.10.0)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
  node_mutation
@@ -8,6 +8,8 @@ module Synvert::Core
8
8
  # One Rewriter checks if the depndency version matches, and it can contain one or many {Synvert::Core::Rewriter::Instance},
9
9
  # which define the behavior what files and what codes to detect and rewrite to what code.
10
10
  class Rewriter
11
+ DEFAULT_OPTIONS = { run_instance: true, write_to_file: true }.freeze
12
+
11
13
  autoload :ReplaceErbStmtWithExprAction, 'synvert/core/rewriter/action/replace_erb_stmt_with_expr_action'
12
14
 
13
15
  autoload :Warning, 'synvert/core/rewriter/warning'
@@ -32,15 +34,6 @@ module Synvert::Core
32
34
  autoload :AnyValue, 'synvert/core/rewriter/any_value'
33
35
 
34
36
  class << self
35
- # Execute the temporary rewriter without group and name.
36
- #
37
- # @yield defines the behaviors of the rewriter.
38
- def execute(&block)
39
- rewriter = Rewriter.new('', '', &block)
40
- rewriter.process
41
- rewriter
42
- end
43
-
44
37
  # Register a rewriter with its group and name.
45
38
  #
46
39
  # @param group [String] the rewriter group.
@@ -68,27 +61,6 @@ module Synvert::Core
68
61
  rewriter
69
62
  end
70
63
 
71
- # Get a registered rewriter by group and name, then process that rewriter.
72
- #
73
- # @param group [String] the rewriter group.
74
- # @param name [String] the rewriter name.
75
- # @param options [Hash]
76
- # @option options [Boolean] :run_instance (true) process the instance.
77
- # @option options [String] :execute_command ('process') process or test the rewriter
78
- # @return [Synvert::Core::Rewriter] the registered rewriter.
79
- # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
80
- def call(group, name, options = { run_instance: true, execute_command: 'process' })
81
- rewriter = fetch(group, name)
82
- if options[:execute_command] == 'test'
83
- rewriter.test
84
- elsif options[:run_instance]
85
- rewriter.process
86
- else
87
- rewriter.process_with_sandbox
88
- end
89
- rewriter
90
- end
91
-
92
64
  # Get all available rewriters
93
65
  #
94
66
  # @return [Hash<String, Hash<String, Rewriter>>]
@@ -125,8 +97,11 @@ module Synvert::Core
125
97
  # @!attribute [r] gem_spec
126
98
  # @return [Rewriter::GemSpec] the gem spec
127
99
  # @!attribute [r] test_results
128
- # @return [Object] the test results
100
+ # @return [Array<Object>] the test results
101
+ # @!attribute [rw] options
102
+ # @return [Hash] the rewriter options
129
103
  attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results
104
+ attr_accessor :options
130
105
 
131
106
  # Initialize a Rewriter.
132
107
  # When a rewriter is initialized, it is already registered.
@@ -143,7 +118,7 @@ module Synvert::Core
143
118
  @warnings = []
144
119
  @affected_files = Set.new
145
120
  @redo_until_no_change = false
146
- @options = { run_instance: true, execute_command: 'process', write_to_file: true }
121
+ @options = DEFAULT_OPTIONS.dup
147
122
  @test_results = []
148
123
  self.class.register(@group, @name, self)
149
124
  end
@@ -170,7 +145,6 @@ module Synvert::Core
170
145
 
171
146
  def test
172
147
  @options[:write_to_file] = false
173
- @options[:execute_command] = 'test'
174
148
  begin
175
149
  @affected_files = Set.new
176
150
  instance_eval(&@block)
@@ -180,7 +154,6 @@ module Synvert::Core
180
154
  end
181
155
  ensure
182
156
  @options[:write_to_file] = true
183
- @options[:execute_command] = 'process'
184
157
  end
185
158
  @test_results
186
159
  end
@@ -260,7 +233,7 @@ module Synvert::Core
260
233
  instance.process
261
234
  else
262
235
  results = instance.test
263
- @test_results += results.select { |result| result.affected? }
236
+ merge_test_results(results)
264
237
  end
265
238
  end
266
239
 
@@ -309,22 +282,34 @@ module Synvert::Core
309
282
  # Synvert::Rewriter.new 'minitest', 'better_syntax' do
310
283
  # add_snippet 'minitest', 'assert_empty'
311
284
  # add_snippet 'minitest', 'assert_equal_arguments_order'
312
- # add_snippet 'minitest', 'assert_includes'
313
- # add_snippet 'minitest', 'assert_instance_of'
314
- # add_snippet 'minitest', 'assert_kind_of'
315
- # add_snippet 'minitest', 'assert_match'
316
- # add_snippet 'minitest', 'assert_nil'
317
- # add_snippet 'minitest', 'assert_operator'
318
- # add_snippet 'minitest', 'assert_path_exists'
319
- # add_snippet 'minitest', 'assert_predicate'
320
- # add_snippet 'minitest', 'assert_respond_to'
321
- # add_snippet 'minitest', 'assert_silent'
322
- # add_snippet 'minitest', 'assert_truthy'
285
+ # add_snippet 'minitest/assert_instance_of'
286
+ # add_snippet 'minitest/assert_kind_of'
287
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_match.rb'
288
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_nil.rb'
289
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_silent.rb'
290
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_truthy.rb'
323
291
  # end
324
- # @param group [String] group of another rewriter.
292
+ # @param group [String] group of another rewriter, if there's no name parameter, the group can be http url, file path or snippet name.
325
293
  # @param name [String] name of another rewriter.
326
- def add_snippet(group, name)
327
- @sub_snippets << self.class.call(group.to_s, name.to_s, @options)
294
+ def add_snippet(group, name = nil)
295
+ rewriter =
296
+ if name
297
+ Rewriter.fetch(group, name) || Utils.eval_snippet([group, name].join('/'))
298
+ else
299
+ Utils.eval_snippet(group)
300
+ end
301
+ return unless rewriter && rewriter.is_a?(Rewriter)
302
+
303
+ rewriter.options = @options
304
+ if !rewriter.options[:write_to_file]
305
+ results = rewriter.test
306
+ merge_test_results(results)
307
+ elsif rewriter.options[:run_instance]
308
+ rewriter.process
309
+ else
310
+ rewriter.process_with_sandbox
311
+ end
312
+ @sub_snippets << rewriter
328
313
  end
329
314
 
330
315
  # Parse +helper_method+ dsl, it defines helper method for {Synvert::Core::Rewriter::Instance}.
@@ -374,5 +359,11 @@ module Synvert::Core
374
359
  def redo_until_no_change
375
360
  @redo_until_no_change = true
376
361
  end
362
+
363
+ private
364
+
365
+ def merge_test_results(results)
366
+ @test_results += results.select { |result| result.affected? }
367
+ end
377
368
  end
378
369
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module Synvert::Core
6
+ class Utils
7
+ class << self
8
+ def eval_snippet(snippet_name)
9
+ if is_valid_url?(snippet_name)
10
+ uri = URI.parse(format_url(snippet_name))
11
+ eval(uri.open.read)
12
+ elsif is_valid_file?(snippet_name)
13
+ eval(File.read(snippet_name))
14
+ else
15
+ eval(File.read(File.join(default_snippets_home, 'lib', "#{snippet_name}.rb")))
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def is_valid_url?(url)
22
+ /^http/.match?(url)
23
+ end
24
+
25
+ def is_valid_file?(path)
26
+ File.exist?(path)
27
+ end
28
+
29
+ def default_snippets_home
30
+ ENV['SYNVERT_SNIPPETS_HOME'] || File.join(ENV['HOME'], '.synvert-ruby')
31
+ end
32
+
33
+ def format_url(url)
34
+ convert_to_github_raw_url(url)
35
+ end
36
+
37
+ def convert_to_github_raw_url(url)
38
+ return url unless url.include?('//github.com/')
39
+
40
+ url.sub('//github.com/', '//raw.githubusercontent.com/').sub('/blob/', '/')
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.9.2'
5
+ VERSION = '1.10.0'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -19,6 +19,7 @@ module Synvert
19
19
  autoload :Engine, 'synvert/core/engine'
20
20
  autoload :RewriterNotFound, 'synvert/core/exceptions'
21
21
  autoload :MethodNotSupported, 'synvert/core/exceptions'
22
+ autoload :Utils, 'synvert/core/utils'
22
23
  end
23
24
  end
24
25
 
@@ -4,12 +4,6 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter do
7
- it '.execute' do
8
- run = false
9
- Rewriter.execute { run = true }
10
- expect(run).to be_truthy
11
- end
12
-
13
7
  it 'parses description' do
14
8
  rewriter =
15
9
  Rewriter.new 'group', 'name' do
@@ -209,7 +203,7 @@ module Synvert::Core
209
203
  rewriter1 = Rewriter.new 'group', 'rewriter1'
210
204
  rewriter2 =
211
205
  Rewriter.new 'group', 'rewriter2' do
212
- add_snippet :group, :rewriter1
206
+ add_snippet 'group', 'rewriter1'
213
207
  end
214
208
  expect(rewriter1).to receive(:process)
215
209
  rewriter2.process
@@ -219,13 +213,32 @@ module Synvert::Core
219
213
  rewriter1 = Rewriter.new 'group', 'rewriter1'
220
214
  rewriter2 =
221
215
  Rewriter.new 'group', 'rewriter2' do
222
- add_snippet :group, :rewriter1
216
+ add_snippet 'group', 'rewriter1'
223
217
  end
224
218
  expect(rewriter1).to receive(:process)
225
219
  rewriter2.process
226
220
  expect(rewriter2.sub_snippets).to eq [rewriter1]
227
221
  end
228
222
 
223
+ it 'adds snippet by http url' do
224
+ expect_any_instance_of(URI).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'sub_rewriter' do\nend"))
225
+ rewriter = Rewriter.new 'group', 'rewriter' do
226
+ add_snippet 'http://synvert.net/foo/bar.rb'
227
+ end
228
+ rewriter.process
229
+ expect(Rewriter.fetch('group', 'sub_rewriter')).not_to be_nil
230
+ end
231
+
232
+ it 'adds snippet by file path' do
233
+ expect(File).to receive(:exist?).and_return(true)
234
+ expect(File).to receive(:read).and_return("Rewriter.new 'group', 'sub_rewriter' do\nend")
235
+ rewriter = Rewriter.new 'group', 'rewriter' do
236
+ add_snippet '/home/richard/foo/bar.rb'
237
+ end
238
+ rewriter.process
239
+ expect(Rewriter.fetch('group', 'sub_rewriter')).not_to be_nil
240
+ end
241
+
229
242
  it 'raises RewriterNotFound' do
230
243
  rewriter =
231
244
  Rewriter.new 'group', 'name' do
@@ -276,23 +289,6 @@ module Synvert::Core
276
289
  expect(Rewriter.fetch('group', 'rewriter')).to eq rewriter
277
290
  end
278
291
 
279
- it 'registers and calls rewriter' do
280
- rewriter = Rewriter.new 'group', 'rewriter'
281
- expect(rewriter).to receive(:process)
282
- Rewriter.call 'group', 'rewriter'
283
- end
284
-
285
- it 'registers and calls rewriter in sandbox mode' do
286
- rewriter = Rewriter.new 'group', 'rewriter'
287
- expect(rewriter).to receive(:process_with_sandbox)
288
- Rewriter.call 'group', 'rewriter', run_instance: false
289
- end
290
-
291
- it 'raises RewriterNotFound if rewriter not found' do
292
- expect { Rewriter.call 'group', 'rewriter' }
293
- .to raise_error(RewriterNotFound)
294
- end
295
-
296
292
  context 'available' do
297
293
  it 'lists empty rewriters' do
298
294
  expect(Rewriter.availables).to eq({})
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ RSpec.describe Utils do
5
+ describe '.eval_snippet' do
6
+ it 'evals snippet by http url' do
7
+ expect_any_instance_of(URI).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'name' do\nend"))
8
+ rewriter = described_class.eval_snippet('http://example.com/rewriter.rb')
9
+ expect(rewriter.group).to eq 'group'
10
+ expect(rewriter.name).to eq 'name'
11
+ end
12
+
13
+ it 'adds snippet by file path' do
14
+ expect(File).to receive(:exist?).and_return(true)
15
+ expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
16
+ rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
17
+ expect(rewriter.group).to eq 'group'
18
+ expect(rewriter.name).to eq 'name'
19
+ end
20
+
21
+ it 'adds snippet by snippet name' do
22
+ expect(File).to receive(:exist?).and_return(false)
23
+ expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
24
+ rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
25
+ expect(rewriter.group).to eq 'group'
26
+ expect(rewriter.name).to eq 'name'
27
+ end
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.2
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-03 00:00:00.000000000 Z
11
+ date: 2022-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -134,6 +134,7 @@ files:
134
134
  - lib/synvert/core/rewriter/scope/query_scope.rb
135
135
  - lib/synvert/core/rewriter/scope/within_scope.rb
136
136
  - lib/synvert/core/rewriter/warning.rb
137
+ - lib/synvert/core/utils.rb
137
138
  - lib/synvert/core/version.rb
138
139
  - spec/spec_helper.rb
139
140
  - spec/support/parser_helper.rb
@@ -154,6 +155,7 @@ files:
154
155
  - spec/synvert/core/rewriter/scope_spec.rb
155
156
  - spec/synvert/core/rewriter/warning_spec.rb
156
157
  - spec/synvert/core/rewriter_spec.rb
158
+ - spec/synvert/core/utils_spec.rb
157
159
  - synvert-core-ruby.gemspec
158
160
  homepage: https://github.com/xinminlabs/synvert-core-ruby
159
161
  licenses:
@@ -174,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
176
  - !ruby/object:Gem::Version
175
177
  version: '0'
176
178
  requirements: []
177
- rubygems_version: 3.3.7
179
+ rubygems_version: 3.3.22
178
180
  signing_key:
179
181
  specification_version: 4
180
182
  summary: convert ruby code to better syntax.
@@ -198,3 +200,4 @@ test_files:
198
200
  - spec/synvert/core/rewriter/scope_spec.rb
199
201
  - spec/synvert/core/rewriter/warning_spec.rb
200
202
  - spec/synvert/core/rewriter_spec.rb
203
+ - spec/synvert/core/utils_spec.rb