Skip to content

Commit 7d8459b

Browse files
committed
🎉 Added L2 checker and update README
1 parent c509393 commit 7d8459b

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ $ crnormz
9797
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> F6 | Comments within a function | <font style="color: green; font-size: 16px;">✓</font> |
9898
| <img src="assets/readme/major.png" width="16" vertical-align="middle"/> F7 | Nested functions | <font style="font-size: 16px;">🔨</font> |
9999
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L1 | Multiple assignments on the same line | <font style="color: green; font-size: 16px;">✓</font> |
100-
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L2 | Bad indentation | <font style="font-size: 16px;">🔨</font> |
100+
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L2 | Bad indentation | <font style="color: green; font-size: 16px;"></font> |
101101
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L3 | Missing space after a keyword | <font style="font-size: 16px;">🔨</font> |
102102
| <img src="assets/readme/major.png" width="16" vertical-align="middle"/> L4 | Misplaced bracket | <font style="color: green; font-size: 16px;">✓</font> |
103103
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L6 | Line jumps | <font style="font-size: 16px;">🔨</font> |

src/coding_style/all/l2_indent.cr

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2022 CustomEntity
4+
#
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:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
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.
22+
require "../coding_style"
23+
require "../../file/file_manager"
24+
25+
# TO IMPROVE (More precision on functions with level)
26+
27+
class Indent < CodingStyle
28+
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
29+
super(@type, @file_target, @level, @name, @desc)
30+
end
31+
32+
def handle(file_path : String, content : String, options : Hash(String, String)) : Set(CodingStyleErrorInfo)
33+
errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new
34+
curr_line = 1
35+
bad_indent_name : String
36+
good_indent_char : Char
37+
bad_indent_regex : Regex
38+
indent_incr : Int32
39+
40+
if get_file_type(file_path) == FileType::Makefile
41+
bad_indent_name = "Spaces"
42+
good_indent_char = '\t'
43+
indent_incr = 4
44+
bad_indent_regex = /^ +.*$/
45+
else
46+
bad_indent_name = "Tabulations"
47+
good_indent_char = ' '
48+
bad_indent_regex = /^\t+.*$/
49+
indent_incr = 1
50+
end
51+
content.each_line { |line|
52+
curr_indent = 0
53+
while curr_indent < line.size && line[curr_indent] == good_indent_char
54+
curr_indent += indent_incr
55+
end
56+
if line =~ bad_indent_regex
57+
errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1, " (#{bad_indent_name} are not allowed)".magenta))
58+
elsif curr_indent % 4 != 0
59+
errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1, " (Bad indentation)".magenta))
60+
end
61+
curr_line += 1
62+
}
63+
errors
64+
end
65+
end

src/coding_style/coding_style_manager.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require "./all/f4_lines_number"
3636
require "./all/f5_arguments"
3737
require "./all/f6_comment_in_func"
3838
require "./all/l1_code_line_content"
39+
require "./all/l2_indent"
3940
require "./all/l4_curly_brackets"
4041
require "./all/v1_naming_identifiers"
4142
require "./all/v3_pointers"
@@ -80,6 +81,8 @@ COMMENT_IN_FUNC =
8081
CommentInFunc.new(CodingStyleType::F6, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Minor, "Comments inside a function", "There should be no comment within a function.")
8182
CODE_LINE_CONTENT =
8283
CodeLineContent.new(CodingStyleType::L1, FileType::Source.value, CodingStyleLevel::Major, "Code line content", "A line should correspond to only one statement.")
84+
INDENT =
85+
Indent.new(CodingStyleType::L2, FileType::Source.value | FileType::Header.value | FileType::Makefile.value, CodingStyleLevel::Minor, "Indentation", "Each indentation level must be done by using 4 spaces. When entering a new scope the indentation level must be incremented.")
8386
CURLY_BRACKETS =
8487
CurlyBrackets.new(CodingStyleType::L4, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Minor, "Curly brackets", "Opening curly brackets should be at the end of their line, except for functions where they must be placed alone on their line.")
8588
NAMING_IDENTIFIERS =
@@ -123,6 +126,7 @@ class CodingStyleManager
123126
@codingstyles[ARGUMENTS.@type] = ARGUMENTS
124127
@codingstyles[COMMENT_IN_FUNC.@type] = COMMENT_IN_FUNC
125128
@codingstyles[CODE_LINE_CONTENT.@type] = CODE_LINE_CONTENT
129+
@codingstyles[INDENT.@type] = INDENT
126130
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
127131
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
128132
@codingstyles[POINTERS.@type] = POINTERS

0 commit comments

Comments
 (0)