Skip to content

Commit

Permalink
regex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
claywar committed May 16, 2024
1 parent 90f4322 commit 9bc2e86
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions tools/ci/lua_stylecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ def check_table_formatting(self, line):
# [ ]{0,} : Any number of spaces
# \n : newline character

for _ in re.finditer("[ ]{0,}=[ ]{0,}\{[ ]{0,}\n", line):
for _ in re.finditer(r"[ ]{0,}=[ ]{0,}\{[ ]{0,}\n", line):
self.error("Incorrectly defined table")

# \{ : Opening curly brace
# [^ \n\}] : Match single characters in list: NOT space or NOT newline or NOT closing curly brace

for _ in re.finditer("\{[^ \n\}]", line):
for _ in re.finditer(r"\{[^ \n\}]", line):
self.error("Table opened without an appropriate following space or newline")

# [^ \n\{] : Match single characters in list: NOT space or NOT newline or NOT opening curly brace
# \} : Closing curly brace

for _ in re.finditer("[^ \n\{]\}", line):
for _ in re.finditer(r"[^ \n\{]\}", line):
self.error("Table closed without an appropriate preceding space or newline")

def check_parameter_padding(self, line):
Expand All @@ -142,15 +142,15 @@ def check_parameter_padding(self, line):
"""
# ,[^ \n] : Any comma that does not have space or newline following

for _ in re.finditer(",[^ \n]", line):
for _ in re.finditer(r",[^ \n]", line):
self.error("Multiple parameters used without an appropriate following space or newline")

def check_conditional_padding(self, line):
# \s{2,}(and|or)(\s{1,}|$)|\s{1,}(and|or)\s{2,}

# lstrip current line to prevent false-positives from indentation
code_line = line.lstrip()
if re.search("\s{2,}(and|or)(\s{1,}|$)|\s{1,}(and|or)\s{2,}", code_line):
if re.search(r"\s{2,}(and|or)(\s{1,}|$)|\s{1,}(and|or)\s{2,}", code_line):
self.error("Multiple spaces detected around logical operator.")

def check_semicolon(self, line):
Expand All @@ -160,12 +160,12 @@ def check_semicolon(self, line):
"""

# Ignore strings in line
quote_regex = regex.compile("\"(([^\"\"]+)|(?R))*+\"|\'(([^\'\']+)|(?R))*+\'", re.S)
quote_regex = regex.compile(r"\"(([^\"\"]+)|(?R))*+\"|\'(([^\'\']+)|(?R))*+\'", re.S)
removed_quote_str = regex.sub(quote_regex, "", line)

# ; : Any line that contains a semicolon.

for _ in re.finditer(";", removed_quote_str):
for _ in re.finditer(r";", removed_quote_str):
self.error("Semicolon detected in line.")

def check_variable_names(self, line):
Expand All @@ -178,7 +178,7 @@ def check_variable_names(self, line):
# [^(ID)]) : A token that is NOT 'ID'
# (?=[A-Z]) : A token that starts with a capital letter

for match in re.finditer("local (?=[^(ID)])(?=[A-Z]){1,}", line):
for match in re.finditer(r"local (?=[^(ID)])(?=[A-Z]){1,}", line):
self.error("Capitalised local name")

if "local " in line and " =" in line:
Expand Down Expand Up @@ -206,14 +206,14 @@ def check_operator_padding(self, line):
"""
# [^ =~\<\>][\=\+\*\~\/\>\<]|[\=\+\*\/\>\<][^ =\n] : Require space before and after >, <, >=, <=, ==, +, *, ~=, / operators or comparators

for _ in re.finditer("[^ =~\<\>][\=\+\*\~\/\>\<]|[\=\+\*\/\>\<][^ =\n]", line):
for _ in re.finditer(r"[^ =~\<\>][\=\+\*\~\/\>\<]|[\=\+\*\/\>\<][^ =\n]", line):
self.error("Operator or comparator without padding detected at end of line")

# For now, ignore all content in single-line tables to allow for formatting
stripped_line = line.lstrip()
brace_regex = regex.compile("\{(([^\}\{]+)|(?R))*+\}", re.S)
brace_regex = regex.compile(r"\{(([^\}\{]+)|(?R))*+\}", re.S)
stripped_line = regex.sub(brace_regex, "", stripped_line)
for _ in re.finditer("\s{2,}(>=|<=|==|~=|\+|\*|%|>|<|\^)|(>=|<=|==|~=|\+|\*|%|>|<|\^)\s{2,}", stripped_line):
for _ in re.finditer(r"\s{2,}(>=|<=|==|~=|\+|\*|%|>|<|\^)|(>=|<=|==|~=|\+|\*|%|>|<|\^)\s{2,}", stripped_line):
self.error("Excessive padding detected around operator or comparator.")

def check_parentheses_padding(self, line):
Expand All @@ -223,7 +223,7 @@ def check_parentheses_padding(self, line):
See: https://github.com/LandSandBoat/server/wiki/Development-Guide#lua-no-excess-whitespace
"""

