synvert-core 1.1.0 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 603dafd1ddc6f40f8ef1479d359e39e1f4dbe90067bcfe51f721b15e9571c4d0
4
- data.tar.gz: e423e49c531e6ba91f490657f34564f781f3313d82dbf9a863ee2c17985500a8
3
+ metadata.gz: b3972bef6a4c4b41b4211e2f45d889c6e3c71b08b1b2b039ca3511a620f79ccf
4
+ data.tar.gz: 7ddecb20188351e72e59b4a72c67a45d395638ae7a20d3c4d46f3fef0184de3c
5
5
  SHA512:
6
- metadata.gz: e6177477cdf8c0cdd67d6dc533145017daebee61905b5e48b992261f6f458b8a949796aa86eb0cd8f161ffaa05b9325e1cd671b6d63fd645ece7f2101e67859c
7
- data.tar.gz: c1daa4cca1056d9266c01037217cd6570f0b7ee0228491e408d43c7bf8bf753a52b2dbfbf20d6aa568876d7e1d7ee11727db40adbc56e17fc603ae091804475a
6
+ metadata.gz: b89480e5d9d05fe0651b5cd9edd1a4408a52364d449d0632baf062dd40251670b8c7d98370fa831f6961454498e9fbfffa67011a4fabb45f21e6031933643c36
7
+ data.tar.gz: c23844951b2938268289099f852f01d554e46a7aa80ad301011d05cc40a022864dd872a8097c71575a42e8770730dbfea2deea8d867af0412022506751cf2671
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.1.1 (2022-04-27)
4
+
5
+ * Parse empty string properly in node query language
6
+ * Parse `[]` and `[]=` properly in node query language
7
+
3
8
  ## 1.1.0 (2022-04-26)
4
9
 
5
10
  * Dynamic define Node methods by `TYPE_CHILDREN` const
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.1.0'
5
+ VERSION = '1.1.1'
6
6
  end
7
7
  end
@@ -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
- type: :class,
1042
- parent_class: nil,
1043
- name: {
1044
- type: :const,
1045
- parent_const: nil,
1046
- name: :Synvert
1047
- },
1048
- body: [{
1049
- type: :def,
1050
- name: :foobar,
1051
- arguments: [
1052
- { type: :arg, name: :foo},
1053
- { type: :arg, name: :bar}
1054
- ],
1055
- body: [{
1056
- type: :send,
1057
- receiver: { name: :foo, type: :lvar },
1058
- message: :+,
1059
- arguments: [{ name: :bar, type: :lvar }]
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.children.last, node.body.second.children.last]
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.children.last, node.body.second.children.last]
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.children.last, node.body.second.children.last]
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 [node.body.first.children.last, node.body.second.children.last]
251
- expression = parser.parse('.send[arguments.size>2]')
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 [node.body.first.children.last, node.body.second.children.last]
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.children.last, node.body.second.children.last]
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.0
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-26 00:00:00.000000000 Z
11
+ date: 2022-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport