Skip to content

Commit

Permalink
Merge pull request #212 from jackyhui96/master
Browse files Browse the repository at this point in the history
Fix 210: New rule for behavior vs behaviour.
  • Loading branch information
elbrujohalcon authored Oct 6, 2021
2 parents 3d1c65a + 0ac1fd5 commit 8388fca
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 7 deletions.
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

0 comments on commit 8388fca

Please sign in to comment.