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
- list: List of files containing eslint-disable statements .
69
+ bool: True if eslint-disable statement is found, False otherwise .
58
70
"""
59
- eslint_issues = []
60
-
61
- src_dir = os .path .join (directory , 'src' )
62
-
63
- if not os .path .exists (src_dir ):
64
- print (f"Source directory '{ src_dir } ' does not exist." )
65
- return eslint_issues
66
-
67
- for root , dirs , files in os .walk (src_dir ):
68
- for file_name in files :
69
- if file_name .endswith ('.tsx' ) and not file_name .endswith ('.test.tsx' ):
70
- file_path = os .path .join (root , file_name )
71
- if has_eslint_disable (file_path ):
72
- eslint_issues .append (f'File { file_path } contains eslint-disable statement.' )
71
+ eslint_found = False
72
+
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" ) :
77
+ if has_eslint_disable (item ):
78
+ print (f"File { item } contains eslint-disable statement. Please remove them and ensure the code adheres to the specified ESLint rules." )
79
+ 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" ) :
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
95
+
96
+ return eslint_found
73
97
74
- setup_path = os .path .join (directory , 'setup.ts' )
75
- if os .path .exists (setup_path ) and has_eslint_disable (setup_path ):
76
- eslint_issues .append (f'Setup file { setup_path } contains eslint-disable statement.' )
77
-
78
- return eslint_issues
79
98
80
99
def arg_parser_resolver ():
81
- """Resolve the CLI arguments provided by the user."""
100
+ """Resolve the CLI arguments provided by the user.
101
+
102
+ Returns:
103
+ result: Parsed argument object
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.
@@ -104,20 +138,18 @@ def main():
104
138
SystemExit: If an error occurs during execution.
105
139
"""
106
140
args = arg_parser_resolver ()
107
- if not os .path .exists (args .directory ):
108
- print (f"Error: The specified directory '{ args .directory } ' does not exist." )
109
- sys .exit (1 )
110
141
111
- eslint_issues = 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 )
112
146
113
- if eslint_issues :
114
- for issue in eslint_issues :
115
- print (issue )
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
- sys . exit ( 0 )
152
+
121
153
122
154
if __name__ == "__main__" :
123
155
main ()
0 commit comments