synvert-core 0.39.0 → 0.40.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: bd079f46cc8d4a8097a794b11c02e98294c217d801538e4c9dafa4305fd3b6d8
4
- data.tar.gz: 81018b9a711fca607aa4b2dc3170e9172531140392172b5cd27d4118383a6c31
3
+ metadata.gz: 5c729d2f456550a622a39f2e43d6d6c7f701f8b1330af038c436a03ec162d828
4
+ data.tar.gz: 8f5e7642d9073bfa38798dbd237a416037047991d530f4ff862623007ffa81f7
5
5
  SHA512:
6
- metadata.gz: 78fd6e0b7b28661b5d352a6835f35b713ca829a2a9ae4260e605541b00e9eb151b66b45018a954e1b98682b45bbbddaac359a582281904dd784b2004ded00c9b
7
- data.tar.gz: 6a4a33419eafeac609f92d68593ee12b5e5608c24865289cd53f6bb631ef047dc83f1cb7648984d70fded95c9141b6c4a58056280675b55f321836e2b3005dbd
6
+ metadata.gz: 1187929417e2611b85d95dad5cc2f985d86cc4a1638d0ebecc4baeacc8dfa124ed5ec6595845a941fe7fe13c1666255333aa9f4249f297899c84ab8518a1b809
7
+ data.tar.gz: 729ff991e4a120d655faaa5517915d6d2614396668f08268a93b70c86f00ce038ec12ff09cf7d5cc060e34b7af8604689f3e4948b32a26bf4fc85ae520da1d21
data/CHANGELOG.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.40.0 (2021-06-23)
4
+
5
+ * Rewrite `insert` action
6
+
3
7
  ## 0.39.0 (2021-06-23)
4
8
 
5
- * Add `prepend` dsl instead of `insert`
9
+ * Add `prepend` action instead of `insert`
6
10
 
7
11
  ## 0.38.0 (2021-06-21)
8
12
 
@@ -1,26 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
- # InsertAction to insert code to the top of node body.
4
+ # AddAction to add code to the node.
5
5
  class Rewriter::InsertAction < Rewriter::Action
6
- DO_LENGTH = ' do'.length
7
-
8
6
  # Begin position to insert code.
9
7
  #
10
8
  # @return [Integer] begin position.
11
9
  def begin_pos
12
- case @node.type
13
- when :block
14
- if @node.children[1].children.empty?
15
- @node.children[0].loc.expression.end_pos + DO_LENGTH
16
- else
17
- @node.children[1].loc.expression.end_pos
18
- end
19
- when :class
20
- @node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
21
- else
22
- @node.children.last.loc.expression.end_pos
23
- end
10
+ @node.loc.expression.end_pos
24
11
  end
25
12
 
26
13
  # End position, always same to begin position.
@@ -30,18 +17,11 @@ module Synvert::Core
30
17
  begin_pos
31
18
  end
32
19
 
33
- private
34
-
35
- # Indent of the node.
20
+ # The rewritten source code.
36
21
  #
37
- # @param node [Parser::AST::Node]
38
- # @return [String] n times whitesphace
39
- def indent(node)
40
- if %i[block class].include? node.type
41
- ' ' * (node.indent + DEFAULT_INDENT)
42
- else
43
- ' ' * node.indent
44
- end
22
+ # @return [String] rewritten code.
23
+ def rewritten_code
24
+ rewritten_source
45
25
  end
46
26
  end
47
27
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.39.0'
5
+ VERSION = '0.40.0'
6
6
  end
7
7
  end
