synvert-core 2.0.2 → 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: 666eb48f4d58bd41854a7f48e9aadf201321023f546b5682abe21e56ed80a204
4
- data.tar.gz: b6a555693e36b13806d156761a7003e06a6be8104c6102fd11ac5e920a687dd2
3
+ metadata.gz: c9a0f5e143582d26ad70583fece58a3e99251a0bb00af3e2ec1815c8b7163454
4
+ data.tar.gz: 0e6e001dc795642ddfe0a245339f4632faeb8492f377cb0739c9d0d89b573fb0
5
5
  SHA512:
6
- metadata.gz: 24b42ba9dd6ba2e16fc4218fbae4f333d8ac193fa4c58de8781836f4b3c7189a342a15088364071b52edf955365f0920bc4773d62f7d36e88c69b50e03f6df60
7
- data.tar.gz: '081411c60033db6c980fe7204e0e7dde12c7110a5839c545bb59943e3da3e881a0f505cc521284f6753e8f34d47319a9e1bc582126ea27cf82e4b696b170ce3f'
6
+ metadata.gz: d696b90c59582437c373ea536901c304ef2e63058cb600fd5d9bef731cf8ecc38e021799979174319ed22c1f1e888245f0a9034828953556e3ac09393b8b55af
7
+ data.tar.gz: bd3db282c2f08918279815aed52e80f8bde651f70f61f5b041094679f1092fac2fee65b92adffa58602b6738f0cd70b8bd2cc0f858c209073200cfd9950c3681
@@ -1,5 +1,21 @@
1
1
  {
2
2
  "cSpell.words": [
3
- "synvert"
3
+ "asgn",
4
+ "autoindent",
5
+ "blockarg",
6
+ "casgn",
7
+ "csend",
8
+ "cvar",
9
+ "erange",
10
+ "fileutils",
11
+ "haml",
12
+ "ivar",
13
+ "lvar",
14
+ "mlhs",
15
+ "restarg",
16
+ "rvmrc",
17
+ "synvert",
18
+ "Yardoc",
19
+ "zsuper"
4
20
  ]
5
21
  }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
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
+
8
+ ## 2.0.3 (2024-06-16)
9
+
10
+ * Fix `gem_spec_spec` test failures
11
+ * Use `NodeMutation` to fetch file line
12
+
3
13
  ## 2.0.2 (2024-05-30)
4
14
 
5
15
  * Do not raise error for prism
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (2.0.2)
4
+ synvert-core (2.1.0)
5
5
  activesupport
6
6
  node_mutation (>= 1.24.4)
7
7
  node_query (>= 1.15.3)
@@ -17,7 +17,7 @@ PATH
17
17
  GEM
18
18
  remote: https://rubygems.org/
19
19
  specs:
20
- activesupport (7.1.3.3)
20
+ activesupport (7.1.3.4)
21
21
  base64
22
22
  bigdecimal
23
23
  concurrent-ruby (~> 1.0, >= 1.0.2)
@@ -31,7 +31,7 @@ GEM
31
31
  base64 (0.2.0)
32
32
  bigdecimal (3.1.8)
33
33
  coderay (1.1.3)
34
- concurrent-ruby (1.3.1)
34
+ concurrent-ruby (1.3.3)
35
35
  connection_pool (2.4.1)
36
36
  diff-lcs (1.5.1)
37
37
  drb (2.2.1)
@@ -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)
@@ -68,14 +68,14 @@ GEM
68
68
  notiffany (0.1.3)
69
69
  nenv (~> 0.1)
70
70
  shellany (~> 0.0)
71
- parallel (1.24.0)
72
- parser (3.3.1.0)
71
+ parallel (1.25.1)
72
+ parser (3.3.3.0)
73
73
  ast (~> 2.4.1)
74
74
  racc
75
75
  parser_node_ext (1.3.2)
76
76
  parser
77
77
  prettier_print (1.2.1)
78
- prism (0.29.0)
78
+ prism (0.30.0)
79
79
  prism_ext (0.3.2)
80
80
  prism
81
81
  pry (0.14.2)
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)
@@ -436,7 +422,8 @@ module Synvert::Core
436
422
  # end
437
423
  # @param message [String] warning message.
438
424
  def warn(message)
439
- @rewriter.add_warning Rewriter::Warning.new(self, message)
425
+ line = @current_mutation.adapter.get_start_loc(@current_node).line
426
+ @rewriter.add_warning Rewriter::Warning.new(@file_path, line, message)
440
427
  end
441
428
 
442
429
  # It adds a callback when visiting an ast node.
@@ -465,12 +452,22 @@ module Synvert::Core
465
452
  quote + escaped_str + quote
466
453
  end
467
454
 
