-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #284: New Rule: Param Pattern Matching
- Loading branch information
1 parent
b1cf9b8
commit 2763872
Showing
9 changed files
with
238 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Param Pattern-Matching | ||
|
||
(since [3.0.0](https://github.com/inaka/elvis_core/releases/tag/3.0.0)) | ||
|
||
When capturing a parameter using pattern matching you can either put the parameter | ||
name on the left (`Param = #{pattern := ToMatch}`) or right (`#{pattern := ToMatch} = Param`) side | ||
of the pattern that you use in the function clause. | ||
This rule will make sure you are consistent through your code and use always the same style. | ||
|
||
> Works on `.beam` file? Yes! | ||
## Options | ||
|
||
- `side :: left | right`. | ||
- default: `left`. | ||
|
||
## Example | ||
|
||
```erlang | ||
{elvis_style, param_pattern_matching, #{side => right}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-module(left_param_pattern_matching). | ||
|
||
-export([single/1, multiple/1, different_param/3, different_clause/1]). | ||
|
||
single(Simple = {left, side, assignment}) -> | ||
fun(SimpleToo = {left, side, assignment}) -> Simple == SimpleToo end. | ||
|
||
multiple(Multiple = Assignments = {on, the, left, side}) -> | ||
fun(MultipleToo = AssignmentsToo = {on, the, left, side}) -> | ||
{Multiple, Assignments} == {MultipleToo, AssignmentsToo} | ||
end. | ||
|
||
different_param(happens_on, TheSecond = #{param := of_the}, function) -> | ||
fun(happens_on, TheSecondToo = #{param := of_the}, function) -> TheSecond == TheSecondToo | ||
end. | ||
|
||
different_clause("it doesn't happen on the first clause") -> | ||
not_here; | ||
different_clause(But = "it does in the second one") -> | ||
fun ("it doesn't happen on the first clause") -> | ||
not_here; | ||
(ButToo = "it does in the second one") -> | ||
But == ButToo | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-module(pass_param_pattern_matching_elvis_attr). | ||
|
||
-elvis([{elvis_style, param_pattern_matching, #{side => right}}]). | ||
|
||
-export([my_fun/1, my_fun/3]). | ||
|
||
my_fun(#{[variable, goes] := [on, the]} = Right) -> | ||
fun (#{[variable, goes] := [on, the]} = RightToo) -> | ||
Right == RightToo; | ||
({can, be, Left} = {_If, _It, "is not a single variable"}) -> | ||
Left | ||
end; | ||
my_fun({can, be, Left} = {_If, _It, "is not a single variable"}) -> | ||
Left. | ||
|
||
my_fun(works, {as, well, on} = TheOther, parameters) -> | ||
fun(works, {as, well, on} = TheOtherToo, parameters) -> | ||
TheOther == TheOtherToo | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-module(right_param_pattern_matching). | ||
|
||
-export([single/1, multiple/1, different_param/3, different_clause/1]). | ||
-export([regular_matching_works_fine/0]). | ||
|
||
single({right, side, assignment} = Simple) -> | ||
fun({right, side, assignment} = SimpleToo) -> Simple == SimpleToo end. | ||
|
||
%% This is not a match. According to Erlang precedence rules, the following code is actually | ||
%% equivalent to: {on, the, right, side} = (Multiple = Assignments). | ||
%% It's a tuple against a match, not a tuple against a variable against another variable | ||
%% That's why no warnings are emitted for this line even if side is 'left'. | ||
multiple({on, the, right, side} = Multiple = Assignments) -> | ||
fun({on, the, right, side} = MultipleToo = AssignmentsToo) -> | ||
{Multiple, Assignments} == {MultipleToo, AssignmentsToo} | ||
end. | ||
|
||
different_param(happens_on, #{the_second := param_of} = The, function) -> | ||
fun(happens_on, #{the_second := param_of} = TheToo, function) -> The == TheToo | ||
end. | ||
|
||
different_clause("it doesn't happen on the first clause") -> | ||
not_here; | ||
different_clause("it does in the second one" = AsYoda) -> | ||
fun ("it doesn't happen on the first clause") -> | ||
not_here; | ||
("it does in the second one" = AsYodaToo) -> | ||
AsYoda == AsYodaToo | ||
end. | ||
|
||
regular_matching_works_fine() -> | ||
This = works:fine(), | ||
case This of | ||
This = #{is := also} -> | ||
valid; | ||
_ -> | ||
fun() -> | ||
Like = This, | ||
valid:as_well(Like) | ||
end | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters