synvert-core 1.2.0 → 1.3.1
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/Gemfile +9 -0
- data/lib/synvert/core/node_ext.rb +14 -2
- data/lib/synvert/core/node_query/compiler/attribute.rb +6 -18
- data/lib/synvert/core/node_query/compiler/comparable.rb +26 -19
- data/lib/synvert/core/node_query/compiler/expression.rb +8 -76
- data/lib/synvert/core/node_query/compiler/identifier.rb +1 -1
- data/lib/synvert/core/node_query/compiler/regexp.rb +2 -2
- data/lib/synvert/core/node_query/compiler/selector.rb +102 -20
- data/lib/synvert/core/node_query/compiler/simple_selector.rb +29 -0
- data/lib/synvert/core/node_query/compiler/string.rb +1 -1
- data/lib/synvert/core/node_query/compiler.rb +1 -0
- data/lib/synvert/core/node_query/lexer.rex +19 -16
- data/lib/synvert/core/node_query/lexer.rex.rb +34 -27
- data/lib/synvert/core/node_query/parser.racc.rb +118 -287
- data/lib/synvert/core/node_query/parser.y +17 -36
- data/lib/synvert/core/node_query.rb +7 -7
- data/lib/synvert/core/rewriter/action/delete_action.rb +4 -2
- data/lib/synvert/core/rewriter/action/remove_action.rb +5 -2
- data/lib/synvert/core/rewriter/instance.rb +8 -4
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +7 -5
- data/spec/synvert/core/node_query/lexer_spec.rb +48 -48
- data/spec/synvert/core/node_query/parser_spec.rb +139 -107
- data/spec/synvert/core/rewriter/instance_spec.rb +12 -7
- data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +3 -3
- data/synvert-core-ruby.gemspec +0 -10
- metadata +3 -128
@@ -1,141 +1,158 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
def assert_parser(source)
|
4
|
+
expect(parser.parse(source).to_s).to eq source
|
5
|
+
end
|
6
|
+
|
3
7
|
module Synvert::Core::NodeQuery
|
4
8
|
RSpec.describe Parser do
|
5
9
|
let(:parser) { described_class.new }
|
6
10
|
|
7
|
-
|
8
|
-
|
9
|
-
|
11
|
+
describe '#toString' do
|
12
|
+
it 'parses one selector' do
|
13
|
+
source = '.send[message=:create]'
|
14
|
+
assert_parser(source)
|
15
|
+
end
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
it 'parses two selectors' do
|
18
|
+
source = '.class[name=Synvert] .def[name="foobar"]'
|
19
|
+
assert_parser(source)
|
20
|
+
end
|
15
21
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
22
|
+
it 'parses three selectors' do
|
23
|
+
source = '.class[name=Synvert] .def[name="foobar"] .send[message=create]'
|
24
|
+
assert_parser(source)
|
25
|
+
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
27
|
+
it 'parses child selector' do
|
28
|
+
source = '.class[name=Synvert] > .def[name="foobar"]'
|
29
|
+
assert_parser(source)
|
30
|
+
end
|
25
31
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
32
|
+
it 'parses scope' do
|
33
|
+
source = '.block <body> > .send'
|
34
|
+
assert_parser(source)
|
35
|
+
end
|
30
36
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
37
|
+
it 'parses :first-child' do
|
38
|
+
source = '.class .def:first-child'
|
39
|
+
assert_parser(source)
|
40
|
+
end
|
35
41
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
42
|
+
it 'parses :last-child' do
|
43
|
+
source = '.class .def:last-child'
|
44
|
+
assert_parser(source)
|
45
|
+
end
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
47
|
+
it 'parses :nth-child(n)' do
|
48
|
+
source = '.class .def:nth-child(2)'
|
49
|
+
assert_parser(source)
|
50
|
+
end
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
52
|
+
it 'parses :nth-last-child(n)' do
|
53
|
+
source = '.class .def:nth-last-child(2)'
|
54
|
+
assert_parser(source)
|
55
|
+
end
|
50
56
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
57
|
+
it 'parses :has selector' do
|
58
|
+
source = '.class:has(> .def)'
|
59
|
+
assert_parser(source)
|
60
|
+
end
|
55
61
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
62
|
+
it 'parses :not_has selector' do
|
63
|
+
source = '.class:not_has(> .def)'
|
64
|
+
assert_parser(source)
|
65
|
+
end
|
60
66
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
67
|
+
it 'parses root :has selector' do
|
68
|
+
source = ':has(.def)'
|
69
|
+
assert_parser(source)
|
70
|
+
end
|
65
71
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
72
|
+
it 'parses multiple attributes' do
|
73
|
+
source = '.send[receiver=nil][message=:create]'
|
74
|
+
assert_parser(source)
|
75
|
+
end
|
70
76
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
77
|
+
it 'parses nested attributes' do
|
78
|
+
source = '.send[receiver.message=:create]'
|
79
|
+
assert_parser(source)
|
80
|
+
end
|
75
81
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
82
|
+
it 'parses selector value' do
|
83
|
+
source = '.send[receiver=.send[message=:create]]'
|
84
|
+
assert_parser(source)
|
85
|
+
end
|
80
86
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
87
|
+
it 'parses start with operator' do
|
88
|
+
source = '.def[name^=synvert]'
|
89
|
+
assert_parser(source)
|
90
|
+
end
|
85
91
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
92
|
+
it 'parses end with operator' do
|
93
|
+
source = '.def[name$=synvert]'
|
94
|
+
assert_parser(source)
|
95
|
+
end
|
90
96
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
97
|
+
it 'parses contain operator' do
|
98
|
+
source = '.def[name*=synvert]'
|
99
|
+
assert_parser(source)
|
100
|
+
end
|
95
101
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
102
|
+
it 'parses not equal operator' do
|
103
|
+
source = '.send[receiver=.send[message!=:create]]'
|
104
|
+
assert_parser(source)
|
105
|
+
end
|
100
106
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
107
|
+
it 'parses greater than operator' do
|
108
|
+
source = '.send[receiver=.send[arguments.size>1]]'
|
109
|
+
assert_parser(source)
|
110
|
+
end
|
105
111
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
112
|
+
it 'parses greater than or equal operator' do
|
113
|
+
source = '.send[receiver=.send[arguments.size>=1]]'
|
114
|
+
assert_parser(source)
|
115
|
+
end
|
110
116
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
117
|
+
it 'parses less than operator' do
|
118
|
+
source = '.send[receiver=.send[arguments.size<1]]'
|
119
|
+
assert_parser(source)
|
120
|
+
end
|
115
121
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
122
|
+
it 'parses less than or equal operator' do
|
123
|
+
source = '.send[receiver=.send[arguments.size<=1]]'
|
124
|
+
assert_parser(source)
|
125
|
+
end
|
120
126
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
127
|
+
it 'parses in operator' do
|
128
|
+
source = '.def[name in (foo bar)]'
|
129
|
+
assert_parser(source)
|
130
|
+
end
|
125
131
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
132
|
+
it 'parses not_in operator' do
|
133
|
+
source = '.def[name not in (foo bar)]'
|
134
|
+
assert_parser(source)
|
135
|
+
end
|
130
136
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
137
|
+
it 'parses includes operator' do
|
138
|
+
source = '.def[arguments includes &block]'
|
139
|
+
assert_parser(source)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'parses empty string' do
|
143
|
+
source = '.send[arguments.first=""]'
|
144
|
+
assert_parser(source)
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'parses []=' do
|
148
|
+
source = '.send[message=[]=]'
|
149
|
+
assert_parser(source)
|
150
|
+
end
|
135
151
|
|
136
|
-
|
137
|
-
|
138
|
-
|
152
|
+
it 'parses :[]' do
|
153
|
+
source = '.send[message=:[]]'
|
154
|
+
assert_parser(source)
|
155
|
+
end
|
139
156
|
end
|
140
157
|
|
141
158
|
describe '#query_nodes' do
|
@@ -203,6 +220,21 @@ module Synvert::Core::NodeQuery
|
|
203
220
|
expect(expression.query_nodes(node)).to eq [node.body[-2]]
|
204
221
|
end
|
205
222
|
|
223
|
+
it 'matches start with' do
|
224
|
+
expression = parser.parse('.def[name^=foo]')
|
225
|
+
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.last]
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'matches end with' do
|
229
|
+
expression = parser.parse('.def[name$=bar]')
|
230
|
+
expect(expression.query_nodes(node)).to eq [node.body.second, node.body.last]
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'matches contain' do
|
234
|
+
expression = parser.parse('.def[name*=oob]')
|
235
|
+
expect(expression.query_nodes(node)).to eq [node.body.last]
|
236
|
+
end
|
237
|
+
|
206
238
|
it 'matches not equal' do
|
207
239
|
expression = parser.parse('.def[name!=foobar]')
|
208
240
|
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.second]
|
@@ -310,7 +342,7 @@ module Synvert::Core::NodeQuery
|
|
310
342
|
end
|
311
343
|
|
312
344
|
it 'matches arguments' do
|
313
|
-
expression = parser.parse('.send[arguments
|
345
|
+
expression = parser.parse('.send[arguments.size=2][arguments.first=.sym][arguments.last=.hash]')
|
314
346
|
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
315
347
|
end
|
316
348
|
|
@@ -71,7 +71,7 @@ module Synvert::Core
|
|
71
71
|
condition = double
|
72
72
|
block = proc {}
|
73
73
|
expect(Rewriter::IfExistCondition).to receive(:new)
|
74
|
-
.with(instance, type: 'send', message: 'create', &block)
|
74
|
+
.with(instance, { type: 'send', message: 'create' }, &block)
|
75
75
|
.and_return(condition)
|
76
76
|
expect(condition).to receive(:process)
|
77
77
|
instance.if_exist_node(type: 'send', message: 'create', &block)
|
@@ -81,7 +81,7 @@ module Synvert::Core
|
|
81
81
|
condition = double
|
82
82
|
block = proc {}
|
83
83
|
expect(Rewriter::UnlessExistCondition).to receive(:new)
|
84
|
-
.with(instance, type: 'send', message: 'create', &block)
|
84
|
+
.with(instance, { type: 'send', message: 'create' }, &block)
|
85
85
|
.and_return(condition)
|
86
86
|
expect(condition).to receive(:process)
|
87
87
|
instance.unless_exist_node(type: 'send', message: 'create', &block)
|
@@ -91,7 +91,7 @@ module Synvert::Core
|
|
91
91
|
condition = double
|
92
92
|
block = proc {}
|
93
93
|
expect(Rewriter::IfOnlyExistCondition).to receive(:new)
|
94
|
-
.with(instance, type: 'send', message: 'create', &block)
|
94
|
+
.with(instance, { type: 'send', message: 'create' }, &block)
|
95
95
|
.and_return(condition)
|
96
96
|
expect(condition).to receive(:process)
|
97
97
|
instance.if_only_exist_node(type: 'send', message: 'create', &block)
|
@@ -167,16 +167,21 @@ module Synvert::Core
|
|
167
167
|
|
168
168
|
it 'parses remove' do
|
169
169
|
action = double
|
170
|
-
expect(Rewriter::RemoveAction).to receive(:new).with(instance).and_return(action)
|
170
|
+
expect(Rewriter::RemoveAction).to receive(:new).with(instance, { and_comma: true }).and_return(action)
|
171
171
|
expect(action).to receive(:process)
|
172
|
-
instance.remove
|
172
|
+
instance.remove and_comma: true
|
173
173
|
end
|
174
174
|
|
175
175
|
it 'parses remove' do
|
176
176
|
action = double
|
177
|
-
expect(Rewriter::DeleteAction).to receive(:new).with(
|
177
|
+
expect(Rewriter::DeleteAction).to receive(:new).with(
|
178
|
+
instance,
|
179
|
+
:dot,
|
180
|
+
:message,
|
181
|
+
{ and_comma: true }
|
182
|
+
).and_return(action)
|
178
183
|
expect(action).to receive(:process)
|
179
|
-
instance.delete :dot, :message
|
184
|
+
instance.delete :dot, :message, and_comma: true
|
180
185
|
end
|
181
186
|
|
182
187
|
it 'parses wrap with' do
|
@@ -59,15 +59,15 @@ module Synvert::Core
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'raises ParseError' do
|
62
|
-
scope = described_class.new(instance, '
|
62
|
+
scope = described_class.new(instance, 'synvert') {}
|
63
63
|
expect {
|
64
64
|
scope.process
|
65
65
|
}.to raise_error(NodeQuery::Compiler::ParseError)
|
66
66
|
|
67
|
-
scope = described_class.new(instance, '.
|
67
|
+
scope = described_class.new(instance, '.send[receiver IN FactoryGirl]') {}
|
68
68
|
expect {
|
69
69
|
scope.process
|
70
|
-
}.to raise_error(NodeQuery::Compiler::
|
70
|
+
}.to raise_error(NodeQuery::Compiler::InvalidOperatorError)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
data/synvert-core-ruby.gemspec
CHANGED
@@ -23,14 +23,4 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_runtime_dependency "activesupport"
|
24
24
|
spec.add_runtime_dependency "erubis"
|
25
25
|
spec.add_runtime_dependency "parser"
|
26
|
-
|
27
|
-
spec.add_development_dependency "bundler"
|
28
|
-
spec.add_development_dependency "guard"
|
29
|
-
spec.add_development_dependency "guard-rspec"
|
30
|
-
spec.add_development_dependency "guard-rake"
|
31
|
-
spec.add_development_dependency "oedipus_lex"
|
32
|
-
spec.add_development_dependency "racc"
|
33
|
-
spec.add_development_dependency "rake"
|
34
|
-
spec.add_development_dependency "rspec", "3.10.0"
|
35
|
-
spec.add_development_dependency "rspec-mocks", "3.10.2" # rspec-mocks 3.10.3 breaks tests
|
36
26
|
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.3.1
|
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-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -52,132 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: bundler
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: guard
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: guard-rspec
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: guard-rake
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: oedipus_lex
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - ">="
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: racc
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - ">="
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '0'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - ">="
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '0'
|
139
|
-
- !ruby/object:Gem::Dependency
|
140
|
-
name: rake
|
141
|
-
requirement: !ruby/object:Gem::Requirement
|
142
|
-
requirements:
|
143
|
-
- - ">="
|
144
|
-
- !ruby/object:Gem::Version
|
145
|
-
version: '0'
|
146
|
-
type: :development
|
147
|
-
prerelease: false
|
148
|
-
version_requirements: !ruby/object:Gem::Requirement
|
149
|
-
requirements:
|
150
|
-
- - ">="
|
151
|
-
- !ruby/object:Gem::Version
|
152
|
-
version: '0'
|
153
|
-
- !ruby/object:Gem::Dependency
|
154
|
-
name: rspec
|
155
|
-
requirement: !ruby/object:Gem::Requirement
|
156
|
-
requirements:
|
157
|
-
- - '='
|
158
|
-
- !ruby/object:Gem::Version
|
159
|
-
version: 3.10.0
|
160
|
-
type: :development
|
161
|
-
prerelease: false
|
162
|
-
version_requirements: !ruby/object:Gem::Requirement
|
163
|
-
requirements:
|
164
|
-
- - '='
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
version: 3.10.0
|
167
|
-
- !ruby/object:Gem::Dependency
|
168
|
-
name: rspec-mocks
|
169
|
-
requirement: !ruby/object:Gem::Requirement
|
170
|
-
requirements:
|
171
|
-
- - '='
|
172
|
-
- !ruby/object:Gem::Version
|
173
|
-
version: 3.10.2
|
174
|
-
type: :development
|
175
|
-
prerelease: false
|
176
|
-
version_requirements: !ruby/object:Gem::Requirement
|
177
|
-
requirements:
|
178
|
-
- - '='
|
179
|
-
- !ruby/object:Gem::Version
|
180
|
-
version: 3.10.2
|
181
55
|
description: convert ruby code to better syntax automatically.
|
182
56
|
email:
|
183
57
|
- flyerhzm@gmail.com
|
@@ -218,6 +92,7 @@ files:
|
|
218
92
|
- lib/synvert/core/node_query/compiler/parse_error.rb
|
219
93
|
- lib/synvert/core/node_query/compiler/regexp.rb
|
220
94
|
- lib/synvert/core/node_query/compiler/selector.rb
|
95
|
+
- lib/synvert/core/node_query/compiler/simple_selector.rb
|
221
96
|
- lib/synvert/core/node_query/compiler/string.rb
|
222
97
|
- lib/synvert/core/node_query/compiler/symbol.rb
|
223
98
|
- lib/synvert/core/node_query/lexer.rex
|