synvert-core 1.18.1 → 1.20.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: 036a5a2a642e96b611185e400b4f458be77f2622c3ef996bdac4bc0cbda1df70
4
- data.tar.gz: 51ec847622d9018176f4dceb51a2a9ac623c1129abea3012d6e2faab650ee7f2
3
+ metadata.gz: d0e41ca1ed27bdc4a8e85f022cab4f220970746e950507c51ca5634dcb0c2f5c
4
+ data.tar.gz: bc61e33e6bf1282befa1f6b67944e372d7ba309c4f47b907b6f64fd5fb677a94
5
5
  SHA512:
6
- metadata.gz: 6c739ddf2691480f5d53fe0033eb24a5c0e784116fec98eff71a58d1d1745a853790121a89979b32624fcb65ea104b837a744b9353fde8ef74ef227433e9be7f
7
- data.tar.gz: 95ad1ab6a96a33f0299aa012f7731915a7af3794b805b5e5743ddd2d11c48e29dc34019773a3948b96db94aea814d928558c0ee0409a750dae3e85c6d503ec67
6
+ metadata.gz: a1dd19db1a689441e0443c524deb067d8bd6454ae0226f3163180b74fa4633bafb27b97d48c1c7506ff114c96fb40ae94beea6d7ed65740d6eeafaef1bd65cc4
7
+ data.tar.gz: bffc0742e22250091392cf453c71e1901920f5f4d8df3cf8d71ae373c351e41c620a2bf62ec3a72f0aac8d6d6f96ef043c81b577f201244c51f52b9cf66de051
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.20.0 (2023-02-08)
4
+
5
+ * Add `Configuration#single_quote`
6
+ * Add `Instance#wrap_with_quotes`
7
+
8
+ ## 1.19.0 (2023-02-06)
9
+
10
+ * Remove `Instance#query_adapter`
11
+
3
12
  ## 1.18.1 (2023-02-05)
4
13
 
5
14
  * Fix glob with only paths
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.18.1)
4
+ synvert-core (1.20.0)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
  node_mutation (>= 1.8.2)
