synvert-core 1.19.0 → 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: 8a431b26d98c476c389746269aade183f241a3c1cb2fabab27b87b2b0689510f
4
- data.tar.gz: 61b1907dbadbef95ca5d8869b6c27f65e43f72dccd0f64ead4ad3cf15a5f9c01
3
+ metadata.gz: d0e41ca1ed27bdc4a8e85f022cab4f220970746e950507c51ca5634dcb0c2f5c
4
+ data.tar.gz: bc61e33e6bf1282befa1f6b67944e372d7ba309c4f47b907b6f64fd5fb677a94
5
5
  SHA512:
6
- metadata.gz: e3ee5356583c8450635e8296657860e7016a8dfe0214fb2518da72aa2631e7511672802e326553bd44b3de1dea7e683a0784ff58770c641dae76ab9bca86e317
7
- data.tar.gz: d7426a0450fbfaf871c8083aa4d06b1a1bf439436ed964cfb8e2325e832a05b479b1f81ae71c2b3b103c614a02b7eb23ddfc4ac6311f14b7e161a54893bb040b
6
+ metadata.gz: a1dd19db1a689441e0443c524deb067d8bd6454ae0226f3163180b74fa4633bafb27b97d48c1c7506ff114c96fb40ae94beea6d7ed65740d6eeafaef1bd65cc4
7
+ data.tar.gz: bffc0742e22250091392cf453c71e1901920f5f4d8df3cf8d71ae373c351e41c620a2bf62ec3a72f0aac8d6d6f96ef043c81b577f201244c51f52b9cf66de051
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
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
+
3
8
  ## 1.19.0 (2023-02-06)
4
9
 
5
10
  * Remove `Instance#query_adapter`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.19.0)
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,6 +101,11 @@ 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
@@ -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
@@ -410,6 +410,19 @@ module Synvert::Core
410
410
  @rewriter.add_warning Rewriter::Warning.new(self, message)
411
411
  end
412
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
+
413
426
  private
414
427
 
415
428
  # Read file source.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.19.0'
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.19.0
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-06 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