parser 2.7.1.2 → 2.7.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -6
  3. data/CHANGELOG.md +17 -1
  4. data/README.md +1 -2
  5. data/Rakefile +1 -1
  6. data/doc/AST_FORMAT.md +22 -0
  7. data/lib/parser/ast/processor.rb +3 -0
  8. data/lib/parser/builders/default.rb +9 -0
  9. data/lib/parser/diagnostic.rb +1 -1
  10. data/lib/parser/diagnostic/engine.rb +1 -2
  11. data/lib/parser/lexer.rl +7 -0
  12. data/lib/parser/messages.rb +15 -0
  13. data/lib/parser/meta.rb +1 -1
  14. data/lib/parser/ruby28.y +62 -20
  15. data/lib/parser/runner.rb +21 -2
  16. data/lib/parser/runner/ruby_rewrite.rb +2 -2
  17. data/lib/parser/source/buffer.rb +3 -1
  18. data/lib/parser/source/comment/associator.rb +14 -4
  19. data/lib/parser/source/range.rb +7 -0
  20. data/lib/parser/source/tree_rewriter.rb +1 -1
  21. data/lib/parser/tree_rewriter.rb +1 -2
  22. data/lib/parser/version.rb +1 -1
  23. data/parser.gemspec +1 -1
  24. data/test/helper.rb +24 -6
  25. data/test/parse_helper.rb +9 -16
  26. data/test/test_ast_processor.rb +32 -0
  27. data/test/test_base.rb +1 -1
  28. data/test/test_diagnostic.rb +6 -7
  29. data/test/test_diagnostic_engine.rb +5 -8
  30. data/test/test_lexer.rb +5 -8
  31. data/test/test_meta.rb +12 -0
  32. data/test/test_parser.rb +84 -13
  33. data/test/test_runner_parse.rb +22 -1
  34. data/test/test_runner_rewrite.rb +1 -1
  35. data/test/test_source_buffer.rb +4 -1
  36. data/test/test_source_comment.rb +2 -2
  37. data/test/test_source_comment_associator.rb +47 -15
  38. data/test/test_source_map.rb +1 -2
  39. data/test/test_source_range.rb +16 -11
  40. data/test/test_source_rewriter.rb +4 -4
  41. data/test/test_source_rewriter_action.rb +2 -2
  42. data/test/test_source_tree_rewriter.rb +3 -3
  43. metadata +11 -7
@@ -7,7 +7,7 @@ class TestRunnerParse < Minitest::Test
7
7
  PATH_TO_RUBY_PARSE = File.expand_path('../bin/ruby-parse', __dir__).freeze
8
8
 
9
9
  def assert_prints(argv, expected_output)
10
- stdout, stderr, status = Open3.capture3(PATH_TO_RUBY_PARSE, *argv)
10
+ stdout, _stderr, status = Open3.capture3(PATH_TO_RUBY_PARSE, *argv)
11
11
 
12
12
  assert_equal 0, status.to_i
13
13
  assert_includes(stdout, expected_output)
@@ -18,6 +18,27 @@ class TestRunnerParse < Minitest::Test
18
18
  's(:int, 123)'
19
19
  end
20
20
 
21
+ def test_emit_modern_ruby
22
+ assert_prints ['-e', '->{}'],
23
+ '(lambda)'
24
+ assert_prints ['-e', 'self[1] = 2'],
25
+ 'indexasgn'
26
+ end
27
+
28
+ def test_emit_legacy
29
+ assert_prints ['--legacy', '-e', '->{}'],
30
+ '(send nil :lambda)'
31
+ assert_prints ['--legacy', '-e', 'self[1] = 2'],
32
+ ':[]='
33
+ end
34
+
35
+ def test_emit_legacy_lambda
36
+ assert_prints ['--legacy-lambda', '-e', '->{}'],
37
+ '(send nil :lambda)'
38
+ assert_prints ['--legacy-lambda', '-e', 'self[1] = 2'],
39
+ 'indexasgn'
40
+ end
41
+
21
42
  def test_emit_json
