Running prettier on if-else statements generates invalid ruby
See original GitHub issueMetadata
- 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:
- Created 5 years ago
- Comments:9 (6 by maintainers)
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
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 tocommand
andcommand_call
nodes.@NoahTheDuke I feel the same way 👍
Particularly when if statements are used for purely side effects, and not for the expressions that they return.