parser 2.7.1.3 → 3.0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parser.rb +1 -1
  3. data/lib/parser/all.rb +1 -1
  4. data/lib/parser/ast/processor.rb +5 -7
  5. data/lib/parser/base.rb +7 -5
  6. data/lib/parser/builders/default.rb +225 -29
  7. data/lib/parser/context.rb +5 -0
  8. data/lib/parser/current.rb +11 -11
  9. data/lib/parser/current_arg_stack.rb +5 -2
  10. data/lib/parser/lexer.rb +23780 -0
  11. data/lib/parser/macruby.rb +6149 -0
  12. data/lib/parser/max_numparam_stack.rb +13 -5
  13. data/lib/parser/messages.rb +3 -0
  14. data/lib/parser/meta.rb +8 -7
  15. data/lib/parser/ruby18.rb +5667 -0
  16. data/lib/parser/ruby19.rb +6092 -0
  17. data/lib/parser/ruby20.rb +6527 -0
  18. data/lib/parser/ruby21.rb +6578 -0
  19. data/lib/parser/ruby22.rb +6613 -0
  20. data/lib/parser/ruby23.rb +6624 -0
  21. data/lib/parser/ruby24.rb +6694 -0
  22. data/lib/parser/ruby25.rb +6662 -0
  23. data/lib/parser/ruby26.rb +6676 -0
  24. data/lib/parser/ruby27.rb +7862 -0
  25. data/lib/parser/ruby28.rb +8047 -0
  26. data/lib/parser/ruby30.rb +8060 -0
  27. data/lib/parser/rubymotion.rb +6086 -0
  28. data/lib/parser/runner.rb +4 -4
  29. data/lib/parser/source/buffer.rb +50 -27
  30. data/lib/parser/source/comment.rb +1 -1
  31. data/lib/parser/source/comment/associator.rb +1 -1
  32. data/lib/parser/source/map/{endless_definition.rb → method_definition.rb} +5 -3
  33. data/lib/parser/source/range.rb +3 -3
  34. data/lib/parser/source/tree_rewriter.rb +94 -1
  35. data/lib/parser/source/tree_rewriter/action.rb +39 -0
  36. data/lib/parser/static_environment.rb +4 -0
  37. data/lib/parser/variables_stack.rb +4 -0
  38. data/lib/parser/version.rb +1 -1
  39. data/parser.gemspec +2 -18
  40. metadata +13 -102
  41. data/.gitignore +0 -34
  42. data/.travis.yml +0 -40
  43. data/.yardopts +0 -21
  44. data/CHANGELOG.md +0 -1101
  45. data/CONTRIBUTING.md +0 -17
  46. data/Gemfile +0 -10
  47. data/README.md +0 -308
  48. data/Rakefile +0 -167
  49. data/ci/run_rubocop_specs +0 -14
  50. data/doc/AST_FORMAT.md +0 -2229
  51. data/doc/CUSTOMIZATION.md +0 -37
  52. data/doc/INTERNALS.md +0 -21
  53. data/doc/css/.gitkeep +0 -0
  54. data/doc/css/common.css +0 -68
  55. data/lib/parser/lexer.rl +0 -2543
  56. data/lib/parser/macruby.y +0 -2198
  57. data/lib/parser/ruby18.y +0 -1934
  58. data/lib/parser/ruby19.y +0 -2175
  59. data/lib/parser/ruby20.y +0 -2353
  60. data/lib/parser/ruby21.y +0 -2357
  61. data/lib/parser/ruby22.y +0 -2364
  62. data/lib/parser/ruby23.y +0 -2370
  63. data/lib/parser/ruby24.y +0 -2408
  64. data/lib/parser/ruby25.y +0 -2405
  65. data/lib/parser/ruby26.y +0 -2413
  66. data/lib/parser/ruby27.y +0 -2941
  67. data/lib/parser/ruby28.y +0 -3016
  68. data/lib/parser/rubymotion.y +0 -2182
  69. data/test/bug_163/fixtures/input.rb +0 -5
  70. data/test/bug_163/fixtures/output.rb +0 -5
  71. data/test/bug_163/rewriter.rb +0 -20
  72. data/test/helper.rb +0 -79
  73. data/test/parse_helper.rb +0 -313
  74. data/test/racc_coverage_helper.rb +0 -133
  75. data/test/test_ast_processor.rb +0 -32
  76. data/test/test_base.rb +0 -31
  77. data/test/test_current.rb +0 -31
  78. data/test/test_diagnostic.rb +0 -95
  79. data/test/test_diagnostic_engine.rb +0 -59
  80. data/test/test_encoding.rb +0 -99
  81. data/test/test_lexer.rb +0 -3617
  82. data/test/test_lexer_stack_state.rb +0 -78
  83. data/test/test_meta.rb +0 -12
  84. data/test/test_parse_helper.rb +0 -80
  85. data/test/test_parser.rb +0 -9596
  86. data/test/test_runner_parse.rb +0 -56
  87. data/test/test_runner_rewrite.rb +0 -47
  88. data/test/test_source_buffer.rb +0 -165
  89. data/test/test_source_comment.rb +0 -36
  90. data/test/test_source_comment_associator.rb +0 -399
  91. data/test/test_source_map.rb +0 -14
  92. data/test/test_source_range.rb +0 -192
  93. data/test/test_source_rewriter.rb +0 -541
  94. data/test/test_source_rewriter_action.rb +0 -46
  95. data/test/test_source_tree_rewriter.rb +0 -263
  96. data/test/test_static_environment.rb +0 -45
  97. data/test/using_tree_rewriter/fixtures/input.rb +0 -3
  98. data/test/using_tree_rewriter/fixtures/output.rb +0 -3
  99. data/test/using_tree_rewriter/using_tree_rewriter.rb +0 -9
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'helper'
4
- require 'open3'
5
-
6
- class TestRunnerParse < Minitest::Test
7
- PATH_TO_RUBY_PARSE = File.expand_path('../bin/ruby-parse', __dir__).freeze
8
-
9
- def assert_prints(argv, expected_output)
10
- stdout, _stderr, status = Open3.capture3(PATH_TO_RUBY_PARSE, *argv)
11
-
12
- assert_equal 0, status.to_i
13
- assert_includes(stdout, expected_output)
14
- end
15
-
16
- def test_emit_ruby
17
- assert_prints ['--emit-ruby', '-e 123'],
18
- 's(:int, 123)'
19
- end
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
-
42
- def test_emit_json
43
- assert_prints ['--emit-json', '-e', '123'],
44
- '["int",123]'
45
- end
46
-
47
- def test_emit_ruby_empty
48
- assert_prints ['--emit-ruby', '-e', ''],
49
- "\n"
50
- end
51
-
52
- def test_emit_json_empty
53
- assert_prints ['--emit-json', '-e', ''],
54
- "\n"
55
- end
56
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
- require 'fileutils'
5
- require 'shellwords'
6
- require 'open3'
7
-
8
- BASE_DIR = Pathname.new(__FILE__) + '..'
9
- require (BASE_DIR + 'helper').expand_path
10
-
11
- class TestRunnerRewrite < Minitest::Test
12
- def assert_rewriter_output(path, args, input: 'input.rb', output: 'output.rb', expected_output: '', expected_error: '')
13
- @ruby_rewrite = BASE_DIR.expand_path + '../bin/ruby-rewrite'
14
- @test_dir = BASE_DIR + path
15
- @fixtures_dir = @test_dir + 'fixtures'
16
-
17
- Dir.mktmpdir("parser", BASE_DIR.expand_path.to_s) do |tmp_dir|
18
- tmp_dir = Pathname.new(tmp_dir)
19
- sample_file = tmp_dir + "#{path}.rb"
20
- sample_file_expanded = sample_file.expand_path
21
- expected_file = @fixtures_dir + output
22
-
23
- FileUtils.cp(@fixtures_dir + input, sample_file_expanded)
24
- stdout, stderr, _exit_code = Dir.chdir @test_dir do
25
- Open3.capture3 %Q{
26
- #{Shellwords.escape(@ruby_rewrite.to_s)} #{args} \
27
- #{Shellwords.escape(sample_file_expanded.to_s)}
28
- }
29
- end
30
-
31
- assert_equal expected_output.chomp, stdout.chomp
32
- assert_match expected_error.chomp, stderr.chomp
33
- assert_equal File.read(expected_file.expand_path), File.read(sample_file)
34
- end
35
- end
36
-
37
- def test_rewriter_bug_163
38
- assert_rewriter_output('bug_163',
39
- '--modify -l rewriter.rb',
40
- expected_error: Parser::Rewriter::DEPRECATION_WARNING
41
- )
42
- end
43
-
44
- def test_tree_rewriter
45
- assert_rewriter_output('using_tree_rewriter', '-l using_tree_rewriter.rb --modify')
46
- end
47
- end
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'helper'
4
-
5
- class TestSourceBuffer < Minitest::Test
6
- def setup
7
- @buffer = Parser::Source::Buffer.new('(string)')
8
- end
9
-
10
- def test_initialize
11
- buffer = Parser::Source::Buffer.new(nil)
12
- assert_equal '', buffer.name
13
-
14
- buffer = Parser::Source::Buffer.new(Pathname('a/b'))
15
- assert_equal 'a/b', buffer.name
16
-
17
- buffer = Parser::Source::Buffer.new('(string)')
18
- assert_equal '(string)', buffer.name
19
- assert_equal 1, buffer.first_line
20
-
21
- buffer = Parser::Source::Buffer.new('(string)', 5)
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
26
- end
27
-
28
- def test_source_setter
29
- @buffer.source = 'foo'
30
- assert_equal 'foo', @buffer.source
31
-
32
- assert @buffer.source.frozen?
33
- end
34
-
35
- def test_source_double_setter
36
- @buffer.source = 'foo'
37
-
38
- assert_raises(ArgumentError) do
39
- @buffer.source = 'bar'
40
- end
41
- end
42
-
43
- def test_source_setter_encoding_error
44
- error = assert_raises EncodingError do
45
- @buffer.source = [
46
- '# encoding: utf-8',
47
- "# \xf9"
48
- ].join("\n")
49
- end
50
-
51
- assert_match(/invalid byte sequence in UTF\-8/, error.message)
52
- end
53
-
54
- def test_read
55
- tempfile = Tempfile.new('parser')
56
- tempfile.write('foobar')
57
- tempfile.flush
58
-
59
- buffer = Parser::Source::Buffer.new(tempfile.path)
60
- buffer.read
61
- assert_equal 'foobar', buffer.source
62
-
63
- assert buffer.source.frozen?
64
- end
65
-
66
- def test_uninitialized
67
- assert_raises RuntimeError do
68
- @buffer.source
69
- end
70
- end
71
-
72
- def test_decompose_position
73
- @buffer.source = "1\nfoo\nbar"
74
-
75
- assert_equal [1, 0], @buffer.decompose_position(0)
76
- assert_equal [1, 1], @buffer.decompose_position(1)
77
- assert_equal [2, 0], @buffer.decompose_position(2)
78
- assert_equal [3, 1], @buffer.decompose_position(7)
79
- end
80
-
81
- def test_decompose_position_mapped
82
- @buffer = Parser::Source::Buffer.new('(string)', 5)
83
- @buffer.source = "1\nfoo\nbar"
84
-
85
- assert_equal [5, 0], @buffer.decompose_position(0)
86
- assert_equal [6, 0], @buffer.decompose_position(2)
87
- end
88
-
89
- def test_line
90
- @buffer.source = "1\nfoo\nbar"
91
-
92
- assert_equal '1', @buffer.source_line(1)
93
- assert_equal 'foo', @buffer.source_line(2)
94
- end
95
-
96
- def test_line_mutate
97
- @buffer.source = "1\nfoo\nbar"
98
-
99
- assert_equal '1', @buffer.source_line(1)
100
-
101
- @buffer.source_line(1)[0] = '2'
102
- assert_equal '1', @buffer.source_line(1)
103
- end
104
-
105
- def test_line_mapped
106
- @buffer = Parser::Source::Buffer.new('(string)', 5)
107
- @buffer.source = "1\nfoo\nbar"
108
-
109
- assert_equal '1', @buffer.source_line(5)
110
- assert_equal 'foo', @buffer.source_line(6)
111
- end
112
-
113
- def test_line_range
114
- @buffer = Parser::Source::Buffer.new('(string)', 5)
115
- @buffer.source = "abc\ndef\nghi\n"
116
-
117
- assert_raises IndexError do
118
- @buffer.line_range(4)
119
- end
120
- assert_equal 'abc', @buffer.line_range(5).source
121
- assert_equal 'def', @buffer.line_range(6).source
122
- assert_equal 'ghi', @buffer.line_range(7).source
123
- assert_equal '', @buffer.line_range(8).source
124
- assert_raises IndexError do
125
- @buffer.line_range(9)
126
- end
127
- end
128
-
129
- def test_source_range
130
- @buffer = Parser::Source::Buffer.new('(string)', 5)
131
-
132
- assert_raises RuntimeError do
133
- @buffer.source_range
134
- end
135
-
136
- @buffer.source = "abc\ndef\nghi\n"
137
-
138
- assert_equal Parser::Source::Range.new(@buffer, 0, @buffer.source.size), @buffer.source_range
139
- end
140
-
141
- def test_last_line
142
- @buffer.source = "1\nfoo\nbar"
143
- assert_equal 3, @buffer.last_line
144
-
145
- @buffer = Parser::Source::Buffer.new('(string)', 5)
146
- @buffer.source = ""
147
- assert_equal 5, @buffer.last_line
148
-
149
- @buffer = Parser::Source::Buffer.new('(string)', 5)
150
- @buffer.source = "abc\n"
151
- assert_equal 6, @buffer.last_line
152
- end
153
-
154
- def test_source_lines
155
- @buffer.source = "1\nfoo\nbar\n"
156
-
157
- assert_equal ['1', 'foo', 'bar', ''], @buffer.source_lines
158
- assert @buffer.source_lines.frozen?
159
- assert @buffer.source_lines.all?(&:frozen?)
160
-
161
- @buffer = Parser::Source::Buffer.new('(string)', 5)
162
- @buffer.source = "foo\nbar"
163
- assert_equal ['foo', 'bar'], @buffer.source_lines
164
- end
165
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'helper'
4
-
5
- class TestSourceComment < Minitest::Test
6
- def setup
7
- @buf = Parser::Source::Buffer.new('(string)',
8
- source: "# foo\n=begin foo\nbar\n=end baz\n")
9
- end
10
-
11
- def range(s, e)
12
- Parser::Source::Range.new(@buf, s, e)
13
- end
14
-
15
- def test_initialize
16
- comment = Parser::Source::Comment.new(range(0, 5))
17
- assert comment.frozen?
18
- end
19
-
20
- def test_text
21
- comment = Parser::Source::Comment.new(range(0, 5))
22
- assert_equal '# foo', comment.text
23
- end
24
-
25
- def test_inline
26
- comment = Parser::Source::Comment.new(range(0, 5))
27
- assert_equal :inline, comment.type
28
- assert comment.inline?
29
- end
30
-
31
- def test_document
32
- comment = Parser::Source::Comment.new(range(6, 25))
33
- assert_equal :document, comment.type
34
- assert comment.document?
35
- end
36
- end
@@ -1,399 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'helper'
4
- require 'parser/ruby18'
5
-
6
- class TestSourceCommentAssociator < Minitest::Test
7
- def parse_with_comments(code)
8
- parser = Parser::Ruby18.new
9
-
10
- buffer = Parser::Source::Buffer.new('(comments)')
11
- buffer.source = code
12
-
13
- parser.parse_with_comments(buffer)
14
- end
15
-
16
- def associate(code)
17
- ast, comments = parse_with_comments(code)
18
- associations = Parser::Source::Comment.associate(ast, comments)
19
-
20
- [ ast, associations ]
21
- end
22
-
23
- def associate_locations(code)
24
- ast, comments = parse_with_comments(code)
25
- associations = Parser::Source::Comment.associate_locations(ast, comments)
26
-
27
- [ ast, associations ]
28
- end
29
-
30
- def test_associate
31
- ast, associations = associate(<<-END)
32
- #!/usr/bin/env ruby
33
- # coding: utf-8
34
- # class preceding
35
- # another class preceding
36
- class Foo # class keyword line
37
- # method foo preceding
38
- def foo
39
- puts 'foo'
40
- end # method foo decorating
41
- # method bar preceding
42
- def bar
43
- # expression preceding
44
- 1 + # 1 decorating
45
- 2
46
- # method bar sparse
47
- end # method bar decorating
48
- # class sparse
49
- end # class decorating
50
- END
51
-
52
- klass_node = ast
53
- klass_name_node = klass_node.children[0]
54
- foo_node = klass_node.children[2].children[0] # def foo
55
- bar_node = klass_node.children[2].children[1] # def bar
56
- expr_node = bar_node.children[2] # 1 + 2
57
- one_node = expr_node.children[0] # 1
58
-
59
- assert_equal 6, associations.size
60
- assert_equal [
61
- '# class preceding',
62
- '# another class preceding',
63
- '# class sparse',
64
- '# class decorating'
65
- ], associations[klass_node].map(&:text)
66
- assert_equal [
67
- '# class keyword line'
68
- ], associations[klass_name_node].map(&:text)
69
- assert_equal [
70
- '# method foo preceding',
71
- '# method foo decorating'
72
- ], associations[foo_node].map(&:text)
73
- assert_equal [
74
- '# method bar preceding',
75
- '# method bar sparse',
76
- '# method bar decorating'
77
- ], associations[bar_node].map(&:text)
78
- assert_equal [
79
- '# expression preceding'
80
- ], associations[expr_node].map(&:text)
81
- assert_equal [
82
- '# 1 decorating'
83
- ], associations[one_node].map(&:text)
84
- end
85
-
86
- # The bug below is fixed by using associate_locations
87
- def test_associate_dupe_statement
88
- ast, associations = associate(<<-END)
89
- class Foo
90
- def bar
91
- f1 # comment on 1st call to f1
92
- f2
93
- f1 # comment on 2nd call to f1
94
- end
95
- end
96
- END
97
-
98
- _klass_node = ast
99
- method_node = ast.children[2]
100
- body = method_node.children[2]
101
- f1_1_node = body.children[0]
102
- f1_2_node = body.children[2]
103
-
104
- assert_equal 1, associations.size
105
- assert_equal ['# comment on 1st call to f1', '# comment on 2nd call to f1'],
106
- associations[f1_1_node].map(&:text)
107
- assert_equal ['# comment on 1st call to f1', '# comment on 2nd call to f1'],
108
- associations[f1_2_node].map(&:text)
109
- end
110
-
111
- def test_associate_locations
112
- ast, associations = associate_locations(<<-END)
113
- #!/usr/bin/env ruby
114
- # coding: utf-8
115
- # class preceding
116
- # another class preceding
117
- class Foo # class keyword line
118
- # method foo preceding
119
- def foo
120
- puts 'foo'
121
- end # method foo decorating
122
- # method bar preceding
123
- def bar
124
- # expression preceding
125
- 1 + # 1 decorating
126
- 2
127
- # method bar sparse
128
- end # method bar decorating
129
- # class sparse
130
- end # class decorating
131
- END
132
-
133
- klass_node = ast
134
- klass_name_node = klass_node.children[0]
135
- foo_node = klass_node.children[2].children[0] # def foo
136
- bar_node = klass_node.children[2].children[1] # def bar
137
- expr_node = bar_node.children[2] # 1 + 2
138
- one_node = expr_node.children[0] # 1
139
-
140
- assert_equal 6, associations.size
141
- assert_equal [
142
- '# class preceding',
143
- '# another class preceding',
144
- '# class sparse',
145
- '# class decorating'
146
- ], associations[klass_node.loc].map(&:text)
147
- assert_equal [
148
- '# class keyword line'
149
- ], associations[klass_name_node.loc].map(&:text)
150
- assert_equal [
151
- '# method foo preceding',
152
- '# method foo decorating'
153
- ], associations[foo_node.loc].map(&:text)
154
- assert_equal [
155
- '# method bar preceding',
156
- '# method bar sparse',
157
- '# method bar decorating'
158
- ], associations[bar_node.loc].map(&:text)
159
- assert_equal [
160
- '# expression preceding'
161
- ], associations[expr_node.loc].map(&:text)
162
- assert_equal [
163
- '# 1 decorating'
164
- ], associations[one_node.loc].map(&:text)
165
- end
166
-
167
- def test_associate_locations_dupe_statement
168
- ast, associations = associate_locations(<<-END)
169
- class Foo
170
- def bar
171
- f1 # comment on 1st call to f1
172
- f2
173
- f1 # comment on 2nd call to f1
174
- end
175
- end
176
- END
177
-
178
- _klass_node = ast
179
- method_node = ast.children[2]
180
- body = method_node.children[2]
181
- f1_1_node = body.children[0]
182
- f1_2_node = body.children[2]
183
-
184
- assert_equal 2, associations.size
185
- assert_equal ['# comment on 1st call to f1'],
186
- associations[f1_1_node.loc].map(&:text)
187
- assert_equal ['# comment on 2nd call to f1'],
188
- associations[f1_2_node.loc].map(&:text)
189
- end
190
-
191
- def test_associate_no_body
192
- ast, associations = associate(<<-END)
193
- # foo
194
- class Foo
195
- end
196
- END
197
-
198
- assert_equal 1, associations.size
199
- assert_equal ['# foo'],
200
- associations[ast].map(&:text)
201
- end
202
-
203
- def test_associate_empty_tree
204
- _ast, associations = associate("")
205
- assert_equal 0, associations.size
206
- end
207
-
208
- def test_associate_shebang_only
209
- _ast, associations = associate(<<-END)
210
- #!ruby
211
- class Foo
212
- end
213
- END
214
-
215
- assert_equal 0, associations.size
216
- end
217
-
218
- def test_associate_frozen_string_literal
219
- _ast, associations = associate(<<-END)
220
- # frozen_string_literal: true
221
- class Foo
222
- end
223
- END
224
-
225
- assert_equal 0, associations.size
226
- end
227
-
228
- def test_associate_frozen_string_literal_dash_star_dash
229
- _ast, associations = associate(<<-END)
230
- # -*- frozen_string_literal: true -*-
231
- class Foo
232
- end
233
- END
234
-
235
- assert_equal 0, associations.size
236
- end
237
-
238
- def test_associate_frozen_string_literal_no_space_after_colon
239
- _ast, associations = associate(<<-END)
240
- # frozen_string_literal:true
241
- class Foo
242
- end
243
- END
244
-
245
- assert_equal 0, associations.size
246
- end
247
-
248
- def test_associate_warn_indent
249
- _ast, associations = associate(<<-END)
250
- # warn_indent: true
251
- class Foo
252
- end
253
- END
254
-
255
- assert_equal 0, associations.size
256
- end
257
-
258
- def test_associate_warn_indent_dash_star_dash
259
- _ast, associations = associate(<<-END)
260
- # -*- warn_indent: true -*-
261
- class Foo
262
- end
263
- END
264
-
265
- assert_equal 0, associations.size
266
- end
267
-
268
- def test_associate_warn_past_scope
269
- _ast, associations = associate(<<-END)
270
- # warn_past_scope: true
271
- class Foo
272
- end
273
- END
274
-
275
- assert_equal 0, associations.size
276
- end
277
-
278
- def test_associate_warn_past_scope_dash_star_dash
279
- _ast, associations = associate(<<-END)
280
- # -*- warn_past_scope: true -*-
281
- class Foo
282
- end
283
- END
284
-
285
- assert_equal 0, associations.size
286
- end
287
-
288
- def test_associate_multiple
289
- _ast, associations = associate(<<-END)
290
- # frozen_string_literal: true; warn_indent: true
291
- class Foo
292
- end
293
- END
294
-
295
- assert_equal 0, associations.size
296
- end
297
-
298
- def test_associate_multiple_dash_star_dash
299
- _ast, associations = associate(<<-END)
300
- # -*- frozen_string_literal: true; warn_indent: true -*-
301
- class Foo
302
- end
303
- END
304
-
305
- assert_equal 0, associations.size
306
- end
307
-
308
- def test_associate_no_comments
309
- _ast, associations = associate(<<-END)
310
- class Foo
311
- end
312
- END
313
-
314
- assert_equal 0, associations.size
315
- end
316
-
317
- def test_associate_comments_after_root_node
318
- _ast, associations = associate(<<-END)
319
- class Foo
320
- end
321
- # not associated
322
- END
323
-
324
- assert_equal 0, associations.size
325
- end
326
-
327
-
328
- def test_associate_stray_comment
329
- ast, associations = associate(<<-END)
330
- def foo
331
- # foo
332
- end
333
- END
334
-
335
- assert_equal 1, associations.size
336
- assert_equal ['# foo'],
337
- associations[ast].map(&:text)
338
- end
339
-
340
- def test_associate___ENCODING__
341
- ast, associations = associate(<<-END)
342
- # foo
343
- __ENCODING__
344
- END
345
-
346
- assert_equal 1, associations.size
347
- assert_equal ['# foo'],
348
- associations[ast].map(&:text)
349
- end
350
-
351
- def test_associate_inside_heredoc
352
- ast, associations = associate(<<-END)
353
- <<x
354
- \#{
355
- foo # bar
356
- }
357
- x
358
- END
359
-
360
- begin_node = ast.children[0]
361
- send_node = begin_node.children[0]
362
-
363
- assert_equal 1, associations.size
364
- assert_equal ['# bar'],
365
- associations[send_node].map(&:text)
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
399
- end