Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
Created December 11, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunlebron/252824d2c7362c5555e7e199db38bf16 to your computer and use it in GitHub Desktop.
Save shaunlebron/252824d2c7362c5555e7e199db38bf16 to your computer and use it in GitHub Desktop.
how elm-format and prettier deal with ambiguity

Case Study: how to branch without options

elm-format inspired prettier to let the user branch between two different formatters without the use of explicit options, by taking cues from the source's original formatting. (see multi-line objects)

  • Context: an object literal is sometimes better displayed on multiple lines even it can fit on one
  • Problem: it was deemed impossible for the formatter to know when multiline is preferable
  • Solution: allow the user to insert a newline after the first curly to force multiline
// before
const a = {foo:1, bar:2};  // <-- no newline after curly allows default formatting

// after
const a = { foo: 1, bar: 2 };
// before
const a = {                // <-- newline after the curly forces MULTILINE formatting
foo:1, bar:2};

// after:
const a = {
  foo:1,
  bar:2
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment