From df7a52d7fdf523826462b76f55a776c854c2e6fe Mon Sep 17 00:00:00 2001 From: Aryan Shah <149894557+ARYANSHAH1567@users.noreply.github.com> Date: Sun, 15 Sep 2024 19:25:20 +0530 Subject: [PATCH] Fixed the eslint_disable_check.py script (#2521) * Added the eslint_disable_check.py script * checking if it throws linting error * checking if it throws linting error * Update setup.ts Once i remove the eslint-disable comment it passes the tests --- .github/workflows/eslint_disable_check.py | 123 ++++++++++++++++++++++ .github/workflows/pull-request.yml | 2 +- package.json | 2 +- 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/eslint_disable_check.py diff --git a/.github/workflows/eslint_disable_check.py b/.github/workflows/eslint_disable_check.py new file mode 100644 index 0000000000..95702064ee --- /dev/null +++ b/.github/workflows/eslint_disable_check.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- +"""ESLint Checker Script. + +Methodology: + + Recursively analyzes TypeScript files in the 'src' directory and its subdirectories + as well as 'setup.ts' files to ensure they do not contain eslint-disable statements. + + This script enforces code quality practices in the project. + +NOTE: + + This script complies with our python3 coding and documentation standards. + It complies with: + + 1) Pylint + 2) Pydocstyle + 3) Pycodestyle + 4) Flake8 + +""" + +import os +import re +import argparse +import sys + +def has_eslint_disable(file_path): + """ + Check if a TypeScript file contains eslint-disable statements. + + Args: + file_path (str): Path to the TypeScript file. + + Returns: + bool: True if eslint-disable statement is found, False otherwise. + """ + eslint_disable_pattern = re.compile(r'//\s*eslint-disable(?:-next-line|-line)?', re.IGNORECASE) + + try: + with open(file_path, 'r', encoding='utf-8') as file: + content = file.read() + return bool(eslint_disable_pattern.search(content)) + except Exception as e: + print(f"Error reading file {file_path}: {e}") + return False + +def check_eslint(directory): + """ + Recursively check TypeScript files for eslint-disable statements in the 'src' directory. + + Args: + directory (str): Path to the directory. + + Returns: + list: List of files containing eslint-disable statements. + """ + eslint_issues = [] + + src_dir = os.path.join(directory, 'src') + + if not os.path.exists(src_dir): + print(f"Source directory '{src_dir}' does not exist.") + return eslint_issues + + for root, dirs, files in os.walk(src_dir): + for file_name in files: + if file_name.endswith('.tsx') and not file_name.endswith('.test.tsx'): + file_path = os.path.join(root, file_name) + if has_eslint_disable(file_path): + eslint_issues.append(f'File {file_path} contains eslint-disable statement.') + + setup_path = os.path.join(directory, 'setup.ts') + if os.path.exists(setup_path) and has_eslint_disable(setup_path): + eslint_issues.append(f'Setup file {setup_path} contains eslint-disable statement.') + + return eslint_issues + +def arg_parser_resolver(): + """Resolve the CLI arguments provided by the user.""" + parser = argparse.ArgumentParser() + parser.add_argument( + "--directory", + type=str, + default=os.getcwd(), + help="Path to the directory to check (default: current directory)" + ) + return parser.parse_args() + +def main(): + """ + Execute the script's main functionality. + + This function serves as the entry point for the script. It performs + the following tasks: + 1. Validates and retrieves the directory to check from + command line arguments. + 2. Recursively checks TypeScript files for eslint-disable statements. + 3. Provides informative messages based on the analysis. + 4. Exits with an error if eslint-disable statements are found. + + Raises: + SystemExit: If an error occurs during execution. + """ + args = arg_parser_resolver() + if not os.path.exists(args.directory): + print(f"Error: The specified directory '{args.directory}' does not exist.") + sys.exit(1) + + eslint_issues = check_eslint(args.directory) + + if eslint_issues: + for issue in eslint_issues: + print(issue) + print("ESLint-disable check failed. Exiting with error.") + sys.exit(1) + + print("ESLint-disable check completed successfully.") + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 0db1546c20..10fd9fd62f 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -74,7 +74,7 @@ jobs: if: steps.changed_files.outputs.any_changed == 'true' env: CHANGED_FILES: ${{ steps.changed_files.outputs.all_changed_files }} - run: npx eslint ${CHANGED_FILES} + run: npx eslint ${CHANGED_FILES} --max-warnings=1500 && python .github/workflows/eslint_disable_check.py - name: Check for formatting errors run: npm run format:check diff --git a/package.json b/package.json index 69c31d2f42..0f2a857e5d 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "setup": "tsx setup.ts", "test": "vitest run --pool=threads --no-file-parallelism --coverage", "typecheck": "graphql-codegen && tsc --noEmit --pretty", - "lint:check": "eslint . --max-warnings=1500", + "lint:check": "eslint . --max-warnings=1500 && python .github/workflows/eslint_disable_check.py", "lint:fix": "eslint . --fix", "lint-staged": "lint-staged", "format:fix": "prettier --write \"**/*.{ts,tsx,json,scss,css}\"",