synvert-core 1.9.2 → 1.10.1

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: 966c0db8262c57bb94be47d44c5638c9c9607728da335b4bde9d39c49f7bc665
4
+ data.tar.gz: e2be9da04e58da3b236d03006883778f6e202d9bbb5a8fa922dd6339117a6a2a
5
5
  SHA512:
6
- metadata.gz: 1ad2b3a8e7baa9d158e9a7bdbb0dbc9f46b5924ccd7c18867c41ba40192b2e3b57ae500e25edfdf7d0735161abb953bdb833aacbb0e2403321d692c1ae070dd1
7
- data.tar.gz: aafdeaa9f19a732c6386bec43c3fafbd56398d9f543e316875d3fa7038a91b1fdc153c1d2c530259a2b8fb137fcecd1ff9ab936723d5bfbdd0cf71d7fd175d28
6
+ metadata.gz: 1e1f6a8a25817ebe46c03bdbf6e7982f559c426bf5537c2ae28549215ad77a03c45768af6b3f74ba84e34105c6d1e8c9cbb365cb1f431220b59028678c373dcd
7
+ data.tar.gz: 44f57f854c537825acb57217b216294f226df391b1eb185fd1558f463084a91ad9fcc296f54eba05c5b66e1d995a40c897d267fff662ff5bf85472d83d993be1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.10.1 (2022-10-09)
4
+
5
+ * Do not reset `@options`
6
+ * `Rewriter.fetch` does not raise error if rewriter not found
7
+
8
+ ## 1.10.0 (2022-10-09)
9
+
10
+ * Eval snippet by http url, file path or snippet name
11
+ * Remove `execute_command` option
12
+ * Remove `Rewriter.call`
13
+ * Remove `Rewriter.execute`
14
+
3
15
  ## 1.9.2 (2022-10-03)
4
16
 
5
17
  * 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.1)
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.
@@ -58,35 +51,10 @@ module Synvert::Core
58
51
  # @param group [String] rewrtier group.
59
52
  # @param name [String] rewrtier name.
60
53
  # @return [Synvert::Core::Rewriter] the matching rewriter.
61
- # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
62
54
  def fetch(group, name)
63
55
  group = group.to_s
64
56
  name = name.to_s
65
- rewriter = rewriters.dig(group, name)
66
- raise RewriterNotFound, "Rewriter #{group} #{name} not found" unless rewriter
67
-
68
- rewriter
69
- end
70
-
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
57
+ rewriters.dig(group, name)
90
58
  end
91
59
 
92
60
  # Get all available rewriters
@@ -125,8 +93,11 @@ module Synvert::Core
125
93
  # @!attribute [r] gem_spec
126
94
  # @return [Rewriter::GemSpec] the gem spec
127
95
  # @!attribute [r] test_results
128
- # @return [Object] the test results
96
+ # @return [Array<Object>] the test results
97
+ # @!attribute [rw] options
98
+ # @return [Hash] the rewriter options
129
99
  attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results
100
+ attr_accessor :options
130
101
 
131
102
  # Initialize a Rewriter.
132
103
  # When a rewriter is initialized, it is already registered.
@@ -143,7 +114,7 @@ module Synvert::Core
143
114
  @warnings = []
144
115
  @affected_files = Set.new
145
116
  @redo_until_no_change = false
146
- @options = { run_instance: true, execute_command: 'process', write_to_file: true }
117
+ @options = DEFAULT_OPTIONS.dup
147
118
  @test_results = []
148
119
  self.class.register(@group, @name, self)
149
120
  end
@@ -161,26 +132,16 @@ module Synvert::Core
161
132
  # It will call the block but doesn't change any file.
162
133
  def process_with_sandbox
163
134
  @options[:run_instance] = false
164
- begin
165
- process
166
- ensure
167
- @options[:run_instance] = true
168
- end
135
+ process
169
136
  end
170
137
 
171
138
  def test
172
139
  @options[:write_to_file] = false
173
- @options[:execute_command] = 'test'
174
- begin
175
- @affected_files = Set.new
176
- instance_eval(&@block)
140
+ @affected_files = Set.new
141
+ instance_eval(&@block)
177
142
 
178
- if !@affected_files.empty? && @redo_until_no_change # redo
179
- test
180
- end
181
- ensure
182
- @options[:write_to_file] = true
183
- @options[:execute_command] = 'process'
143
+ if !@affected_files.empty? && @redo_until_no_change # redo
144
+ test
184
145
  end
185
146
  @test_results
186
147
  end
@@ -260,7 +221,7 @@ module Synvert::Core
260
221
  instance.process
261
222
  else
262
223
  results = instance.test
263
- @test_results += results.select { |result| result.affected? }
224
+ merge_test_results(results)
264
225
  end
265
226
  end
266
227
 
@@ -309,22 +270,34 @@ module Synvert::Core
309
270
  # Synvert::Rewriter.new 'minitest', 'better_syntax' do
310
271
  # add_snippet 'minitest', 'assert_empty'
