4
4
5
5
Methodology:
6
6
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.
9
9
10
10
This script enforces code quality practices in the project.
11
11
12
12
NOTE:
13
-
14
13
This script complies with our python3 coding and documentation standards.
15
14
It complies with:
16
15
17
16
1) Pylint
18
17
2) Pydocstyle
19
18
3) Pycodestyle
20
19
4) Flake8
20
+ 5) Python Black
21
21
22
22
"""
23
23
26
26
import argparse
27
27
import sys
28
28
29
+
29
30
def has_eslint_disable (file_path ):
30
31
"""
31
32
Check if a TypeScript file contains eslint-disable statements.
@@ -36,43 +37,71 @@ def has_eslint_disable(file_path):
36
37
Returns:
37
38
bool: True if eslint-disable statement is found, False otherwise.
38
39
"""
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
+
41
46
try :
42
- with open (file_path , 'r' , encoding = ' utf-8' ) as file :
47
+ with open (file_path , "r" , encoding = " utf-8" ) as file :
43
48
content = file .read ()
44
49
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 :
46
57
print (f"Error reading file { file_path } : { e } " )
47
58
return False
48
59
49
- def check_eslint (directory ):
60
+
61
+ def check_eslint (directories ):
50
62
"""
51
- Recursively check TypeScript files for eslint-disable statements in the 'src' directory .
63
+ Recursively check TypeScript files for eslint-disable statements.
52
64
53
65
Args:
54
- directory (str ): Path to the directory .
66
+ directories (list ): List of directories .
55
67
56
68
Returns:
57
69
bool: True if eslint-disable statement is found, False otherwise.
58
70
"""
59
71
eslint_found = False
60
72
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
73
101
74
102
return eslint_found
75
103
104
+
76
105
def arg_parser_resolver ():
77
106
"""Resolve the CLI arguments provided by the user.
78
107
@@ -83,11 +112,14 @@ def arg_parser_resolver():
83
112
parser .add_argument (
84
113
"--directory" ,
85
114
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).""" ,
88
119
)
89
120
return parser .parse_args ()
90
121
122
+
91
123
def main ():
92
124
"""
93
125
Execute the script's main functionality.
@@ -104,11 +136,7 @@ def main():
104
136
SystemExit: If an error occurs during execution.
105
137
"""
106
138
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 } " )
112
140
# Check eslint in the specified directory
113
141
eslint_found = check_eslint (args .directory )
114
142
@@ -118,5 +146,6 @@ def main():
118
146
119
147
print ("ESLint-disable check completed successfully." )
120
148
149
+
121
150
if __name__ == "__main__" :
122
151
main ()
0 commit comments