View Source Es6Maps.Formatter (es6_maps v0.2.2)

Replaces all map keys with their shorthand form.

Add the plugin to .formatter.exs, then call mix format to reformat your code:

# .formatter.exs
[
plugins: [Es6Maps.Formatter],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

The plugin manipulates the AST, not raw strings, so it's precise and will only change your code by:

  1. changing map keys into the shorthand form;
  2. reordering map keys so the shorthand form comes first;
  3. formatting the results like mix format would.

Reverting to the vanilla-style maps

The formatting plugin can also be used to revert all of the ES6-style map shorthand uses back to the "vanilla" style. Set the es6_maps: [map_style: :vanilla] option in .formatter.exs, then call mix format to reformat your code:

# .formatter.exs
[
plugins: [Es6Maps.Formatter],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
es6_maps: [map_style: :vanilla]
]

Formatting pragmas

The plugin supports pragmas in the comments to control the formatting. The pragma must be in the form # es6_maps: [map_style: :vanilla] and can be placed anywhere in the file. The map_style option can be set to :es6 to convert to shorthand form or :vanilla to revert to the vanilla-style maps. The pragma takes effect only on the line following the comment.

For example in the code below, the first map will be formatted to the shorthand form, while the second map will be left as is:

  %{foo, bar: 1} = var
  # es6_maps: [map_style: :vanilla]
  %{hello: hello, foo: foo, bar: 1} = var

es6_maps: [map_style: :vanilla] option in .formatter.exs can be combined with # es6_maps: [map_style: :es6] comment pragmas.

Options

  • es6_maps:
    • map_style - :es6 to convert to shorthand form, :vanilla to revert to the vanilla-style maps.
  • all other options of mix format, such as line_length, are supported and passed down to formatting functions.

Summary

Functions