Skip to content

Commit d188f2d

Browse files
feat:script to check ts-ignore (#3246)
* feat:script for ts-ignore * fix:improved codequality * fix:added logging * fix:improved code-quality * fix:trailing commas
1 parent 4ff9d91 commit d188f2d

File tree

2 files changed

+103
-20
lines changed

2 files changed

+103
-20
lines changed

.github/workflows/pull-request.yml

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ jobs:
4848
echo "Error: Close this PR and try again."
4949
exit 1
5050
51-
check_biome_ignore:
52-
name: Check for biome_ignore
51+
python_checks:
52+
name: Run Python Checks
5353
runs-on: ubuntu-latest
5454
steps:
5555
- name: Checkout code
@@ -64,30 +64,19 @@ jobs:
6464
with:
6565
python-version: 3.9
6666

67-
- name: Run Python script
67+
- name: Run Biome Ignore Check
6868
run: |
6969
python .github/workflows/scripts/biome_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
7070
71-
check_code_coverage_disable:
72-
name: Check for code coverage disable
73-
runs-on: ubuntu-latest
74-
steps:
75-
- name: Checkout code
76-
uses: actions/checkout@v4
77-
78-
- name: Get changed files
79-
id: changed-files
80-
uses: tj-actions/changed-files@v45
8171
82-
- name: Set up Python
83-
uses: actions/setup-python@v5
84-
with:
85-
python-version: 3.9
86-
87-
- name: Run Python script
72+
- name: Run Code Coverage Disable Check
8873
run: |
8974
python .github/workflows/scripts/code_coverage_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
9075
76+
- name: Run TS Ignore Check
77+
run: |
78+
python .github/workflows/scripts/detect_ts_ignore.py --files ${{ steps.changed-files.outputs.all_changed_files }}
79+
9180
check_gql_tada:
9281
name: Check gql tada files and configuration
9382
runs-on: ubuntu-latest
@@ -195,7 +184,7 @@ jobs:
195184
Run-Tests:
196185
name: Run tests for talawa api
197186
runs-on: ubuntu-latest
198-
needs: [Code-Quality-Checks, check_code_coverage_disable]
187+
needs: [Code-Quality-Checks, python_checks]
199188
env:
200189
CODECOV_UNIQUE_NAME: ${{github.workflow}}-${{github.ref_name}}
201190
steps:
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python3
2+
"""Script to detect and report usage of @ts-ignore comments in TypeScript."""
3+
4+
import argparse
5+
import re
6+
import sys
7+
import logging
8+
9+
# Configure logging
10+
logging.basicConfig(
11+
level=logging.INFO,
12+
format="%(levelname)s: %(message)s",
13+
)
14+
15+
TS_IGNORE_PATTERN = r"(?://|/\*)\s*@ts-ignore(?:\s+|$)"
16+
17+
18+
def check_ts_ignore(files: list[str]) -> int:
19+
"""Check for occurrences of '@ts-ignore' in the given files.
20+
21+
Args:
22+
files (list[str]): List of file paths to check.
23+
24+
Returns:
25+
int: 0 if no violations, 1 if violations are found.
26+
"""
27+
ts_ignore_found = False
28+
29+
for file in files:
30+
try:
31+
logging.info("Checking file: %s", file)
32+
with open(file, encoding="utf-8") as f:
33+
for line_num, line in enumerate(f, start=1):
34+
# Handle more variations of @ts-ignore
35+
if re.search(
36+
TS_IGNORE_PATTERN,
37+
line.strip(),
38+
):
39+
print(
40+
"❌ Error: '@ts-ignore' found in %s at line %d",
41+
file,
42+
line_num,
43+
)
44+
logging.debug(
45+
"Found @ts-ignore in line: %s",
46+
line.strip(),
47+
)
48+
ts_ignore_found = True
49+
except FileNotFoundError:
50+
logging.warning("File not found: %s", file)
51+
except OSError:
52+
logging.exception("Could not read %s", file)
53+
if not ts_ignore_found:
54+
print("✅ No '@ts-ignore' comments found in the files.")
55+
56+
return 1 if ts_ignore_found else 0
57+
58+
59+
def main() -> None:
60+
"""Main function to parse arguments and run the check.
61+
62+
This function sets up argument parsing for file paths and runs the ts-ignore
63+
check on the specified files.
64+
65+
Args:
66+
None
67+
68+
Returns:
69+
None: The function exits the program with status code 0 if no ts-ignore
70+
comments are found, or 1 if any are detected.
71+
"""
72+
parser = argparse.ArgumentParser(
73+
description="Check for @ts-ignore in changed files.",
74+
)
75+
parser.add_argument(
76+
"--files",
77+
nargs="+",
78+
help="List of changed files",
79+
required=True,
80+
)
81+
args = parser.parse_args()
82+
83+
# Filter TypeScript files
84+
ts_files = [f for f in args.files if f.endswith((".ts", ".tsx"))]
85+
if not ts_files:
86+
logging.info("No TypeScript files to check.")
87+
sys.exit(0)
88+
89+
exit_code = check_ts_ignore(args.files)
90+
sys.exit(exit_code)
91+
92+
93+
if __name__ == "__main__":
94+
main()

0 commit comments

Comments
 (0)