Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax error when method is passed a heredoc string and a block #301

Open
mmcnl opened this issue May 9, 2019 · 2 comments · May be fixed by #649
Open

syntax error when method is passed a heredoc string and a block #301

mmcnl opened this issue May 9, 2019 · 2 comments · May be fixed by #649
Labels
bug

Comments

@mmcnl
Copy link

@mmcnl mmcnl commented May 9, 2019

Metadata

  • Ruby version: ruby 2.5.1
  • @prettier/plugin-ruby version: v0.12.2

Input

puts(<<~TEXT
  Hello
TEXT
) { "sample block" }
      
# >> Hello

Current output

puts(<<~TEXT)
  Hello
TEXT { 'sample block' }

# ~> -:5: can't find string "TEXT" anywhere before EOF
# ~> -:1: syntax error, unexpected end-of-input
# ~> puts(<<~TEXT)
# ~>             ^

Expected output

puts(<<~TEXT
  Hello
TEXT
) { 'sample block' }
@kddeisz
Copy link
Member

@kddeisz kddeisz commented May 9, 2019

Oof yeah that's a tough one. I'll take a look.

@UnderpantsGnome
Copy link

@UnderpantsGnome UnderpantsGnome commented Apr 2, 2020

  • Ruby 2.6.5
  • prettier-ruby 0.18.0
  • prettier 1.19.1

Before

scope :late_for_checkin, -> {
  select(
    <<-EOS.squish
      devices.*, (
        SELECT device_readings.id
        FROM device_readings
        WHERE device_readings.device_id = devices.id
          AND device_readings.taken_at > DATE_SUB(NOW(), INTERVAL (frequency * 2) MINUTE)
        LIMIT 1
      ) AS reading_id
    EOS
  )
    .data_push
    .having("reading_id IS NULL")
}

After

scope :late_for_checkin,
      lambda {
        select(
          <<-EOS
      devices.*, (
        SELECT device_readings.id
        FROM device_readings
        WHERE device_readings.device_id = devices.id
          AND device_readings.taken_at > DATE_SUB(NOW(), INTERVAL (frequency * 2) MINUTE)
        LIMIT 1
      ) AS reading_id
    EOS
            .squish
        )
          .data_push
          .having("reading_id IS NULL")
      }

Expected

scope :late_for_checkin, -> {
      select(
        <<-EOS.squish
          devices.*, (
            SELECT device_readings.id
            FROM device_readings
            WHERE device_readings.device_id = devices.id
              AND device_readings.taken_at > DATE_SUB(NOW(), INTERVAL (frequency * 2) MINUTE)
            LIMIT 1
          ) AS reading_id
        EOS
      )
        .data_push
        .having("reading_id IS NULL")
    }
@kddeisz kddeisz added the bug label Apr 5, 2020
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 10, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 10, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 10, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 10, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 10, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 11, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 11, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 11, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
jbielick added a commit to AdWerx/plugin-ruby that referenced this issue Oct 11, 2020
Instead of ad-hoc code in various places plucking
out heredoc beging, contents, and ending in order
to print it more intelligently in certain cases,
this changes the way heredocs are printed
altogether so they can simply be printed in any
context and still work correctly. This prevents
heredocs appearing in various parts of the syntax
tree from absorbing other lines of code because
they break after the heredoc literal and before
the heredoc contents because they were manually
plucked out the AST and inserted in the Doc
contextually.

fixes prettier#624
fixes prettier#586
fixes prettier#316
fixes prettier#301

There exists a command in prettier's set of
builders called lineSufix. It's listed as used for
implementing trailing comments. The doc contents
you wrap in lineSuffix() are appended to the end
of the line during printing. This seemed
appropriate for this use case because of the
following facts about heredocs:

A heredoc literal beging can appear anywhere and
can be treated like a string literal. things can
be chained to it, it may need a trailing comma,
etc).  Conclusion: When printing a heredoc, more
code should be able to print after the heredoc
literal before the next line.

The next line break after the heredoc beging is
part of the heredoc contents. period.  Conclusion:
A line break must always appear, after which the
contents printed then the ending. Code can
continue on the next line.

Conclusion: Code chained or in the same expression
as the heredoc beging must fit on the same line as
the heredoc beging or be broken and continue on
the newline after the ending.

Using lineSuffix when printing heredocs allows us
to guarantee an append to the end of that line
where we can insert a literalline (non-negotiable
line break), the contents, and the ending. Code
continues printing after this ending newline.
Conclusion: lineSuffix's behavior matches the
behavior of heredocs in general and would allow a
heredoc to be printed correctly in any context.
The heredoc print would look like this:

concat([ beging, lineSuffix(concat([literalline,
concat(parts), ending])) ]); In words: since a
heredoc beging appears "here", print the beging
"here", but guarantee the following contents will
appear at the end of the "here" line: linebreak,
contents, ending.

Result: Thus, the end of line break and next few
lines are guaranteed to be the heredoc contents
(no absorbing other lines), the beging is the only
doc portion printed in place so trailing commas
and method chaining works fine. At this point,
_all heredocs are printing themselves correctly
anywhere they appear in the AST/Doc. The code used
to manually slice up the heredoc and print it
correctly in various places (hash.js, array.js,
calls.js, utils.js) could now be removed and
everything would print valid ruby code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.