AaronC81 / sord

Convert YARD docs to Sorbet RBI and Ruby 3/Steep RBS files
https://sord.aaronc.cc
MIT License
307 stars 18 forks source link

HereDoc constant generation causes syntax errors in generated RBIs #102

Open AaronC81 opened 5 years ago

AaronC81 commented 5 years ago

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
connorshea commented 5 years ago

  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 :)

AaronC81 commented 4 years ago

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 1 year ago

Is https://github.com/lsegal/yard/issues/1315 the resulting issue? If so, please take a look at https://github.com/lsegal/yard/pull/1495 🙏