Skip to content

Commit

Permalink
🎉 Added L6 checker.
Browse files Browse the repository at this point in the history
  • Loading branch information
CustomEntity committed Jun 29, 2022
1 parent 8c03c49 commit 5e8599a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/coding_style/all/f4_lines_number.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require "../../file/file_manager"

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

class LinesNumber < CodingStyle
class LinesNumber < CodingStyle
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
super(@type, @file_target, @level, @name, @desc)
end
Expand Down
59 changes: 59 additions & 0 deletions src/coding_style/all/l6_line_jumps.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# MIT License
#
# Copyright (c) 2022 CustomEntity
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require "../coding_style"
require "../../file/file_manager"

FUNCTION_DECL_REGEX = /^(?:\w*[ ]*(?:unsigned|signed)?[ \t]*\w\s+\**)+(\w+)\s*\([^;]*?{/m
VARIABLE_DECL_REGEX = /^[ \t]*(?!return|typedef|goto)((\w+[ \t,]*\*?[ \t,]+)+)+(\**\w+)(\[\w*\])?([ \t,]*=|;)/

class LineJumps < CodingStyle
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
super(@type, @file_target, @level, @name, @desc)
end

def handle(file_path : String, comments : Set(Comment), content : String, lines : Array(String), options : Hash(String, String)) : Set(CodingStyleErrorInfo)
errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new

content.scan(FUNCTION_DECL_REGEX).each { |match|
row, _ = self.get_row_column(lines, match.end)
indent_level = 1
is_in_decl_zone : Bool = true

(row...lines.size).each { |i|
indent_level += lines[i].count("{")
if lines[i] =~ /}/
indent_level -= lines[i].count("}")
if indent_level <= 0
break
end
end
if lines[i] =~ VARIABLE_DECL_REGEX && is_in_decl_zone == false
errors.add(CodingStyleErrorInfo.new(self, file_path, i + 1, -1))
end
if is_in_decl_zone && lines[i].strip.empty?
is_in_decl_zone = false
end
}
}
errors
end
end
4 changes: 4 additions & 0 deletions src/coding_style/coding_style_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require "./all/f5_arguments"
require "./all/f6_comment_in_func"
require "./all/l1_code_line_content"
require "./all/l2_indent"
require "./all/l6_line_jumps"
require "./all/l4_curly_brackets"
require "./all/v1_naming_identifiers"
require "./all/v3_pointers"
Expand Down Expand Up @@ -84,6 +85,8 @@ INDENT =
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.")
CURLY_BRACKETS =
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.")
LINE_JUMPS =
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.")
NAMING_IDENTIFIERS =
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.")
POINTERS =
Expand Down Expand Up @@ -126,6 +129,7 @@ class CodingStyleManager
@codingstyles[CODE_LINE_CONTENT.@type] = CODE_LINE_CONTENT
@codingstyles[INDENT.@type] = INDENT
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
@codingstyles[LINE_JUMPS.@type] = LINE_JUMPS
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
@codingstyles[POINTERS.@type] = POINTERS
@codingstyles[CONDITIONAL_BRANCHING.@type] = CONDITIONAL_BRANCHING
Expand Down

0 comments on commit 5e8599a

Please sign in to comment.