468
- # Add leading spaces before the str according to Configuration.tab_width.
469
- # @param str [String]
470
- # @param tab_size [Integer] tab size
471
- # @return [String]
472
- def add_leading_spaces(str, tab_size: 1)
473
- (" " * 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
474
471
  end
475
472
 
476
473
  private
@@ -5,11 +5,12 @@ module Synvert::Core
5
5
  class Rewriter::Warning
6
6
  # Initialize a Warning.
7
7
  #
8
- # @param instance [Synvert::Core::Rewriter::Instance]
8
+ # @param file_path [String] file path.
9
+ # @param line [Integer] file line.
9
10
  # @param message [String] warning message.
10
- def initialize(instance, message)
11
- @file_path = instance.file_path
12
- @line = instance.current_node.loc.expression.line
11
+ def initialize(file_path, line, message)
12
+ @file_path = file_path
13
+ @line = line
13
14
  @message = message
14
15
  end
15
16
 
@@ -201,7 +201,7 @@ module Synvert::Core
201
201
  # @option strategy [String] 'allow_insert_at_same_position'
202
202
  def configure(options)
203
203
  @options = @options.merge(options)
204
- if options[:parser] && ![Synvert::PARSER_PARSER, Synvert::SYNTAX_TREE_PARSER, Synvert::PRISM_PARSER].include?(options[:parser])
204
+ if options[:parser] && !Synvert::ALL_PARSERS.include?(options[:parser])
205
205
  raise Errors::ParserNotSupported.new("Parser #{options[:parser]} not supported")
206
206
  end
207
207
  end
@@ -40,7 +40,7 @@ module Synvert::Core
40
40
  ignored_files = []
41
41
 
42
42
  if Configuration.respect_gitignore
43
- Open3.popen3('git check-ignore --stdin') do |stdin, stdout, stderr, wait_thr|
43
+ Open3.popen3('git check-ignore --stdin') do |stdin, stdout, _stderr, _wait_thr|
44
44
  stdin.puts(all_files.join("\n"))
45
45
  stdin.close
46
46
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '2.0.2'
5
+ VERSION = '2.1.0'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -77,4 +77,5 @@ module Synvert
77
77
  PARSER_PARSER = 'parser'
78
78
  SYNTAX_TREE_PARSER = 'syntax_tree'
79
79
  PRISM_PARSER = 'prism'
80
+ ALL_PARSERS = [PARSER_PARSER, SYNTAX_TREE_PARSER, PRISM_PARSER].freeze
80
81
  end
@@ -22,4 +22,4 @@ module Synvert::Core
22
22
  end
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -16,7 +16,17 @@ module Synvert::Core
16
16
  slop (3.4.7)
17
17
  EOS
18
18
  let(:lock_path) { File.absolute_path('./Gemfile.lock') }
19
- before { allow(File).to receive(:exist?).with(File.join(ENV['HOME'], '.gem/specs')).and_return(false) }
19
+
20
+ before do
21
+ Configuration.root_path = File.dirname(lock_path)
22
+ allow(File).to receive(:exist?).with(File.join(ENV['HOME'])).and_return(true)
23
+ allow(File).to receive(:exist?).with(File.join(ENV['HOME'], '.gem')).and_return(false)
24
+ allow(File).to receive(:exist?).with(File.join(ENV['HOME'], '.gem/specs')).and_return(false)
25
+ end
26
+
27
+ after do
28
+ Configuration.root_path = nil
29
+ end
20
30
 
21
31
  it 'returns true if version in Gemfile.lock is greater than definition' do
22
32
  expect(File).to receive(:exist?).with(lock_path).and_return(true)
@@ -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
@@ -260,14 +250,20 @@ module Synvert::Core
260
250
  end
261
251
 
262
252
  it 'parses warn' do
263
- expect(Rewriter::Warning).to receive(:new).with(instance, 'foobar')
253
+ instance.instance_variable_set(:@file_path, 'app/test.rb')
254
+ expect(@current_mutation).to receive_message_chain(:adapter, :get_start_loc, :line).and_return(2)
255
+ expect(Rewriter::Warning).to receive(:new).with('app/test.rb', 2, 'foobar')
264
256
  instance.warn 'foobar'
265
257
  end
266
258
 
267
259
  it 'parsers add_callback' do
268
260
  instance.instance_variable_set(:@current_visitor, double)
269
261
  block = proc {}
270
- 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
+ )
271
267
  instance.add_callback(:class_node, at: 'start', &block)
272
268
  end
273
269
 
@@ -549,10 +545,6 @@ module Synvert::Core
549
545
  = form_for @post do |f|
550
546
  = form_for @post do |f|
551
547
  EOS
552
- output = <<~EOS
553
- = form_for post do |f|
554
- = form_for post do |f|
555
- EOS
556
548
  allow(File).to receive(:read).with('./app/views/posts/_form.html.haml', encoding: 'UTF-8').and_return(input)
557
549
  result = instance.test
558
550
  expect(result.file_path).to eq 'app/views/posts/_form.html.haml'
@@ -578,10 +570,6 @@ module Synvert::Core
578
570
  = form_for @post do |f|
579
571
  = form_for @post do |f|
580
572
  EOS
581
- output = <<~EOS
582
- = form_for post do |f|
583
- = form_for post do |f|
584
- EOS
585
573
  allow(File).to receive(:read).with('./app/views/posts/_form.html.slim', encoding: 'UTF-8').and_return(input)
586
574
  result = instance.test
587
575
  expect(result.file_path).to eq 'app/views/posts/_form.html.slim'
@@ -677,10 +665,19 @@ module Synvert::Core
677
665
  end
678
666
  end
679
667
 
680
- describe '#add_leading_spaces' do
681
- it 'adds leading spaces' do
682
- expect(instance.add_leading_spaces('foo')).to eq ' foo';
683
- 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"
684
681
  end
685
682
  end
686
683
  end
@@ -5,10 +5,7 @@ require 'spec_helper'
5
5
  module Synvert::Core
6
6
  describe Rewriter::Warning do
7
7
  subject {
8
- source = "def test\n debugger\nend"
9
- send_node = Parser::CurrentRuby.parse(source).body.first
10
- instance = double(current_node: send_node, file_path: 'app/test.rb')
11
- Rewriter::Warning.new(instance, 'remove debugger')
8
+ Rewriter::Warning.new('app/test.rb', 2, 'remove debugger')
12
9
  }
13
10
 
14
11
  it 'gets message with filename and line number' do
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.2
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-05-30 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.