synvert-core 1.1.0 → 1.1.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 +5 -0
- data/lib/synvert/core/node_query/compiler/string.rb +0 -11
- data/lib/synvert/core/node_query/lexer.rex +4 -0
- data/lib/synvert/core/node_query/lexer.rex.rb +8 -0
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +29 -23
- data/spec/synvert/core/node_query/lexer_spec.rb +26 -0
- data/spec/synvert/core/node_query/parser_spec.rb +40 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3972bef6a4c4b41b4211e2f45d889c6e3c71b08b1b2b039ca3511a620f79ccf
|
4
|
+
data.tar.gz: 7ddecb20188351e72e59b4a72c67a45d395638ae7a20d3c4d46f3fef0184de3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b89480e5d9d05fe0651b5cd9edd1a4408a52364d449d0632baf062dd40251670b8c7d98370fa831f6961454498e9fbfffa67011a4fabb45f21e6031933643c36
|
7
|
+
data.tar.gz: c23844951b2938268289099f852f01d554e46a7aa80ad301011d05cc40a022864dd872a8097c71575a42e8770730dbfea2deea8d867af0412022506751cf2671
|
data/CHANGELOG.md
CHANGED
@@ -16,17 +16,6 @@ module Synvert::Core::NodeQuery::Compiler
|
|
16
16
|
SIMPLE_VALID_OPERATORS
|
17
17
|
end
|
18
18
|
|
19
|
-
# Get the actual value of a node.
|
20
|
-
# @param node [Parser::AST::Node] the node
|
21
|
-
# @return [String] if node is a Parser::AST::Node, return the node source code, otherwise, return the string value.
|
22
|
-
def actual_value(node)
|
23
|
-
if node.is_a?(::Parser::AST::Node)
|
24
|
-
node.to_source
|
25
|
-
else
|
26
|
-
node.to_s
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
19
|
def to_s
|
31
20
|
"\"#{@value}\""
|
32
21
|
end
|
@@ -54,6 +54,10 @@ rules
|
|
54
54
|
:KEY /in/i { @state = :VALUE; [:tIN, text] }
|
55
55
|
:KEY /#{IDENTIFIER}/ { [:tKEY, text] }
|
56
56
|
:VALUE /\s+/
|
57
|
+
:VALUE /\[\]=/ { [:tIDENTIFIER_VALUE, text] }
|
58
|
+
:VALUE /\[\]/ { [:tIDENTIFIER_VALUE, text] }
|
59
|
+
:VALUE /:\[\]=/ { [:tSYMBOL, text[1..-1].to_sym] }
|
60
|
+
:VALUE /:\[\]/ { [:tSYMBOL, text[1..-1].to_sym] }
|
57
61
|
:VALUE /#{OPEN_DYNAMIC_ATTRIBUTE}/ { @state = :DYNAMIC_ATTRIBUTE; [:tOPEN_DYNAMIC_ATTRIBUTE, text] }
|
58
62
|
:VALUE /#{OPEN_ARRAY}/ { @state = :ARRAY_VALUE; [:tOPEN_ARRAY, text] }
|
59
63
|
:VALUE /#{CLOSE_ATTRIBUTE}/ { @nested_count -= 1; @state = @nested_count == 0 ? nil : :VALUE; [:tCLOSE_ATTRIBUTE, text] }
|
@@ -191,6 +191,14 @@ class Synvert::Core::NodeQuery::Lexer
|
|
191
191
|
case
|
192
192
|
when ss.skip(/\s+/) then
|
193
193
|
# do nothing
|
194
|
+
when text = ss.scan(/\[\]=/) then
|
195
|
+
action { [:tIDENTIFIER_VALUE, text] }
|
196
|
+
when text = ss.scan(/\[\]/) then
|
197
|
+
action { [:tIDENTIFIER_VALUE, text] }
|
198
|
+
when text = ss.scan(/:\[\]=/) then
|
199
|
+
action { [:tSYMBOL, text[1..-1].to_sym] }
|
200
|
+
when text = ss.scan(/:\[\]/) then
|
201
|
+
action { [:tSYMBOL, text[1..-1].to_sym] }
|
194
202
|
when text = ss.scan(/#{OPEN_DYNAMIC_ATTRIBUTE}/) then
|
195
203
|
action { @state = :DYNAMIC_ATTRIBUTE; [:tOPEN_DYNAMIC_ATTRIBUTE, text] }
|
196
204
|
when text = ss.scan(/#{OPEN_ARRAY}/) then
|
data/lib/synvert/core/version.rb
CHANGED
@@ -1037,29 +1037,35 @@ describe Parser::AST::Node do
|
|
1037
1037
|
end
|
1038
1038
|
end
|
1039
1039
|
EOS
|
1040
|
-
expect(node.to_hash).to eq(
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1040
|
+
expect(node.to_hash).to eq(
|
1041
|
+
{
|
1042
|
+
type: :class,
|
1043
|
+
parent_class: nil,
|
1044
|
+
name: {
|
1045
|
+
type: :const,
|
1046
|
+
parent_const: nil,
|
1047
|
+
name: :Synvert
|
1048
|
+
},
|
1049
|
+
body: [
|
1050
|
+
{
|
1051
|
+
type: :def,
|
1052
|
+
name: :foobar,
|
1053
|
+
arguments: [
|
1054
|
+
{ type: :arg, name: :foo },
|
1055
|
+
{ type: :arg, name: :bar }
|
1056
|
+
],
|
1057
|
+
body: [
|
1058
|
+
{
|
1059
|
+
type: :send,
|
1060
|
+
receiver: { name: :foo, type: :lvar },
|
1061
|
+
message: :+,
|
1062
|
+
arguments: [{ name: :bar, type: :lvar }]
|
1063
|
+
}
|
1064
|
+
]
|
1065
|
+
}
|
1066
|
+
]
|
1067
|
+
}
|
1068
|
+
)
|
1063
1069
|
end
|
1064
1070
|
end
|
1065
1071
|
end
|
@@ -200,6 +200,32 @@ module Synvert::Core::NodeQuery
|
|
200
200
|
assert_tokens source, expected_tokens
|
201
201
|
end
|
202
202
|
|
203
|
+
it 'matches :[] message' do
|
204
|
+
source = ".send[message=[]]"
|
205
|
+
expected_tokens = [
|
206
|
+
[:tNODE_TYPE, "send"],
|
207
|
+
[:tOPEN_ATTRIBUTE, "["],
|
208
|
+
[:tKEY, "message"],
|
209
|
+
[:tEQUAL, "="],
|
210
|
+
[:tIDENTIFIER_VALUE, "[]"],
|
211
|
+
[:tCLOSE_ATTRIBUTE, "]"]
|
212
|
+
]
|
213
|
+
assert_tokens source, expected_tokens
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'matches :[] message' do
|
217
|
+
source = ".send[message=:[]=]"
|
218
|
+
expected_tokens = [
|
219
|
+
[:tNODE_TYPE, "send"],
|
220
|
+
[:tOPEN_ATTRIBUTE, "["],
|
221
|
+
[:tKEY, "message"],
|
222
|
+
[:tEQUAL, "="],
|
223
|
+
[:tSYMBOL, :[]=],
|
224
|
+
[:tCLOSE_ATTRIBUTE, "]"]
|
225
|
+
]
|
226
|
+
assert_tokens source, expected_tokens
|
227
|
+
end
|
228
|
+
|
203
229
|
it 'matches attribute value' do
|
204
230
|
source = '.pair[key={{value}}]'
|
205
231
|
expected_tokens = [
|
@@ -118,6 +118,16 @@ module Synvert::Core::NodeQuery
|
|
118
118
|
assert_parser(source)
|
119
119
|
end
|
120
120
|
|
121
|
+
it 'parses []=' do
|
122
|
+
source = '.send[message=[]=]'
|
123
|
+
assert_parser(source)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'parses :[]' do
|
127
|
+
source = '.send[message=:[]]'
|
128
|
+
assert_parser(source)
|
129
|
+
end
|
130
|
+
|
121
131
|
describe '#query_nodes' do
|
122
132
|
let(:node) {
|
123
133
|
parse(<<~EOS)
|
@@ -134,6 +144,8 @@ module Synvert::Core::NodeQuery
|
|
134
144
|
{ a: a, b: b }
|
135
145
|
foo.merge(bar)
|
136
146
|
arr[index]
|
147
|
+
arr[index] = value
|
148
|
+
call('')
|
137
149
|
end
|
138
150
|
end
|
139
151
|
EOS
|
@@ -212,17 +224,17 @@ module Synvert::Core::NodeQuery
|
|
212
224
|
|
213
225
|
it 'matches descendant node' do
|
214
226
|
expression = parser.parse('.class .send[message=:create]')
|
215
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.
|
227
|
+
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
216
228
|
end
|
217
229
|
|
218
230
|
it 'matches three level descendant node' do
|
219
231
|
expression = parser.parse('.class .def .send[message=:create]')
|
220
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.
|
232
|
+
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
221
233
|
end
|
222
234
|
|
223
235
|
it 'matches child node' do
|
224
236
|
expression = parser.parse('.def > .send[message=:create]')
|
225
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.
|
237
|
+
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
226
238
|
end
|
227
239
|
|
228
240
|
it 'matches next sibling node' do
|
@@ -246,17 +258,25 @@ module Synvert::Core::NodeQuery
|
|
246
258
|
end
|
247
259
|
|
248
260
|
it 'matches arguments.size' do
|
249
|
-
expression = parser.parse('.send[arguments.size=2]')
|
250
|
-
expect(expression.query_nodes(node)).to eq [
|
251
|
-
|
261
|
+
expression = parser.parse('.def .send[arguments.size=2]')
|
262
|
+
expect(expression.query_nodes(node)).to eq [
|
263
|
+
node.body.first.body.last,
|
264
|
+
node.body.second.body.last,
|
265
|
+
node.body.third.body.fourth
|
266
|
+
]
|
267
|
+
expression = parser.parse('.def .send[arguments.size>2]')
|
252
268
|
expect(expression.query_nodes(node)).to eq []
|
253
|
-
expression = parser.parse('.send[arguments.size>=2]')
|
254
|
-
expect(expression.query_nodes(node)).to eq [
|
269
|
+
expression = parser.parse('.def .send[arguments.size>=2]')
|
270
|
+
expect(expression.query_nodes(node)).to eq [
|
271
|
+
node.body.first.body.last,
|
272
|
+
node.body.second.body.last,
|
273
|
+
node.body.third.body.fourth
|
274
|
+
]
|
255
275
|
end
|
256
276
|
|
257
277
|
it 'matches arguments' do
|
258
278
|
expression = parser.parse('.send[arguments=[size=2][first=.sym][last=.hash]]')
|
259
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.
|
279
|
+
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
260
280
|
end
|
261
281
|
|
262
282
|
it 'matches regexp value' do
|
@@ -277,9 +297,19 @@ module Synvert::Core::NodeQuery
|
|
277
297
|
end
|
278
298
|
|
279
299
|
it 'matches []' do
|
280
|
-
expression = parser.parse('.send[message=
|
300
|
+
expression = parser.parse('.send[message=[]]')
|
281
301
|
expect(expression.query_nodes(node)).to eq [node.body.last.body.third]
|
282
302
|
end
|
303
|
+
|
304
|
+
it 'matches []=' do
|
305
|
+
expression = parser.parse('.send[message=:[]=]')
|
306
|
+
expect(expression.query_nodes(node)).to eq [node.body.last.body.fourth]
|
307
|
+
end
|
308
|
+
|
309
|
+
it 'matches empty string' do
|
310
|
+
expression = parser.parse('.send[message=call][arguments.first=""]')
|
311
|
+
expect(expression.query_nodes(node)).to eq [node.body.last.body.last]
|
312
|
+
end
|
283
313
|
end
|
284
314
|
end
|
285
315
|
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.1.
|
4
|
+
version: 1.1.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-04-
|
11
|
+
date: 2022-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|