synvert-core 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile +0 -3
- data/Guardfile +0 -9
- data/README.md +30 -12
- data/Rakefile +1 -15
- data/lib/synvert/core/engine/erb.rb +1 -1
- data/lib/synvert/core/engine.rb +1 -1
- data/lib/synvert/core/node_ext.rb +0 -466
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +20 -17
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/instance.rb +83 -133
- data/lib/synvert/core/rewriter/scope/query_scope.rb +2 -2
- data/lib/synvert/core/rewriter/scope/within_scope.rb +4 -4
- data/lib/synvert/core/rewriter.rb +0 -10
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +4 -6
- data/spec/synvert/core/engine/erb_spec.rb +3 -3
- data/spec/synvert/core/node_ext_spec.rb +0 -795
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +21 -1
- data/spec/synvert/core/rewriter/instance_spec.rb +47 -115
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +1 -4
- data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +1 -4
- data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +1 -4
- data/synvert-core-ruby.gemspec +4 -2
- metadata +44 -61
- data/lib/synvert/core/array_ext.rb +0 -48
- data/lib/synvert/core/node_query/compiler/array.rb +0 -34
- data/lib/synvert/core/node_query/compiler/attribute.rb +0 -39
- data/lib/synvert/core/node_query/compiler/attribute_list.rb +0 -24
- data/lib/synvert/core/node_query/compiler/basic_selector.rb +0 -28
- data/lib/synvert/core/node_query/compiler/boolean.rb +0 -23
- data/lib/synvert/core/node_query/compiler/comparable.rb +0 -86
- data/lib/synvert/core/node_query/compiler/dynamic_attribute.rb +0 -51
- data/lib/synvert/core/node_query/compiler/expression.rb +0 -41
- data/lib/synvert/core/node_query/compiler/float.rb +0 -23
- data/lib/synvert/core/node_query/compiler/identifier.rb +0 -41
- data/lib/synvert/core/node_query/compiler/integer.rb +0 -23
- data/lib/synvert/core/node_query/compiler/invalid_operator_error.rb +0 -7
- data/lib/synvert/core/node_query/compiler/nil.rb +0 -23
- data/lib/synvert/core/node_query/compiler/parse_error.rb +0 -7
- data/lib/synvert/core/node_query/compiler/regexp.rb +0 -37
- data/lib/synvert/core/node_query/compiler/selector.rb +0 -113
- data/lib/synvert/core/node_query/compiler/string.rb +0 -23
- data/lib/synvert/core/node_query/compiler/symbol.rb +0 -23
- data/lib/synvert/core/node_query/compiler.rb +0 -25
- data/lib/synvert/core/node_query/lexer.rex +0 -99
- data/lib/synvert/core/node_query/lexer.rex.rb +0 -299
- data/lib/synvert/core/node_query/parser.racc.rb +0 -306
- data/lib/synvert/core/node_query/parser.y +0 -60
- data/lib/synvert/core/node_query.rb +0 -36
- data/lib/synvert/core/rewriter/action/append_action.rb +0 -28
- data/lib/synvert/core/rewriter/action/delete_action.rb +0 -34
- data/lib/synvert/core/rewriter/action/insert_action.rb +0 -34
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +0 -22
- data/lib/synvert/core/rewriter/action/prepend_action.rb +0 -44
- data/lib/synvert/core/rewriter/action/remove_action.rb +0 -56
- data/lib/synvert/core/rewriter/action/replace_action.rb +0 -33
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +0 -36
- data/lib/synvert/core/rewriter/action/wrap_action.rb +0 -37
- data/lib/synvert/core/rewriter/action.rb +0 -102
- data/spec/synvert/core/node_query/lexer_spec.rb +0 -580
- data/spec/synvert/core/node_query/parser_spec.rb +0 -337
- data/spec/synvert/core/rewriter/action/append_action_spec.rb +0 -70
- data/spec/synvert/core/rewriter/action/delete_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +0 -70
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +0 -175
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/replace_action_spec.rb +0 -28
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +0 -59
- data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +0 -31
- data/spec/synvert/core/rewriter/action_spec.rb +0 -14
@@ -3,6 +3,26 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
|
-
describe Rewriter::ReplaceErbStmtWithExprAction do
|
6
|
+
RSpec.describe Rewriter::ReplaceErbStmtWithExprAction do
|
7
|
+
context 'replace with single line' do
|
8
|
+
subject {
|
9
|
+
source = "<% form_for post do |f| %>\n<% end %>"
|
10
|
+
source = Engine::Erb.encode(source)
|
11
|
+
node = Parser::CurrentRuby.parse(source).children[1]
|
12
|
+
described_class.new(node).process
|
13
|
+
}
|
14
|
+
|
15
|
+
it 'gets start' do
|
16
|
+
expect(subject.start).to eq '@output_buffer = output_buffer || ActionView::OutputBuffer.new;'.length
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'gets end' do
|
20
|
+
expect(subject.end).to eq '@output_buffer = output_buffer || ActionView::OutputBuffer.new;; ; form_for post do |f| ; ;'.length
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'gets new_code' do
|
24
|
+
expect(subject.new_code).to eq '@output_buffer.append= form_for post do |f| ; ;'
|
25
|
+
end
|
26
|
+
end
|
7
27
|
end
|
8
28
|
end
|
@@ -4,8 +4,6 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::Instance do
|
7
|
-
before { Rewriter::Instance.reset }
|
8
|
-
|
9
7
|
let(:instance) {
|
10
8
|
rewriter = Rewriter.new('foo', 'bar')
|
11
9
|
Rewriter::Instance.new(rewriter, ['file pattern'])
|
@@ -98,97 +96,83 @@ module Synvert::Core
|
|
98
96
|
end
|
99
97
|
|
100
98
|
it 'parses append' do
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
).and_return(action)
|
106
|
-
expect(action).to receive(:process)
|
107
|
-
instance.append 'include FactoryGirl::Syntax::Methods'
|
99
|
+
instance.current_mutation = double
|
100
|
+
instance.current_node = double
|
101
|
+
expect(instance.current_mutation).to receive(:append).with(instance.current_node, 'Foobar')
|
102
|
+
instance.append 'Foobar'
|
108
103
|
end
|
109
104
|
|
110
105
|
it 'parses prepend' do
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
).and_return(action)
|
116
|
-
expect(action).to receive(:process)
|
117
|
-
instance.prepend '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
106
|
+
instance.current_mutation = double
|
107
|
+
instance.current_node = double
|
108
|
+
expect(instance.current_mutation).to receive(:prepend).with(instance.current_node, 'Foobar')
|
109
|
+
instance.prepend 'Foobar'
|
118
110
|
end
|
119
111
|
|
120
112
|
it 'parses insert at end' do
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
at: 'end',
|
126
|
-
to: 'receiver'
|
127
|
-
).and_return(action)
|
128
|
-
expect(action).to receive(:process)
|
129
|
-
instance.insert '.first', to: 'receiver'
|
113
|
+
instance.current_mutation = double
|
114
|
+
instance.current_node = double
|
115
|
+
expect(instance.current_mutation).to receive(:insert).with(instance.current_node, 'Foobar', at: 'end', to: 'receiver')
|
116
|
+
instance.insert 'Foobar', to: 'receiver'
|
130
117
|
end
|
131
118
|
|
132
119
|
it 'parses insert at beginning' do
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
at: 'beginning',
|
138
|
-
to: nil
|
139
|
-
).and_return(action)
|
140
|
-
expect(action).to receive(:process)
|
141
|
-
instance.insert 'URI.', at: 'beginning'
|
120
|
+
instance.current_mutation = double
|
121
|
+
instance.current_node = double
|
122
|
+
expect(instance.current_mutation).to receive(:insert).with(instance.current_node, 'Foobar', at: 'beginning', to: nil)
|
123
|
+
instance.insert 'Foobar', at: 'beginning'
|
142
124
|
end
|
143
125
|
|
144
126
|
it 'parses insert_after' do
|
127
|
+
instance.current_mutation = double
|
128
|
+
instance.current_node = double
|
129
|
+
expect(instance.current_mutation).to receive(:insert_after).with(instance.current_node, 'Foobar')
|
130
|
+
instance.insert_after 'Foobar'
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'parses replace_erb_stmt_with_expr' do
|
134
|
+
instance.current_mutation = double
|
135
|
+
instance.current_node = double
|
145
136
|
action = double
|
146
|
-
expect(
|
147
|
-
|
148
|
-
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
149
|
-
).and_return(action)
|
137
|
+
expect(instance.current_mutation).to receive(:actions).and_return([])
|
138
|
+
expect(Rewriter::ReplaceErbStmtWithExprAction).to receive(:new).with(instance.current_node).and_return(action)
|
150
139
|
expect(action).to receive(:process)
|
151
|
-
instance.
|
140
|
+
instance.replace_erb_stmt_with_expr
|
152
141
|
end
|
153
142
|
|
154
143
|
it 'parses replace_with' do
|
155
|
-
|
156
|
-
|
157
|
-
expect(
|
158
|
-
instance.replace_with '
|
144
|
+
instance.current_mutation = double
|
145
|
+
instance.current_node = double
|
146
|
+
expect(instance.current_mutation).to receive(:replace_with).with(instance.current_node, 'Foobar')
|
147
|
+
instance.replace_with 'Foobar'
|
159
148
|
end
|
160
149
|
|
161
150
|
it 'parses replace with' do
|
162
|
-
|
163
|
-
|
164
|
-
expect(
|
165
|
-
instance.replace :message, with: '
|
151
|
+
instance.current_mutation = double
|
152
|
+
instance.current_node = double
|
153
|
+
expect(instance.current_mutation).to receive(:replace).with(instance.current_node, :message, with: 'Foobar')
|
154
|
+
instance.replace :message, with: 'Foobar'
|
166
155
|
end
|
167
156
|
|
168
157
|
it 'parses remove' do
|
169
|
-
|
170
|
-
|
171
|
-
expect(
|
158
|
+
instance.current_mutation = double
|
159
|
+
instance.current_node = double
|
160
|
+
expect(instance.current_mutation).to receive(:remove).with(instance.current_node, and_comma: true)
|
172
161
|
instance.remove and_comma: true
|
173
162
|
end
|
174
163
|
|
175
|
-
it 'parses
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
:dot,
|
180
|
-
:message,
|
181
|
-
{ and_comma: true }
|
182
|
-
).and_return(action)
|
183
|
-
expect(action).to receive(:process)
|
164
|
+
it 'parses delete' do
|
165
|
+
instance.current_mutation = double
|
166
|
+
instance.current_node = double
|
167
|
+
expect(instance.current_mutation).to receive(:delete).with(instance.current_node, :dot, :message, and_comma: true)
|
184
168
|
instance.delete :dot, :message, and_comma: true
|
185
169
|
end
|
186
170
|
|
187
171
|
it 'parses wrap with' do
|
188
|
-
|
189
|
-
|
190
|
-
expect(
|
191
|
-
instance.wrap with: 'module
|
172
|
+
instance.current_mutation = double
|
173
|
+
instance.current_node = double
|
174
|
+
expect(instance.current_mutation).to receive(:wrap).with(instance.current_node, with: 'module Foobar')
|
175
|
+
instance.wrap with: 'module Foobar'
|
192
176
|
end
|
193
177
|
|
194
178
|
it 'parses warn' do
|
@@ -251,32 +235,6 @@ module Synvert::Core
|
|
251
235
|
instance.process
|
252
236
|
end
|
253
237
|
|
254
|
-
it 'does not read file if already read' do
|
255
|
-
instance =
|
256
|
-
Rewriter::Instance.new rewriter, ['spec/spec_helper.rb'] do
|
257
|
-
with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
|
258
|
-
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
259
|
-
insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
263
|
-
input = <<~EOS
|
264
|
-
RSpec.configure do |config|
|
265
|
-
config.include FactoryGirl::Syntax::Methods
|
266
|
-
end
|
267
|
-
EOS
|
268
|
-
output = <<~EOS
|
269
|
-
RSpec.configure do |config|
|
270
|
-
config.include FactoryGirl::Syntax::Methods
|
271
|
-
end
|
272
|
-
EOS
|
273
|
-
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
|
274
|
-
expect(File).to receive(:read).with('spec/spec_helper.rb', encoding: 'UTF-8').and_return(input).once
|
275
|
-
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
276
|
-
instance.process
|
277
|
-
instance.process
|
278
|
-
end
|
279
|
-
|
280
238
|
it 'updates file_source and file_ast when writing a file' do
|
281
239
|
instance =
|
282
240
|
Rewriter::Instance.new rewriter, ['spec/**/*_spec.rb'] do
|
@@ -308,32 +266,6 @@ module Synvert::Core
|
|
308
266
|
end
|
309
267
|
end
|
310
268
|
|
311
|
-
describe '#get_conflict_actions' do
|
312
|
-
let(:rewriter) { Rewriter.new('foo', 'bar') }
|
313
|
-
|
314
|
-
it 'has no conflict' do
|
315
|
-
action1 = double(begin_pos: 10, end_pos: 20)
|
316
|
-
action2 = double(begin_pos: 30, end_pos: 40)
|
317
|
-
action3 = double(begin_pos: 50, end_pos: 60)
|
318
|
-
instance = Rewriter::Instance.new rewriter, ['spec/spec_helper.rb']
|
319
|
-
instance.instance_variable_set :@actions, [action1, action2, action3]
|
320
|
-
conflict_actions = instance.send(:get_conflict_actions)
|
321
|
-
expect(conflict_actions).to eq []
|
322
|
-
expect(instance.instance_variable_get(:@actions)).to eq [action1, action2, action3]
|
323
|
-
end
|
324
|
-
|
325
|
-
it 'has no conflict' do
|
326
|
-
action1 = double(begin_pos: 30, end_pos: 40)
|
327
|
-
action2 = double(begin_pos: 50, end_pos: 60)
|
328
|
-
action3 = double(begin_pos: 10, end_pos: 20)
|
329
|
-
instance = Rewriter::Instance.new rewriter, ['spec/spec_helper.rb']
|
330
|
-
instance.instance_variable_set :@actions, [action1, action2, action3]
|
331
|
-
conflict_actions = instance.send(:get_conflict_actions)
|
332
|
-
expect(conflict_actions).to eq [action2, action1]
|
333
|
-
expect(instance.instance_variable_get(:@actions)).to eq [action3]
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
269
|
describe '#process_with_node' do
|
338
270
|
it 'resets current_node' do
|
339
271
|
node1 = double
|
@@ -14,10 +14,7 @@ module Synvert::Core
|
|
14
14
|
EOS
|
15
15
|
|
16
16
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
17
|
-
before
|
18
|
-
Rewriter::Instance.reset
|
19
|
-
instance.current_node = node
|
20
|
-
end
|
17
|
+
before { instance.current_node = node }
|
21
18
|
|
22
19
|
describe '#process' do
|
23
20
|
it 'call block with child node' do
|
@@ -18,10 +18,7 @@ module Synvert::Core
|
|
18
18
|
|
19
19
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
20
20
|
|
21
|
-
before
|
22
|
-
Rewriter::Instance.reset
|
23
|
-
instance.current_node = node
|
24
|
-
end
|
21
|
+
before { instance.current_node = node }
|
25
22
|
|
26
23
|
describe '#process' do
|
27
24
|
it 'not call block if no matching node' do
|
@@ -18,10 +18,7 @@ module Synvert::Core
|
|
18
18
|
|
19
19
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
20
20
|
|
21
|
-
before
|
22
|
-
Rewriter::Instance.reset
|
23
|
-
instance.current_node = node
|
24
|
-
end
|
21
|
+
before { instance.current_node = node }
|
25
22
|
|
26
23
|
describe '#process' do
|
27
24
|
it 'not call block if no matching node' do
|
data/synvert-core-ruby.gemspec
CHANGED
@@ -14,13 +14,15 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.homepage = "https://github.com/xinminlabs/synvert-core-ruby"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0")
|
18
|
-
%w[lib/synvert/core/node_query/lexer.rex.rb lib/synvert/core/node_query/parser.racc.rb]
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
20
|
spec.require_paths = ["lib"]
|
22
21
|
|
23
22
|
spec.add_runtime_dependency "activesupport"
|
24
23
|
spec.add_runtime_dependency "erubis"
|
24
|
+
spec.add_runtime_dependency "node_query"
|
25
|
+
spec.add_runtime_dependency "node_mutation"
|
25
26
|
spec.add_runtime_dependency "parser"
|
27
|
+
spec.add_runtime_dependency "parser_node_ext"
|
26
28
|
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: 1.
|
4
|
+
version: 1.5.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: 2022-
|
11
|
+
date: 2022-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: node_query
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: node_mutation
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: parser
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +80,20 @@ dependencies:
|
|
52
80
|
- - ">="
|
53
81
|
- !ruby/object:Gem::Version
|
54
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: parser_node_ext
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
55
97
|
description: convert ruby code to better syntax automatically.
|
56
98
|
email:
|
57
99
|
- flyerhzm@gmail.com
|
@@ -69,48 +111,13 @@ files:
|
|
69
111
|
- README.md
|
70
112
|
- Rakefile
|
71
113
|
- lib/synvert/core.rb
|
72
|
-
- lib/synvert/core/array_ext.rb
|
73
114
|
- lib/synvert/core/configuration.rb
|
74
115
|
- lib/synvert/core/engine.rb
|
75
116
|
- lib/synvert/core/engine/erb.rb
|
76
117
|
- lib/synvert/core/exceptions.rb
|
77
118
|
- lib/synvert/core/node_ext.rb
|
78
|
-
- lib/synvert/core/node_query.rb
|
79
|
-
- lib/synvert/core/node_query/compiler.rb
|
80
|
-
- lib/synvert/core/node_query/compiler/array.rb
|
81
|
-
- lib/synvert/core/node_query/compiler/attribute.rb
|
82
|
-
- lib/synvert/core/node_query/compiler/attribute_list.rb
|
83
|
-
- lib/synvert/core/node_query/compiler/basic_selector.rb
|
84
|
-
- lib/synvert/core/node_query/compiler/boolean.rb
|
85
|
-
- lib/synvert/core/node_query/compiler/comparable.rb
|
86
|
-
- lib/synvert/core/node_query/compiler/dynamic_attribute.rb
|
87
|
-
- lib/synvert/core/node_query/compiler/expression.rb
|
88
|
-
- lib/synvert/core/node_query/compiler/float.rb
|
89
|
-
- lib/synvert/core/node_query/compiler/identifier.rb
|
90
|
-
- lib/synvert/core/node_query/compiler/integer.rb
|
91
|
-
- lib/synvert/core/node_query/compiler/invalid_operator_error.rb
|
92
|
-
- lib/synvert/core/node_query/compiler/nil.rb
|
93
|
-
- lib/synvert/core/node_query/compiler/parse_error.rb
|
94
|
-
- lib/synvert/core/node_query/compiler/regexp.rb
|
95
|
-
- lib/synvert/core/node_query/compiler/selector.rb
|
96
|
-
- lib/synvert/core/node_query/compiler/string.rb
|
97
|
-
- lib/synvert/core/node_query/compiler/symbol.rb
|
98
|
-
- lib/synvert/core/node_query/lexer.rex
|
99
|
-
- lib/synvert/core/node_query/lexer.rex.rb
|
100
|
-
- lib/synvert/core/node_query/parser.racc.rb
|
101
|
-
- lib/synvert/core/node_query/parser.y
|
102
119
|
- lib/synvert/core/rewriter.rb
|
103
|
-
- lib/synvert/core/rewriter/action.rb
|
104
|
-
- lib/synvert/core/rewriter/action/append_action.rb
|
105
|
-
- lib/synvert/core/rewriter/action/delete_action.rb
|
106
|
-
- lib/synvert/core/rewriter/action/insert_action.rb
|
107
|
-
- lib/synvert/core/rewriter/action/insert_after_action.rb
|
108
|
-
- lib/synvert/core/rewriter/action/prepend_action.rb
|
109
|
-
- lib/synvert/core/rewriter/action/remove_action.rb
|
110
|
-
- lib/synvert/core/rewriter/action/replace_action.rb
|
111
120
|
- lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
|
112
|
-
- lib/synvert/core/rewriter/action/replace_with_action.rb
|
113
|
-
- lib/synvert/core/rewriter/action/wrap_action.rb
|
114
121
|
- lib/synvert/core/rewriter/any_value.rb
|
115
122
|
- lib/synvert/core/rewriter/condition.rb
|
116
123
|
- lib/synvert/core/rewriter/condition/if_exist_condition.rb
|
@@ -130,19 +137,7 @@ files:
|
|
130
137
|
- spec/support/parser_helper.rb
|
131
138
|
- spec/synvert/core/engine/erb_spec.rb
|
132
139
|
- spec/synvert/core/node_ext_spec.rb
|
133
|
-
- spec/synvert/core/node_query/lexer_spec.rb
|
134
|
-
- spec/synvert/core/node_query/parser_spec.rb
|
135
|
-
- spec/synvert/core/rewriter/action/append_action_spec.rb
|
136
|
-
- spec/synvert/core/rewriter/action/delete_action_spec.rb
|
137
|
-
- spec/synvert/core/rewriter/action/insert_action_spec.rb
|
138
|
-
- spec/synvert/core/rewriter/action/insert_after_action_spec.rb
|
139
|
-
- spec/synvert/core/rewriter/action/prepend_action_spec.rb
|
140
|
-
- spec/synvert/core/rewriter/action/remove_action_spec.rb
|
141
|
-
- spec/synvert/core/rewriter/action/replace_action_spec.rb
|
142
140
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
143
|
-
- spec/synvert/core/rewriter/action/replace_with_action_spec.rb
|
144
|
-
- spec/synvert/core/rewriter/action/wrap_action_spec.rb
|
145
|
-
- spec/synvert/core/rewriter/action_spec.rb
|
146
141
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|
147
142
|
- spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb
|
148
143
|
- spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb
|
@@ -186,19 +181,7 @@ test_files:
|
|
186
181
|
- spec/support/parser_helper.rb
|
187
182
|
- spec/synvert/core/engine/erb_spec.rb
|
188
183
|
- spec/synvert/core/node_ext_spec.rb
|
189
|
-
- spec/synvert/core/node_query/lexer_spec.rb
|
190
|
-
- spec/synvert/core/node_query/parser_spec.rb
|
191
|
-
- spec/synvert/core/rewriter/action/append_action_spec.rb
|
192
|
-
- spec/synvert/core/rewriter/action/delete_action_spec.rb
|
193
|
-
- spec/synvert/core/rewriter/action/insert_action_spec.rb
|
194
|
-
- spec/synvert/core/rewriter/action/insert_after_action_spec.rb
|
195
|
-
- spec/synvert/core/rewriter/action/prepend_action_spec.rb
|
196
|
-
- spec/synvert/core/rewriter/action/remove_action_spec.rb
|
197
|
-
- spec/synvert/core/rewriter/action/replace_action_spec.rb
|
198
184
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
199
|
-
- spec/synvert/core/rewriter/action/replace_with_action_spec.rb
|
200
|
-
- spec/synvert/core/rewriter/action/wrap_action_spec.rb
|
201
|
-
- spec/synvert/core/rewriter/action_spec.rb
|
202
185
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|
203
186
|
- spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb
|
204
187
|
- spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Extend Array.
|
4
|
-
class Array
|
5
|
-
# Get child node by the name.
|
6
|
-
#
|
7
|
-
# @param child_name [String] name of child node.
|
8
|
-
# @return [Parser::AST::Node] the child node.
|
9
|
-
def child_node_by_name(child_name)
|
10
|
-
direct_child_name, nested_child_name = child_name.split('.', 2)
|
11
|
-
child_direct_child_node = direct_child_name =~ /\A\d+\z/ ? self[direct_child_name.to_i - 1] : self.send(direct_child_name)
|
12
|
-
return child_direct_child_node.child_node_by_name(nested_child_name) if nested_child_name
|
13
|
-
return child_direct_child_node if child_direct_child_node
|
14
|
-
|
15
|
-
raise Synvert::Core::MethodNotSupported,
|
16
|
-
"child_node_by_name is not handled for #{debug_info}, child_name: #{child_name}"
|
17
|
-
end
|
18
|
-
|
19
|
-
# Get the source range of child node.
|
20
|
-
#
|
21
|
-
# @param child_name [String] name of child node.
|
22
|
-
# @return [Parser::Source::Range] source range of child node.
|
23
|
-
def child_node_range(child_name)
|
24
|
-
direct_child_name, nested_child_name = child_name.split('.', 2)
|
25
|
-
child_direct_child_node = direct_child_name =~ /\A\d+\z/ ? self[direct_child_name.to_i - 1] : self.send(direct_child_name)
|
26
|
-
if nested_child_name
|
27
|
-
return child_direct_child_node.child_node_range(nested_child_name)
|
28
|
-
elsif child_direct_child_node
|
29
|
-
return (
|
30
|
-
Parser::Source::Range.new(
|
31
|
-
'(string)',
|
32
|
-
child_direct_child_node.loc.expression.begin_pos,
|
33
|
-
child_direct_child_node.loc.expression.end_pos
|
34
|
-
)
|
35
|
-
)
|
36
|
-
else
|
37
|
-
raise Synvert::Core::MethodNotSupported,
|
38
|
-
"child_node_range is not handled for #{debug_info}, child_name: #{child_name}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# Return the debug info.
|
43
|
-
#
|
44
|
-
# @return [String] file, line, source and node.
|
45
|
-
def debug_info
|
46
|
-
map(&:debug_info).join("\n")
|
47
|
-
end
|
48
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core::NodeQuery::Compiler
|
4
|
-
# Array represents a ruby array value.
|
5
|
-
class Array
|
6
|
-
include Comparable
|
7
|
-
|
8
|
-
# Initialize an Array.
|
9
|
-
# @param value the first value of the array
|
10
|
-
# @param rest the rest value of the array
|
11
|
-
def initialize(value: nil, rest: nil)
|
12
|
-
@value = value
|
13
|
-
@rest = rest
|
14
|
-
end
|
15
|
-
|
16
|
-
# Get the expected value.
|
17
|
-
# @return [Array]
|
18
|
-
def expected_value
|
19
|
-
expected = []
|
20
|
-
expected.push(@value) if @value
|
21
|
-
expected += @rest.expected_value if @rest
|
22
|
-
expected
|
23
|
-
end
|
24
|
-
|
25
|
-
# Get valid operators.
|
26
|
-
def valid_operators
|
27
|
-
ARRAY_VALID_OPERATORS
|
28
|
-
end
|
29
|
-
|
30
|
-
def to_s
|
31
|
-
[@value, @rest].compact.join(' ')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core::NodeQuery::Compiler
|
4
|
-
# Attribute is a pair of key, value and operator,
|
5
|
-
class Attribute
|
6
|
-
# Initialize a Attribute.
|
7
|
-
# @param key [String] the key
|
8
|
-
# @param value the value can be any class implement {Synvert::Core::NodeQuery::Compiler::Comparable}
|
9
|
-
# @param operator [Symbol] the operator
|
10
|
-
def initialize(key:, value:, operator: '==')
|
11
|
-
@key = key
|
12
|
-
@value = value
|
13
|
-
@operator = operator
|
14
|
-
end
|
15
|
-
|
16
|
-
# Check if the node matches the attribute.
|
17
|
-
# @param node [Parser::AST::Node] the node
|
18
|
-
# @return [Boolean]
|
19
|
-
def match?(node)
|
20
|
-
@value.base_node = node if @value.is_a?(DynamicAttribute)
|
21
|
-
node && @value.match?(node.child_node_by_name(@key), @operator)
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_s
|
25
|
-
case @operator
|
26
|
-
when '^=', '$=', '*=', '!=', '=~', '!~', '>=', '>', '<=', '<'
|
27
|
-
"#{@key}#{@operator}#{@value}"
|
28
|
-
when 'in'
|
29
|
-
"#{@key} in (#{@value})"
|
30
|
-
when 'not_in'
|
31
|
-
"#{@key} not in (#{@value})"
|
32
|
-
when 'includes'
|
33
|
-
"#{@key} includes #{@value}"
|
34
|
-
else
|
35
|
-
"#{@key}=#{@value}"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core::NodeQuery::Compiler
|
4
|
-
# AttributeList contains one or more {Synvert::Core::NodeQuery::Compiler::Attribute}.
|
5
|
-
class AttributeList
|
6
|
-
# Initialize a AttributeList.
|
7
|
-
# @param attribute [Synvert::Core::NodeQuery::Compiler::Attribute] the attribute
|
8
|
-
# @param rest [Synvert::Core::NodeQuery::Compiler::AttributeList] the rest attribute list
|
9
|
-
def initialize(attribute:, rest: nil)
|
10
|
-
@attribute = attribute
|
11
|
-
@rest = rest
|
12
|
-
end
|
13
|
-
|
14
|
-
# Check if the node matches the attribute list.
|
15
|
-
# @return [Boolean]
|
16
|
-
def match?(node)
|
17
|
-
@attribute.match?(node) && (!@rest || @rest.match?(node))
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_s
|
21
|
-
"[#{@attribute}]#{@rest}"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core::NodeQuery::Compiler
|
4
|
-
# BasicSelector used to match nodes, it combines by node type and/or attribute list.
|
5
|
-
class BasicSelector
|
6
|
-
# Initialize a BasicSelector.
|
7
|
-
# @param node_type [String] the node type
|
8
|
-
# @param attribute_list [Synvert::Core::NodeQuery::Compiler::AttributeList] the attribute list
|
9
|
-
def initialize(node_type:, attribute_list: nil)
|
10
|
-
@node_type = node_type
|
11
|
-
@attribute_list = attribute_list
|
12
|
-
end
|
13
|
-
|
14
|
-
# Check if node matches the selector.
|
15
|
-
# @param node [Parser::AST::Node] the node
|
16
|
-
def match?(node, _operator = '==')
|
17
|
-
return false unless node
|
18
|
-
|
19
|
-
@node_type.to_sym == node.type && (!@attribute_list || @attribute_list.match?(node))
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_s
|
23
|
-
result = [".#{@node_type}"]
|
24
|
-
result << @attribute_list.to_s if @attribute_list
|
25
|
-
result.join('')
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core::NodeQuery::Compiler
|
4
|
-
# Boolean represents a ruby boolean value.
|
5
|
-
class Boolean
|
6
|
-
include Comparable
|
7
|
-
|
8
|
-
# Initialize a Boolean.
|
9
|
-
# @param value [Boolean] the boolean value
|
10
|
-
def initialize(value:)
|
11
|
-
@value = value
|
12
|
-
end
|
13
|
-
|
14
|
-
# Get valid operators.
|
15
|
-
def valid_operators
|
16
|
-
SIMPLE_VALID_OPERATORS
|
17
|
-
end
|
18
|
-
|
19
|
-
def to_s
|
20
|
-
@value.to_s
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|