|
| 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 | +FUNCTION_DECL_REGEX = /^(?:\w*[ ]*(?:unsigned|signed)?[ \t]*\w\s+\**)+(\w+)\s*\([^;]*?{/m |
| 26 | +VARIABLE_DECL_REGEX = /^[ \t]*(?!return|typedef|goto)((\w+[ \t,]*\*?[ \t,]+)+)+(\**\w+)(\[\w*\])?([ \t,]*=|;)/ |
| 27 | + |
| 28 | +class LineJumps < CodingStyle |
| 29 | + def initialize(@type : CodingStyleType, @file_target : Int32, @level : CodingStyleLevel, @name : String, @desc : String) |
| 30 | + super(@type, @file_target, @level, @name, @desc) |
| 31 | + end |
| 32 | + |
| 33 | + def handle(file_path : String, comments : Set(Comment), content : String, lines : Array(String), options : Hash(String, String)) : Set(CodingStyleErrorInfo) |
| 34 | + errors : Set(CodingStyleErrorInfo) = Set(CodingStyleErrorInfo).new |
| 35 | + |
| 36 | + content.scan(FUNCTION_DECL_REGEX).each { |match| |
| 37 | + row, _ = self.get_row_column(lines, match.end) |
| 38 | + indent_level = 1 |
| 39 | + is_in_decl_zone : Bool = true |
| 40 | + |
| 41 | + (row...lines.size).each { |i| |
| 42 | + indent_level += lines[i].count("{") |
| 43 | + if lines[i] =~ /}/ |
| 44 | + indent_level -= lines[i].count("}") |
| 45 | + if indent_level <= 0 |
| 46 | + break |
| 47 | + end |
| 48 | + end |
| 49 | + if lines[i] =~ VARIABLE_DECL_REGEX && is_in_decl_zone == false |
| 50 | + errors.add(CodingStyleErrorInfo.new(self, file_path, i + 1, -1)) |
| 51 | + end |
| 52 | + if is_in_decl_zone && lines[i].strip.empty? |
| 53 | + is_in_decl_zone = false |
| 54 | + end |
| 55 | + } |
| 56 | + } |
| 57 | + errors |
| 58 | + end |
| 59 | +end |
0 commit comments