question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running prettier on if-else statements generates invalid ruby

See original GitHub issue

Metadata

  • OS: mac 10.14.3 (mojave)
  • Node version: 11.2.0
  • Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
  • @prettier/plugin-ruby version: 0.7.0

Background

I’m not super familiar with ruby so I don’t know what’s legal ternary syntax. However, I’m experiencing an issue with @prettier/plugin-ruby. I have an if/else block that I’ve simplified from my original code base (so it’s a little contrived, I’ll admit). When I run it through prettier, the first time it converts the if/else to a ternary. When I run prettier a second time however I get an error saying my code isn’t valid ruby.

Original Input

module TestModule
  def broken_ternary
    if true
      var = 'value1'
    else
      return 'value2'
    end
  end
end

After running prettier once

module TestModule
  def broken_ternary
    true ? var = 'value1' : return 'value2'
  end
end

After running prettier a second time

I get the following error:

$ ./node_modules/.bin/prettier --write TestModule.rb 
TestModule.rb
[error] TestModule.rb: Error: Invalid ruby
[error] 
[error]     at Object.module.exports [as parse] (/myProj/node_modules/@prettier/plugin-ruby/src/parse.js:9:11)
[error]     at Object.parse$2 [as parse] (/myProj/node_modules/prettier/bin-prettier.js:10641:19)
[error]     at coreFormat (/myProj/node_modules/prettier/bin-prettier.js:13858:23)
[error]     at format (/myProj/node_modules/prettier/bin-prettier.js:14117:73)
[error]     at formatWithCursor (/myProj/node_modules/prettier/bin-prettier.js:14133:12)
[error]     at Object.formatWithCursor (/myProj/node_modules/prettier/bin-prettier.js:42401:15)
[error]     at format$1 (/myProj/node_modules/prettier/bin-prettier.js:43770:21)
[error]     at /myProj/node_modules/prettier/bin-prettier.js:43965:16
[error]     at /myProj/node_modules/prettier/bin-prettier.js:43905:14
[error]     at Array.forEach (<anonymous>)

Expected output

So it seems like prettier is converting my if/else to a ternary where each part after the condition is a block of code that gets run. I don’t know if that’s legal in ruby but I’ve always used ternaries more like this:

module TestModule
  def broken_ternary
    return test_condition ? 'value1'  :  'value2'
  end
end

So, based on some condition, you will assign one of two values to a variable (or return them). (Side note, if I run this through prettier it doesn’t change anything). The way that prettier writes its ternaries is more like an if else block where, based on some condition, it will execute each part of the block. That might be valid, but prettier itself doesn’t think so.

All this to say that if you run prettier on the original input, I wouldn’t expect it to change at all.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kddnewtoncommented, Feb 28, 2019

I still feel like we should be in certain circumstances. Obviously we’ll need to handle cases like assignments and control flow but for instance

if foo
  a = 1
else
  a = 2
end

is not idiomatic ruby. That should be a = foo ? 1 : 2. I can take a look at this issue and making sure we have a good list of exceptions to ternary expressions. At the moment I believe it’s limited to command and command_call nodes.

2reactions
AlanFostercommented, Feb 28, 2019

@NoahTheDuke I feel the same way 👍

Particularly when if statements are used for purely side effects, and not for the expressions that they return.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code doesn't run when I had one more elsif statement
You tried to search in wrong array. Change destination_index = line_two.index(user_input_2).
Read more >
if / else errors - learn how to fix these - Codecademy
First off, a quick review of what if statements look like. A typical if () statement ... and then they post a question...
Read more >
@prettier/plugin-ruby | Yarn - Package Manager
To run prettier with the Ruby plugin, you're going to need ruby (version 2.7. 3 or newer) and node (version 8.3 or newer)....
Read more >
Ruby Style Guide
If you're into Rails or RSpec you might want to check out the complementary Ruby on Rails Style ... Prefer a guard clause...
Read more >
How to Use Ruby BEGIN and END Blocks | Scout APM Blog
HOWEVER, uppercase BEGIN and END create these specialized blocks that run at the beginning or end. They use curly braces {} to enclose...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found