synvert-core 0.31.0 → 0.35.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf20c3a501940025291f9e0ae6f306d1613fb4db8ee5d5cf649d0ad7da4d1c5a
4
- data.tar.gz: 425f42f736ec76ca6ade1f881ab252bfa6659f0e2cc8f3b55dd31153a4d8853a
3
+ metadata.gz: 9c718983fb8e6e6216ac660fba2aa1b9beeacf96db451d65b4df3df827a396c0
4
+ data.tar.gz: a6fd2918b8de9d92bfe4a50f1c135154d5b94eacd21b3e88c740e1869e4c4686
5
5
  SHA512:
6
- metadata.gz: 52c43ad9b8986c68a4c77200e84b9cf7497fec7c46f0beb74fa695ed13d986f38519c167a3242d870dca79a0e09844fe6b564d6a54c258801801d4920be2f9da
7
- data.tar.gz: 20aeb9c56536f335d8181e307bcf96ad8b3cbc7c02471077657a1ec0fa23db6c6f10269a4477aef3007ffc0b1dd07f3bb522289ecfed2c5f8f581abd49b3ee8b
6
+ metadata.gz: 8a828ec38b3d152a6620dd8fb5101a4fa0594da95eaede5612dd805f8b719bc52ebcaa1c00de48bb17fb306ebcf0cd6a7beb109dc91602afd5e305aafe521e47
7
+ data.tar.gz: 496d20ef84817d2e8073573b67635f8cb520fc430a647eba909ff9a49dce8fa4f9f5c99245123aa390d7b7d0356c955ca1f78c8073ee17984e1dfa14a8914e51
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.35.0 (2021-05-17)
4
+
5
+ * Add `contain` rule
6
+
7
+ ## 0.34.0 (2021-05-16)
8
+
9
+ * `child_node_name` supports [:def, :parentheses] and [:defs, :parentheses]
10
+ * Rename `pipe` to `pipes`
11
+
12
+ ## 0.33.0 (2021-05-10)
13
+
14
+ * Add `body` for `class` node
15
+
16
+ ## 0.32.0 (2021-05-07)
17
+
18
+ * Remove `ArgumentsNode`
19
+
3
20
  ## 0.31.0 (2021-04-27)
4
21
 
5
22
  * Add `in` and `not_in` rules
@@ -1,29 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Parser::AST
4
- # ArgumentsNode allows to handle all args as one node or handle all args as an array.
5
- class ArgumentsNode
6
- # Initialize
7
- #
8
- # @param node [Parser::AST::Node] args node.
9
- def initialize(node)
10
- @node = node
11
- end
12
-
13
- # If args node responds method itself, call method on args node.
14
- # If args children (array) responds method, call method on args children.
15
- # Otherwise raise method missing error.
16
- def method_missing(meth, *args, &block)
17
- if @node.respond_to?(meth)
18
- @node.send meth, *args, &block
19
- elsif @node.children.respond_to?(meth)
20
- @node.children.send meth, *args, &block
21
- else
22
- super
23
- end
24
- end
25
- end
26
-
27
4
  # Parser::AST::Node monkey patch.
28
5
  class Node
29
6
  # Get name node of :class, :module, :const, :mlhs, :def and :defs node.
@@ -101,9 +78,9 @@ module Parser::AST
101
78
  def arguments
102
79
  case type
103
80
  when :def, :block
104
- ArgumentsNode.new(children[1])
81
+ children[1]
105
82
  when :defs
106
- ArgumentsNode.new(children[2])
83
+ children[2]
107
84
  when :send
108
85
  children[2..-1]
109
86
  when :defined?
@@ -133,7 +110,7 @@ module Parser::AST
133
110
  case type
134
111
  when :begin
135
112
  children
136
- when :def, :block
113
+ when :def, :block, :class
137
114
  return [] if children[2].nil?
138
115
 
139
116
  :begin == children[2].type ? children[2].body : children[2..-1]
@@ -285,7 +262,9 @@ module Parser::AST
285
262
  # Current node is s(:hash, s(:pair, s(:sym, :number), s(:int, 10)))
286
263
  # node.number_value is 10
287
264
  def method_missing(method_name, *args, &block)
288
- if :hash == type && method_name.to_s.include?('_value')
265
+ if :args == type && children.respond_to?(method_name)
266
+ return children.send(method_name, *args, &block)
267
+ elsif :hash == type && method_name.to_s.include?('_value')
289
268
  key = method_name.to_s.sub('_value', '')