data/README.md CHANGED
@@ -101,9 +101,13 @@ Actions:
101
101
  * [noop](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#noop-instance_method) - no operation
102
102
  * [add_action](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#add_action-instance_method) - add custom action
103
103
 
104
+ Others:
105
+
106
+ * [wrap_with_quotes](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#wrap_with_quotes-instance_method) - wrap string code with single or doulbe quotes
107
+
108
+
104
109
  Attributes:
105
110
 
106
111
  * [file_path](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#file_path-instance_method) - current file path
107
112
  * [node](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#node-instance_method) - current ast node
108
- * [query_adapter](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#query_adapter-instance_method) - [query adapter](https://xinminlabs.github.io/node-query-ruby/NodeQuery/Adapter.html) to get some helper methods
109
113
  * [mutation_adapter](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#mutation_adapter-instance_method) - [mutation adapter](https://xinminlabs.github.io/node-mutation-ruby/NodeMutation/Adapter.html) to get some helper methods
@@ -9,7 +9,8 @@ module Synvert::Core
9
9
  # @!attribute [w] only_paths
10
10
  # @!attribute [w] show_run_process
11
11
  # @!attribute [w] number_of_workers
12
- attr_writer :root_path, :skip_paths, :only_paths, :show_run_process, :number_of_workers
12
+ # @!attribute [w] single_quote
13
+ attr_writer :root_path, :skip_paths, :only_paths, :show_run_process, :number_of_workers, :single_quote
13
14
 
14
15
  # Get the path.
15
16
  #
@@ -45,6 +46,13 @@ module Synvert::Core
45
46
  def number_of_workers
46
47
  @number_of_workers || 1
47
48
  end
49
+
50
+ # Use single quote or double quote.
51
+ #
52
+ # @return [Boolean] true if use single quote, default is true
53
+ def single_quote
54
+ @single_quote.nil? ? true : @single_quote
55
+ end
48
56
  end
49
57
  end
50
58
  end
@@ -30,11 +30,9 @@ module Synvert::Core
30
30
  # @return file path
31
31
  # @!attribute [rw] current_node
32
32
  # @return current ast node
33
- # @!attribute [r] query_adapter
34
- # @return NodeQuery Adapter
35
33
  # @!attribute [r] mutation_adapter
36
34
  # @return NodeMutation Adapter
37
- attr_reader :file_path, :current_node, :query_adapter, :mutation_adapter
35
+ attr_reader :file_path, :current_node, :mutation_adapter
38
36
  attr_accessor :current_node
39
37
 
40
38
  # Process the instance.
@@ -48,7 +46,6 @@ module Synvert::Core
48
46
  source = read_source(absolute_file_path)
49
47
  @current_mutation = NodeMutation.new(source)
50
48
  @mutation_adapter = NodeMutation.adapter
51
- @query_adapter = NodeQuery.adapter
52
49
  begin
53
50
  node = parse_code(@file_path, source)
54
51
 
@@ -85,7 +82,6 @@ module Synvert::Core
85
82
  source = read_source(absolute_file_path)
86
83
  @current_mutation = NodeMutation.new(source)
87
84
  @mutation_adapter = NodeMutation.adapter
88
- @query_adapter = NodeQuery.adapter
89
85
  begin
90
86
  node = parse_code(file_path, source)
91
87
 
@@ -414,6 +410,19 @@ module Synvert::Core
414
410
  @rewriter.add_warning Rewriter::Warning.new(self, message)
415
411
  end
416
412
 
413
+ # Wrap str string with single or doulbe quotes based on Configuration.single_quote.
414
+ # @param str [String]
415
+ # @return [String] quoted string
416
+ def wrap_with_quotes(str)
417
+ quote = Configuration.single_quote ? "'" : '"';
418
+ another_quote = Configuration.single_quote ? '"' : "'";
419
+ if str.include?(quote) && !str.include?(another_quote)
420
+ return "#{another_quote}#{str}#{another_quote}"
421
+ end
422
+ escaped_str = str.gsub(quote) { |char| '\\' + quote }
423
+ quote + escaped_str + quote
424
+ end
425
+
417
426
  private
418
427
 
419
428
  # Read file source.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.18.1'
5
+ VERSION = '1.20.0'
6
6
  end
7
7
  end
@@ -361,5 +361,38 @@ module Synvert::Core
361
361
  expect(instance.current_node).to eq node1
362
362
  end
363
363
  end
364
+
365
+ describe '#wrap_with_quotes' do
366
+ context 'Configuration.single_quote = true' do
367
+ it 'wraps with single quotes' do
368
+ expect(instance.wrap_with_quotes('foobar')).to eq "'foobar'"
369
+ end
370
+
371
+ it 'wraps with double quotes if it contains single quote' do
372
+ expect(instance.wrap_with_quotes("foo'bar")).to eq '"foo\'bar"'
373
+ end
374
+
375
+ it 'wraps with signle quotes and escapes single quote' do
376
+ expect(instance.wrap_with_quotes("foo'\"bar")).to eq "'foo\\'\"bar'"
377
+ end
378
+ end
379
+
380
+ context 'Configuration.single_quote = false' do
381
+ before { Configuration.single_quote = false }
382
+ after { Configuration.single_quote = true }
383
+
384
+ it 'wraps with double quotes' do
385
+ expect(instance.wrap_with_quotes('foobar')).to eq '"foobar"'
386
+ end
387
+
388
+ it 'wraps with single quotes if it contains double quote' do
389
+ expect(instance.wrap_with_quotes('foo"bar')).to eq "'foo\"bar'"
390
+ end
391
+
392
+ it 'wraps with double quotes and escapes double quote' do
393
+ expect(instance.wrap_with_quotes("foo'\"bar")).to eq '"foo\'\\"bar"'
394
+ end
395
+ end
396
+ end
364
397
  end
365
398
  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.18.1
4
+ version: 1.20.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: 2023-02-05 00:00:00.000000000 Z
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport