Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Closes #160] Rules config as maps #161

Merged
merged 7 commits into from
Oct 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
Erlang Style Reviewer

## Contact Us
For **questions** or **general comments** regarding the use of this library, please use our public
[hipchat room](https://www.hipchat.com/gpBpW3SsT).
For **questions** or **general comments** regarding the use of this library,
please use our public [hipchat room](https://www.hipchat.com/gpBpW3SsT).

If you find any **bugs** or have a **problem** while using this library, please [open an issue](https://github.com/inaka/elvis/issues/new) in this repo (or a pull request :)).
If you find any **bugs** or have a **problem** while using this library, please
[open an issue](https://github.com/inaka/elvis/issues/new) in this repo
(or a pull request :)).

And you can check all of our open-source projects at [inaka.github.io](http://inaka.github.io)
And you can check all of our open-source projects at [inaka.github.io](http://inaka.github.io).

## Usage

Expand Down Expand Up @@ -158,7 +160,7 @@ rules:

## Configuration

To provide a default configuration for `elvis` you should either provide an
To provide a default configuration for `elvis` you should either create an
`elvis.config` file located in the root directory or set the following
environment values in your [configuration][config] file:

Expand All @@ -170,15 +172,15 @@ environment values in your [configuration][config] file:
{config,
[#{dirs => ["src", "test"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}
]}
]
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
{right, "++"},
{left, "++"}]}
}
]
},
#{dirs => ["."],
filter => "Makefile",
Expand All @@ -200,14 +202,15 @@ environment values in your [configuration][config] file:

The `dirs` key is a list that indicates where `elvis` should look for the
files that match `filter`, which will be run through each of the rules
specified by the `rules` entry, which is list of items with the following
structure `{Module, Function, Args}`.
specified by the `rules` entry, which is a list of items with the following
structure `{Module, Function, RuleConfig}` or `{Module, Function}` if the rule
takes no configuration values.

