From 3557fc3b47c0b9e047f77b014e449d5822e1369c Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Thu, 19 Nov 2020 16:01:39 +0100 Subject: [PATCH] [#217] Fix #217: Put no-macro-parsing behind an option (#218) --- README.md | 5 +++++ src/rebar3_ast_formatter.erl | 9 ++++++--- test_app/after/src/dont_parse_macros.erl | 5 +++++ test_app/after/src/macros.erl | 2 ++ test_app/after/src/parse_macros.erl | 5 +++++ test_app/after/src/syntax_tools_test.erl | 2 ++ test_app/src/dont_parse_macros.erl | 5 +++++ test_app/src/macros.erl | 2 ++ test_app/src/parse_macros.erl | 5 +++++ test_app/src/syntax_tools_test.erl | 2 ++ 10 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test_app/after/src/dont_parse_macros.erl create mode 100644 test_app/after/src/parse_macros.erl create mode 100644 test_app/src/dont_parse_macros.erl create mode 100644 test_app/src/parse_macros.erl diff --git a/README.md b/README.md index eb5c8f7..cb383eb 100644 --- a/README.md +++ b/README.md @@ -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"]` diff --git a/src/rebar3_ast_formatter.erl b/src/rebar3_ast_formatter.erl index 65d14ef..919974a 100644 --- a/src/rebar3_ast_formatter.erl +++ b/src/rebar3_ast_formatter.erl @@ -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), @@ -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 [] -> diff --git a/test_app/after/src/dont_parse_macros.erl b/test_app/after/src/dont_parse_macros.erl new file mode 100644 index 0000000..ab9c5db --- /dev/null +++ b/test_app/after/src/dont_parse_macros.erl @@ -0,0 +1,5 @@ +-module(dont_parse_macros). + +-format #{parse_macro_definitions => false}. + +-define(WITH_A_FLOAT, 1.90999999999999992006e+00). diff --git a/test_app/after/src/macros.erl b/test_app/after/src/macros.erl index c7791cb..6c7e098 100644 --- a/test_app/after/src/macros.erl +++ b/test_app/after/src/macros.erl @@ -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)). diff --git a/test_app/after/src/parse_macros.erl b/test_app/after/src/parse_macros.erl new file mode 100644 index 0000000..6e7de5e --- /dev/null +++ b/test_app/after/src/parse_macros.erl @@ -0,0 +1,5 @@ +-module(parse_macros). + +-format #{parse_macro_definitions => true}. + +-define(WITH_A_FLOAT, 1.91). diff --git a/test_app/after/src/syntax_tools_test.erl b/test_app/after/src/syntax_tools_test.erl index bb598de..fb2be2a 100644 --- a/test_app/after/src/syntax_tools_test.erl +++ b/test_app/after/src/syntax_tools_test.erl @@ -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"). diff --git a/test_app/src/dont_parse_macros.erl b/test_app/src/dont_parse_macros.erl new file mode 100644 index 0000000..b6e7d06 --- /dev/null +++ b/test_app/src/dont_parse_macros.erl @@ -0,0 +1,5 @@ +-module(dont_parse_macros). + +-format #{parse_macro_definitions => false}. + +-define(WITH_A_FLOAT, 1.91). diff --git a/test_app/src/macros.erl b/test_app/src/macros.erl index f2a0f2a..35b8cbb 100644 --- a/test_app/src/macros.erl +++ b/test_app/src/macros.erl @@ -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)). diff --git a/test_app/src/parse_macros.erl b/test_app/src/parse_macros.erl new file mode 100644 index 0000000..6e7de5e --- /dev/null +++ b/test_app/src/parse_macros.erl @@ -0,0 +1,5 @@ +-module(parse_macros). + +-format #{parse_macro_definitions => true}. + +-define(WITH_A_FLOAT, 1.91). diff --git a/test_app/src/syntax_tools_test.erl b/test_app/src/syntax_tools_test.erl index d65cd43..59bf0ea 100644 --- a/test_app/src/syntax_tools_test.erl +++ b/test_app/src/syntax_tools_test.erl @@ -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").