Skip to content

Commit

Permalink
add functionality in eslint_disable_check.py to run for mutliple dire…
Browse files Browse the repository at this point in the history
…ctories
  • Loading branch information
im-vedant committed Dec 22, 2024
1 parent 6e68380 commit 679fc49
Showing 1 changed file with 58 additions and 29 deletions.
87 changes: 58 additions & 29 deletions .github/workflows/eslint_disable_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
Methodology:
Recursively analyzes TypeScript files in the 'src' directory and its subdirectories
as well as 'setup.ts' files to ensure they do not contain eslint-disable statements.
Recursively analyzes TypeScript files in the specified directory
to ensure they do not contain eslint-disable statements.
This script enforces code quality practices in the project.
NOTE:
This script complies with our python3 coding and documentation standards.
It complies with:
1) Pylint
2) Pydocstyle
3) Pycodestyle
4) Flake8
5) Python Black
"""

Expand All @@ -26,6 +26,7 @@
import argparse
import sys


def has_eslint_disable(file_path):
"""
Check if a TypeScript file contains eslint-disable statements.
Expand All @@ -36,43 +37,71 @@ def has_eslint_disable(file_path):
Returns:
bool: True if eslint-disable statement is found, False otherwise.
"""
eslint_disable_pattern = re.compile(r'//\s*eslint-disable(?:-next-line|-line)?', re.IGNORECASE)

eslint_disable_pattern = re.compile(
r"""\/\/\s*eslint-disable(?:-next-line
|-line)?[^\n]*|\/\*\s*eslint-disable[^\*]*\*\/""",
re.IGNORECASE,
)

try:
with open(file_path, 'r', encoding='utf-8') as file:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
return bool(eslint_disable_pattern.search(content))
except Exception as e:
except FileNotFoundError:
print(f"File not found: {file_path}")
return False
except PermissionError:
print(f"Permission denied: {file_path}")
return False
except (IOError, OSError) as e:
print(f"Error reading file {file_path}: {e}")
return False

def check_eslint(directory):

def check_eslint(directories):
"""
Recursively check TypeScript files for eslint-disable statements in the 'src' directory.
Recursively check TypeScript files for eslint-disable statements.
Args:
directory (str): Path to the directory.
directories (list): List of directories.
Returns:
bool: True if eslint-disable statement is found, False otherwise.
"""
eslint_found = False

for root, dirs, files in os.walk(os.path.join(directory, 'src')):
for file_name in files:
if file_name.endswith('.tsx') and not file_name.endswith('.test.tsx'):
file_path = os.path.join(root, file_name)
if has_eslint_disable(file_path):
print(f'File {file_path} contains eslint-disable statement.')
eslint_found = True

setup_path = os.path.join(directory, 'setup.ts')
if os.path.exists(setup_path) and has_eslint_disable(setup_path):
print(f'Setup file {setup_path} contains eslint-disable statement.')
eslint_found = True
for directory in directories:
if not os.path.exists(directory):
print(
f"""Error: The specified directory '{directory}' does not
exist."""
)
sys.exit(1)
for root, _, files in os.walk(directory):
for file_name in files:
if (
file_name.endswith(".tsx")
and not file_name.endswith(".test.tsx")
):
file_path = os.path.join(root, file_name)
if has_eslint_disable(file_path):
print(
f"""File {file_path} contains eslint-disable
statement."""
)
eslint_found = True

setup_path = os.path.join(directory, "setup.ts")
if os.path.exists(setup_path) and has_eslint_disable(setup_path):
print(
f"""Setup file {setup_path} contains eslint-disable
statement."""
)
eslint_found = True

return eslint_found


def arg_parser_resolver():
"""Resolve the CLI arguments provided by the user.
Expand All @@ -83,11 +112,14 @@ def arg_parser_resolver():
parser.add_argument(
"--directory",
type=str,
default=os.getcwd(),
help="Path to the directory to check (default: current directory)"
nargs="+",
default=[os.getcwd()],
help="""One or more directories to check for eslint disable
statements (default: current directory).""",
)
return parser.parse_args()


def main():
"""
Execute the script's main functionality.
Expand All @@ -104,11 +136,7 @@ def main():
SystemExit: If an error occurs during execution.
"""
args = arg_parser_resolver()

if not os.path.exists(args.directory):
print(f"Error: The specified directory '{args.directory}' does not exist.")
sys.exit(1)

print(f"Checking directories: {args.directory}")
# Check eslint in the specified directory
eslint_found = check_eslint(args.directory)

Expand All @@ -118,5 +146,6 @@ def main():

print("ESLint-disable check completed successfully.")


if __name__ == "__main__":
main()

0 comments on commit 679fc49

Please sign in to comment.