avh4/elm-format

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

Open

#551 opened on Sep 16, 2018

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Haskell (1,329 stars) (148 forks)batch import
help wantedlenient parser

Description

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 documents, e.g.:
https://github.com/gicentre/litvis/blob/master/examples/features/simpleChart.md (raw)

I'm able to overcome this elm-format limitation by appending a dummy comment to the end of all files and then removing it. This works fine, but I'm wondering if the solution could get cleaner if the edge case works upstream.

WDYT?

Contributor guide