22
43
  assert_prints ['--emit-json', '-e', '123'],
23
44
  '["int",123]'
@@ -21,7 +21,7 @@ class TestRunnerRewrite < Minitest::Test
21
21
  expected_file = @fixtures_dir + output
22
22
 
23
23
  FileUtils.cp(@fixtures_dir + input, sample_file_expanded)
24
- stdout, stderr, exit_code = Dir.chdir @test_dir do
24
+ stdout, stderr, _exit_code = Dir.chdir @test_dir do
25
25
  Open3.capture3 %Q{
26
26
  #{Shellwords.escape(@ruby_rewrite.to_s)} #{args} \
27
27
  #{Shellwords.escape(sample_file_expanded.to_s)}
@@ -20,6 +20,9 @@ class TestSourceBuffer < Minitest::Test
20
20
 
21
21
  buffer = Parser::Source::Buffer.new('(string)', 5)
22
22
  assert_equal 5, buffer.first_line
23
+
24
+ buffer = Parser::Source::Buffer.new('(string)', source: '2+2')
25
+ assert_equal '2+2', buffer.source
23
26
  end
24
27
 
25
28
  def test_source_setter
@@ -45,7 +48,7 @@ class TestSourceBuffer < Minitest::Test
45
48
  ].join("\n")
46
49
  end
47
50
 
48
- assert_match /invalid byte sequence in UTF\-8/, error.message
51
+ assert_match(/invalid byte sequence in UTF\-8/, error.message)
49
52
  end
50
53
 
51
54
  def test_read
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceComment < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(string)')
8
- @buf.source = "# foo\n=begin foo\nbar\n=end baz\n"
7
+ @buf = Parser::Source::Buffer.new('(string)',
8
+ source: "# foo\n=begin foo\nbar\n=end baz\n")
9
9
  end
10
10
 
11
11
  def range(s, e)
@@ -95,7 +95,7 @@ class Foo
95
95
  end
96
96
  END
97
97
 
98
- klass_node = ast
98
+ _klass_node = ast
99
99
  method_node = ast.children[2]
100
100
  body = method_node.children[2]
101
101
  f1_1_node = body.children[0]
@@ -175,7 +175,7 @@ class Foo
175
175
  end
176
176
  END
177
177
 
178
- klass_node = ast
178
+ _klass_node = ast
179
179
  method_node = ast.children[2]
180
180
  body = method_node.children[2]
181
181
  f1_1_node = body.children[0]
@@ -201,12 +201,12 @@ end
201
201
  end
202
202
 
203
203
  def test_associate_empty_tree
204
- ast, associations = associate("")
204
+ _ast, associations = associate("")
205
205
  assert_equal 0, associations.size
206
206
  end
207
207
 
208
208
  def test_associate_shebang_only
209
- ast, associations = associate(<<-END)
209
+ _ast, associations = associate(<<-END)
210
210
  #!ruby
211
211
  class Foo
212
212
  end
@@ -216,7 +216,7 @@ end
216
216
  end
217
217
 
218
218
  def test_associate_frozen_string_literal
219
- ast, associations = associate(<<-END)
219
+ _ast, associations = associate(<<-END)
220
220
  # frozen_string_literal: true
221
221
  class Foo
222
222
  end
@@ -226,7 +226,7 @@ end
226
226
  end
227
227
 
228
228
  def test_associate_frozen_string_literal_dash_star_dash
229
- ast, associations = associate(<<-END)
229
+ _ast, associations = associate(<<-END)
230
230
  # -*- frozen_string_literal: true -*-
231
231
  class Foo
232
232
  end
@@ -236,7 +236,7 @@ end
236
236
  end
237
237
 
238
238
  def test_associate_frozen_string_literal_no_space_after_colon
239
- ast, associations = associate(<<-END)
239
+ _ast, associations = associate(<<-END)
240
240
  # frozen_string_literal:true
241
241
  class Foo
242
242
  end
@@ -246,7 +246,7 @@ end
246
246
  end
