Skip to content

Commit

Permalink
Fixed eslint_disable_check.py (Fixes #3272) (#3273)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adithyanotfound authored Jan 14, 2025
1 parent a23d95b commit 334f0f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ jobs:
run: |
python .github/workflows/scripts/eslint_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
Check-Code-Coverage-Disable:
name: Check for code coverage disable
runs-on: ubuntu-latest
Expand Down
45 changes: 19 additions & 26 deletions .github/workflows/scripts/eslint_disable_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ def has_eslint_disable(file_path):
"""
# Initialize key variables
eslint_disable_pattern = re.compile(
r"\/\/\s*eslint-disable(?:-next-line"
r"|-line)?[^\n]*|\/\*\s*eslint-disable[^\*]*\*\/",
re.IGNORECASE,
r"\/\/.*eslint-disable.*|\/\*[\s\S]*?eslint-disable[\s\S]*?\*\/",
re.IGNORECASE | re.DOTALL,
)

try:
Expand All @@ -49,13 +48,11 @@ def has_eslint_disable(file_path):
return bool(eslint_disable_pattern.search(content))
except FileNotFoundError:
print(f"File not found: {file_path}")
return False
except PermissionError:
print(f"Permission denied: {file_path}")
return False
except (IOError, OSError) as e:
print(f"Error reading file {file_path}: {e}")
return False
return False


def check_eslint(files_or_directories):
Expand All @@ -71,31 +68,27 @@ def check_eslint(files_or_directories):

for item in files_or_directories:
if os.path.isfile(item):
# If it's a file, directly check it
if item.endswith(".ts") or item.endswith(".tsx"):
if has_eslint_disable(item):
print(
f"""\
File {item} contains eslint-disable statement. Please remove them and \
ensure the code adheres to the specified ESLint rules."""
)
eslint_found = True
# Check a single file
if item.endswith((".ts", ".tsx")) and has_eslint_disable(item):
print(
f"Error: File {item} contains eslint-disable statements."
)
eslint_found = True
elif os.path.isdir(item):
# If it's a directory, walk through it and check all
# .ts and .tsx files
# Recursively check files in a directory
for root, _, files in os.walk(item):
if "node_modules" in root:
continue
for file_name in files:
if file_name.endswith(".ts") or file_name.endswith(".tsx"):
if file_name.endswith((".ts", ".tsx")):
file_path = os.path.join(root, file_name)
if has_eslint_disable(file_path):
print(
f"""File {file_path} contains eslint-disable
statement."""
f"Error: File {file_path} contains "
"eslint-disable statements."
)
eslint_found = True

eslint_found = True
return eslint_found


Expand All @@ -107,22 +100,22 @@ def arg_parser_resolver():
Returns:
result: Parsed argument object
"""
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(
description="Check TypeScript files for eslint-disable statements."
)
parser.add_argument(
"--files",
type=str,
nargs="+",
default=[],
help="""List of files to check for eslint disable
statements (default: None).""",
help="List of files to check for eslint-disable statements.",
)
parser.add_argument(
"--directory",
type=str,
nargs="+",
default=[os.getcwd()],
help="""One or more directories to check for eslint disable
statements (default: current directory).""",
help="One or more directories to check for eslint-disable statements.",
)
return parser.parse_args()

Expand Down

0 comments on commit 334f0f7

Please sign in to comment.