Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed eslint_disable_check.py (Fixes #3272) #3273

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##############################################################################

Check warning on line 1 in .github/workflows/pull-request.yml

View workflow job for this annotation

GitHub Actions / Performs linting, formatting, type-checking, checking for different source and target branch

File ignored by default.
##############################################################################
#
# NOTE!
Expand Down Expand Up @@ -195,14 +195,16 @@
id: changed-files
uses: tj-actions/changed-files@v45

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Filter TypeScript files
id: filter-files
run: |
echo "ts_files=$(echo '${{ steps.changed-files.outputs.all_changed_files }}' | tr ',' '\n' | grep -E '\.(ts|tsx)$' | tr '\n' ' ')" >> $GITHUB_OUTPUT
palisadoes marked this conversation as resolved.
Show resolved Hide resolved
shell: bash
adithyanotfound marked this conversation as resolved.
Show resolved Hide resolved

- name: Run Python script
if: steps.filter-files.outputs.ts_files != ''
run: |
python .github/workflows/scripts/eslint_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
python .github/workflows/scripts/eslint_disable_check.py --files ${{ steps.filter-files.outputs.ts_files }}

Check-Code-Coverage-Disable:
name: Check for code coverage disable
Expand Down
47 changes: 21 additions & 26 deletions .github/workflows/scripts/eslint_disable_check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3

Check warning on line 1 in .github/workflows/scripts/eslint_disable_check.py

View workflow job for this annotation

GitHub Actions / Performs linting, formatting, type-checking, checking for different source and target branch

File ignored by default.
# -*- coding: UTF-8 -*-
"""ESLint Checker Script.

Expand Down Expand Up @@ -38,9 +38,10 @@
"""
# Initialize key variables
eslint_disable_pattern = re.compile(
r"\/\/\s*eslint-disable(?:-next-line"
r"|-line)?[^\n]*|\/\*\s*eslint-disable[^\*]*\*\/",
re.IGNORECASE,
r"\/\/\s*eslint-disable(?:-next-line|-line)?[^\n]*|"
r"\/\*\s*eslint-disable[^\*]*\*\/|"
palisadoes marked this conversation as resolved.
Show resolved Hide resolved
r"\/\*[\s\S]*?eslint-disable[\s\S]*?\*\/",
re.IGNORECASE | re.DOTALL,
)

try:
Expand All @@ -49,13 +50,11 @@
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
adithyanotfound marked this conversation as resolved.
Show resolved Hide resolved


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

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 {item} contains "
"eslint-disable statements."
)
eslint_found = True

eslint_found = True
return eslint_found


Expand All @@ -107,22 +102,22 @@
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
Loading