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
+ or checks specific files directly to ensure they do not contain
9
+ eslint-disable statements.
9
10
10
11
This script enforces code quality practices in the project.
11
12
12
13
NOTE:
13
-
14
14
This script complies with our python3 coding and documentation standards.
15
15
It complies with:
16
16
17
17
1) Pylint
18
18
2) Pydocstyle
19
19
3) Pycodestyle
20
20
4) Flake8
21
+ 5) Python Black
21
22
22
23
"""
23
-
24
24
import os
25
25
import re
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,65 +37,98 @@ 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 (files_or_directories ):
50
62
"""
51
- Recursively check TypeScript files for eslint-disable statements in the 'src' directory .
63
+ Check TypeScript files for eslint-disable statements.
52
64
53
65
Args:
54
- directory (str ): Path to the directory .
66
+ files_or_directories (list ): List of files or directories to check .
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.' )
73
+ for item in files_or_directories :
74
+ if os . path . isfile ( item ) :
75
+ # If it's a file, directly check it
76
+ if item . endswith ( ".ts" ) or item . endswith ( ".tsx" ):
77
+ if has_eslint_disable (item ):
78
+ print (f" File { item } contains eslint-disable statement." )
67
79
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
80
+ elif os .path .isdir (item ):
81
+ # If it's a directory, walk through it and check all
82
+ # .ts and .tsx files
83
+ for root , _ , files in os .walk (item ):
84
+ if "node_modules" in root :
85
+ continue
86
+ for file_name in files :
87
+ if file_name .endswith (".ts" ) or file_name .endswith (".tsx" ):
88
+ file_path = os .path .join (root , file_name )
89
+ if has_eslint_disable (file_path ):
90
+ print (
91
+ f"""File { file_path } contains eslint-disable
92
+ statement."""
93
+ )
94
+ eslint_found = True
73
95
74
96
return eslint_found
75
97
98
+
76
99
def arg_parser_resolver ():
77
100
"""Resolve the CLI arguments provided by the user.
78
101
79
102
Returns:
80
103
result: Parsed argument object
81
104
"""
82
105
parser = argparse .ArgumentParser ()
106
+ parser .add_argument (
107
+ "--files" ,
108
+ type = str ,
109
+ nargs = "+" ,
110
+ default = [],
111
+ help = """List of files to check for eslint disable
112
+ statements (default: None).""" ,
113
+ )
83
114
parser .add_argument (
84
115
"--directory" ,
85
116
type = str ,
86
- default = os .getcwd (),
87
- help = "Path to the directory to check (default: current directory)"
117
+ nargs = "+" ,
118
+ default = [os .getcwd ()],
119
+ help = """One or more directories to check for eslint disable
120
+ statements (default: current directory).""" ,
88
121
)
89
122
return parser .parse_args ()
90
123
124
+
91
125
def main ():
92
126
"""
93
127
Execute the script's main functionality.
94
128
95
129
This function serves as the entry point for the script. It performs
96
130
the following tasks:
97
- 1. Validates and retrieves the directory to check from
131
+ 1. Validates and retrieves the files and directories to check from
98
132
command line arguments.
99
133
2. Recursively checks TypeScript files for eslint-disable statements.
100
134
3. Provides informative messages based on the analysis.
@@ -105,18 +139,17 @@ def main():
105
139
"""
106
140
args = arg_parser_resolver ()
107
141
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
-
112
- # Check eslint in the specified directory
113
- eslint_found = check_eslint (args .directory )
142
+ # Determine whether to check files or directories based on the arguments
143
+ files_or_directories = args .files if args .files else args .directory
144
+ # Check eslint in the specified files or directories
145
+ eslint_found = check_eslint (files_or_directories )
114
146
115
147
if eslint_found :
116
148
print ("ESLint-disable check failed. Exiting with error." )
117
149
sys .exit (1 )
118
150
119
151
print ("ESLint-disable check completed successfully." )
120
152
153
+
121
154
if __name__ == "__main__" :
122
155
main ()
0 commit comments