Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve checkstyle double equals test regex & types #1457

Merged
merged 3 commits into from
Jan 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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