Skip to content

Commit

Permalink
🎉 Added L2 checker and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
CustomEntity committed Jun 1, 2022
1 parent c509393 commit 7d8459b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ $ crnormz
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> F6 | Comments within a function | <font style="color: green; font-size: 16px;">✓</font> |
| <img src="assets/readme/major.png" width="16" vertical-align="middle"/> F7 | Nested functions | <font style="font-size: 16px;">🔨</font> |
| <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> |
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L2 | Bad indentation | <font style="font-size: 16px;">🔨</font> |
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L2 | Bad indentation | <font style="color: green; font-size: 16px;"></font> |
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L3 | Missing space after a keyword | <font style="font-size: 16px;">🔨</font> |
| <img src="assets/readme/major.png" width="16" vertical-align="middle"/> L4 | Misplaced bracket | <font style="color: green; font-size: 16px;">✓</font> |
| <img src="assets/readme/minor.png" width="16" vertical-align="middle"/> L6 | Line jumps | <font style="font-size: 16px;">🔨</font> |
Expand Down
65 changes: 65 additions & 0 deletions src/coding_style/all/l2_indent.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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"

# TO IMPROVE (More precision on functions with level)

class Indent < 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, content : String, options : Hash(String, String)) : Set(CodingStyleErrorInfo)
errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new
curr_line = 1
bad_indent_name : String
good_indent_char : Char
bad_indent_regex : Regex
indent_incr : Int32

if get_file_type(file_path) == FileType::Makefile
bad_indent_name = "Spaces"
good_indent_char = '\t'
indent_incr = 4
bad_indent_regex = /^ +.*$/
else
bad_indent_name = "Tabulations"
good_indent_char = ' '
bad_indent_regex = /^\t+.*$/
indent_incr = 1
end
content.each_line { |line|
curr_indent = 0
while curr_indent < line.size && line[curr_indent] == good_indent_char
curr_indent += indent_incr
end
if line =~ bad_indent_regex
errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1, " (#{bad_indent_name} are not allowed)".magenta))
elsif curr_indent % 4 != 0
errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1, " (Bad indentation)".magenta))
end
curr_line += 1
}
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 @@ -36,6 +36,7 @@ require "./all/f4_lines_number"
require "./all/f5_arguments"
require "./all/f6_comment_in_func"
require "./all/l1_code_line_content"
require "./all/l2_indent"
require "./all/l4_curly_brackets"
require "./all/v1_naming_identifiers"
require "./all/v3_pointers"
Expand Down Expand Up @@ -80,6 +81,8 @@ COMMENT_IN_FUNC =
CommentInFunc.new(CodingStyleType::F6, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Minor, "Comments inside a function", "There should be no comment within a function.")
CODE_LINE_CONTENT =
CodeLineContent.new(CodingStyleType::L1, FileType::Source.value, CodingStyleLevel::Major, "Code line content", "A line should correspond to only one statement.")
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.")
NAMING_IDENTIFIERS =
Expand Down Expand Up @@ -123,6 +126,7 @@ class CodingStyleManager
@codingstyles[ARGUMENTS.@type] = ARGUMENTS
@codingstyles[COMMENT_IN_FUNC.@type] = COMMENT_IN_FUNC
@codingstyles[CODE_LINE_CONTENT.@type] = CODE_LINE_CONTENT
@codingstyles[INDENT.@type] = INDENT
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
@codingstyles[POINTERS.@type] = POINTERS
Expand Down

0 comments on commit 7d8459b

Please sign in to comment.