Skip to content

Commit a5131ef

Browse files
committed
🎉 Options are now compatible with regular expressions
1 parent 3f564bf commit a5131ef

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
class ConditionalBranching < CodingStyle
26+
def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String)
27+
super(@type, @file_target, @level, @name, @desc)
28+
end
29+
30+
def handle(file_path : String, content : String, options : Hash(String, String)) : Set(CodingStyleErrorInfo)
31+
errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new
32+
33+
34+
content.scan(GOTO_REGEX).each { |match|
35+
row, column = get_row_column(content.split("\n"), match.begin)
36+
errors.add(CodingStyleErrorInfo.new(self, file_path, row, column))
37+
}
38+
errors
39+
end
40+
end

src/coding_style/coding_style_manager.cr

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require "./all/l2_indent"
4040
require "./all/l4_curly_brackets"
4141
require "./all/v1_naming_identifiers"
4242
require "./all/v3_pointers"
43+
require "./all/c1_conditional_branching"
4344
require "./all/c3_goto"
4445
require "./all/a3_line_break"
4546
require "./all/h2_include_guard"
@@ -67,10 +68,8 @@ TRAILING_SPACES =
6768
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.")
6869
TRAILING_LINES =
6970
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.")
70-
7171
NAMING_FUNCTIONS =
7272
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)..")
73-
7473
COLUMNS_NUMBER =
7574
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!).")
7675
LINES_NUMBER =
@@ -89,12 +88,12 @@ NAMING_IDENTIFIERS =
8988
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.")
9089
POINTERS =
9190
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.")
91+
CONDITIONAL_BRANCHING =
92+
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.")
9293
GOTO =
9394
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 ?")
94-
9595
LINE_BREAK =
9696
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.")
97-
9897
INCLUDE_GUARD =
9998
IncludeGuard.new(CodingStyleType::H2, FileType::Header.value, CodingStyleLevel::Minor, "Include Guard", "Headers should be protected from double inclusion.")
10099
MACROS =
@@ -119,7 +118,6 @@ class CodingStyleManager
119118
@codingstyles[LINE_ENDINGS.@type] = LINE_ENDINGS
120119
@codingstyles[TRAILING_SPACES.@type] = TRAILING_SPACES
121120
@codingstyles[TRAILING_LINES.@type] = TRAILING_LINES
122-
123121
@codingstyles[NAMING_FUNCTIONS.@type] = NAMING_FUNCTIONS
124122
@codingstyles[COLUMNS_NUMBER.@type] = COLUMNS_NUMBER
125123
@codingstyles[LINES_NUMBER.@type] = LINES_NUMBER
@@ -130,6 +128,7 @@ class CodingStyleManager
130128
@codingstyles[CURLY_BRACKETS.@type] = CURLY_BRACKETS
131129
@codingstyles[NAMING_IDENTIFIERS.@type] = NAMING_IDENTIFIERS
132130
@codingstyles[POINTERS.@type] = POINTERS
131+
#@codingstyles[CONDITIONAL_BRANCHING.@type] = CONDITIONAL_BRANCHING
133132
@codingstyles[GOTO.@type] = GOTO
134133
@codingstyles[LINE_BREAK.@type] = LINE_BREAK
135134
@codingstyles[INCLUDE_GUARD.@type] = INCLUDE_GUARD

src/crnormz.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ file_manager.@files.each { |file_path|
5959
if get_file_type(file_path) != FileType::Directory
6060
content = File.read(file_path)
6161
end
62-
if options.has_key?("ignoring-files") && options["ignoring-files"].split(",").count { |s| s == file_path } != 0
62+
if options.has_key?("ignoring-files") && Regex.new(options["ignoring-files"]).match(file_path)
6363
next
6464
end
6565
codingstyle_manager.@codingstyles.each_value { |codingstyle|

0 commit comments

Comments
 (0)