Run `elm-format` when file saved even when there are syntax errors
See original GitHub issueProblem
elm-format
isn’t executed when a file is saved, and I think this is because the plugin detects syntax errors (I am not confident in that, but that matches my experiments).
Motivation
Coming from Atom and elmjutsu
, I am a bit frustrated with how many more syntax errors I get now compared to with elmjutsu
.
From what I understand, elm-format
has looser syntax requirements than IntelliJ, but fixes them when it is run. I show a few examples where elm-format
fixes the syntax errors below. They already get fixed today when I run elm-format
from the CLI or from IntelliJ, so it would only be a matter of enabling it on save.
I feel that this would be a huge improvement to the user’s experience, especially for newcomers to the language. With elmjutsu
doing this this way, I never felt that the language was too strict on the syntax (like indentation for instance), but without elm-format
covering for me, it feels more painful to write Elm.
Examples of fixed syntax errors
(Would fix the error reported in https://github.com/klazuka/intellij-elm/issues/443 if the users enabled elm-format
on save)
-- Doesn't compile
type alias A =
{}
-- After elm-format, compiles
type alias A =
{}
(Would fix the error reported in https://github.com/klazuka/intellij-elm/issues/22 if the users enabled elm-format
on save)
-- Doesn't compile
foo =
a (\x ->
case x of
[] -> 0
_ -> 1)
-- After elm-format, compiles
foo =
a
(\x ->
case x of
[] ->
0
_ ->
1
)
-- Doesn't compile
type alias A =
{ a = Int }
-- After elm-format, compiles
type alias A =
{ a : Int }
-- Doesn't compile
value =
{ a : 1 }
-- After elm-format, compiles
value =
{ a = 1 }
-- Doesn't compile
func a =
let
b = 1
in
a
-- After elm-format, compiles
func a =
let
b =
1
in
a
Thanks
I will use this opportunity to thank you for the hard work you have put into this project. It is really a splendid tool ❤️
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (2 by maintainers)
Top GitHub Comments
I recently learned on the Elm Town podcast that elm-format can fix
type alias Foo = { foo = Int }
intotype alias Foo = { foo : Int }
. When trying this feature out, I was confused because I tried it via format-on-save in IntelliJ. Now I understand why it didn’t work! (Running format via keyboard shortcut works, fortunately.)I’d also like it if elm-format was (attempted to be) run on save even if there are syntax errors.
Oops, I’m sorry, it was not
elmjutsu
but theelm-format
Atom plugin.Anyway, this is the result when you have a syntax error: A (not very helpful) notification in the top-right corner
I have checked, and
elm-syntax
exits with status code 0 when there are syntax errors that it can (and does) fix. I have also looked at the code, and here is the section that deals with whenelm-format
exits with an error code. https://github.com/humio/atom-elm-format/blob/master/src/index.js#L100-L128So basically, they strip the ANSI codes and whatever information that is not really relevant, find the line number in order to create something actionable like “Jump to Syntax Error” (which doesn’t really work for me).
The reason why I was confused about
atom-elm-format
/elmjutsu
is that after that, I then see this (more helpful) compiler error message, thatelmjutsu
creates because the compiler returned with an exit code.Summary:
atom-elm-format
tries to runelm-format
elmjutsu
comes in and runs the compiler, which gives me a useful error messageI think that in the case of this plugin, you can run
elm-format
on every save, and if it exits with status code 1, you pretty much ignore the error message, and use the current behavior to indicate there is a syntax error.