AaronC81/sord

HereDoc constant generation causes syntax errors in generated RBIs

AaronC81 opened this issue · 3 comments

Describe the bug
If a constant's value is a HereDoc, the generated RBI contains syntax errors due to the weird syntax of HereDocs in method calls. (I found this when generating an RBI for Rack.)

To Reproduce
Generate an RBI for this code:

module Foo
  CONST = <<-HERE
    This is a heredoc.
  HERE
end

Expected behavior

# typed: strong
module Foo
  CONST = T.let(<<-HERE, T.untyped)
    This is a heredoc.
  HERE
end

Actual behavior

# typed: strong
module Foo
  CONST = T.let(<<-HERE
  This is a heredoc.
HERE, T.untyped)
end
  class IDNumber < Faker::Base
    CHECKS = T.let('TRWAGMYFPDXBNJZSQVHLCKE', T.untyped)
    INVALID_SSN = T.let([
  /0{3}-\d{2}-\d{4}/,
  /\d{3}-0{2}-\d{4}/,
  /\d{3}-\d{2}-0{4}/,
  /666-\d{2}-\d{4}/,
  /9\d{2}-\d{2}-\d{4}/
].freeze, T.untyped)
  end

This also happens in Faker :)

YARD doesn't appear to parse heredocs quite right, which means this can't be implemented perfectly now.

Given this file:

module X
  A = <<-EOF
    bar!
    EOF

  B = <<-EOF
  bar!
  EOF
end

p X::A
p X::B

Ruby preserves the indentation properly:

$ ruby test.rb                   
"    bar!\n"
"  bar!\n"

But YARD strips the indentation from both of them:

irb(main):010:0> YARD::Registry.root.children.first.children.map(&:value)
=> ["<<-EOF\nbar!\nEOF", "<<-EOF\nbar!\nEOF"]

I'll see if there's an existing issue for this on the YARD repo and open one if not.

dduugg commented

Is lsegal/yard#1315 the resulting issue? If so, please take a look at lsegal/yard#1495 🙏