if len(re.findall("\([ ]| [\)]", line)) > 0:
if len(re.findall(r"\([ ]| [\)]", line)) > 0:
if not line.lstrip(' ')[0] == '(' and not line.lstrip(' ')[0] == ')': # Ignore large blocks ending or opening
self.error("No excess whitespace inside of parentheses or solely for alignment.")

Expand Down Expand Up @@ -283,7 +283,7 @@ def check_no_function_decl_padding(self, line):
See: TBD
"""

if re.search("function\s{1,}\(", line):
if re.search(r"function\s{1,}\(", line):
self.error("Padding detected between function and opening parenthesis")

def check_multiline_condition_format(self, line):
Expand All @@ -293,7 +293,7 @@ def check_multiline_condition_format(self, line):
See: https://github.com/LandSandBoat/server/wiki/Development-Guide#lua-formatting-conditional-blocks
"""

stripped_line = re.sub("\".*?\"|'.*?'", "", line) # Ignore data in quotes
stripped_line = re.sub(r"\".*?\"|'.*?'", "", line) # Ignore data in quotes
if contains_word('if')(stripped_line) or contains_word('elseif')(stripped_line):
condition_start = stripped_line.replace('elseif','').replace('if','').strip()
if not 'then' in condition_start and condition_start != '':
Expand Down Expand Up @@ -367,12 +367,12 @@ def run_style_check(self):
continue

comment_header = line.rstrip("\n")
if re.search("^-+$", comment_header) and len(comment_header) > 2 and len(comment_header) != 35:
if re.search(r"^-+$", comment_header) and len(comment_header) > 2 and len(comment_header) != 35:
# For now, ignore empty comments with only `--`
self.error("Standard comment block lines of '-' should be 35 characters.")

# Remove in-line comments
code_line = re.sub("(?=--)(.*?)(?=\r\n|\n)", "", line).rstrip()
code_line = re.sub(r"(?=--)(.*?)(?=\r\n|\n)", "", line).rstrip()

# Before replacing strings, see if we're only using single quotes and check requires
if re.search(r"\"[^\"']*\"(?=(?:[^']*'[^']*')*[^']*$)", code_line):
Expand All @@ -383,8 +383,8 @@ def run_style_check(self):
code_line = code_line.replace("\\'", '')
code_line = code_line.replace('\\"', '')

code_line = re.sub('\"([^\"]*?)\"', "strVal", code_line)
code_line = re.sub("\'([^\"]*?)\'", "strVal", code_line)
code_line = re.sub(r'\"([^\"]*?)\"', "strVal", code_line)
code_line = re.sub(r"\'([^\"]*?)\'", "strVal", code_line)

# Checks that apply to all lines
self.check_table_formatting(code_line)
Expand All @@ -406,10 +406,10 @@ def run_style_check(self):
# Keep track of ID variable assignments and if they are referenced.
# TODO: Track each unique variable, and expand this to potentially something
# more generic for other tests.
if re.search("ID[ ]+=[ ]+zones\[", code_line):
if re.search(r"ID[ ]+=[ ]+zones\[", code_line):
uses_id = True

if uses_id == True and re.search("ID\.", code_line):
if uses_id == True and re.search(r"ID\.", code_line):
has_id_ref = True

# Multiline conditionals should not have data in if, elseif, or then
Expand Down Expand Up @@ -441,16 +441,16 @@ def run_style_check(self):

if contains_word('then')(code_line):
condition_str = full_condition.replace('elseif','').replace('if','').replace('then','').strip()
paren_regex = regex.compile("\((([^\)\(]+)|(?R))*+\)", re.S)
paren_regex = regex.compile(r"\((([^\)\(]+)|(?R))*+\)", re.S)
removed_paren_str = regex.sub(paren_regex, "", condition_str)

if removed_paren_str == "":
self.error("Outer parentheses should be removed in condition")

if len(re.findall("== true|== false|~= true|~= false", condition_str)) > 0:
if len(re.findall(r"== true|== false|~= true|~= false", condition_str)) > 0:
self.error("Boolean with explicit value check")

if not in_condition and len(re.findall(" and | or ", condition_str)) > 0 and len(condition_str) > 72:
if not in_condition and len(re.findall(r" and | or ", condition_str)) > 0 and len(condition_str) > 72:
self.error("Multiline conditional format required")

in_condition = False
Expand Down

0 comments on commit 9bc2e86

Please sign in to comment.