synvert-core 1.33.2 → 1.34.0

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: 6b61075876259ba185ddf48421d30e1d0dfb6cd92f447f31218581a555f69ef5
4
- data.tar.gz: 9d7cd7d69f324e8c02d6c51014d8deb7524ca027b5b0099834953527d40d0da1
3
+ metadata.gz: 7407325ab10af6e8e90b2a2e10bc737686a555603f362319779a2a9868d8ba71
4
+ data.tar.gz: eb2f33a8ea48fc4836af3529ca4f684c7c8e1c98a797d55703ada5b60505878e
5
5
  SHA512:
6
- metadata.gz: 56267c1b2d6955d040f38b211dfb058e59e66b3bf14a2d2072532e215be46427fbcac36b171da8058a95fc9d1a93fb4398d4b82989e46cc53e732b56c35f08f3
7
- data.tar.gz: 1112d3c2985ad5b50a66f7210bd89c3fb512f13fc5710288591c6267dc320ac5a3e3746e6cfa66d08a4275bc21fd72428c260b431216d37efa57da1c3450bece
6
+ metadata.gz: 70c762d52252e31200861d30d418f4f634ff4f9dfcacd1d11964865be664118a78a6a76362614232f5a1b465ad8862d974adfa1f3baed6395cac6dac000ebffc
7
+ data.tar.gz: df130135e5df9453b3f4d6847c68aaf0703fb31765b384ca86c31ca52c0d60c60f64717ac2b26457d872a5a523a640cb5f6a9b7715fefe2b3df2008f691e3b01
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.34.0 (2024-02-29)
4
+
5
+ * Add `with_configurations` dsl
6
+ * Add `save_data` dsl
7
+ * Add `load_data` dsl
8
+
9
+ ## 1.33.3 (2024-02-28)
10
+
11
+ * Preserve current parser
12
+
3
13
  ## 1.33.2 (2024-02-24)
4
14
 
5
15
  * Reset `@warnings`, `@affected_files`, and `@test_results`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.33.2)
4
+ synvert-core (1.34.0)
5
5
  activesupport (< 7.0.0)
6
6
  node_mutation (>= 1.23.2)
7
7
  node_query (>= 1.15.1)
