|
| 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