Skip to content

Commit

Permalink
🎉 Options are now compatible with regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
CustomEntity committed Jun 1, 2022
1 parent 3f564bf commit a5131ef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
40 changes: 40 additions & 0 deletions src/coding_style/all/c1_conditional_branching.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#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"

class ConditionalBranching < 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


content.scan(GOTO_REGEX).each { |match|
row, column = get_row_column(content.split("\n"), match.begin)
errors.add(CodingStyleErrorInfo.new(self, file_path, row, column))
}
errors
end
end
9 changes: 4 additions & 5 deletions src/coding_style/coding_style_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require "./all/l2_indent"
require "./all/l4_curly_brackets"
require "./all/v1_naming_identifiers"
require "./all/v3_pointers"
require "./all/c1_conditional_branching"
require "./all/c3_goto"
require "./all/a3_line_break"
require "./all/h2_include_guard"
Expand Down Expand Up @@ -67,10 +68,8 @@ TRAILING_SPACES =
TrailingSpaces.new(CodingStyleType::G8, FileType::Source.value | FileType::Header.value | FileType::Makefile.value, CodingStyleLevel::Minor, "Trailing Spaces", "No trailing spaces must be present at the end of a line.")
TRAILING_LINES =
TrailingLines.new(CodingStyleType::G9, FileType::Source.value | FileType::Header.value | FileType::Makefile.value, CodingStyleLevel::Minor, "Trailing Lines", "No more than 1 trailing empty line must be present.")

NAMING_FUNCTIONS =
NamingFunctions.new(CodingStyleType::F2, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Major, "Naming functions", "All function names should be in English, according to the snake_case convention (meaning that it is composed only of lowercase, numbers, and underscores)..")

COLUMNS_NUMBER =
ColumnsNumber.new(CodingStyleType::F3, FileType::Source.value | FileType::Header.value | FileType::Makefile.value, CodingStyleLevel::Major, "Number of columns", "The length of a line should not exceed 80 columns (not to be confused with 80 characters!).")
LINES_NUMBER =
Expand All @@ -89,12 +88,12 @@ 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 =
Pointers.new(CodingStyleType::V3, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Minor, "Pointers", "The pointer symbol (*) should be attached to the associated variable, with no spaces.")
CONDITIONAL_BRANCHING =
ConditionalBranching.new(CodingStyleType::C1, FileType::Source.value, CodingStyleLevel::Minor, "Conditional Branching", "A conditionnal block (while, for, if, else, ...) should not contain more than 3 branchings.")
GOTO =
Goto.new(CodingStyleType::C3, FileType::Source.value | FileType::Header.value, CodingStyleLevel::Minor, "Goto", "Est-ce que ta déjà léché les deux boules d'un goto ?")

LINE_BREAK =
LineBreak.new(CodingStyleType::A3, FileType::Source.value | FileType::Header.value | FileType::Makefile.value, CodingStyleLevel::Info, "Line break at the end of file", "Files should end with a line break.")

INCLUDE_GUARD =
IncludeGuard.new(CodingStyleType::H2, FileType::Header.value, CodingStyleLevel::Minor, "Include Guard", "Headers should be protected from double inclusion.")
MACROS =
Expand All @@ -119,7 +118,6 @@ class CodingStyleManager
@codingstyles[LINE_ENDINGS.@type] = LINE_ENDINGS
@codingstyles[TRAILING_SPACES.@type] = TRAILING_SPACES
@codingstyles[TRAILING_LINES.@type] = TRAILING_LINES

@codingstyles[NAMING_FUNCTIONS.@type] = NAMING_FUNCTIONS
@codingstyles[COLUMNS_NUMBER.@type] = COLUMNS_NUMBER
@codingstyles[LINES_NUMBER.@type] = LINES_NUMBER
Expand All @@ -130,6 +128,7 @@ class CodingStyleManager
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
@codingstyles[POINTERS.@type] = POINTERS
#@codingstyles[CONDITIONAL_BRANCHING.@type] = CONDITIONAL_BRANCHING
@codingstyles[GOTO.@type] = GOTO
@codingstyles[LINE_BREAK.@type] = LINE_BREAK
@codingstyles[INCLUDE_GUARD.@type] = INCLUDE_GUARD
Expand Down
2 changes: 1 addition & 1 deletion src/crnormz.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ file_manager.@files.each { |file_path|
if get_file_type(file_path) != FileType::Directory
content = File.read(file_path)
end
if options.has_key?("ignoring-files") && options["ignoring-files"].split(",").count { |s| s == file_path } != 0
if options.has_key?("ignoring-files") && Regex.new(options["ignoring-files"]).match(file_path)
next
end
codingstyle_manager.@codingstyles.each_value { |codingstyle|
Expand Down

0 comments on commit a5131ef

Please sign in to comment.