synvert-core 2.0.3 → 2.1.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: fce0541de0c11b2a281d837c23669ea6fbe768f53b551549e1693119be907a1b
4
- data.tar.gz: 413625544e125e8d9ec114e6dbe1021d2ca46969b319b56037a2fad4a5ba1c45
3
+ metadata.gz: c9a0f5e143582d26ad70583fece58a3e99251a0bb00af3e2ec1815c8b7163454
4
+ data.tar.gz: 0e6e001dc795642ddfe0a245339f4632faeb8492f377cb0739c9d0d89b573fb0
5
5
  SHA512:
6
- metadata.gz: 2695eb816b145e15644104da335f58d7038ed698f998348e35f23c0e42a0c3e119bc1feefc2cc12875d4dc26e3b9dbccd468c61e117f8e5dbd1efdebb2f95fea
7
- data.tar.gz: fe5d4072638d277b8eedbf488540bd85632118721c2092f9c6117a41f7a761e70d21a9652a7f8fd007f260885ef9930dfd9000d295f6733dc3fda73925c29aee
6
+ metadata.gz: d696b90c59582437c373ea536901c304ef2e63058cb600fd5d9bef731cf8ecc38e021799979174319ed22c1f1e888245f0a9034828953556e3ac09393b8b55af
7
+ data.tar.gz: bd3db282c2f08918279815aed52e80f8bde651f70f61f5b041094679f1092fac2fee65b92adffa58602b6738f0cd70b8bd2cc0f858c209073200cfd9950c3681
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.1.0 (2024-06-24)
4
+
5
+ * Remove `add_leading_spaces` method
6
+ * Add `indent` and `dedent` helper methods
7
+
3
8
  ## 2.0.3 (2024-06-16)
4
9
 
5
10
  * Fix `gem_spec_spec` test failures
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (2.0.3)
4
+ synvert-core (2.1.0)
5
5
  activesupport
6
6
  node_mutation (>= 1.24.4)
7
7
  node_query (>= 1.15.3)
@@ -59,7 +59,7 @@ GEM
59
59
  rb-inotify (~> 0.9, >= 0.9.10)
60
60
  lumberjack (1.2.10)
61
61
  method_source (1.1.0)
62
- minitest (5.23.1)
62
+ minitest (5.24.0)
63
63
  mutex_m (0.2.0)
64
64
  nenv (0.3.0)
65
65
  node_mutation (1.24.4)
data/README.md CHANGED
@@ -97,7 +97,6 @@ Actions:
97
97
 
