|
1 | | -#MIT License |
| 1 | +# MIT License |
2 | 2 | # |
3 | | -#Copyright (c) 2022 CustomEntity |
| 3 | +# Copyright (c) 2022 CustomEntity |
4 | 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: |
| 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 | 11 | # |
12 | | -#The above copyright notice and this permission notice shall be included in all |
13 | | -#copies or substantial portions of the Software. |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
14 | 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. |
| 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 | 22 | require "../coding_style" |
23 | 23 | require "../../file/file_manager" |
24 | 24 |
|
| 25 | +CONDITION_REGEX = /^[^\w]*(\s*if *\()|(\s*while *\()|(\s*for *\()|(\s*switch *\()|(\s*else *)|(\s*do *\()/ |
| 26 | +ELSE_IF_COND_REGEX = /^[^\w]*\s*else if *\(/ |
| 27 | + |
25 | 28 | class ConditionalBranching < CodingStyle |
26 | 29 | def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String) |
27 | 30 | super(@type, @file_target, @level, @name, @desc) |
28 | 31 | end |
29 | 32 |
|
30 | 33 | def handle(file_path : String, content : String, options : Hash(String, String)) : Set(CodingStyleErrorInfo) |
31 | 34 | errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new |
| 35 | + splitted_content = content.split("\n") |
| 36 | + curr_line : Int32 = 1 |
| 37 | + depth : Int32 = 0 |
| 38 | + is_no_bracket_cond : Bool = false |
| 39 | + is_in_elsif : Bool = false |
| 40 | + depth_when_first_elsif : Int32 = -1 |
| 41 | + depth_prev_elsif : Int32 = -1 |
| 42 | + is_no_bracket_cond_elsif : Bool = false |
| 43 | + is_prev_line_no_bracket_elsif : Bool = false |
| 44 | + just_now : Bool = false |
32 | 45 |
|
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)) |
| 46 | + splitted_content.each { |line| |
| 47 | + if is_no_bracket_cond |
| 48 | + is_no_bracket_cond = false |
| 49 | + if is_no_bracket_cond_elsif |
| 50 | + is_prev_line_no_bracket_elsif = true |
| 51 | + just_now = true |
| 52 | + is_no_bracket_cond_elsif = false |
| 53 | + else |
| 54 | + if splitted_content[curr_line] !~ ELSE_IF_COND_REGEX |
| 55 | + depth -= 1 |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + if line !~ ELSE_IF_COND_REGEX |
| 60 | + depth -= line.count("}") |
| 61 | + if line.count("}") != 0 |
| 62 | + end |
| 63 | + end |
| 64 | + if is_prev_line_no_bracket_elsif && line !~ ELSE_IF_COND_REGEX |
| 65 | + if just_now == false |
| 66 | + depth = depth_when_first_elsif - 1 |
| 67 | + is_prev_line_no_bracket_elsif = false |
| 68 | + else |
| 69 | + just_now = false |
| 70 | + end |
| 71 | + end |
| 72 | + if is_in_elsif && depth == depth_prev_elsif |
| 73 | + is_in_elsif = false |
| 74 | + depth = depth_when_first_elsif - 1 |
| 75 | + depth_when_first_elsif = -1 |
| 76 | + depth_prev_elsif = -1 |
| 77 | + end |
| 78 | + if line =~ CONDITION_REGEX |
| 79 | + if line =~ ELSE_IF_COND_REGEX |
| 80 | + if depth_when_first_elsif == -1 |
| 81 | + depth_when_first_elsif = depth |
| 82 | + end |
| 83 | + depth_prev_elsif = depth |
| 84 | + is_in_elsif = true |
| 85 | + end |
| 86 | + if line.count("{") == 0 |
| 87 | + if line =~ ELSE_IF_COND_REGEX |
| 88 | + is_no_bracket_cond_elsif = true |
| 89 | + end |
| 90 | + is_no_bracket_cond = true |
| 91 | + end |
| 92 | + depth += 1 |
| 93 | + if depth >= 4 |
| 94 | + errors.add(CodingStyleErrorInfo.new(self, file_path, curr_line, -1)) |
| 95 | + end |
| 96 | + else |
| 97 | + if line.count("{") != 0 |
| 98 | + depth += 1 |
| 99 | + end |
| 100 | + end |
| 101 | + curr_line += 1 |
37 | 102 | } |
38 | 103 | errors |
39 | 104 | end |
|
0 commit comments