247
247
 
248
248
  def test_associate_warn_indent
249
- ast, associations = associate(<<-END)
249
+ _ast, associations = associate(<<-END)
250
250
  # warn_indent: true
251
251
  class Foo
252
252
  end
@@ -256,7 +256,7 @@ end
256
256
  end
257
257
 
258
258
  def test_associate_warn_indent_dash_star_dash
259
- ast, associations = associate(<<-END)
259
+ _ast, associations = associate(<<-END)
260
260
  # -*- warn_indent: true -*-
261
261
  class Foo
262
262
  end
@@ -266,7 +266,7 @@ end
266
266
  end
267
267
 
268
268
  def test_associate_warn_past_scope
269
- ast, associations = associate(<<-END)
269
+ _ast, associations = associate(<<-END)
270
270
  # warn_past_scope: true
271
271
  class Foo
272
272
  end
@@ -276,7 +276,7 @@ end
276
276
  end
277
277
 
278
278
  def test_associate_warn_past_scope_dash_star_dash
279
- ast, associations = associate(<<-END)
279
+ _ast, associations = associate(<<-END)
280
280
  # -*- warn_past_scope: true -*-
281
281
  class Foo
282
282
  end
@@ -286,7 +286,7 @@ end
286
286
  end
287
287
 
288
288
  def test_associate_multiple
289
- ast, associations = associate(<<-END)
289
+ _ast, associations = associate(<<-END)
290
290
  # frozen_string_literal: true; warn_indent: true
291
291
  class Foo
292
292
  end
@@ -296,7 +296,7 @@ end
296
296
  end
297
297
 
298
298
  def test_associate_multiple_dash_star_dash
299
- ast, associations = associate(<<-END)
299
+ _ast, associations = associate(<<-END)
300
300
  # -*- frozen_string_literal: true; warn_indent: true -*-
301
301
  class Foo
302
302
  end
@@ -306,7 +306,7 @@ end
306
306
  end
307
307
 
308
308
  def test_associate_no_comments
309
- ast, associations = associate(<<-END)
309
+ _ast, associations = associate(<<-END)
310
310
  class Foo
311
311
  end
312
312
  END
@@ -315,7 +315,7 @@ end
315
315
  end
316
316
 
317
317
  def test_associate_comments_after_root_node
318
- ast, associations = associate(<<-END)
318
+ _ast, associations = associate(<<-END)
319
319
  class Foo
320
320
  end
321
321
  # not associated
@@ -364,4 +364,36 @@ x
364
364
  assert_equal ['# bar'],
365
365
  associations[send_node].map(&:text)
366
366
  end
367
+
368
+ def test_associate_conditional_parent_class
369
+ ast, associations = associate(<<-END)
370
+ class Foo
371
+ # bar
372
+ class Bar
373
+ end
374
+ end if some_condition
375
+ END
376
+
377
+ if_node = ast
378
+ _condition, foo_class = if_node.children
379
+ _foo, _sub_class, bar_class = foo_class.children
380
+
381
+ assert_equal 1, associations.size
382
+ assert_equal ['# bar'],
383
+ associations[bar_class].map(&:text)
384
+ end
385
+
386
+ def test_children_in_source_order
387
+ obj = Parser::Source::Comment::Associator.new(nil, nil)
388
+ for_each_node do |node|
389
+ with_loc = node.children.select do |child|
390
+ child.is_a?(AST::Node) && child.loc && child.loc.expression
391
+ end
392
+ slow_sort = with_loc.sort_by.with_index do |child, index| # Index to ensure stable sort
393
+ [child.loc.expression.begin_pos, index]
394
+ end
395
+ optimized = obj.send(:children_in_source_order, node)
396
+ assert_equal slow_sort, optimized, "children_in_source_order incorrect for #{node}"
397
+ end
398
+ end
367
399
  end
@@ -7,8 +7,7 @@ class TestSourceMap < Minitest::Test
7
7
  include ParseHelper
8
8
 
9
9
  def test_to_hash
10
- buf = Parser::Source::Buffer.new("<input>")
11
- buf.source = "1"
10
+ buf = Parser::Source::Buffer.new("<input>", source: "1")
12
11
  ast = parser_for_ruby_version('1.8').parse(buf)
13
12
  assert_equal [:expression, :operator], ast.loc.to_hash.keys.sort_by(&:to_s)
14
13
  end
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceRange < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(string)')
8
- @buf.source = "foobar\nbaz"
7
+ @buf = Parser::Source::Buffer.new('(string)',
8
+ source: "foobar\nbaz")
9
9
  @sr1_3 = Parser::Source::Range.new(@buf, 1, 3)
10
10
  @sr2_2 = Parser::Source::Range.new(@buf, 2, 2)
11
11
  @sr3_3 = Parser::Source::Range.new(@buf, 3, 3)
@@ -95,15 +95,15 @@ class TestSourceRange < Minitest::Test
95
95
 
96
96
  def test_order
97
97
  assert_equal 0, @sr1_3 <=> @sr1_3
98
- assert_equal -1, @sr1_3 <=> @sr5_8
99
- assert_equal -1, @sr2_2 <=> @sr2_6
100
- assert_equal +1, @sr2_6 <=> @sr2_2
98
+ assert_equal(-1, @sr1_3 <=> @sr5_8)
99
+ assert_equal(-1, @sr2_2 <=> @sr2_6)
100
+ assert_equal(+1, @sr2_6 <=> @sr2_2)
101
101
 
102
- assert_equal -1, @sr1_3 <=> @sr2_6
102
+ assert_equal(-1, @sr1_3 <=> @sr2_6)
103
103
 
104
- assert_equal +1, @sr2_2 <=> @sr1_3
105
- assert_equal -1, @sr1_3 <=> @sr2_2
106
- assert_equal -1, @sr5_7 <=> @sr5_8
104
+ assert_equal(+1, @sr2_2 <=> @sr1_3)
105
+ assert_equal(-1, @sr1_3 <=> @sr2_2)
106
+ assert_equal(-1, @sr5_7 <=> @sr5_8)
107
107
 
108
108
  assert_nil @sr1_3 <=> Parser::Source::Range.new(@buf.dup, 1, 3)
109
109
  assert_nil @sr1_3 <=> 4
@@ -155,6 +155,11 @@ class TestSourceRange < Minitest::Test
155
155
  refute sr.is?('bar')
156
156
  end
157
157
 
158
+ def test_to_range
159
+ sr = Parser::Source::Range.new(@buf, 10, 20)
160
+ assert_equal (10...20), sr.to_range
161
+ end
162
+
158
163
  def test_to_s
159
164
  sr = Parser::Source::Range.new(@buf, 8, 9)
160
165
  assert_equal '(string):2:2', sr.to_s
@@ -178,8 +183,8 @@ class TestSourceRange < Minitest::Test
178
183
  assert_equal true, @sr1_3.eql?(also_1_3)
179
184
  assert_equal @sr1_3.hash, also_1_3.hash
180
185
 
181
- buf2 = Parser::Source::Buffer.new('(string)')
182
- buf2.source = "foobar\nbaz"
186
+ buf2 = Parser::Source::Buffer.new('(string)',
187
+ source: "foobar\nbaz")
183
188
  from_other_buf = Parser::Source::Range.new(buf2, 1, 3)
184
189
  assert_equal false, @sr1_3.eql?(from_other_buf)
185
190
  assert @sr1_3.hash != from_other_buf.hash
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceRewriter < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter)')
8
- @buf.source = 'foo bar baz'
7
+ @buf = Parser::Source::Buffer.new('(rewriter)',
8
+ source: 'foo bar baz')
9
9
  Parser::Source::Rewriter.warned_of_deprecation = true
10
10
  @rewriter = Parser::Source::Rewriter.new(@buf)
11
11
  end
@@ -522,7 +522,7 @@ class TestSourceRewriter < Minitest::Test
522
522
  end
