Skip to content

Commit

Permalink
[#217] Fix #217: Put no-macro-parsing behind an option (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brujo Benavides authored Nov 19, 2020
1 parent 4251d71 commit 3557fc3
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ The plugin supports the following configuration options in the `format` section
* This option is only used when `inline_expressions` is `false`.
* If this option is `true`, one empty line will preserved for each group of empty lines that are placed between expressions in a clause.
* The default value is `true`.
+ `parse_macro_definitions` (`boolean()`):
* `ktn_dodger` (the module we use to parse the code) doesn't parse macro definitions by default. That's to prevent removing parentheses where they're actually meaningful in the context where the macro is used, but not in the context where it's defined.
* With this option in `true`, the formatter will instruct `ktn_dodger` to actually parse the macros.
* The default value is `true`.
* The idea is for users to turn it to `false` only for the module that contain macros that would be broken otherwise.
* `files` (`[file:filename_all()]`):
- List of wildcard patterns representing the files that will be formatted by default (i.e. when not using `--files` on command line).
- The default value is `["src/**/*.?rl"]`
Expand Down
9 changes: 6 additions & 3 deletions src/rebar3_ast_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
-spec format(file:filename_all(), module(), rebar3_formatter:opts()) ->
rebar3_formatter:result().
format(File, Formatter, Opts) ->
AST = get_ast(File),
AST = get_ast(File, Opts),
QuickAST = get_quick_ast(File),
Comments = get_comments(File),
{ok, Original} = file:read_file(File),
Expand All @@ -38,8 +38,11 @@ format(File, Formatter, Opts) ->
end
end.

get_ast(File) ->
case ktn_dodger:parse_file(File, [{scan_opts, [text]}, no_fail]) of
get_ast(File, Opts) ->
DodgerOpts =
[{scan_opts, [text]}, no_fail]
++ [parse_macro_definitions || maps:get(parse_macro_definitions, Opts, true)],
case ktn_dodger:parse_file(File, DodgerOpts) of
{ok, AST} ->
case [Error || {error, Error} <- AST] of
[] ->
Expand Down
5 changes: 5 additions & 0 deletions test_app/after/src/dont_parse_macros.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-module(dont_parse_macros).

-format #{parse_macro_definitions => false}.

-define(WITH_A_FLOAT, 1.90999999999999992006e+00).
2 changes: 2 additions & 0 deletions test_app/after/src/macros.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-module(macros).

-format #{parse_macro_definitions => false}.

-define(ADULT_AGE, 21).
-define(NEW_PERSON(Name, Age), #{name => Name, age => Age}).
-define(SOMEONE, ?NEW_PERSON("Someone", 44)).
Expand Down
5 changes: 5 additions & 0 deletions test_app/after/src/parse_macros.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-module(parse_macros).

-format #{parse_macro_definitions => true}.

-define(WITH_A_FLOAT, 1.91).
2 changes: 2 additions & 0 deletions test_app/after/src/syntax_tools_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

-module(syntax_tools_test).

-format #{parse_macro_definitions => false}.

-export([foo1/0, foo2/2, foo3/0, foo4/3, foo5/1]).

-include_lib("kernel/include/file.hrl").
Expand Down
5 changes: 5 additions & 0 deletions test_app/src/dont_parse_macros.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-module(dont_parse_macros).

-format #{parse_macro_definitions => false}.

-define(WITH_A_FLOAT, 1.91).
2 changes: 2 additions & 0 deletions test_app/src/macros.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-module(macros).

-format #{parse_macro_definitions => false}.

-define(ADULT_AGE, 21).
-define(NEW_PERSON(Name, Age), #{name => Name, age => Age}).
-define(SOMEONE, ?NEW_PERSON("Someone", 44)).
Expand Down
5 changes: 5 additions & 0 deletions test_app/src/parse_macros.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-module(parse_macros).

-format #{parse_macro_definitions => true}.

-define(WITH_A_FLOAT, 1.91).
2 changes: 2 additions & 0 deletions test_app/src/syntax_tools_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

-module(syntax_tools_test).

-format #{parse_macro_definitions => false}.

-export([foo1/0,foo2/2,foo3/0,foo4/3,foo5/1]).

-include_lib("kernel/include/file.hrl").
Expand Down

0 comments on commit 3557fc3

Please sign in to comment.