data/README.md CHANGED
@@ -70,6 +70,9 @@ DSLs are as follows
70
70
  * [helper_method](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
71
71
  * [add_snippet](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_snippet-instance_method) - call another rewriter
72
72
  * [call_helper](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#call_helper-instance_method) - call a shared rewriter
73
+ * [with_configurations](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#with_configurations-instance_method) - execute a block of code with temporary configurations
74
+ * [save_data](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#save_data-instance_method) - save data
75
+ * [load_data](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#load_data-instance_method) - load data
73
76
 
74
77
  Scopes:
75
78
 
@@ -100,6 +100,32 @@ module Synvert::Core
100
100
  def test_result
101
101
  @test_result || 'actions'
102
102
  end
103
+
104
+ # Temporarily sets the specified configurations, executes the given block, and then restores the original configurations.
105
+ #
106
+ # @param configurations [Hash] The configurations to be set temporarily.
107
+ # @yield The block of code to be executed.
108
+ #
109
+ # @example
110
+ # with_temporary_configurations({ number_of_workers: 1 }) do
111
+ # # Code to be executed with temporary configurations
112
+ # end
113
+ def with_temporary_configurations(configurations, &block)
114
+ old_instance_variables = instance_variables.reduce({}) do |hash, var|
115
+ hash[var] = instance_variable_get(var)
116
+ hash
117
+ end
118
+
119
+ configurations.each do |variable, value|
120
+ instance_variable_set("@#{variable}", value)
121
+ end
122
+
123
+ block.call
124
+
125
+ old_instance_variables.each do |var, value|
126
+ instance_variable_set(var, value)
127
+ end
128
+ end
103
129
  end
104
130
  end
105
131
  end
@@ -361,13 +361,15 @@ module Synvert::Core
361
361
  return unless rewriter && rewriter.is_a?(Rewriter)
362
362
 
363
363
  rewriter.options = @options
364
- if !rewriter.options[:write_to_file]
365
- results = rewriter.test
366
- merge_test_results(results)
367
- elsif rewriter.options[:run_instance]
368
- rewriter.process
369
- else
370
- rewriter.process_with_sandbox
364
+ preserve_current_parser do
365
+ if !rewriter.options[:write_to_file]
366
+ results = rewriter.test
367
+ merge_test_results(results)
368
+ elsif rewriter.options[:run_instance]
369
+ rewriter.process
370
+ else
371
+ rewriter.process_with_sandbox
372
+ end
371
373
  end
372
374
  @sub_snippets << rewriter
373
375
  end
@@ -385,7 +387,9 @@ module Synvert::Core
385
387
  helper = Synvert::Core::Helper.fetch(name) || Utils.eval_snippet(name)
386
388
  return unless helper && helper.is_a?(Synvert::Core::Helper)
387
389
 
388
- instance_exec(options, &helper.block)
390
+ preserve_current_parser do
391
+ instance_exec(options, &helper.block)
392
+ end
389
393
  end
390
394
 
391
395
  # It defines helper method for {Synvert::Core::Rewriter::Instance}.
@@ -405,6 +409,30 @@ module Synvert::Core
405
409
  @helpers << { name: name, block: block }
406
410
  end
407
411
 
412
+ # Executes a block of code with temporary configurations.
413
+ #
414
+ # @param configurations [Hash] The temporary configurations to apply.
415
+ # @yield The block of code to execute.
416
+ def with_configurations(configurations, &block)
417
+ Configuration.with_temporary_configurations(configurations, &block)
418
+ end
419
+
420
+ # Saves data with a given key and value.
421
+ #
422
+ # @param key [Symbol] the key to identify the data
423
+ # @param value [Object] the value to be saved
424
+ def save_data(key, value)
425
+ Synvert::Core.instance_variable_set("@#{key}", value)
426
+ end
427
+
428
+ # Loads data with the given key.
429
+ #
430
+ # @param key [Symbol] the key of the data to be loaded
431
+ # @return [Object] the data loaded
432
+ def load_data(key)
433
+ Synvert::Core.instance_variable_get("@#{key}")
434
+ end
435
+
408
436
  private
409
437
 
410
438
  # Handle one file.
@@ -432,5 +460,12 @@ module Synvert::Core
432
460
  def merge_test_result(result)
433
461
  @test_results[result.file_path] << result
434
462
  end
463
+
464
+ def preserve_current_parser
465
+ current_parser = @options[:parser]
466
+ yield
467
+ ensure
468
+ @options[:parser] = current_parser
469
+ end
435
470
  end
436
471
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.33.2'
5
+ VERSION = '1.34.0'
6
6
  end
7
7
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ RSpec.describe Configuration do
5
+ after do
6
+ Configuration.number_of_workers = nil
7
+ Configuration.strict = nil
8
+ end
9
+
10
+ describe '.with_temporary_configurations' do
11
+ it 'temporarily sets instance variables and restores them after block execution' do
12
+ Configuration.number_of_workers = 4
13
+ Configuration.strict = true
14
+
15
+ Configuration.with_temporary_configurations(number_of_workers: 1, strict: false) do
16
+ expect(Configuration.number_of_workers).to eq(1)
17
+ expect(Configuration.strict).to eq(false)
18
+ end
19
+
20
+ expect(Configuration.number_of_workers).to eq(4)
21
+ expect(Configuration.strict).to eq(true)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -395,6 +395,18 @@ module Synvert::Core
395
395
  expect(instance.dynamic_helper('arg1', 'arg2')).to eq 'dynamic result'
396
396
  end
397
397
 
398
+ it 'parses save_data and load_data' do
399
+ data = nil
400
+ rewriter =
401
+ Rewriter.new 'group', 'name' do
402
+ hash = { foo: 'bar' }
403
+ save_data :object, hash
404
+ data = load_data :object
405
+ end
406
+ rewriter.process
407
+ expect(data).to eq({ foo: 'bar' })
408
+ end
409
+
398
410
  describe 'class methods' do
399
411
  before :each do
400
412
  Rewriter.clear
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.33.2
4
+ version: 1.34.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: 2024-02-24 00:00:00.000000000 Z
11
+ date: 2024-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -196,6 +196,7 @@ files:
196
196
  - lib/synvert/core/version.rb
197
197
  - spec/spec_helper.rb
198
198
  - spec/support/parser_helper.rb
199
+ - spec/synvert/core/configuration_spec.rb
199
200
  - spec/synvert/core/engine/erb_spec.rb
200
201
  - spec/synvert/core/engine/haml_spec.rb
201
202
  - spec/synvert/core/engine/slim_spec.rb
@@ -242,6 +243,7 @@ summary: convert ruby code to better syntax.
242
243
  test_files:
243
244
  - spec/spec_helper.rb
244
245
  - spec/support/parser_helper.rb
246
+ - spec/synvert/core/configuration_spec.rb
245
247
  - spec/synvert/core/engine/erb_spec.rb
246
248
  - spec/synvert/core/engine/haml_spec.rb
247
249
  - spec/synvert/core/engine/slim_spec.rb