311
272
  # 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'
273
+ # add_snippet 'minitest/assert_instance_of'
274
+ # add_snippet 'minitest/assert_kind_of'
275
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_match.rb'
276
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_nil.rb'
277
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_silent.rb'
278
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_truthy.rb'
323
279
  # end
324
- # @param group [String] group of another rewriter.
280
+ # @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
281
  # @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)
282
+ def add_snippet(group, name = nil)
283
+ rewriter =
284
+ if name
285
+ Rewriter.fetch(group, name) || Utils.eval_snippet([group, name].join('/'))
286
+ else
287
+ Utils.eval_snippet(group)
288
+ end
289
+ return unless rewriter && rewriter.is_a?(Rewriter)
290
+
291
+ rewriter.options = @options
292
+ if !rewriter.options[:write_to_file]
293
+ results = rewriter.test
294
+ merge_test_results(results)
295
+ elsif rewriter.options[:run_instance]
296
+ rewriter.process
297
+ else
298
+ rewriter.process_with_sandbox
299
+ end
300
+ @sub_snippets << rewriter
328
301
  end
329
302
 
330
303
  # Parse +helper_method+ dsl, it defines helper method for {Synvert::Core::Rewriter::Instance}.
@@ -374,5 +347,11 @@ module Synvert::Core
374
347
  def redo_until_no_change
375
348
  @redo_until_no_change = true
376
349
  end
350
+
351
+ private
352
+
353
+ def merge_test_results(results)
354
+ @test_results += results.select { |result| result.affected? }
355
+ end
377
356
  end
378
357
  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.1'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -17,8 +17,7 @@ module Synvert
17
17
  autoload :Configuration, 'synvert/core/configuration'
18
18
  autoload :Rewriter, 'synvert/core/rewriter'
19
19
  autoload :Engine, 'synvert/core/engine'
20
- autoload :RewriterNotFound, 'synvert/core/exceptions'
21
- autoload :MethodNotSupported, 'synvert/core/exceptions'
20
+ autoload :Utils, 'synvert/core/utils'
22
21
  end
23
22
  end
24
23
 
@@ -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,20 +213,30 @@ 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
 
229
- it 'raises RewriterNotFound' do
230
- rewriter =
231
- Rewriter.new 'group', 'name' do
232
- add_snippet :group, :not_exist
233
- end
234
- expect { rewriter.process }
235
- .to raise_error(RewriterNotFound)
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
236
240
  end
237
241
  end
238
242
 
@@ -276,23 +280,6 @@ module Synvert::Core
276
280
  expect(Rewriter.fetch('group', 'rewriter')).to eq rewriter
277
281
  end
278
282
 
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
283
  context 'available' do
297
284
  it 'lists empty rewriters' do
298
285
  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.1
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
@@ -116,7 +116,6 @@ files:
116
116
  - lib/synvert/core/configuration.rb
117
117
  - lib/synvert/core/engine.rb
118
118
  - lib/synvert/core/engine/erb.rb
119
- - lib/synvert/core/exceptions.rb
120
119
  - lib/synvert/core/node_ext.rb
121
120
  - lib/synvert/core/rewriter.rb
122
121
  - lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
@@ -134,6 +133,7 @@ files:
134
133
  - lib/synvert/core/rewriter/scope/query_scope.rb
135
134
  - lib/synvert/core/rewriter/scope/within_scope.rb
136
135
  - lib/synvert/core/rewriter/warning.rb
136
+ - lib/synvert/core/utils.rb
137
137
  - lib/synvert/core/version.rb
138
138
  - spec/spec_helper.rb
139
139
  - spec/support/parser_helper.rb
@@ -154,6 +154,7 @@ files:
154
154
  - spec/synvert/core/rewriter/scope_spec.rb
155
155
  - spec/synvert/core/rewriter/warning_spec.rb
156
156
  - spec/synvert/core/rewriter_spec.rb
157
+ - spec/synvert/core/utils_spec.rb
157
158
  - synvert-core-ruby.gemspec
158
159
  homepage: https://github.com/xinminlabs/synvert-core-ruby
159
160
  licenses:
@@ -174,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
175
  - !ruby/object:Gem::Version
175
176
  version: '0'
176
177
  requirements: []
177
- rubygems_version: 3.3.7
178
+ rubygems_version: 3.3.22
178
179
  signing_key:
179
180
  specification_version: 4
180
181
  summary: convert ruby code to better syntax.
@@ -198,3 +199,4 @@ test_files:
198
199
  - spec/synvert/core/rewriter/scope_spec.rb
199
200
  - spec/synvert/core/rewriter/warning_spec.rb
200
201
  - spec/synvert/core/rewriter_spec.rb
202
+ - spec/synvert/core/utils_spec.rb
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # Rewriter not found exception.
5
- class RewriterNotFound < RuntimeError
6
- end
7
-
8
- # Method not supported exception.
9
- class MethodNotSupported < RuntimeError
10
- end
11
- end