Skip to content

Commit a36fd7c

Browse files
committed
🎉 Added C1 checker
1 parent a5131ef commit a36fd7c

File tree

2 files changed

+87
-22
lines changed

2 files changed

+87
-22
lines changed

src/coding_style/all/c1_conditional_branching.cr

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,104 @@
1-
#MIT License
1+
# MIT License
22
#
3-
#Copyright (c) 2022 CustomEntity
3+
# Copyright (c) 2022 CustomEntity
44
#
5-
#Permission is hereby granted, free of charge, to any person obtaining a copy
6-
#of this software and associated documentation files (the "Software"), to deal
7-
#in the Software without restriction, including without limitation the rights
8-
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
#copies of the Software, and to permit persons to whom the Software is
10-
#furnished to do so, subject to the following conditions:
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
1111
#
12-
#The above copyright notice and this permission notice shall be included in all
13-
#copies or substantial portions of the Software.
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
1414
#
15-
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
#SOFTWARE.
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
2222
require "../coding_style"
2323
require "../../file/file_manager"
2424

25+
CONDITION_REGEX = /^[^\w]*(\s*if *\()|(\s*while *\()|(\s*for *\()|(\s*switch *\()|(\s*else *)|(\s*do *\()/
26+
ELSE_IF_COND_REGEX = /^[^\w]*\s*else if *\(/
27+
2528
class ConditionalBranching < CodingStyle
2629
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
2730
super(@type, @file_target, @level, @name, @desc)
2831
end
2932

3033
def handle(file_path : String, content : String, options : Hash(String, String)) : Set(CodingStyleErrorInfo)
3134
errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new
35+
splitted_content = content.split("\n")
36+
curr_line : Int32 = 1
37+
depth : Int32 = 0
38+
is_no_bracket_cond : Bool = false
39+
is_in_elsif : Bool = false
40+
depth_when_first_elsif : Int32 = -1
41+
depth_prev_elsif : Int32 = -1
42+
is_no_bracket_cond_elsif : Bool = false
43+
is_prev_line_no_bracket_elsif : Bool = false
44+
just_now : Bool = false
3245

33-
34-
content.scan(GOTO_REGEX).each { |match|
35-
row, column = get_row_column(content.split("\n"), match.begin)
36-
errors.add(CodingStyleErrorInfo.new(self, file_path, row, column))
46+
splitted_content.each { |line|
47+
if is_no_bracket_cond
48+
is_no_bracket_cond = false
49+
if is_no_bracket_cond_elsif
50+
is_prev_line_no_bracket_elsif = true
51+
just_now = true
52+
is_no_bracket_cond_elsif = false
53+
else
54+
if splitted_content[curr_line] !~ ELSE_IF_COND_REGEX
55+
depth -= 1
56+
end
57+
end
58+
end
59+
if line !~ ELSE_IF_COND_REGEX
60+
depth -= line.count("}")
61+
if line.count("}") != 0
62+
end
63+
end
64+
if is_prev_line_no_bracket_elsif && line !~ ELSE_IF_COND_REGEX
65+
if just_now == false
66+
depth = depth_when_first_elsif - 1
67+
is_prev_line_no_bracket_elsif = false
68+
else
69+
just_now = false
70+
end
71+
end
72+
if is_in_elsif && depth == depth_prev_elsif
73+
is_in_elsif = false
74+
depth = depth_when_first_elsif - 1
75+
depth_when_first_elsif = -1
76+
depth_prev_elsif = -1
77+
end
78+
if line =~ CONDITION_REGEX
79+
if line =~ ELSE_IF_COND_REGEX
80+
if depth_when_first_elsif == -1
81+
depth_when_first_elsif = depth
82+
end
83+
depth_prev_elsif = depth
84+
is_in_elsif = true
85+
end
86+
if line.count("{") == 0
87+
if line =~ ELSE_IF_COND_REGEX
88+
is_no_bracket_cond_elsif = true
89+
end
90+
is_no_bracket_cond = true
91+
end
92+
depth += 1
93+
if depth >= 4
94+
errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1))
95+
end
96+
else
97+
if line.count("{") != 0
98+
depth += 1
99+
end
100+
end
101+
curr_line += 1
37102
}
38103
errors
39104
end

src/coding_style/coding_style_manager.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class CodingStyleManager
128128
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
129129
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
130130
@codingstyles[POINTERS.@type] = POINTERS
131-
#@codingstyles[CONDITIONAL_BRANCHING.@type] = CONDITIONAL_BRANCHING
131+
@codingstyles[CONDITIONAL_BRANCHING.@type] = CONDITIONAL_BRANCHING
132132
@codingstyles[GOTO.@type] = GOTO
133133
@codingstyles[LINE_BREAK.@type] = LINE_BREAK
134134
@codingstyles[INCLUDE_GUARD.@type] = INCLUDE_GUARD

0 commit comments

Comments
 (0)