1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
# frozen_string_literal: true
RSpec.describe YARD::Parser::Ruby::RubyParser do
def stmt(stmt)
YARD::Parser::Ruby::RubyParser.new(stmt, nil).parse.root.first
end
def stmts(stmts)
YARD::Parser::Ruby::RubyParser.new(stmts, nil).parse.root
end
def tokenize(stmt)
YARD::Parser::Ruby::RubyParser.new(stmt, nil).parse.tokens
end
describe "#parse" do
it "gets comment line numbers" do
s = stmt <<-eof
# comment
# comment
# comment
def method; end
eof
expect(s.comments).to eq "comment\ncomment\ncomment"
expect(s.comments_range).to eq(1..3)
s = stmt <<-eof
# comment
# comment
def method; end
eof
expect(s.comments).to eq "comment\ncomment"
expect(s.comments_range).to eq(2..3)
s = stmt <<-eof
# comment
# comment
def method; end
eof
expect(s.comments).to eq "comment\ncomment"
expect(s.comments_range).to eq(1..2)
s = stmt <<-eof
# comment
def method; end
eof
expect(s.comments).to eq "comment"
expect(s.comments_range).to eq(1..1)
s = stmt <<-eof
def method; end # comment
eof
expect(s.comments).to eq "comment"
expect(s.comments_range).to eq(1..1)
end
it "only looks up to two lines back for comments" do
s = stmts <<-eof
# comments
# comments
def method; end
eof
expect(s[1].comments).to eq "comments"
s = stmts <<-eof
# comments
def method; end
eof
expect(s[1].comments).to eq nil
ss = stmts <<-eof
# comments
def method; end
# hello
def method2; end
eof
expect(ss[1].comments).to eq nil
expect(ss[2].comments).to eq 'hello'
end
it "handles block comment followed by line comment" do
ss = stmts <<-eof
# comments1
=begin
comments2
=end
# comments3
def hello; end
eof
expect(ss.last.comments).to eq "comments3"
end
it "handles block comment followed by block comment" do
ss = stmts <<-eof
=begin
comments1
=end
=begin
comments2
=end
def hello; end
eof
expect(ss.last.comments.strip).to eq "comments2"
end
it "handles 1.9 lambda syntax with args" do
src = "->(a,b,c=1,*args,&block) { hello_world }"
expect(stmt(src).source).to eq src
end
it "handles 1.9 lambda syntax" do
src = "-> { hello_world }"
expect(stmt(src).source).to eq src
end
it "handles standard lambda syntax" do
src = "lambda { hello_world }"
expect(stmt(src).source).to eq src
end
it "throws a ParserSyntaxError on invalid code" do
expect { stmt("Foo, bar.") }.to raise_error(YARD::Parser::ParserSyntaxError)
end
it "handles bare hashes as method parameters" do
src = "command :a => 1, :b => 2, :c => 3"
expect(stmt(src).jump(:command)[1].source).to eq ":a => 1, :b => 2, :c => 3"
src = "command a: 1, b: 2, c: 3"
expect(stmt(src).jump(:command)[1].source).to eq "a: 1, b: 2, c: 3"
end
it "handles source for hash syntax" do
src = "{ :a => 1, :b => 2, :c => 3 }"
expect(stmt(src).jump(:hash).source).to eq "{ :a => 1, :b => 2, :c => 3 }"
end
it "handles an empty hash" do
expect(stmt("{}").jump(:hash).source).to eq "{}"
end
it "new hash label syntax should show label without colon" do
ast = stmt("{ a: 1 }").jump(:label)
expect(ast[0]).to eq "a"
expect(ast.source).to eq "a:"
end
it "handles begin/rescue blocks" do
ast = stmt("begin; X; rescue => e; Y end").jump(:rescue)
expect(ast.source).to eq "rescue => e; Y end"
ast = stmt("begin; X; rescue A => e; Y end").jump(:rescue)
expect(ast.source).to eq "rescue A => e; Y end"
ast = stmt("begin; X; rescue A, B => e; Y end").jump(:rescue)
expect(ast.source).to eq "rescue A, B => e; Y end"
end
it "handles method rescue blocks" do
ast = stmt("def x; A; rescue Y; B end")
expect(ast.source).to eq "def x; A; rescue Y; B end"
expect(ast.jump(:rescue).source).to eq "rescue Y; B end"
end
it "handles defs with keywords as method name" do
ast = stmt("# docstring\nclass A;\ndef class; end\nend")
expect(ast.jump(:class).docstring).to eq "docstring"
expect(ast.jump(:class).line_range).to eq(2..4)
end
it "handles defs with unnamed argument with default values" do
ast = stmt('def hello(one, two = 2, three = 3) end').jump(:params)
expect(ast.source).to eq 'one, two = 2, three = 3'
end
it "handles defs with splats" do
ast = stmt('def hello(one, *two) end').jump(:params)
expect(ast.source).to eq 'one, *two'
end
if YARD.ruby2?
it "handles defs with named arguments with default values" do
ast = stmt('def hello(one, two: 2, three: 3) end').jump(:params)
expect(ast.source).to eq 'one, two: 2, three: 3'
end
end
if NAMED_OPTIONAL_ARGUMENTS
it "handles defs with named arguments without default values" do
ast = stmt('def hello(one, two:, three:) end').jump(:params)
expect(ast.source).to eq 'one, two:, three:'
end
it "handles defs with double splats" do
ast = stmt('def hello(one, **two) end').jump(:params)
expect(ast.source).to eq 'one, **two'
end
end
it "ends source properly on array reference" do
ast = stmt("AS[0, 1 ] ")
expect(ast.source).to eq 'AS[0, 1 ]'
ast = stmt('def x(a = S[1]) end').jump(:params)
expect(ast.source).to eq 'a = S[1]'
ast = stmt('a[b[c]]')
expect(ast.source).to eq 'a[b[c]]'
end
it "ends source properly on if/unless mod" do
%w(if unless while).each do |mod|
expect(stmt("A=1 #{mod} true").source).to eq "A=1 #{mod} true"
end
end
it "shows proper source for assignment" do
expect(stmt("A=1").jump(:assign).source).to eq "A=1"
end
it "shows proper source for a top_const_ref" do
s = stmt("::\nFoo::Bar")
expect(s.jump(:top_const_ref).source).to eq "::\nFoo"
expect(s).to be_ref
expect(s.jump(:top_const_ref)).to be_ref
expect(s.source).to eq "::\nFoo::Bar"
expect(s.line_range.to_a).to eq [1, 2]
end
it "shows proper source for inline heredoc" do
src = "def foo\n foo(<<-XML, 1, 2)\n bar\n\n XML\nend"
s = stmt(src)
t = tokenize(src)
expect(s.source).to eq src
expect(t.map {|x| x[1] }.join).to eq src
end
it "shows proper source for regular heredoc" do
src = "def foo\n x = <<-XML\n Hello \#{name}!\n Bye!\n XML\nend"
s = stmt(src)
t = tokenize(src)
expect(s.source).to eq src
expect(t.map {|x| x[1] }.join).to eq src
end
it "shows proper source for heredoc with comment" do
src = "def foo\n x = <<-XML # HI!\n Hello \#{name}!\n Bye!\n XML\nend"
s = stmt(src)
t = tokenize(src)
expect(s.source).to eq src
expect(t.map {|x| x[1] }.join).to eq src
end
it "shows proper source for string" do
["'", '"'].each do |q|
src = "#{q}hello\n\nworld#{q}"
s = stmt(src)
expect(s.jump(:string_content).source).to eq "hello\n\nworld"
expect(s.source).to eq src
end
src = '("this is a string")'
expect(stmt(src).jump(:string_literal).source).to eq '"this is a string"'
end
%w(w W i I).each do |tok|
it "shows proper source for %#{tok}() array" do
src = "%#{tok}(\na b c\n d e f\n)"
expect(stmt(src).source).to eq src
end
it "shows proper source for %#{tok}{} array" do
src = "%#{tok}{\na b c\n d e f\n}"
expect(stmt(src).source).to eq src
end
end
{'i' => :qsymbols_literal, 'I' => :symbols_literal,
'w' => :qwords_literal, 'W' => :words_literal}.each do |id, sym|
it "parses %#{id}(...) literals" do
[
"TEST = %#{id}(A B C)",
"TEST = %#{id}( A B C )",
"TEST = %#{id}( \nA \nB \nC \n)",
"TEST = %#{id}(\n\nAD\n\nB\n\nC\n\n)",
"TEST = %#{id}(\n A\n B\n C\n )"
].each do |str|
node = stmt(str).jump(sym)
expect(node.source).to eq(str[/(\%#{id}\(.+\))/m, 1])
end
end
it "tokenizing %#{id}(...) returns correct tokens" do
toks = tokenize("TEST = %#{id}(A B C)").flatten
expect(toks.count(:tstring_content)).to eq(3)
end
end
it "properly tokenizes symbols" do
tokens = tokenize(<<-eof)
class X
Foo = :''
Fuu = :bar
Bar = :BAR
Baz = :"B+z"
Qux = :if
end
eof
symbols = tokens.select {|t| t[0] == :symbol }.map {|t| t[1] }
expect(symbols).to eq %w(:'' :bar :BAR :"B+z" :if)
end
# @bug gh-1313
it "tokenizes comments in-order" do
src = <<-eof
def method
# Method comment not docstring
end
eof
tokens = tokenize(src.gsub(/^ +/, ''))
expect(tokens).to eq(tokens.sort_by {|t| t.last })
expect(tokens.map {|t| t.first }).to eq %i(kw sp ident nl comment kw nl)
end
it "parses %w() array in constant declaration" do
s = stmt(<<-eof)
class Foo
FOO = %w( foo bar )
end
eof
expect(s.jump(:qwords_literal).source).to eq '%w( foo bar )'
if RUBY_VERSION >= '1.9.3' # ripper fix: array node encapsulates qwords
expect(s.jump(:array).source).to eq '%w( foo bar )'
end
end
it "parses %w() array source in object[] parsed context" do
s = stmts(<<-eof)
{}[:key]
FOO = %w( foo bar )
eof
expect(s[1].jump(:array).source).to eq '%w( foo bar )'
end
it "parses %w() array source in object[]= parsed context" do
s = stmts(<<-eof)
{}[:key] = :value
FOO = %w( foo bar )
eof
expect(s[1].jump(:array).source).to eq '%w( foo bar )'
end
it "parses [] as array" do
s = stmt(<<-eof)
class Foo
FOO = ['foo', 'bar']
end
eof
expect(s.jump(:array).source).to eq "['foo', 'bar']"
end
it "shows source for unary minus" do
expect(stmt("X = - 1").jump(:unary).source).to eq '- 1'
end
it "shows source for unary exclamation" do
expect(stmt("X = !1").jump(:unary).source).to eq '!1'
end
it "has the correct line range for class/modules" do
s = stmt(<<-eof)
class Foo
def foo; end
# Ending comment
end
eof
expect(s.jump(:class).line_range).to eq(1..7)
end
it "has the correct line range for blocks" do
Registry.clear
ast = YARD.parse_string(<<-eof).enumerator
module A
some_method
end
eof
expect(ast.first.block.source.strip).to eq "some_method"
end
it "finds lone comments" do
Registry.clear
ast = YARD.parse_string(<<-eof).enumerator
class Foo
##
# comment here
def foo; end
# end comment
end
eof
comment = ast.first.last.jump(:comment)
expect(comment.type).to eq :comment
expect(comment.docstring_hash_flag).to be true
expect(comment.docstring.strip).to eq "comment here"
expect(ast.first.last.last.type).to eq :comment
expect(ast.first.last.last.docstring).to eq "end comment"
end
it "does not group comments if they don't begin the line" do
Registry.clear
YARD.parse_string(<<-eof).enumerator
class Foo
CONST1 = 1 # Comment here
CONST2 = 2 # Another comment here
end
eof
expect(Registry.at("Foo::CONST1").docstring).to eq "Comment here"
expect(Registry.at("Foo::CONST2").docstring).to eq "Another comment here"
end
it "handles comments in the middle of a multi-line statement" do
Registry.clear
YARD.parse_string <<-eof
foo # BREAK
.bar
# Documentation
class Baz; end
eof
expect(Registry.at('Baz')).not_to be_nil
expect(Registry.at('Baz').docstring).to eq 'Documentation'
end
%w(if unless).each do |type|
it "does not get confused by modifier '#{type}' statements" do
Registry.clear
YARD.parse_string(<<-eof).enumerator
module Foo
#{type} test?
# Docstring
class Bar
# Docstring2
def foo
x #{type} true
end
end
end
end
eof
expect(Registry.at("Foo::Bar").docstring).to eq "Docstring"
expect(Registry.at("Foo::Bar#foo").docstring).to eq "Docstring2"
end
it "supports #{type} statements at start of source" do
Registry.clear
YARD.parse_string <<-eof
#{type} condition?
class Foo; def bar; #{type} true; end end end
end
eof
expect(log.io.string).to eq ""
expect(Registry.at('Foo#bar')).not_to eq nil
end
it "can handle complex non-modifier '#{type}' statements" do
Registry.clear
YARD.parse_string <<-eof
class Foo
def initialize(data, options = Hash.new)
#{type} true; raise "error" end
@x = ( #{type} 1; true end ) # This line should not blow up
end
end
eof
expect(log.io.string).to eq ""
expect(Registry.at('Foo#initialize')).not_to eq nil
end
it "does not add comment blocks to #{type}_mod nodes" do
Registry.clear
YARD.parse_string(<<-eof).enumerator
class Foo
# Docstring
def bar; end if true
end
eof
expect(Registry.at("Foo#bar").docstring).to eq "Docstring"
end
end
it "removes frozen string line from initial file comments" do
YARD.parse_string "# frozen_string_literal: true\n# this is a comment\nclass Foo; end"
YARD.parse_string "# Frozen-string-literal: false\n# this is a comment\nclass Bar; end"
expect(Registry.at(:Foo).docstring).to eq "this is a comment"
expect(Registry.at(:Bar).docstring).to eq "this is a comment"
end
it "handles compile errors" do
expect { stmt(":~$ Do not clobber") }.to raise_error(Parser::ParserSyntaxError)
end
it "handles cls/mod comments without line spacing" do
ast = stmt <<-eof
module A
# comment 1
# comment 2
class B
end
end
eof
expect(ast.jump(:class).docstring).to eq "comment 1\ncomment 2"
end
%w(if unless).each do |type|
let(:condition_type) { type }
let(:ast) { stmt '"#{' + type + ' condition?; 42; end}" ' + type + ' verbose?' }
let(:subject) { ast.jump(:string_embexpr)[0][0].source }
it "returns correct source for interpolated non-ternary '#{type}' conditionals" do
is_expected.to eq "#{condition_type} condition?; 42; end"
end
end
it "handles single-line method declaration syntax" do
YARD.parse_string <<-eof
class A
# Adds two numbers
def add(x) = x + 1
end
eof
expect(Registry.at('A#add').docstring).to eq('Adds two numbers')
end if RUBY_VERSION >= '3.'
end
end if HAVE_RIPPER
|