elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.51k stars 3.38k forks source link

Do not enforce parentheses in `Code.format_string!/2` on single-line `defmodule/2` calls #9006

Closed bceskavich closed 5 years ago

bceskavich commented 5 years ago

Environment

Erlang/OTP 21 [erts-10.0.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe] Elixir 1.8.1 (compiled with Erlang/OTP 20)

macOS Mojave 10.14.1

Current behavior

With the default setup, the code formatter will enforce parentheses on single-line module definitions with the defmodule/2 macro.

Say you have a module that simply calls use on another module to inject some code:

defmodule MyModule, do: use MyOtherModule

The formatter will replace the spaces with parentheses:

defmodule(MyModule, do: use(MyOtherModule))

Expected behavior

While I understand there may be reasoning for this design, it initially strikes me as a bit unexpected—mainly because the formatter doesn't enforce parentheses use for single-line calls to other Kernel marcos. For example, the very common single-line function definition:

def my_function(), do: :ok 

Can the default behavior of Code.format_string!/2 be changed to allow single-line defmodule/2 calls that omit parentheses?

I think this may be as simple as adding defmodule: 2 to the locals_without_parens list in Code.Formatter, but I may be wrong.

josevalim commented 5 years ago

Yes, it is as simple as adding defmodule/2! Please send a PR! --

José Valim www.plataformatec.com.br Skype: jv.ptec Founder and Director of R&D

bceskavich commented 5 years ago

@josevalim great, will do!