@@ -4,88 +4,23 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::InsertAction do
7
- describe 'block node without args' do
8
- subject {
9
- source = "Synvert::Application.configure do\nend"
10
- block_node = Parser::CurrentRuby.parse(source)
11
- instance = double(current_node: block_node)
12
- Rewriter::InsertAction.new(instance, 'config.eager_load = true')
13
- }
14
-
15
- it 'gets begin_pos' do
16
- expect(subject.begin_pos).to eq 'Synvert::Application.configure do'.length
17
- end
18
-
19
- it 'gets end_pos' do
20
- expect(subject.end_pos).to eq 'Synvert::Application.configure do'.length
21
- end
22
-
23
- it 'gets rewritten_code' do
24
- expect(subject.rewritten_code).to eq "\n config.eager_load = true"
25
- end
26
- end
27
-
28
- describe 'block node with args' do
29
- subject {
30
- source = "RSpec.configure do |config|\nend"
31
- block_node = Parser::CurrentRuby.parse(source)
32
- instance = double(current_node: block_node)
33
- Rewriter::InsertAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
34
- }
35
-
36
- it 'gets begin_pos' do
37
- expect(subject.begin_pos).to eq 'RSpec.configure do |config|'.length
38
- end
39
-
40
- it 'gets end_pos' do
41
- expect(subject.end_pos).to eq 'RSpec.configure do |config|'.length
42
- end
43
-
44
- it 'gets rewritten_code' do
45
- expect(subject.rewritten_code).to eq "\n config.include FactoryGirl::Syntax::Methods"
46
- end
7
+ subject {
8
+ source = "User.where(username: 'Richard')"
9
+ node = Parser::CurrentRuby.parse(source)
10
+ instance = double(current_node: node)
11
+ Rewriter::InsertAction.new(instance, '.first')
12
+ }
13
+
14
+ it 'gets begin_pos' do
15
+ expect(subject.begin_pos).to eq "User.where(username: 'Richard')".length
47
16
  end
48
17
 
49
- describe 'class node without superclass' do
50
- subject {
51
- source = "class User\n has_many :posts\nend"
52
- class_node = Parser::CurrentRuby.parse(source)
53
- instance = double(current_node: class_node)
54
- Rewriter::InsertAction.new(instance, 'include Deletable')
55
- }
56
-
57
- it 'gets begin_pos' do
58
- expect(subject.begin_pos).to eq 'class User'.length
59
- end
60
-
61
- it 'gets end_pos' do
62
- expect(subject.end_pos).to eq 'class User'.length
63
- end
64
-
65
- it 'gets rewritten_code' do
66
- expect(subject.rewritten_code).to eq "\n include Deletable"
67
- end
18
+ it 'gets end_pos' do
19
+ expect(subject.end_pos).to eq "User.where(username: 'Richard')".length
68
20
  end
69
21
 
70
- describe 'class node with superclass' do
71
- subject {
72
- source = "class User < ActiveRecord::Base\n has_many :posts\nend"
73
- class_node = Parser::CurrentRuby.parse(source)
74
- instance = double(current_node: class_node)
75
- Rewriter::InsertAction.new(instance, 'include Deletable')
76
- }
77
-
78
- it 'gets begin_pos' do
79
- expect(subject.begin_pos).to eq 'class User < ActionRecord::Base'.length
80
- end
81
-
82
- it 'gets end_pos' do
83
- expect(subject.end_pos).to eq 'class User < ActionRecord::Base'.length
84
- end
85
-
86
- it 'gets rewritten_code' do
87
- expect(subject.rewritten_code).to eq "\n include Deletable"
88
- end
22
+ it 'gets rewritten_code' do
23
+ expect(subject.rewritten_code).to eq ".first"
89
24
  end
90
25
  end
91
26
  end
@@ -106,10 +106,10 @@ module Synvert::Core
106
106
  it 'parses insert' do
107
107
  expect(Rewriter::InsertAction).to receive(:new).with(
108
108
  instance,
109
- '{{arguments.first}}.include FactoryGirl::Syntax::Methods',
109
+ '.first',
110
110
  {}
111
111
  )
112
- instance.insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
112
+ instance.insert '.first'
113
113
  end
114
114
 
115
115
  it 'parses insert_after' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.39.0
4
+ version: 0.40.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -146,7 +146,6 @@ files:
146
146
  - lib/synvert/core/node_ext.rb
147
147
  - lib/synvert/core/rewriter.rb
148
148
  - lib/synvert/core/rewriter/action.rb
149
- - lib/synvert/core/rewriter/action/add_action.rb
150
149
  - lib/synvert/core/rewriter/action/append_action.rb
151
150
  - lib/synvert/core/rewriter/action/delete_action.rb
152
151
  - lib/synvert/core/rewriter/action/insert_action.rb
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # AddAction to add code to the node.
5
- class Rewriter::AddAction < Rewriter::Action
6
- # Begin position to insert code.
7
- #
8
- # @return [Integer] begin position.
9
- def begin_pos
10
- @node.loc.expression.end_pos
11
- end
12
-
13
- # End position, always same to begin position.
14
- #
15
- # @return [Integer] end position.
16
- def end_pos
17
- begin_pos
18
- end
19
-
20
- private
21
-
22
- # The rewritten source code.
23
- #
24
- # @return [String] rewritten code.
25
- def rewritten_code
26
- rewritten_source
27
- end
28
- end
29
- end