avh4 / elm-format

elm-format formats Elm source code according to a standard set of rules based on the official Elm Style Guide
BSD 3-Clause "New" or "Revised" License
1.31k stars 145 forks source link

Import statement not followed by a new line is a syntax error #551

Open kachkaev opened 5 years ago

kachkaev commented 5 years ago

Hi @avh4! I am upgrading prettier-plugin-elm in https://github.com/gicentre/prettier-plugin-elm/pull/4 and it looks like I have found a broken edge case. Elm-format reports a syntax error when a given file only contains an import statement that is not followed by \n.

Steps to reproduce:

npm install --global elm-format@0.8.1-rc2
echo "import Something exposing (something)" > ModuleWithTrailingNewLine.elm
elm-format ModuleWithTrailingNewLine.elm --yes --elm-version 0.19
cat ModuleWithTrailingNewLine.elm 
module Main exposing (..)

import Something exposing (something)

(all ok)


echo -n "import Something exposing (something)" > ModuleWithoutTrailingNewLine.elm
elm-format ModuleWithoutTrailingNewLine.elm --yes --elm-version 0.19

Processing file ModuleWithoutTrailingNewLine.elm
ERRORS
-- SYNTAX PROBLEM ----------------------------- ModuleWithoutTrailingNewLine.elm

I ran into something unexpected when parsing your code!

1│ import Something exposing (something)
                                        ^
I am looking for one of the following things:

    whitespace

(I did not expect this)


Import statements not followed by new lines are these kind of blocks in markdown files:

```elm
import Something exposing (something)


There's plenty of these in [litvis](http://github.com/gicentre/litvis) documents, e.g.:  
https://github.com/gicentre/litvis/blob/master/examples/features/simpleChart.md ([raw](https://raw.githubusercontent.com/gicentre/litvis/master/examples/features/simpleChart.md))

I'm able to overcome this `elm-format` limitation by [appending a dummy comment to the end of all files and then removing it](https://github.com/gicentre/prettier-plugin-elm/pull/4/commits/5c52f5948d7311d682ebe7e423893a3225d6b158#diff-6947033678b93d106e25614dd972e66fR25). This works fine, but I'm wondering if the solution could get cleaner if the edge case works upstream.

WDYT?
kachkaev commented 5 years ago

I released prettier-plugin-elm@0.4.0-rc.1, but will be happy to wait for elm-format@0.8.1 before making it stable. It'd be great to know if this issue could be closed before then or if the behaviour is intentional.

avh4 commented 5 years ago

Huh, I'm surprised your first example works, because according to elm make, an Elm file is not valid unless it contains at least one definition in it. I guess this seems to me like something that is relevant to https://github.com/avh4/elm-format/issues/65 but that isn't meant to be supported in the normal format-a-full-file mode.

kachkaev commented 5 years ago

IMHO just having imports, comments or the module definition should not make the file unformattable 🤔

Similarly, writing import Something exposing (something) won't compile if Something is not around. However, formatting this line is totally fine.