98
98
  * [append](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#append-instance_method) - append the code to the bottom of current node body
99
99
  * [prepend](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#prepend-instance_method) - prepend the code to the bottom of current node body
100
- * [indent](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#indent-instance_method) - indent code
101
100
  * [insert](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert-instance_method) - insert code
102
101
  * [insert_after](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert_after-instance_method) - insert the code next to the current node
103
102
  * [insert_before](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#insert_before-instance_method) - insert the code previous to the current node
@@ -111,10 +111,11 @@ module Synvert::Core
111
111
  # # Code to be executed with temporary configurations
112
112
  # end
113
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
114
+ old_instance_variables =
115
+ instance_variables.reduce({}) do |hash, var|
116
+ hash[var] = instance_variable_get(var)
117
+ hash
118
+ end
118
119
 
119
120
  configurations.each do |variable, value|
120
121
  instance_variable_set("@#{variable}", value)
@@ -391,20 +391,6 @@ module Synvert::Core
391
391
  @current_mutation.wrap(@current_node, prefix: prefix, suffix: suffix, newline: newline)
392
392
  end
393
393
 
394
- # It indent the code of current node.
395
- # @example
396
- # # class Foobar
397
- # # end
398
- # # =>
399
- # # class Foobar
400
- # # end
401
- # within_node type: 'class' do
402
- # indent
403
- # end
404
- def indent(tab_size: 1)
405
- @current_mutation.indent(@current_node, tab_size: tab_size)
406
- end
407
-
408
394
  # No operation.
409
395
  def noop
410
396
  @current_mutation.noop(@current_node)
@@ -466,12 +452,22 @@ module Synvert::Core
466
452
  quote + escaped_str + quote
467
453
  end
468
454
 
469
- # Add leading spaces before the str according to Configuration.tab_width.
470
- # @param str [String]
471
- # @param tab_size [Integer] tab size
472
- # @return [String]
473
- def add_leading_spaces(str, tab_size: 1)
474
- (" " * Configuration.tab_width * tab_size) + str;
455
+ # Indents the given source code by the specified tab size.
456
+ #
457
+ # @param source [String] The source code to be indented.
458
+ # @param tab_size [Integer] The number of spaces per tab.
459
+ # @return [String] The indented source code.
460
+ def indent(source, tab_size: 1)
461
+ source.each_line.map { |line| (' ' * NodeMutation.tab_width * tab_size) + line }.join
462
+ end
463
+
464
+ # Dedents the given source code by removing leading spaces or tabs.
465
+ #
466
+ # @param source [String] The source code to dedent.
467
+ # @param tab_size [Integer] The number of spaces per tab (default is 1).
468
+ # @return [String] The dedented source code.
469
+ def dedent(source, tab_size: 1)
470
+ source.each_line.map { |line| line.sub(/^ {#{NodeMutation.tab_width * tab_size}}/, '') }.join
475
471
  end
476
472
 
477
473
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '2.0.3'
5
+ VERSION = '2.1.0'
6
6
  end
7
7
  end
@@ -22,4 +22,4 @@ module Synvert::Core
22
22
  end
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -235,16 +235,6 @@ module Synvert::Core
235
235
  instance.wrap prefix: 'module Foobar', suffix: 'end', newline: true
236
236
  end
237
237
 
238
- it 'parses indent' do
239
- instance.instance_variable_set(:@current_mutation, double)
240
- instance.current_node = double
241
- expect(instance.instance_variable_get(:@current_mutation)).to receive(:indent).with(
242
- instance.current_node,
243
- tab_size: 1
244
- )
245
- instance.indent
246
- end
247
-
248
238
  it 'parses noop' do
249
239
  instance.instance_variable_set(:@current_mutation, double)
250
240
  instance.current_node = double
@@ -269,7 +259,11 @@ module Synvert::Core
269
259
  it 'parsers add_callback' do
270
260
  instance.instance_variable_set(:@current_visitor, double)
271
261
  block = proc {}
272
- expect(instance.instance_variable_get(:@current_visitor)).to receive(:add_callback).with(:class_node, at: 'start', &block)
262
+ expect(instance.instance_variable_get(:@current_visitor)).to receive(:add_callback).with(
263
+ :class_node,
264
+ at: 'start',
265
+ &block
266
+ )
273
267
  instance.add_callback(:class_node, at: 'start', &block)
274
268
  end
275
269
 
@@ -671,10 +665,19 @@ module Synvert::Core
671
665
  end
672
666
  end
673
667
 
674
- describe '#add_leading_spaces' do
675
- it 'adds leading spaces' do
676
- expect(instance.add_leading_spaces('foo')).to eq ' foo';
677
- expect(instance.add_leading_spaces('foo', tab_size: 2)).to eq ' foo';
668
+ describe '#indent' do
669
+ it 'adds white spaces' do
670
+ old_code = "def foo\n bar\nend"
671
+ new_code = instance.indent(old_code, tab_size: 2)
672
+ expect(new_code).to eq " def foo\n bar\n end"
673
+ end
674
+ end
675
+
676
+ describe '#dedent' do
677
+ it 'removes white spaces' do
678
+ old_code = " def foo\n bar\n end"
679
+ new_code = instance.dedent(old_code, tab_size: 2)
680
+ expect(new_code).to eq "def foo\n bar\nend"
678
681
  end
679
682
  end
680
683
  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: 2.0.3
4
+ version: 2.1.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-06-16 00:00:00.000000000 Z
11
+ date: 2024-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -249,7 +249,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
251
  requirements: []
252
- rubygems_version: 3.5.9
252
+ rubygems_version: 3.5.13
253
253
  signing_key:
254
254
  specification_version: 4
255
255
  summary: convert ruby code to better syntax.