290
269
  return hash_value(key.to_sym)&.to_value if key?(key.to_sym)
291
270
  return hash_value(key.to_s)&.to_value if key?(key.to_s)
@@ -297,7 +276,9 @@ module Parser::AST
297
276
  end
298
277
 
299
278
  def respond_to_missing?(method_name, *args)
300
- if :hash == type && method_name.to_s.include?('_value')
279
+ if :args == type && children.respond_to?(method_name)
280
+ return true
281
+ elsif :hash == type && method_name.to_s.include?('_value')
301
282
  key = method_name.to_s.sub('_value', '')
302
283
  return true if key?(key.to_sym) || key?(key.to_s)
303
284
  end
@@ -348,18 +329,16 @@ module Parser::AST
348
329
  # @return [Parser::Source::Range] source range of child node.
349
330
  def child_node_range(child_name)
350
331
  case [type, child_name]
351
- when %i[block pipe]
332
+ when %i[block pipes], %i[def parentheses], %i[defs parentheses]
352
333
  Parser::Source::Range.new('(string)', arguments.loc.expression.begin_pos, arguments.loc.expression.end_pos)
353
- when %i[class name]
354
- loc.name
355
- when %i[def name]
356
- loc.name
357
- when %i[defs name]
334
+ when %i[block arguments], %i[def arguments], %i[defs arguments]
335
+ Parser::Source::Range.new('(string)', arguments.first.loc.expression.begin_pos, arguments.last.loc.expression.end_pos)
336
+ when %i[class name], %i[def name], %i[defs name]
358
337
  loc.name
359
338
  when %i[defs dot]
360
339
  loc.operator
361
340
  when %i[defs self]
362
- Parser::Source::Range.new('(string)', loc.operator.begin_pos - 4, loc.operator.begin_pos)
341
+ Parser::Source::Range.new('(string)', loc.operator.begin_pos - 'self'.length, loc.operator.begin_pos)
363
342
  when %i[send dot]
364
343
  loc.dot
365
344
  when %i[send message]
@@ -420,7 +399,7 @@ module Parser::AST
420
399
  def match?(rules)
421
400
  flat_hash(rules).keys.all? do |multi_keys|
422
401
  case multi_keys.last
423
- when :any
402
+ when :any, :contain
424
403
  actual_values = actual_value(self, multi_keys[0...-1])
425
404
  expected = expected_value(rules, multi_keys)
426
405
  actual_values.any? { |actual| match_value?(actual, expected) }
@@ -458,8 +437,12 @@ module Parser::AST
458
437
  evaluated = instance_eval old_code
459
438
  case evaluated
460
439
  when Parser::AST::Node
461
- evaluated.loc.expression.source
462
- when Array, ArgumentsNode
440
+ if evaluated.type == :args
441
+ evaluated.loc.expression.source[1...-1]
442
+ else
443
+ evaluated.loc.expression.source
444
+ end
445
+ when Array
463
446
  if evaluated.size > 0
464
447
  file_source = evaluated.first.loc.expression.source_buffer.source
465
448
  source = file_source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
@@ -302,7 +302,7 @@ module Synvert::Core
302
302
 
303
303
  begin_pos = @actions[i].begin_pos
304
304
  while j > -1
305
- if begin_pos <= @actions[j].end_pos
305
+ if begin_pos < @actions[j].end_pos
306
306
  conflict_actions << @actions.delete_at(j)
307
307
  else
308
308
  i = j
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.31.0'
5
+ VERSION = '0.35.0'
6
6
  end
7
7
  end
@@ -104,17 +104,17 @@ describe Parser::AST::Node do
104
104
  describe '#arguments' do
105
105
  it 'gets for def node' do
106
106
  node = parse('def test(foo, bar); foo + bar; end')
107
- expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
107
+ expect(node.arguments.type).to eq :args
108
108
  end
109
109
 
110
110
  it 'gets for defs node' do
111
111
  node = parse('def self.test(foo, bar); foo + bar; end')
112
- expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
112
+ expect(node.arguments.type).to eq :args
113
113
  end
114
114
 
115
115
  it 'gets for block node' do
116
116
  node = parse('RSpec.configure do |config|; end')
117
- expect(node.arguments.map { |argument| argument.to_source }).to eq ['config']
117
+ expect(node.arguments.type).to eq :args
118
118
  end
