Skip to content

Commit

Permalink
chore: improve checkstyle double equals test regex & types (#1457)
Browse files Browse the repository at this point in the history
Co-authored-by: James Hegedus <jthegedus@hey.com>
  • Loading branch information
hyperupcall and jthegedus authored Jan 28, 2023
1 parent 22be95e commit d9a0454
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions scripts/checkstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#
# Lint a particular file
# $ ./scripts/checkstyle.py ./lib/functions/installs.bash
#
# Check to ensure all regexes are working as intended
# $ ./scripts/checkstyle.py --internal-test-regex

Rule = Dict[str, Any]

class c:
RED = '\033[91m'
Expand Down Expand Up @@ -50,12 +55,14 @@ def noPwdCaptureFixer(line: str, m) -> str:

return f'{prestr}$PWD{poststr}'

# Before: [ a == b ]
# After: [ a = b ]
def noTestDoubleEqualsFixer(line: str, m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)

return f'{prestr}={poststr}'

def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any]):
def lintfile(filepath: Path, rules: List[Rule], options: Dict[str, Any]):
content_arr = filepath.read_text().split('\n')

for line_i, line in enumerate(content_arr):
Expand Down Expand Up @@ -84,7 +91,7 @@ def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any
filepath.write_text('\n'.join(content_arr))

def main():
rules = [
rules: List[Rule] = [
{
'name': 'no-double-backslash',
'regex': '".*?(?P<match>\\\\\\\\[abeEfnrtv\'"?xuUc]).*?(?<!\\\\)"',
Expand Down Expand Up @@ -115,21 +122,24 @@ def main():
},
{
'name': 'no-test-double-equals',
'regex': '(?<!\\[)\\[[^[]*?(?P<match>==).*?\\]',
'reason': 'Although it is valid Bash, it reduces consistency and copy-paste-ability',
'regex': '(?<!\\[)\\[ (?:[^]]|](?=}))*?(?P<match>==).*?]',
'reason': 'Disallow double equals in places where they are not necessary for consistency',
'fixerFn': noTestDoubleEqualsFixer,
'testPositiveMatches': [
'[ a == b ]',
'[ "${lines[0]}" == blah ]',
],
'testNegativeMatches': [
'[ a = b ]',
'[[ a = b ]]',
'[[ a == b ]]',
'[ a = b ] || [[ a == b ]]',
'[[ a = b ]] || [[ a == b ]]'
'[[ a = b ]] || [[ a == b ]]',
'[[ "${lines[0]}" == \'usage: \'* ]]',
'[ "${lines[0]}" = blah ]',
],
'found': 0
}
},
]

parser = argparse.ArgumentParser()
Expand Down

0 comments on commit d9a0454

Please sign in to comment.