523
523
  end
524
524
 
525
- assert_match /nested/i, error.message
525
+ assert_match(/nested/i, error.message)
526
526
  end
527
527
 
528
528
  def test_process_in_transaction_raises_error
@@ -532,7 +532,7 @@ class TestSourceRewriter < Minitest::Test
532
532
  end
533
533
  end
534
534
 
535
- assert_match /transaction/, error.message
535
+ assert_match(/transaction/, error.message)
536
536
  end
537
537
 
538
538
  def silence_diagnostics
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceRewriterAction < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter_action)')
8
- @buf.source = 'foo bar baz'
7
+ @buf = Parser::Source::Buffer.new('(rewriter_action)',
8
+ source: 'foo bar baz')
9
9
  end
10
10
 
11
11
  def range(from, len)
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceTreeRewriter < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter)')
8
- @buf.source = 'puts(:hello, :world)'
7
+ @buf = Parser::Source::Buffer.new('(rewriter)',
8
+ source: 'puts(:hello, :world)')
9
9
 
10
10
  @hello = range(5, 6)
11
11
  @ll = range(7, 2)
@@ -36,7 +36,7 @@ class TestSourceTreeRewriter < Minitest::Test
36
36
  else
37
37
  diags.call
38
38
  end
39
- rescue ::Parser::ClobberingError => e
39
+ rescue ::Parser::ClobberingError => _e
40
40
  [::Parser::ClobberingError, diags.call]
41
41
  end
42
42
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.1.2
4
+ version: 2.7.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - whitequark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-29 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '10.0'
53
+ version: 13.0.1
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '10.0'
60
+ version: 13.0.1
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: racc
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -272,6 +272,7 @@ files:
272
272
  - test/helper.rb
273
273
  - test/parse_helper.rb
274
274
  - test/racc_coverage_helper.rb
275
+ - test/test_ast_processor.rb
275
276
  - test/test_base.rb
276
277
  - test/test_current.rb
277
278
  - test/test_diagnostic.rb
@@ -279,6 +280,7 @@ files:
279
280
  - test/test_encoding.rb
280
281
  - test/test_lexer.rb
281
282
  - test/test_lexer_stack_state.rb
283
+ - test/test_meta.rb
282
284
  - test/test_parse_helper.rb
283
285
  - test/test_parser.rb
284
286
  - test/test_runner_parse.rb
@@ -300,9 +302,9 @@ licenses:
300
302
  - MIT
301
303
  metadata:
302
304
  bug_tracker_uri: https://github.com/whitequark/parser/issues
303
- changelog_uri: https://github.com/whitequark/parser/blob/v2.7.1.2/CHANGELOG.md
304
- documentation_uri: https://www.rubydoc.info/gems/parser/2.7.1.2
305
- source_code_uri: https://github.com/whitequark/parser/tree/v2.7.1.2
305
+ changelog_uri: https://github.com/whitequark/parser/blob/v2.7.1.3/CHANGELOG.md
306
+ documentation_uri: https://www.rubydoc.info/gems/parser/2.7.1.3
307
+ source_code_uri: https://github.com/whitequark/parser/tree/v2.7.1.3
306
308
  post_install_message:
307
309
  rdoc_options: []
308
310
  require_paths:
@@ -329,6 +331,7 @@ test_files:
329
331
  - test/helper.rb
330
332
  - test/parse_helper.rb
331
333
  - test/racc_coverage_helper.rb
334
+ - test/test_ast_processor.rb
332
335
  - test/test_base.rb
333
336
  - test/test_current.rb
334
337
  - test/test_diagnostic.rb
@@ -336,6 +339,7 @@ test_files:
336
339
  - test/test_encoding.rb
337
340
  - test/test_lexer.rb
338
341
  - test/test_lexer_stack_state.rb
342
+ - test/test_meta.rb
339
343
  - test/test_parse_helper.rb
340
344
  - test/test_parser.rb
341
345
  - test/test_runner_parse.rb