As you can see a rule is just a function and it takes 3 arguments: `elvis`'s
The implementation of a rule is just a function that takes 3 arguments: `elvis`'s
`config` entry from its [configuration](#configuration); the file to be
analyzed; and some configuration `Args` (arguments) specified for the rule.
This means you can define rules of your own as long as the functions that
implement them respect this arity.
analyzed; and a configuration map specified for the rule. This means you can
define rules of your own as long as the functions that implement them respect
this arity.

There's currently no default configuration for `elvis`, but in the meantime
you can take the one in `config/elvis.config` as a starting point.
Expand Down
39 changes: 20 additions & 19 deletions config/elvis-test.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@
{config,
[#{dirs => ["../../src"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
{right, "++"},
{left, "++"}]}},
{elvis_style, nesting_level, #{level => 3}},
{elvis_style, god_modules, #{limit => 25}},
{elvis_style, no_if_expression},
{elvis_style, invalid_dynamic_call, #{ignore => [elvis]}},
{elvis_style, used_ignored_variable},
{elvis_style, no_behavior_info},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
#{regex => "^([a-z][a-z0-9]*_?)*(_SUITE)?$",
ignore => []}
},
{elvis_style, state_record_and_type, []},
{elvis_style, no_spec_with_records, []}
{elvis_style, state_record_and_type},
{elvis_style, no_spec_with_records}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
rules => [{elvis_project, no_deps_master_erlang_mk, #{ignore => []}}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
rules => [{elvis_project, no_deps_master_rebar, #{ignore => []}}]
},
#{dirs => ["."],
filter => "elvis.config",
rules => [{elvis_project, old_configuration_format, []}]
rules => [{elvis_project, old_configuration_format}]
}
]
}
Expand Down
39 changes: 20 additions & 19 deletions config/elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@
{config,
[#{dirs => ["src"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
{right, "++"},
{left, "++"}]}},
{elvis_style, nesting_level, #{level => 3}},
{elvis_style, god_modules, #{limit => 25}},
{elvis_style, no_if_expression},
{elvis_style, invalid_dynamic_call, #{ignore => [elvis]}},
{elvis_style, used_ignored_variable},
{elvis_style, no_behavior_info},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
#{regex => "^([a-z][a-z0-9]*_?)*(_SUITE)?$",
ignore => []}
},
{elvis_style, state_record_and_type, []},
{elvis_style, no_spec_with_records, []}
{elvis_style, state_record_and_type},
{elvis_style, no_spec_with_records}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
rules => [{elvis_project, no_deps_master_erlang_mk, #{ignore => [sync]}}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
rules => [{elvis_project, no_deps_master_rebar, #{ignore => []}}]
},
#{dirs => ["."],
filter => "elvis.config",
rules => [{elvis_project, old_configuration_format, []}]
rules => [{elvis_project, old_configuration_format}]
}
]
}
Expand Down
46 changes: 46 additions & 0 deletions config/old/elvis-test-rule-config-list.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
elvis,
[
{config,
[#{dirs => ["../../test/examples"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
},
{elvis_style, state_record_and_type, []},
{elvis_style, no_spec_with_records, []}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
},
#{dirs => ["."],
filter => "elvis.config",
rules => [{elvis_project, old_configuration_format, []}]
}
]
}
]
}
].
39 changes: 20 additions & 19 deletions config/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@
{config,
[#{dirs => ["../../test/examples"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
{right, "++"},
{left, "++"}]}},
{elvis_style, nesting_level, #{level => 3}},
{elvis_style, god_modules, #{limit => 25}},
{elvis_style, no_if_expression},
{elvis_style, invalid_dynamic_call, #{ignore => [elvis]}},
{elvis_style, used_ignored_variable},
{elvis_style, no_behavior_info},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
#{regex => "^([a-z][a-z0-9]*_?)*(_SUITE)?$",
ignore => []}
},
{elvis_style, state_record_and_type, []},
{elvis_style, no_spec_with_records, []}
{elvis_style, state_record_and_type},
{elvis_style, no_spec_with_records}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
rules => [{elvis_project, no_deps_master_erlang_mk, #{ignore => []}}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
rules => [{elvis_project, no_deps_master_rebar, #{ignore => []}}]
},
#{dirs => ["."],
filter => "elvis.config",
rules => [{elvis_project, old_configuration_format, []}]
rules => [{elvis_project, old_configuration_format}]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
]
}.
{escript_name, "elvis"}.
{escript_incl_apps, [getopt, jiffy, ibrowse, aleppo, zipper]}.
{escript_incl_apps, [getopt, jiffy, ibrowse, aleppo, zipper, katana]}.
29 changes: 27 additions & 2 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ apply_rules(Config, File) ->
elvis_result:print(Results),
Results.

apply_rule({Module, Function, Args}, {Result, Config, File}) ->
apply_rule({Module, Function}, {Result, Config, File}) ->
apply_rule({Module, Function, #{}}, {Result, Config, File});
apply_rule({Module, Function, ConfigArgs}, {Result, Config, File}) ->
ConfigMap = ensure_config_map(Module, Function, ConfigArgs),
RuleResult = try
Results = Module:Function(Config, File, Args),
Results = Module:Function(Config, File, ConfigMap),
elvis_result:new(rule, Function, Results)
catch
_:Reason ->
Expand Down Expand Up @@ -234,3 +237,25 @@ github_credentials() ->
User = application:get_env(elvis, github_user, ""),
Password = application:get_env(elvis, github_password, ""),
egithub:basic_auth(User, Password).

%% @doc Process a tules configuration argument and converts it to a map.
ensure_config_map(_, _, Map) when is_map(Map) ->
Map;
ensure_config_map(elvis_style, line_length, [Limit]) ->
#{limit => Limit};
ensure_config_map(elvis_style, operator_spaces, Rules) ->
#{rules => Rules};
ensure_config_map(elvis_style, nesting_level, [Level]) ->
#{level => Level};
ensure_config_map(elvis_style, god_modules, [Limit]) ->
#{limit => Limit};
ensure_config_map(elvis_style, god_modules, [Limit, IgnoreModules]) ->
#{limit => Limit, ignore => IgnoreModules};
ensure_config_map(elvis_style, invalid_dynamic_call, IgnoreModules) ->
#{ignore => IgnoreModules};
ensure_config_map(elvis_style,
module_naming_convention,
[Regex, IgnoreModules]) ->
#{regex => Regex, ignore => IgnoreModules};
ensure_config_map(_, _, []) ->
#{}.
Loading