119
119
 
120
120
  it 'gets for send node' do
@@ -146,6 +146,21 @@ describe Parser::AST::Node do
146
146
  expect(node.body).to eq [parse('include EmailSpec::Helpers'), parse('include EmailSpec::Matchers')]
147
147
  end
148
148
 
149
+ it 'gets empty for class node' do
150
+ node = parse('class User; end')
151
+ expect(node.body).to be_empty
152
+ end
153
+
154
+ it 'gets one line for class node' do
155
+ node = parse('class User; attr_accessor :email; end')
156
+ expect(node.body).to eq [parse('attr_accessor :email')]
157
+ end
158
+
159
+ it 'gets one line for class node' do
160
+ node = parse('class User; attr_accessor :email; attr_accessor :username; end')
161
+ expect(node.body).to eq [parse('attr_accessor :email'), parse('attr_accessor :username')]
162
+ end
163
+
149
164
  it 'gets for begin node' do
150
165
  node = parse('foo; bar')
151
166
  expect(node.body).to eq [parse('foo'), parse('bar')]
@@ -399,6 +414,12 @@ describe Parser::AST::Node do
399
414
  expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
400
415
  end
401
416
 
417
+ it 'matches arguments contain' do
418
+ source = 'def slow(foo, bar, &block); end'
419
+ node = parse(source)
420
+ expect(node).to be_match(type: 'def', arguments: { contain: '&block' })
421
+ end
422
+
402
423
  it 'matches not' do
403
424
  source = 'class Synvert; end'
404
425
  node = parse(source)
@@ -408,13 +429,13 @@ describe Parser::AST::Node do
408
429
  it 'matches in' do
409
430
  source = 'FactoryBot.create(:user)'
410
431
  node = parse(source)
411
- expect(node).to be_match(type: 'send', message: { in: [:create, :build] })
432
+ expect(node).to be_match(type: 'send', message: { in: %i[create build] })
412
433
  end
413
434
 
414
435
  it 'matches not_in' do
415
436
  source = 'FactoryBot.create(:user)'
416
437
  node = parse(source)
417
- expect(node).not_to be_match(type: 'send', message: { not_in: [:create, :build] })
438
+ expect(node).not_to be_match(type: 'send', message: { not_in: %i[create build] })
418
439
  end
419
440
  end
420
441
 
@@ -432,9 +453,9 @@ describe Parser::AST::Node do
432
453
  expect(range.to_range).to eq(25...29)
433
454
  end
434
455
 
435
- it 'checks pipe' do
456
+ it 'checks pipes' do
436
457
  node = parse('Factory.define :user do |user|; end')
437
- range = node.child_node_range(:pipe)
458
+ range = node.child_node_range(:pipes)
438
459
  expect(range.to_range).to eq(24...30)
439
460
  end
440
461
  end
@@ -469,6 +490,12 @@ describe Parser::AST::Node do
469
490
  range = node.child_node_range(:arguments)
470
491
  expect(range.to_range).to eq(8...11)
471
492
  end
493
+
494
+ it 'checks parentheses' do
495
+ node = parse('def foo(bar); end')
496
+ range = node.child_node_range(:parentheses)
497
+ expect(range.to_range).to eq(7...12)
498
+ end
472
499
  end
473
500
 
474
501
  context 'defs node' do
@@ -495,6 +522,12 @@ describe Parser::AST::Node do
495
522
  range = node.child_node_range(:arguments)
496
523
  expect(range.to_range).to eq(13...16)
497
524
  end
525
+
526
+ it 'checks parentheses' do
527
+ node = parse('def self.foo(bar); end')
528
+ range = node.child_node_range(:parentheses)
529
+ expect(range.to_range).to eq(12...17)
530
+ end
498
531
  end
499
532
 
500
533
  context 'send node' do
@@ -566,7 +599,7 @@ describe Parser::AST::Node do
566
599
  expect(node.rewritten_source('{{name}}')).to eq 'Synvert'
567
600
  end
568
601
 
569
- it 'rewrites for ArgumentsNode' do
602
+ it 'rewrites for arguments' do
570
603
  source = 'test { |a, b| }'
571
604
  node = parse(source)
572
605
  expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
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: 0.31.0
4
+ version: 0.35.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: 2021-04-27 00:00:00.000000000 Z
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport