Skip to content

Commit 5e8599a

Browse files
committed
🎉 Added L6 checker.
1 parent 8c03c49 commit 5e8599a

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/coding_style/all/f4_lines_number.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require "../../file/file_manager"
2424

2525
FUNCTION_DECLARATION_REGEX = /^(?:\w*[ ]*(?:unsigned|signed)?[ \t]*\w\s+\**)+\w+\s*\([^;]*?{/
2626

27-
class LinesNumber < CodingStyle
27+
class LinesNumber < CodingStyle
2828
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
2929
super(@type, @file_target, @level, @name, @desc)
3030
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
FUNCTION_DECL_REGEX = /^(?:\w*[ ]*(?:unsigned|signed)?[ \t]*\w\s+\**)+(\w+)\s*\([^;]*?{/m
26+
VARIABLE_DECL_REGEX = /^[ \t]*(?!return|typedef|goto)((\w+[ \t,]*\*?[ \t,]+)+)+(\**\w+)(\[\w*\])?([ \t,]*=|;)/
27+
28+
class LineJumps < CodingStyle
29+
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
30+
super(@type, @file_target, @level, @name, @desc)
31+
end
32+
33+
def handle(file_path : String, comments : Set(Comment), content : String, lines : Array(String), options : Hash(String, String)) : Set(CodingStyleErrorInfo)
34+
errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new
35+
36+
content.scan(FUNCTION_DECL_REGEX).each { |match|
37+
row, _ = self.get_row_column(lines, match.end)
38+
indent_level = 1
39+
is_in_decl_zone : Bool = true
40+
41+
(row...lines.size).each { |i|
42+
indent_level += lines[i].count("{")
43+
if lines[i] =~ /}/
44+
indent_level -= lines[i].count("}")
45+
if indent_level <= 0
46+
break
47+
end
48+
end
49+
if lines[i] =~ VARIABLE_DECL_REGEX && is_in_decl_zone == false
50+
errors.add(CodingStyleErrorInfo.new(self, file_path, i + 1, -1))
51+
end
52+
if is_in_decl_zone && lines[i].strip.empty?
53+
is_in_decl_zone = false
54+
end
55+
}
56+
}
57+
errors
58+
end
59+
end

src/coding_style/coding_style_manager.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require "./all/f5_arguments"
3737
require "./all/f6_comment_in_func"
3838
require "./all/l1_code_line_content"
3939
require "./all/l2_indent"
40+
require "./all/l6_line_jumps"
4041
require "./all/l4_curly_brackets"
4142
require "./all/v1_naming_identifiers"
4243
require "./all/v3_pointers"
@@ -84,6 +85,8 @@ INDENT =
8485
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.")
8586
CURLY_BRACKETS =
8687
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.")
88+
LINE_JUMPS =
89+
LineJumps.new(CodingStyleType::L6, FileType::Source.value, CodingStyleLevel::Minor, "Line Jumps", "A line break should separate the variable declarations from the remainder of the function. No other line breaks should be present in the scope of a function.")
8790
NAMING_IDENTIFIERS =
8891
NamingIdentifiers.new(CodingStyleType::V1, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Major, "Naming Identifiers", "All identifier names should be in English, according to the snake_case convention.")
8992
POINTERS =
@@ -126,6 +129,7 @@ class CodingStyleManager
126129
@codingstyles[CODE_LINE_CONTENT.@type] = CODE_LINE_CONTENT
127130
@codingstyles[INDENT.@type] = INDENT
128131
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
132+
@codingstyles[LINE_JUMPS.@type] = LINE_JUMPS
129133
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
130134
@codingstyles[POINTERS.@type] = POINTERS
131135
@codingstyles[CONDITIONAL_BRANCHING.@type] = CONDITIONAL_BRANCHING

0 commit comments

Comments
 (0)