Skip to content

Commit 334f0f7

Browse files
Fixed eslint_disable_check.py (Fixes #3272) (#3273)
* Fixed eslint-disable script * Update regex to handle edge cases * Remove test eslint-disable * Fixed lint * Fixed lint * Fixed redundant \n * Fixed flake8 lint * Implemented suggestions * Removed test eslint-disable
1 parent a23d95b commit 334f0f7

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

.github/workflows/pull-request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ jobs:
204204
run: |
205205
python .github/workflows/scripts/eslint_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
206206
207+
207208
Check-Code-Coverage-Disable:
208209
name: Check for code coverage disable
209210
runs-on: ubuntu-latest

.github/workflows/scripts/eslint_disable_check.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ def has_eslint_disable(file_path):
3838
"""
3939
# Initialize key variables
4040
eslint_disable_pattern = re.compile(
41-
r"\/\/\s*eslint-disable(?:-next-line"
42-
r"|-line)?[^\n]*|\/\*\s*eslint-disable[^\*]*\*\/",
43-
re.IGNORECASE,
41+
r"\/\/.*eslint-disable.*|\/\*[\s\S]*?eslint-disable[\s\S]*?\*\/",
42+
re.IGNORECASE | re.DOTALL,
4443
)
4544

4645
try:
@@ -49,13 +48,11 @@ def has_eslint_disable(file_path):
4948
return bool(eslint_disable_pattern.search(content))
5049
except FileNotFoundError:
5150
print(f"File not found: {file_path}")
52-
return False
5351
except PermissionError:
5452
print(f"Permission denied: {file_path}")
55-
return False
5653
except (IOError, OSError) as e:
5754
print(f"Error reading file {file_path}: {e}")
58-
return False
55+
return False
5956

6057

6158
def check_eslint(files_or_directories):
@@ -71,31 +68,27 @@ def check_eslint(files_or_directories):
7168

7269
for item in files_or_directories:
7370
if os.path.isfile(item):
74-
# If it's a file, directly check it
75-
if item.endswith(".ts") or item.endswith(".tsx"):
76-
if has_eslint_disable(item):
77-
print(
78-
f"""\
79-
File {item} contains eslint-disable statement. Please remove them and \
80-
ensure the code adheres to the specified ESLint rules."""
81-
)
82-
eslint_found = True
71+
# Check a single file
72+
if item.endswith((".ts", ".tsx")) and has_eslint_disable(item):
73+
print(
74+
f"Error: File {item} contains eslint-disable statements."
75+
)
76+
eslint_found = True
8377
elif os.path.isdir(item):
84-
# If it's a directory, walk through it and check all
85-
# .ts and .tsx files
78+
# Recursively check files in a directory
8679
for root, _, files in os.walk(item):
8780
if "node_modules" in root:
8881
continue
8982
for file_name in files:
90-
if file_name.endswith(".ts") or file_name.endswith(".tsx"):
83+
if file_name.endswith((".ts", ".tsx")):
9184
file_path = os.path.join(root, file_name)
9285
if has_eslint_disable(file_path):
9386
print(
94-
f"""File {file_path} contains eslint-disable
95-
statement."""
87+
f"Error: File {file_path} contains "
88+
"eslint-disable statements."
9689
)
97-
eslint_found = True
9890

91+
eslint_found = True
9992
return eslint_found
10093

10194

@@ -107,22 +100,22 @@ def arg_parser_resolver():
107100
Returns:
108101
result: Parsed argument object
109102
"""
110-
parser = argparse.ArgumentParser()
103+
parser = argparse.ArgumentParser(
104+
description="Check TypeScript files for eslint-disable statements."
105+
)
111106
parser.add_argument(
112107
"--files",
113108
type=str,
114109
nargs="+",
115110
default=[],
116-
help="""List of files to check for eslint disable
117-
statements (default: None).""",
111+
help="List of files to check for eslint-disable statements.",
118112
)
119113
parser.add_argument(
120114
"--directory",
121115
type=str,
122116
nargs="+",
123117
default=[os.getcwd()],
124-
help="""One or more directories to check for eslint disable
125-
statements (default: current directory).""",
118+
help="One or more directories to check for eslint-disable statements.",
126119
)
127120
return parser.parse_args()
128121

0 commit comments

Comments
 (0)