Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Elm fragments get turned into Elm modules named Main #744

Open
robsimmons opened this issue Jul 19, 2021 · 2 comments
Open

Elm fragments get turned into Elm modules named Main #744

robsimmons opened this issue Jul 19, 2021 · 2 comments

Comments

@robsimmons
Copy link

If blah.txt contains the following contents:

whatever =
    6 + 12

elm-format (version 0.8.5) will change the file to one that has different semantics:

 % ./node_modules/.bin/elm-format blah.txt --yes && cat blah.txt
Processing file blah.txt
module Main exposing (whatever)


whatever =
    6 + 12

If blah.txt is an elm fragment that was later going to be merged into a larger piece of code, then the addition of module Main exposing (whatever) violates expected behavior for an autoformatter, which is to never change the meaning of file, by turning something that's not broken into something that is.

Elm does support fragmentary files that don't start as module; wouldn't it be more consistent to leave those files fragmentary when they're autoformatted?

@avh4
Copy link
Owner

avh4 commented Jul 19, 2021

The internals sort of support this, and it's partially used for formatting Elm code fragments that are in markdown code blocks within documentation comments. But there's not currently a way to invoke this directly. A safe workaround for the moment would be to format as you have, and then remove lines starting with /^module /

I think a command line argument to specify what type of thing is being input would be worth adding. I think the things that make sense to format are:

  • modules (currently the only option)
  • a list of declarations (I think this is your use case)
  • a single expression
  • a single type
  • (maybe) a list of expressions

This issue is also related: #65

@robsimmons
Copy link
Author

I see this as simpler, and I kinda don't think this even needs to/should be a command line argument? The spec in my mind is "an autoformatter turns a valid file into an 'equivalent-whatever-that-means' file, or raise an error."

A list of declarations in a file is accepted by the elm compiler (yesterday-I-learned!!!), and I'd ask elm-format to output a semantically 'equivalent-whatever-that-means' list of declarations, rather than a module, which doesn't seem equivalent to me.

At first I assumed a list of declarations wouldn't be accepted by the elm compiler! If that had been the case I would have argued elm-format should raise an error on a fragment without a module declaration. That state of the world would be preferable all around to me but I will acknowledge that x = "Hello " ++ String.fromInt 12 just being accepted, as a file, by the Elm compiler is potentially a quite nice-for-beginners touch. Given that Elm accepts it, elm-format certainly shouldn't reject it outright.

(Context: for us, this happened because elm-format got run on a fragmentary elm file it actually shouldn't have and broke some stuff... we do the "remove module lines" with elm-format in other cases.)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants