Skip to content

Commit 679fc49

Browse files
author
im-vedant
committed
add functionality in eslint_disable_check.py to run for mutliple directories
1 parent 6e68380 commit 679fc49

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

.github/workflows/eslint_disable_check.py

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
55
Methodology:
66
7-
Recursively analyzes TypeScript files in the 'src' directory and its subdirectories
8-
as well as 'setup.ts' files to ensure they do not contain eslint-disable statements.
7+
Recursively analyzes TypeScript files in the specified directory
8+
to ensure they do not contain eslint-disable statements.
99
1010
This script enforces code quality practices in the project.
1111
1212
NOTE:
13-
1413
This script complies with our python3 coding and documentation standards.
1514
It complies with:
1615
1716
1) Pylint
1817
2) Pydocstyle
1918
3) Pycodestyle
2019
4) Flake8
20+
5) Python Black
2121
2222
"""
2323

@@ -26,6 +26,7 @@
2626
import argparse
2727
import sys
2828

29+
2930
def has_eslint_disable(file_path):
3031
"""
3132
Check if a TypeScript file contains eslint-disable statements.
@@ -36,43 +37,71 @@ def has_eslint_disable(file_path):
3637
Returns:
3738
bool: True if eslint-disable statement is found, False otherwise.
3839
"""
39-
eslint_disable_pattern = re.compile(r'//\s*eslint-disable(?:-next-line|-line)?', re.IGNORECASE)
40-
40+
eslint_disable_pattern = re.compile(
41+
r"""\/\/\s*eslint-disable(?:-next-line
42+
|-line)?[^\n]*|\/\*\s*eslint-disable[^\*]*\*\/""",
43+
re.IGNORECASE,
44+
)
45+
4146
try:
42-
with open(file_path, 'r', encoding='utf-8') as file:
47+
with open(file_path, "r", encoding="utf-8") as file:
4348
content = file.read()
4449
return bool(eslint_disable_pattern.search(content))
45-
except Exception as e:
50+
except FileNotFoundError:
51+
print(f"File not found: {file_path}")
52+
return False
53+
except PermissionError:
54+
print(f"Permission denied: {file_path}")
55+
return False
56+
except (IOError, OSError) as e:
4657
print(f"Error reading file {file_path}: {e}")
4758
return False
4859

49-
def check_eslint(directory):
60+
61+
def check_eslint(directories):
5062
"""
51-
Recursively check TypeScript files for eslint-disable statements in the 'src' directory.
63+
Recursively check TypeScript files for eslint-disable statements.
5264
5365
Args:
54-
directory (str): Path to the directory.
66+
directories (list): List of directories.
5567
5668
Returns:
5769
bool: True if eslint-disable statement is found, False otherwise.
5870
"""
5971
eslint_found = False
6072

61-
for root, dirs, files in os.walk(os.path.join(directory, 'src')):
62-
for file_name in files:
63-
if file_name.endswith('.tsx') and not file_name.endswith('.test.tsx'):
64-
file_path = os.path.join(root, file_name)
65-
if has_eslint_disable(file_path):
66-
print(f'File {file_path} contains eslint-disable statement.')
67-
eslint_found = True
68-
69-
setup_path = os.path.join(directory, 'setup.ts')
70-
if os.path.exists(setup_path) and has_eslint_disable(setup_path):
71-
print(f'Setup file {setup_path} contains eslint-disable statement.')
72-
eslint_found = True
73+
for directory in directories:
74+
if not os.path.exists(directory):
75+
print(
76+
f"""Error: The specified directory '{directory}' does not
77+
exist."""
78+
)
79+
sys.exit(1)
80+
for root, _, files in os.walk(directory):
81+
for file_name in files:
82+
if (
83+
file_name.endswith(".tsx")
84+
and not file_name.endswith(".test.tsx")
85+
):
86+
file_path = os.path.join(root, file_name)
87+
if has_eslint_disable(file_path):
88+
print(
89+
f"""File {file_path} contains eslint-disable
90+
statement."""
91+
)
92+
eslint_found = True
93+
94+
setup_path = os.path.join(directory, "setup.ts")
95+
if os.path.exists(setup_path) and has_eslint_disable(setup_path):
96+
print(
97+
f"""Setup file {setup_path} contains eslint-disable
98+
statement."""
99+
)
100+
eslint_found = True
73101

74102
return eslint_found
75103

104+
76105
def arg_parser_resolver():
77106
"""Resolve the CLI arguments provided by the user.
78107
@@ -83,11 +112,14 @@ def arg_parser_resolver():
83112
parser.add_argument(
84113
"--directory",
85114
type=str,
86-
default=os.getcwd(),
87-
help="Path to the directory to check (default: current directory)"
115+
nargs="+",
116+
default=[os.getcwd()],
117+
help="""One or more directories to check for eslint disable
118+
statements (default: current directory).""",
88119
)
89120
return parser.parse_args()
90121

122+
91123
def main():
92124
"""
93125
Execute the script's main functionality.
@@ -104,11 +136,7 @@ def main():
104136
SystemExit: If an error occurs during execution.
105137
"""
106138
args = arg_parser_resolver()
107-
108-
if not os.path.exists(args.directory):
109-
print(f"Error: The specified directory '{args.directory}' does not exist.")
110-
sys.exit(1)
111-
139+
print(f"Checking directories: {args.directory}")
112140
# Check eslint in the specified directory
113141
eslint_found = check_eslint(args.directory)
114142

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

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

149+
121150
if __name__ == "__main__":
122151
main()

0 commit comments

Comments
 (0)