synvert-core 1.22.1 → 1.23.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 +4 -4
- data/CHANGELOG.md +13 -0
- data/Gemfile.lock +4 -6
- data/lib/synvert/core/engine/erb.rb +14 -3
- data/lib/synvert/core/engine/haml.rb +84 -0
- data/lib/synvert/core/engine.rb +31 -0
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +8 -2
- data/lib/synvert/core/rewriter/instance.rb +12 -6
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +3 -3
- data/spec/synvert/core/engine/haml_spec.rb +78 -0
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +25 -4
- data/spec/synvert/core/rewriter/instance_spec.rb +56 -3
- data/synvert-core-ruby.gemspec +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6590e4796366364169c7e2cc65779406ce8a8f88221ffcd7f0ad05df4487f78b
|
4
|
+
data.tar.gz: b1792cc8d6744fbef809d43ba428f0d5592702c6abee4653fe8160a87261acd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec759f01184a5730b99742f28310b72df7e5c844fcafa535238dcfacaca8582e9e13b695219ec76f3b8a45194d15aec2caed1e714848cad33263de53a6a56be2
|
7
|
+
data.tar.gz: eef33bbc2623a9ff16e908722ef358cbec51188911e07e1c9c8e44bcb4029c4e73cc1c0da54581d557c3d1edde0f8ed224fff75dffca5b2fcd347e1eed3f83f8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.23.0 (2023-04-05)
|
4
|
+
|
5
|
+
* Add haml engine
|
6
|
+
* Add `Engine.register` and `Engine.encode` and `Engine.generate_transform_proc`
|
7
|
+
* Set NodeMutation `transform_proc`
|
8
|
+
* Update `node_mutation` to 1.14.0
|
9
|
+
|
10
|
+
## 1.22.2 (2023-03-31)
|
11
|
+
|
12
|
+
* Do not replace white space characters in erb engine
|
13
|
+
* Check erb source in `ReplaceErbStmtWithExprAction`
|
14
|
+
* Update `node_mutation` to 1.13.1
|
15
|
+
|
3
16
|
## 1.22.1 (2023-03-31)
|
4
17
|
|
5
18
|
* Fix indent when `insert_after` or `insert_before` to a child node
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
synvert-core (1.
|
4
|
+
synvert-core (1.23.0)
|
5
5
|
activesupport (< 7.0.0)
|
6
|
-
node_mutation (>= 1.
|
6
|
+
node_mutation (>= 1.14.0)
|
7
7
|
node_query (>= 1.12.0)
|
8
8
|
parallel
|
9
9
|
parser
|
@@ -22,7 +22,6 @@ GEM
|
|
22
22
|
coderay (1.1.3)
|
23
23
|
concurrent-ruby (1.2.2)
|
24
24
|
diff-lcs (1.5.0)
|
25
|
-
erubis (2.7.0)
|
26
25
|
fakefs (2.4.0)
|
27
26
|
ffi (1.15.5)
|
28
27
|
formatador (1.1.0)
|
@@ -49,14 +48,13 @@ GEM
|
|
49
48
|
method_source (1.0.0)
|
50
49
|
minitest (5.18.0)
|
51
50
|
nenv (0.3.0)
|
52
|
-
node_mutation (1.
|
53
|
-
erubis
|
51
|
+
node_mutation (1.14.0)
|
54
52
|
node_query (1.12.0)
|
55
53
|
notiffany (0.1.3)
|
56
54
|
nenv (~> 0.1)
|
57
55
|
shellany (~> 0.0)
|
58
56
|
parallel (1.22.1)
|
59
|
-
parser (3.2.
|
57
|
+
parser (3.2.2.0)
|
60
58
|
ast (~> 2.4.1)
|
61
59
|
parser_node_ext (1.0.0)
|
62
60
|
parser
|
@@ -9,9 +9,20 @@ module Synvert::Core
|
|
9
9
|
# @param source [String] erb code.
|
10
10
|
# @return [String] encoded ruby code.
|
11
11
|
def encode(source)
|
12
|
-
source.gsub(/%>.*?<%=?/m) { |str|
|
13
|
-
.sub(/^.*?<%=?/m) { |str|
|
14
|
-
.sub(/%>.*?$/m) { |str|
|
12
|
+
source.gsub(/%>.*?<%=?/m) { |str| replace_all_code_but_white_space_characters(str) }
|
13
|
+
.sub(/^.*?<%=?/m) { |str| replace_all_code_but_white_space_characters(str) }
|
14
|
+
.sub(/%>.*?$/m) { |str| replace_all_code_but_white_space_characters(str) }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Generate an empty proc.
|
18
|
+
def generate_transform_proc(_encoded_source)
|
19
|
+
proc {}
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def replace_all_code_but_white_space_characters(source)
|
25
|
+
source.gsub(/\S/, ' ')
|
15
26
|
end
|
16
27
|
end
|
17
28
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
module Engine
|
5
|
+
class Haml
|
6
|
+
class << self
|
7
|
+
END_LINE = "end\n"
|
8
|
+
|
9
|
+
# Encode haml string, leave only ruby code, replace other haml code with whitespace.
|
10
|
+
# And insert `end\n` for each if, unless, begin, case to make it a valid ruby code.
|
11
|
+
#
|
12
|
+
# @param source [String] haml code.
|
13
|
+
# @return [String] encoded ruby code.
|
14
|
+
def encode(source)
|
15
|
+
tab_sizes = []
|
16
|
+
lines =
|
17
|
+
source.lines.map do |line|
|
18
|
+
if line =~ /\A(\s*)(- ?)(.*)/ # match " - if currenet_user"
|
19
|
+
code = (' ' * ($1.size + $2.size)) + $3
|
20
|
+
new_line =
|
21
|
+
$3.start_with?('else', 'elsif', 'when') ? code : check_and_insert_end(code, tab_sizes, $1.size)
|
22
|
+
tab_sizes.push($1.size) if $3.start_with?('if', 'unless', 'begin', 'case') || $3.include?(' do ')
|
23
|
+
|
24
|
+
new_line
|
25
|
+
else
|
26
|
+
if line =~ /\A(\s*)([%#\.].*)?=(.*)/ # match " %span= current_user.login"
|
27
|
+
code = (' ' * ($1.size + ($2 || '').size + 1)) + $3
|
28
|
+
new_line = check_and_insert_end(code, tab_sizes, $1.size)
|
29
|
+
tab_sizes.push($1.size) if line.include?(' do ')
|
30
|
+
new_line
|
31
|
+
elsif line =~ /\A(\s*)(.*)/ # match any other line
|
32
|
+
check_and_insert_end(' ' * line.size, tab_sizes, $1.size)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
lines.push(END_LINE) unless tab_sizes.empty?
|
37
|
+
lines.join("\n")
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generate transform proc, it's used to adjust start and end position of actions.
|
41
|
+
# Due to the fact that we insert `end\n` when encode the source code, we need to adjust
|
42
|
+
# start and end position of actions to match the original source code.
|
43
|
+
# e.g. if end\n exists in position 10, action start position is 20 and end position is 30,
|
44
|
+
# then action start position should be 16 and end position should be 26.
|
45
|
+
#
|
46
|
+
# @param encoded_source [String] encoded source.
|
47
|
+
# @return [Proc] transform proc.
|
48
|
+
def generate_transform_proc(encoded_source)
|
49
|
+
proc do |actions|
|
50
|
+
start = 0
|
51
|
+
indices = []
|
52
|
+
loop do
|
53
|
+
index = encoded_source[start..-1].index(END_LINE)
|
54
|
+
break unless index
|
55
|
+
|
56
|
+
indices << (start + index)
|
57
|
+
start += index + END_LINE.length
|
58
|
+
end
|
59
|
+
indices.each do |index|
|
60
|
+
actions.each do |action|
|
61
|
+
action.start -= END_LINE.length if action.start > index
|
62
|
+
action.end -= END_LINE.length if action.end > index
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Check if the current tab_size is less than or equal to the last tab_size in tab_sizes.
|
71
|
+
# If so, pop the last tab_size and insert "end\n" before code.
|
72
|
+
# otherwise, return code.
|
73
|
+
def check_and_insert_end(code, tab_sizes, current_tab_size)
|
74
|
+
if !tab_sizes.empty? && current_tab_size <= tab_sizes[-1]
|
75
|
+
tab_sizes.pop
|
76
|
+
"end\n" + code
|
77
|
+
else
|
78
|
+
code
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/synvert/core/engine.rb
CHANGED
@@ -4,5 +4,36 @@ module Synvert::Core
|
|
4
4
|
# Engine defines how to encode / decode other files (like erb).
|
5
5
|
module Engine
|
6
6
|
autoload :Erb, 'synvert/core/engine/erb'
|
7
|
+
autoload :Haml, 'synvert/core/engine/haml'
|
8
|
+
|
9
|
+
# Register an engine
|
10
|
+
# @param [String] extension
|
11
|
+
# @param [Class] engine
|
12
|
+
def self.register(extension, engine)
|
13
|
+
@engines ||= {}
|
14
|
+
@engines[extension] = engine
|
15
|
+
end
|
16
|
+
|
17
|
+
# Encode source code by registered engine.
|
18
|
+
# @param [String] extension
|
19
|
+
# @param [String] source
|
20
|
+
# @return [String] encoded source
|
21
|
+
def self.encode(extension, source)
|
22
|
+
engine = @engines[extension]
|
23
|
+
engine ? engine.encode(source) : source
|
24
|
+
end
|
25
|
+
|
26
|
+
# Generate a transform_proc by registered engine,
|
27
|
+
# which is used to adjust start and end position of actions.
|
28
|
+
# @param [String] extension
|
29
|
+
# @param [String] encoded_source
|
30
|
+
# @return [Proc] transform_proc
|
31
|
+
def self.generate_transform_proc(extension, encoded_source)
|
32
|
+
engine = @engines[extension]
|
33
|
+
engine ? engine.generate_transform_proc(encoded_source) : proc {}
|
34
|
+
end
|
7
35
|
end
|
36
|
+
|
37
|
+
Engine.register('.erb', Engine::Erb)
|
38
|
+
Engine.register('.haml', Engine::Haml)
|
8
39
|
end
|
@@ -8,8 +8,9 @@ module Synvert::Core
|
|
8
8
|
# Initialize a ReplaceErbStmtWithExprAction.
|
9
9
|
#
|
10
10
|
# @param node [Synvert::Core::Rewriter::Node]
|
11
|
-
def initialize(node)
|
11
|
+
def initialize(node, erb_source)
|
12
12
|
super(node, nil)
|
13
|
+
@erb_source = erb_source
|
13
14
|
end
|
14
15
|
|
15
16
|
# The new erb expr code.
|
@@ -23,7 +24,12 @@ module Synvert::Core
|
|
23
24
|
|
24
25
|
# Calculate the begin the end positions.
|
25
26
|
def calculate_position
|
26
|
-
@start = NodeMutation.adapter.get_start(@node)
|
27
|
+
@start = NodeMutation.adapter.get_start(@node)
|
28
|
+
loop do
|
29
|
+
@start -= 1
|
30
|
+
break if @erb_source[@start] == '%'
|
31
|
+
end
|
32
|
+
@start += 1
|
27
33
|
@end = @start
|
28
34
|
end
|
29
35
|
end
|
@@ -44,10 +44,12 @@ module Synvert::Core
|
|
44
44
|
absolute_file_path = File.join(Configuration.root_path, @file_path)
|
45
45
|
while true
|
46
46
|
source = read_source(absolute_file_path)
|
47
|
+
encoded_source = Engine.encode(File.extname(file_path), source)
|
47
48
|
@current_mutation = NodeMutation.new(source)
|
49
|
+
@current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source)
|
48
50
|
@mutation_adapter = NodeMutation.adapter
|
49
51
|
begin
|
50
|
-
node = parse_code(@file_path,
|
52
|
+
node = parse_code(@file_path, encoded_source)
|
51
53
|
|
52
54
|
process_with_node(node) do
|
53
55
|
instance_eval(&@block)
|
@@ -73,9 +75,11 @@ module Synvert::Core
|
|
73
75
|
absolute_file_path = File.join(Configuration.root_path, file_path)
|
74
76
|
source = read_source(absolute_file_path)
|
75
77
|
@current_mutation = NodeMutation.new(source)
|
78
|
+
encoded_source = Engine.encode(File.extname(file_path), source)
|
79
|
+
@current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source)
|
76
80
|
@mutation_adapter = NodeMutation.adapter
|
77
81
|
begin
|
78
|
-
node = parse_code(file_path,
|
82
|
+
node = parse_code(file_path, encoded_source)
|
79
83
|
|
80
84
|
process_with_node(node) do
|
81
85
|
instance_eval(&@block)
|
@@ -300,7 +304,9 @@ module Synvert::Core
|
|
300
304
|
# replace_erb_stmt_with_expr
|
301
305
|
# end
|
302
306
|
def replace_erb_stmt_with_expr
|
303
|
-
|
307
|
+
absolute_file_path = File.join(Configuration.root_path, @file_path)
|
308
|
+
erb_source = read_source(absolute_file_path)
|
309
|
+
action = Rewriter::ReplaceErbStmtWithExprAction.new(@current_node, erb_source)
|
304
310
|
add_action(action)
|
305
311
|
end
|
306
312
|
|
@@ -438,11 +444,11 @@ module Synvert::Core
|
|
438
444
|
# Parse code ast node.
|
439
445
|
#
|
440
446
|
# @param file_path [String] file path
|
441
|
-
# @param
|
447
|
+
# @param encoded_source [String] encoded source code
|
442
448
|
# @return [Node] ast node for file
|
443
|
-
def parse_code(file_path,
|
449
|
+
def parse_code(file_path, encoded_source)
|
444
450
|
buffer = Parser::Source::Buffer.new file_path
|
445
|
-
buffer.source =
|
451
|
+
buffer.source = encoded_source
|
446
452
|
|
447
453
|
parser = Parser::CurrentRuby.new
|
448
454
|
parser.reset
|
data/lib/synvert/core/version.rb
CHANGED
data/lib/synvert/core.rb
CHANGED
@@ -8,7 +8,6 @@ require 'parser_node_ext'
|
|
8
8
|
require_relative './core/node_ext'
|
9
9
|
require 'active_support'
|
10
10
|
require 'active_support/core_ext'
|
11
|
-
require 'erubis'
|
12
11
|
require 'node_query'
|
13
12
|
require 'node_mutation'
|
14
13
|
|
@@ -29,8 +28,9 @@ module Synvert
|
|
29
28
|
|
30
29
|
ALL_RUBY_FILES = %w[**/*.rb]
|
31
30
|
ALL_ERB_FILES = %w[**/*.erb]
|
31
|
+
ALL_HAML_FILES = %w[**/*.haml]
|
32
32
|
ALL_RAKE_FILES = %w[**/*.rake]
|
33
|
-
ALL_FILES = ALL_RUBY_FILES + ALL_ERB_FILES + ALL_RAKE_FILES
|
33
|
+
ALL_FILES = ALL_RUBY_FILES + ALL_ERB_FILES + ALL_HAML_FILES + ALL_RAKE_FILES
|
34
34
|
|
35
35
|
RAILS_APP_FILES = %w[app/**/*.rb engines/*/app/**/*.rb]
|
36
36
|
RAILS_CONTROLLER_FILES = %w[app/controllers/**/*.rb engines/*/app/controllers/**/*.rb]
|
@@ -47,7 +47,7 @@ module Synvert
|
|
47
47
|
engines/*/config/routes.rb
|
48
48
|
engines/*/config/routes/**/*.rb
|
49
49
|
]
|
50
|
-
RAILS_VIEW_FILES =
|
50
|
+
RAILS_VIEW_FILES = ALL_ERB_FILES + ALL_HAML_FILES
|
51
51
|
|
52
52
|
RAILS_CONTROLLER_TEST_FILES = %w[
|
53
53
|
test/functional/**/*.rb
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Synvert::Core
|
6
|
+
describe Engine::Haml do
|
7
|
+
describe '#encode' do
|
8
|
+
it 'encodes source' do
|
9
|
+
source = <<~EOS
|
10
|
+
%p
|
11
|
+
Date/Time:
|
12
|
+
- now = DateTime.now
|
13
|
+
%strong= now
|
14
|
+
- if now > DateTime.parse("December 31, 2006")
|
15
|
+
= "Happy new " + "year!"
|
16
|
+
- else
|
17
|
+
= "Hello!"
|
18
|
+
- if current_admin?
|
19
|
+
%strong= "Admin"
|
20
|
+
- elsif current_user?
|
21
|
+
%span= "User"
|
22
|
+
#error= error_message
|
23
|
+
.form-actions
|
24
|
+
= form_for @user do |f|
|
25
|
+
Test
|
26
|
+
EOS
|
27
|
+
encoded_source = Engine::Haml.encode(source)
|
28
|
+
expect(encoded_source).to be_include 'now = DateTime.now'
|
29
|
+
expect(encoded_source).to be_include 'now'
|
30
|
+
expect(encoded_source).to be_include 'if now > DateTime.parse("December 31, 2006")'
|
31
|
+
expect(encoded_source).to be_include '"Happy new " + "year!"'
|
32
|
+
expect(encoded_source).to be_include '"Hello!"'
|
33
|
+
expect(encoded_source).to be_include 'if current_admin?'
|
34
|
+
expect(encoded_source).to be_include '"Admin"'
|
35
|
+
expect(encoded_source).to be_include 'if current_user?'
|
36
|
+
expect(encoded_source).to be_include '"User"'
|
37
|
+
expect(encoded_source).to be_include 'error_message'
|
38
|
+
expect(encoded_source).to be_include 'form_for @user do |f|'
|
39
|
+
expect(encoded_source.scan("end\n").length).to eq 3
|
40
|
+
expect(encoded_source).not_to be_include '%p'
|
41
|
+
expect(encoded_source).not_to be_include 'strong'
|
42
|
+
expect(encoded_source).not_to be_include '#error'
|
43
|
+
expect(encoded_source).not_to be_include 'Test'
|
44
|
+
expect(encoded_source).not_to be_include '.form-actions'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#generate_transform_proc' do
|
49
|
+
it 'generates transform proc' do
|
50
|
+
encoded_source = <<~EOS
|
51
|
+
now = DateTime.now
|
52
|
+
if now > DateTime.parse("December 31, 2006")
|
53
|
+
else
|
54
|
+
end
|
55
|
+
if current_admin?
|
56
|
+
elsif current_user?
|
57
|
+
end
|
58
|
+
DateTime.now - now
|
59
|
+
EOS
|
60
|
+
proc = Engine::Haml.generate_transform_proc(encoded_source)
|
61
|
+
actions = [
|
62
|
+
NodeMutation::Struct::Action.new(50, 55, ''),
|
63
|
+
# first end position is 69
|
64
|
+
NodeMutation::Struct::Action.new(100, 105, ''),
|
65
|
+
# second end position is 111
|
66
|
+
NodeMutation::Struct::Action.new(120, 125, '')
|
67
|
+
]
|
68
|
+
proc.call(actions)
|
69
|
+
expect(actions.first.start).to eq 50
|
70
|
+
expect(actions.first.end).to eq 55
|
71
|
+
expect(actions.second.start).to eq 96
|
72
|
+
expect(actions.second.end).to eq 101
|
73
|
+
expect(actions.third.start).to eq 112
|
74
|
+
expect(actions.third.end).to eq 117
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -4,12 +4,33 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
RSpec.describe Rewriter::ReplaceErbStmtWithExprAction do
|
7
|
-
context 'replace with
|
7
|
+
context 'replace with whitespace' do
|
8
8
|
subject {
|
9
|
-
|
10
|
-
source = Engine::Erb.encode(
|
9
|
+
erb_source = "<% form_for post do |f| %>\n<% end %>"
|
10
|
+
source = Engine::Erb.encode(erb_source)
|
11
11
|
node = Parser::CurrentRuby.parse(source).children.first
|
12
|
-
described_class.new(node).process
|
12
|
+
described_class.new(node, erb_source).process
|
13
|
+
}
|
14
|
+
|
15
|
+
it 'gets start' do
|
16
|
+
expect(subject.start).to eq '<%'.length
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'gets end' do
|
20
|
+
expect(subject.end).to eq '<%'.length
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'gets new_code' do
|
24
|
+
expect(subject.new_code).to eq '='
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'replace without whitespace' do
|
29
|
+
subject {
|
30
|
+
erb_source = "<%form_for post do |f|%>\n<%end%>"
|
31
|
+
source = Engine::Erb.encode(erb_source)
|
32
|
+
node = Parser::CurrentRuby.parse(source).children.first
|
33
|
+
described_class.new(node, erb_source).process
|
13
34
|
}
|
14
35
|
|
15
36
|
it 'gets start' do
|
@@ -179,8 +179,13 @@ module Synvert::Core
|
|
179
179
|
instance.instance_variable_set(:@current_mutation, double)
|
180
180
|
instance.current_node = double
|
181
181
|
action = double
|
182
|
+
erb_source = '<% form_for @post do |f| %><% end %>'
|
183
|
+
allow(File).to receive(:read).and_return(erb_source)
|
182
184
|
expect(instance.instance_variable_get(:@current_mutation)).to receive(:actions).and_return([])
|
183
|
-
expect(Rewriter::ReplaceErbStmtWithExprAction).to receive(:new).with(
|
185
|
+
expect(Rewriter::ReplaceErbStmtWithExprAction).to receive(:new).with(
|
186
|
+
instance.current_node,
|
187
|
+
erb_source
|
188
|
+
).and_return(action)
|
184
189
|
expect(action).to receive(:process)
|
185
190
|
instance.replace_erb_stmt_with_expr
|
186
191
|
end
|
@@ -356,10 +361,30 @@ module Synvert::Core
|
|
356
361
|
<%= form_for @post do |f| %>
|
357
362
|
<% end %>
|
358
363
|
EOS
|
359
|
-
|
364
|
+
allow(File).to receive(:read).with('./app/views/posts/_form.html.erb', encoding: 'UTF-8').and_return(input)
|
360
365
|
expect(File).to receive(:write).with('./app/views/posts/_form.html.erb', output)
|
361
366
|
instance.process
|
362
367
|
end
|
368
|
+
|
369
|
+
it 'updates haml file' do
|
370
|
+
instance =
|
371
|
+
Rewriter::Instance.new rewriter, 'app/views/posts/_form.html.haml' do
|
372
|
+
with_node node_type: 'ivar' do
|
373
|
+
replace_with 'post'
|
374
|
+
end
|
375
|
+
end
|
376
|
+
input = <<~EOS
|
377
|
+
= form_for @post do |f|
|
378
|
+
= form_for @post do |f|
|
379
|
+
EOS
|
380
|
+
output = <<~EOS
|
381
|
+
= form_for post do |f|
|
382
|
+
= form_for post do |f|
|
383
|
+
EOS
|
384
|
+
allow(File).to receive(:read).with('./app/views/posts/_form.html.haml', encoding: 'UTF-8').and_return(input)
|
385
|
+
expect(File).to receive(:write).with('./app/views/posts/_form.html.haml', output)
|
386
|
+
instance.process
|
387
|
+
end
|
363
388
|
end
|
364
389
|
|
365
390
|
describe '#test' do
|
@@ -419,11 +444,39 @@ module Synvert::Core
|
|
419
444
|
<% form_for @post do |f| %>
|
420
445
|
<% end %>
|
421
446
|
EOS
|
422
|
-
|
447
|
+
allow(File).to receive(:read).with('./app/views/posts/_form.html.erb', encoding: 'UTF-8').and_return(input)
|
423
448
|
result = instance.test
|
424
449
|
expect(result.file_path).to eq 'app/views/posts/_form.html.erb'
|
425
450
|
expect(result.actions).to eq [NodeMutation::Struct::Action.new(2, 2, '=')]
|
426
451
|
end
|
452
|
+
|
453
|
+
it 'updates haml file' do
|
454
|
+
instance =
|
455
|
+
Rewriter::Instance.new rewriter, 'app/views/posts/_form.html.haml' do
|
456
|
+
with_node node_type: 'ivar' do
|
457
|
+
replace_with 'post'
|
458
|
+
end
|
459
|
+
end
|
460
|
+
input = <<~EOS
|
461
|
+
= form_for @post do |f|
|
462
|
+
= form_for @post do |f|
|
463
|
+
EOS
|
464
|
+
output = <<~EOS
|
465
|
+
= form_for post do |f|
|
466
|
+
= form_for post do |f|
|
467
|
+
EOS
|
468
|
+
allow(File).to receive(:read).with('./app/views/posts/_form.html.haml', encoding: 'UTF-8').and_return(input)
|
469
|
+
result = instance.test
|
470
|
+
expect(result.file_path).to eq 'app/views/posts/_form.html.haml'
|
471
|
+
expect(result.actions).to eq [
|
472
|
+
NodeMutation::Struct::Action.new("= form_for ".length, "= form_for @post".length, 'post'),
|
473
|
+
NodeMutation::Struct::Action.new(
|
474
|
+
"= form_for @post do |f|\n= form_for ".length,
|
475
|
+
"= form_for @post do |f|\n= form_for @post".length,
|
476
|
+
'post'
|
477
|
+
),
|
478
|
+
]
|
479
|
+
end
|
427
480
|
end
|
428
481
|
|
429
482
|
describe '#process_with_node' do
|
data/synvert-core-ruby.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_runtime_dependency "activesupport", "< 7.0.0"
|
23
23
|
spec.add_runtime_dependency "node_query", ">= 1.12.0"
|
24
|
-
spec.add_runtime_dependency "node_mutation", ">= 1.
|
24
|
+
spec.add_runtime_dependency "node_mutation", ">= 1.14.0"
|
25
25
|
spec.add_runtime_dependency "parser"
|
26
26
|
spec.add_runtime_dependency "parser_node_ext", ">= 1.0.0"
|
27
27
|
spec.add_runtime_dependency "parallel"
|
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.
|
4
|
+
version: 1.23.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-
|
11
|
+
date: 2023-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.
|
47
|
+
version: 1.14.0
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.
|
54
|
+
version: 1.14.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: parser
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/synvert/core/configuration.rb
|
117
117
|
- lib/synvert/core/engine.rb
|
118
118
|
- lib/synvert/core/engine/erb.rb
|
119
|
+
- lib/synvert/core/engine/haml.rb
|
119
120
|
- lib/synvert/core/node_ext.rb
|
120
121
|
- lib/synvert/core/rewriter.rb
|
121
122
|
- lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
|
@@ -138,6 +139,7 @@ files:
|
|
138
139
|
- spec/spec_helper.rb
|
139
140
|
- spec/support/parser_helper.rb
|
140
141
|
- spec/synvert/core/engine/erb_spec.rb
|
142
|
+
- spec/synvert/core/engine/haml_spec.rb
|
141
143
|
- spec/synvert/core/node_ext_spec.rb
|
142
144
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
143
145
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|
@@ -182,6 +184,7 @@ test_files:
|
|
182
184
|
- spec/spec_helper.rb
|
183
185
|
- spec/support/parser_helper.rb
|
184
186
|
- spec/synvert/core/engine/erb_spec.rb
|
187
|
+
- spec/synvert/core/engine/haml_spec.rb
|
185
188
|
- spec/synvert/core/node_ext_spec.rb
|
186
189
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
187
190
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|