synvert-core 0.48.0 → 0.51.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/lib/synvert/core/node_ext.rb +6 -4
- data/lib/synvert/core/rewriter.rb +15 -4
- data/lib/synvert/core/rewriter/action/wrap_action.rb +39 -0
- data/lib/synvert/core/rewriter/instance.rb +9 -0
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +18 -2
- data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +31 -0
- data/spec/synvert/core/rewriter/instance_spec.rb +5 -0
- data/spec/synvert/core/rewriter_spec.rb +9 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc8d09c26c005557ec18ab8d877a8cbf5af6ba0f9de0ab44efe40298419647fa
|
4
|
+
data.tar.gz: abcc824dfbf93e3aab1d3d3e33dd3862886015b899b79ce7ceb97066d2b47e85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f46377a1cc5b7eb69bb69608b3f018eae1e4cd2f4aed6bf3e3200bfa1f1fa9527829ba2a64fb0c9dd909e0125028f6fef15100fb76b0a88acfb4f8c6b9e33879
|
7
|
+
data.tar.gz: 3de1571672cff28c01937d4f1281873592847578367320123d32e657429634cde392000b9400124be0ae90f3d896627763864eaabe7539df13e98886b1c00ce1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.51.0 (2021-08-12)
|
4
|
+
|
5
|
+
* Add `wrap` action
|
6
|
+
* Add new dsl `redo_until_no_change`
|
7
|
+
|
8
|
+
## 0.50.0 (2021-08-11)
|
9
|
+
|
10
|
+
* Support `:module` in `body`
|
11
|
+
* Fix symbol match
|
12
|
+
|
13
|
+
## 0.49.0 (2021-08-04)
|
14
|
+
|
15
|
+
* Support :erange in to_value
|
16
|
+
* Do not use to_value in match_value?
|
17
|
+
|
3
18
|
## 0.48.0 (2021-08-01)
|
4
19
|
|
5
20
|
* Force to read file as utf-8
|
@@ -110,7 +110,7 @@ module Parser::AST
|
|
110
110
|
case type
|
111
111
|
when :begin
|
112
112
|
children
|
113
|
-
when :def, :block, :class
|
113
|
+
when :def, :block, :class, :module
|
114
114
|
return [] if children[2].nil?
|
115
115
|
|
116
116
|
:begin == children[2].type ? children[2].body : children[2..-1]
|
@@ -252,6 +252,8 @@ module Parser::AST
|
|
252
252
|
children.map(&:to_value)
|
253
253
|
when :irange
|
254
254
|
(children.first.to_value..children.last.to_value)
|
255
|
+
when :erange
|
256
|
+
(children.first.to_value...children.last.to_value)
|
255
257
|
when :begin
|
256
258
|
children.first.to_value
|
257
259
|
else
|
@@ -548,14 +550,14 @@ module Parser::AST
|
|
548
550
|
case expected
|
549
551
|
when Symbol
|
550
552
|
if actual.is_a?(Parser::AST::Node)
|
551
|
-
actual.
|
553
|
+
actual.to_source == ":#{expected}" || actual.to_source == expected.to_s
|
552
554
|
else
|
553
555
|
actual.to_sym == expected
|
554
556
|
end
|
555
557
|
when String
|
556
558
|
if actual.is_a?(Parser::AST::Node)
|
557
|
-
actual.to_source == expected || actual.
|
558
|
-
actual.to_source ==
|
559
|
+
actual.to_source == expected || actual.to_source == unwrap_quote(expected) ||
|
560
|
+
unwrap_quote(actual.to_source) == expected || unwrap_quote(actual.to_source) == unwrap_quote(expected)
|
559
561
|
else
|
560
562
|
actual.to_s == expected || wrap_quote(actual.to_s) == expected
|
561
563
|
end
|
@@ -18,14 +18,15 @@ module Synvert::Core
|
|
18
18
|
class Rewriter
|
19
19
|
autoload :Action, 'synvert/core/rewriter/action'
|
20
20
|
autoload :AppendAction, 'synvert/core/rewriter/action/append_action'
|
21
|
-
autoload :
|
21
|
+
autoload :DeleteAction, 'synvert/core/rewriter/action/delete_action'
|
22
22
|
autoload :InsertAction, 'synvert/core/rewriter/action/insert_action'
|
23
23
|
autoload :InsertAfterAction, 'synvert/core/rewriter/action/insert_after_action'
|
24
|
-
autoload :
|
24
|
+
autoload :RemoveAction, 'synvert/core/rewriter/action/remove_action'
|
25
|
+
autoload :PrependAction, 'synvert/core/rewriter/action/prepend_action'
|
25
26
|
autoload :ReplaceAction, 'synvert/core/rewriter/action/replace_action'
|
26
27
|
autoload :ReplaceErbStmtWithExprAction, 'synvert/core/rewriter/action/replace_erb_stmt_with_expr_action'
|
27
|
-
autoload :
|
28
|
-
autoload :
|
28
|
+
autoload :ReplaceWithAction, 'synvert/core/rewriter/action/replace_with_action'
|
29
|
+
autoload :WrapAction, 'synvert/core/rewriter/action/wrap_action'
|
29
30
|
|
30
31
|
autoload :Warning, 'synvert/core/rewriter/warning'
|
31
32
|
|
@@ -175,13 +176,18 @@ module Synvert::Core
|
|
175
176
|
@sub_snippets = []
|
176
177
|
@warnings = []
|
177
178
|
@affected_files = Set.new
|
179
|
+
@redo_until_no_change = false
|
178
180
|
self.class.register(@group, @name, self)
|
179
181
|
end
|
180
182
|
|
181
183
|
# Process the rewriter.
|
182
184
|
# It will call the block.
|
183
185
|
def process
|
186
|
+
@affected_files = Set.new
|
184
187
|
instance_eval(&@block)
|
188
|
+
if !@affected_files.empty? && @redo_until_no_change
|
189
|
+
process
|
190
|
+
end
|
185
191
|
end
|
186
192
|
|
187
193
|
# Process rewriter with sandbox mode.
|
@@ -314,5 +320,10 @@ module Synvert::Core
|
|
314
320
|
@todo
|
315
321
|
end
|
316
322
|
end
|
323
|
+
|
324
|
+
# Rerun the snippet until no change.
|
325
|
+
def redo_until_no_change
|
326
|
+
@redo_until_no_change = true
|
327
|
+
end
|
317
328
|
end
|
318
329
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
# WrapAction to warp node within a block, class or module.
|
5
|
+
#
|
6
|
+
# Note: if WrapAction is conflicted with another action (begin_pos and end_pos are overlapped),
|
7
|
+
# we have to put those 2 actions into 2 within_file scopes.
|
8
|
+
class Rewriter::WrapAction < Rewriter::Action
|
9
|
+
def initialize(instance, with:, indent: nil)
|
10
|
+
@instance = instance
|
11
|
+
@code = with
|
12
|
+
@node = @instance.current_node
|
13
|
+
@indent = indent || @node.indent
|
14
|
+
end
|
15
|
+
|
16
|
+
# Begin position of code to wrap.
|
17
|
+
#
|
18
|
+
# @return [Integer] begin position.
|
19
|
+
def begin_pos
|
20
|
+
@node.loc.expression.begin_pos
|
21
|
+
end
|
22
|
+
|
23
|
+
# End position of code to wrap.
|
24
|
+
#
|
25
|
+
# @return [Integer] end position.
|
26
|
+
def end_pos
|
27
|
+
@node.loc.expression.end_pos
|
28
|
+
end
|
29
|
+
|
30
|
+
# The rewritten source code.
|
31
|
+
#
|
32
|
+
# @return [String] rewritten code.
|
33
|
+
def rewritten_code
|
34
|
+
"#{@code}\n#{' ' * @indent}" +
|
35
|
+
@node.to_source.split("\n").map { |line| " #{line}" }.join("\n") +
|
36
|
+
"\n#{' ' * @indent}end"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -279,6 +279,15 @@ module Synvert::Core
|
|
279
279
|
@actions << Rewriter::DeleteAction.new(self, *selectors)
|
280
280
|
end
|
281
281
|
|
282
|
+
# Parse wrap with dsl, it creates a [Synvert::Core::Rewriter::WrapAction] to
|
283
|
+
# wrap current node with code.
|
284
|
+
#
|
285
|
+
# @param with [String] code need to be wrapped with.
|
286
|
+
# @param indent [Integer] number of whitespaces.
|
287
|
+
def wrap(with:, indent: nil)
|
288
|
+
@actions << Rewriter::WrapAction.new(self, with: with, indent: indent)
|
289
|
+
end
|
290
|
+
|
282
291
|
# Parse warn dsl, it creates a [Synvert::Core::Rewriter::Warning] to save warning message.
|
283
292
|
#
|
284
293
|
# @param message [String] warning message.
|
data/lib/synvert/core/version.rb
CHANGED
@@ -151,6 +151,11 @@ describe Parser::AST::Node do
|
|
151
151
|
expect(node.body).to be_empty
|
152
152
|
end
|
153
153
|
|
154
|
+
it 'gets empty for module node' do
|
155
|
+
node = parse('module Admin; end')
|
156
|
+
expect(node.body).to be_empty
|
157
|
+
end
|
158
|
+
|
154
159
|
it 'gets one line for class node' do
|
155
160
|
node = parse('class User; attr_accessor :email; end')
|
156
161
|
expect(node.body).to eq [parse('attr_accessor :email')]
|
@@ -316,18 +321,23 @@ describe Parser::AST::Node do
|
|
316
321
|
expect(node.to_value).to eq :str
|
317
322
|
end
|
318
323
|
|
319
|
-
it '
|
324
|
+
it 'gets for boolean' do
|
320
325
|
node = parse('true')
|
321
326
|
expect(node.to_value).to be_truthy
|
322
327
|
node = parse('false')
|
323
328
|
expect(node.to_value).to be_falsey
|
324
329
|
end
|
325
330
|
|
326
|
-
it '
|
331
|
+
it 'gets for irange' do
|
327
332
|
node = parse('(1..10)')
|
328
333
|
expect(node.to_value).to eq(1..10)
|
329
334
|
end
|
330
335
|
|
336
|
+
it 'gets for erange' do
|
337
|
+
node = parse('(1...10)')
|
338
|
+
expect(node.to_value).to eq(1...10)
|
339
|
+
end
|
340
|
+
|
331
341
|
it 'gets for array' do
|
332
342
|
node = parse("['str', :str]")
|
333
343
|
expect(node.to_value).to eq ['str', :str]
|
@@ -418,6 +428,12 @@ describe Parser::AST::Node do
|
|
418
428
|
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: [:user])
|
419
429
|
end
|
420
430
|
|
431
|
+
it 'matches pair key with symbol' do
|
432
|
+
source = '{ type: :model }'
|
433
|
+
node = parse(source).children[0]
|
434
|
+
expect(node).to be_match(type: 'pair', key: :type)
|
435
|
+
end
|
436
|
+
|
421
437
|
it 'matches assign number' do
|
422
438
|
source = 'at_least(0)'
|
423
439
|
node = parse(source)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Synvert::Core
|
6
|
+
describe Rewriter::WrapAction do
|
7
|
+
subject {
|
8
|
+
source = "class Bar\nend"
|
9
|
+
node = Parser::CurrentRuby.parse(source)
|
10
|
+
instance = double(current_node: node)
|
11
|
+
Rewriter::WrapAction.new(instance, with: 'module Foo')
|
12
|
+
}
|
13
|
+
|
14
|
+
it 'gets begin_pos' do
|
15
|
+
expect(subject.begin_pos).to eq 0
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'gets end_pos' do
|
19
|
+
expect(subject.end_pos).to eq "class Bar\nend".length
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'gets rewritten_code' do
|
23
|
+
expect(subject.rewritten_code).to eq <<~EOS.strip
|
24
|
+
module Foo
|
25
|
+
class Bar
|
26
|
+
end
|
27
|
+
end
|
28
|
+
EOS
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -140,6 +140,11 @@ module Synvert::Core
|
|
140
140
|
instance.delete :dot, :message
|
141
141
|
end
|
142
142
|
|
143
|
+
it 'parses wrap with' do
|
144
|
+
expect(Rewriter::WrapAction).to receive(:new).with(instance, with: 'module Foo', indent: nil)
|
145
|
+
instance.wrap with: 'module Foo'
|
146
|
+
end
|
147
|
+
|
143
148
|
it 'parses warn' do
|
144
149
|
expect(Rewriter::Warning).to receive(:new).with(instance, 'foobar')
|
145
150
|
instance.warn 'foobar'
|
@@ -214,6 +214,15 @@ module Synvert::Core
|
|
214
214
|
expect(rewriter.todo).to eq "this rewriter doesn't do blah blah blah"
|
215
215
|
end
|
216
216
|
|
217
|
+
it 'parses redo_until_no_change' do
|
218
|
+
rewriter =
|
219
|
+
Rewriter.new 'group', 'name' do
|
220
|
+
redo_until_no_change
|
221
|
+
end
|
222
|
+
rewriter.process
|
223
|
+
expect(rewriter.instance_variable_get('@redo_until_no_change')).to be_truthy
|
224
|
+
end
|
225
|
+
|
217
226
|
describe 'class methods' do
|
218
227
|
before :each do
|
219
228
|
Rewriter.clear
|
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: 0.
|
4
|
+
version: 0.51.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: 2021-08-
|
11
|
+
date: 2021-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -155,6 +155,7 @@ files:
|
|
155
155
|
- lib/synvert/core/rewriter/action/replace_action.rb
|
156
156
|
- lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
|
157
157
|
- lib/synvert/core/rewriter/action/replace_with_action.rb
|
158
|
+
- lib/synvert/core/rewriter/action/wrap_action.rb
|
158
159
|
- lib/synvert/core/rewriter/any_value.rb
|
159
160
|
- lib/synvert/core/rewriter/condition.rb
|
160
161
|
- lib/synvert/core/rewriter/condition/if_exist_condition.rb
|
@@ -182,6 +183,7 @@ files:
|
|
182
183
|
- spec/synvert/core/rewriter/action/replace_action_spec.rb
|
183
184
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
184
185
|
- spec/synvert/core/rewriter/action/replace_with_action_spec.rb
|
186
|
+
- spec/synvert/core/rewriter/action/wrap_action_spec.rb
|
185
187
|
- spec/synvert/core/rewriter/action_spec.rb
|
186
188
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|
187
189
|
- spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb
|
@@ -234,6 +236,7 @@ test_files:
|
|
234
236
|
- spec/synvert/core/rewriter/action/replace_action_spec.rb
|
235
237
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
236
238
|
- spec/synvert/core/rewriter/action/replace_with_action_spec.rb
|
239
|
+
- spec/synvert/core/rewriter/action/wrap_action_spec.rb
|
237
240
|
- spec/synvert/core/rewriter/action_spec.rb
|
238
241
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|
239
242
|
- spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb
|