Formatting code in Elixir • The mix format command formats your code # Elixir lang example ## before IO . puts ("Elixir is Nice") ## after `mix format` IO.puts("Elixir is Nice")
It's safe with --check-equivalent option $ mix format --check-equivalent check if the file after formatting has the same AST. If the ASTs are not equivalent, it is a bug in the code formatter. This option is recommended if you are automatically formatting files. — https:/ /hexdocs.pm/mix/1.6.1/ Mix.Tasks.Format.html
What is AST? • AST is Abstract Syntax Tree ! • Elixir's internal representation of code • Elixir parses code into AST before executing your program • Same AST means that code behavior is also same Code ! -> AST " -> ... -> Execute #
Detail of formatting process with --check-equivalent option 1. generate formatted code from original code • ! -> " 2. generate AST both the codes • ! -> # • " -> # 3. then compare • # ≒ #
Example code # before defmodule My do; def coooolest do; "one-liner"; end; end # after `mix format` defmodule My do def coooolest do "one-liner" end end
Compare both of ASTs • The diff shows almost same but [line: X] is indifferent ! • But this is metadata so it does not affect behavior " # diff + [do: {:def, [line: 2], [{:coooolest, [line: 2], nil}, [do: "one-liner"]]}] - [do: {:def, [line: 1], [{:coooolest, [line: 1], nil}, [do: "one-liner"]]}]