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

Fix 210: New rule for behavior vs behaviour. #212

Merged
merged 5 commits into from
Oct 6, 2021
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
2 changes: 2 additions & 0 deletions src/elvis_rulesets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rules(erl_files) ->
, {elvis_style, no_nested_try_catch}
, {elvis_style, atom_naming_convention}
, {elvis_style, numeric_format}
, {elvis_style, behaviour_spelling}
]
);
rules(beam_files) ->
Expand All @@ -59,6 +60,7 @@ rules(beam_files) ->
, variable_naming_convention
, no_nested_try_catch
, atom_naming_convention
, behaviour_spelling
]
);
rules(makefiles) ->
Expand Down
33 changes: 33 additions & 0 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
no_nested_try_catch/3,
atom_naming_convention/3,
numeric_format/3,
behaviour_spelling/3,
option/3
]).

Expand Down Expand Up @@ -126,6 +127,10 @@
"Number ~p on line ~p does not respect the format "
"defined by the regular expression '~p'.").

-define(BEHAVIOUR_SPELLING,
"The behavior/behaviour in line ~p is misspelt, please use the "
"~p spelling.").

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Default values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -233,6 +238,10 @@ default(numeric_format) ->
#{ regex => ".*"
, int_regex => same
, float_regex => same
};

default(behaviour_spelling) ->
#{ spelling => behaviour
}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -765,6 +774,30 @@ numeric_format(Config, Target, RuleConfig) ->
IntNodes,
check_numeric_format(FloatRegex, FloatNodes, [])).

-spec behaviour_spelling(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
[elvis_result:item()].
behaviour_spelling(Config, Target, RuleConfig) ->
Spelling = option(spelling, RuleConfig, behaviour_spelling),
Root = get_root(Config, Target, RuleConfig),
Predicate =
fun(Node) ->
NodeType = ktn_code:type(Node),
lists:member(NodeType, [behaviour, behavior]) andalso NodeType /= Spelling
end,
case elvis_code:find(Predicate, Root) of
[] -> [];
InconsistentBehaviorNodes ->
ResultFun =
fun(Node) ->
{Line, _} = ktn_code:attr(location, Node),
Info = [Line, Spelling],
elvis_result:new(item, ?BEHAVIOUR_SPELLING, Info, Line)
end,
lists:map(ResultFun, InconsistentBehaviorNodes)
end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Private
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
38 changes: 38 additions & 0 deletions test/examples/american_behavior_spelling.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-module(american_behavior_spelling).

-dialyzer(no_behaviours).

-behavior(gen_server).

-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3,
terminate/2
]).

-spec init(term()) -> ok.
init(_Args) ->
ok.

-spec handle_call(term(), term(), term()) -> ok.
handle_call(_Request, _From, _State) ->
ok.

-spec handle_cast(term(), term()) -> ok.
handle_cast(_Request, _State) ->
ok.

-spec handle_info(term(), term()) -> ok.
handle_info(_Info, _State) ->
ok.

-spec code_change(term(), term(), term()) -> term().
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

-spec terminate(term(), term()) -> ok.
terminate(_Reason, _State) ->
ok.
38 changes: 38 additions & 0 deletions test/examples/british_behaviour_spelling.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-module(british_behaviour_spelling).

-dialyzer(no_behaviours).

-behaviour(gen_server).

-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3,
terminate/2
]).

-spec init(term()) -> ok.
init(_Args) ->
ok.

-spec handle_call(term(), term(), term()) -> ok.
handle_call(_Request, _From, _State) ->
ok.

-spec handle_cast(term(), term()) -> ok.
handle_cast(_Request, _State) ->
ok.

-spec handle_info(term(), term()) -> ok.
handle_info(_Info, _State) ->
ok.

-spec code_change(term(), term(), term()) -> term().
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

-spec terminate(term(), term()) -> ok.
terminate(_Reason, _State) ->
ok.
2 changes: 1 addition & 1 deletion test/examples/fail_state_record_and_type.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-dialyzer(no_behaviours).

-behavior(gen_server).
-behaviour(gen_server).

-export([
init/1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-dialyzer(no_behaviours).

-behavior(gen_statem).
-behaviour(gen_statem).

-export([
init/1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-dialyzer(no_behaviours).

-behavior(gen_statem).
-behaviour(gen_statem).

-export([
init/1,
Expand Down
2 changes: 1 addition & 1 deletion test/examples/fail_state_type.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-dialyzer(no_contracts).
-dialyzer(no_behaviours).

-behavior(gen_server).
-behaviour(gen_server).

-export([
init/1,
Expand Down
2 changes: 1 addition & 1 deletion test/examples/pass_state_record_and_type.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-dialyzer(no_behaviours).

-behavior(gen_server).
-behaviour(gen_server).

-export([
init/1,
Expand Down
2 changes: 1 addition & 1 deletion test/examples/pass_state_record_and_type_elvis_attr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-dialyzer(no_behaviours).

-behavior(gen_server).
-behaviour(gen_server).

-export([
init/1,
Expand Down
2 changes: 1 addition & 1 deletion test/examples/pass_state_record_and_type_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-dialyzer(no_behaviours).

-behavior(gen_statem).
-behaviour(gen_statem).

-export([
init/1,
Expand Down
15 changes: 15 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
verify_elvis_attr_state_record_and_type/1,
verify_elvis_attr_used_ignored_variable/1,
verify_elvis_attr_variable_naming_convention/1,
verify_behaviour_spelling/1,
%% Non-rule
results_are_ordered_by_line/1,
oddities/1
Expand Down Expand Up @@ -550,6 +551,20 @@ verify_state_record_and_type(Config) ->
PathPassGenStateMState = "fail_state_record_and_type_gen_statem_state." ++ Ext,
[_] = elvis_core_apply_rule(Config, elvis_style, state_record_and_type, #{}, PathPassGenStateMState).

-spec verify_behaviour_spelling(config()) -> any().
verify_behaviour_spelling(Config) ->
Ext = proplists:get_value(test_file_ext, Config, "erl"),

PathFail = "british_behaviour_spelling." ++ Ext,
[_] = elvis_core_apply_rule(Config, elvis_style, behaviour_spelling, #{spelling => behavior}, PathFail),
PathFail1 = "american_behavior_spelling." ++ Ext,
[_] = elvis_core_apply_rule(Config, elvis_style, behaviour_spelling, #{spelling => behaviour}, PathFail1),

PathPass = "british_behaviour_spelling." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, behaviour_spelling, #{spelling => behaviour}, PathPass),
PathPass1 = "american_behavior_spelling." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, behaviour_spelling, #{spelling => behavior}, PathPass1).

-spec verify_no_spec_with_records(config()) -> any().
verify_no_spec_with_records(Config) ->
Ext = proplists:get_value(test_file_ext, Config, "erl"),
Expand Down