From f9fdea1fbcb2906398c74388f2404534d06e639e Mon Sep 17 00:00:00 2001 From: Yugal Sadhwani Date: Wed, 22 Jan 2025 07:59:01 +0530 Subject: [PATCH] Fixed ESLint bugs that required ts-specific rules for js functions and added prettier formatting (re-open #3184) (#3229) * Fixed ESLint bugs that required ts-specific rules for js functions * Removed eslint-ignore comments and added override rule specifically for the target file * Changed the file to typescript file and added return types to avoid linting errors * adding npx to run the tsx command * coderabbit suggestions and ignore codeconv for this file since it is not applicable * removed the ignore statement * Resolved merge conflicts in package.json --- .github/workflows/pull-request.yml | 4 +- .lintstagedrc.json | 2 +- package.json | 2 +- ...e-usage.js => check-localstorage-usage.ts} | 48 +++++++++++-------- 4 files changed, 31 insertions(+), 25 deletions(-) rename scripts/githooks/{check-localstorage-usage.js => check-localstorage-usage.ts} (60%) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 17273e36d0..8ce684ad69 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -65,9 +65,7 @@ jobs: run: npm run check-tsdoc # Run the TSDoc check script - name: Check for localStorage Usage - run: | - chmod +x scripts/githooks/check-localstorage-usage.js - node scripts/githooks/check-localstorage-usage.js --scan-entire-repo + run: npx tsx scripts/githooks/check-localstorage-usage.ts --scan-entire-repo - name: Compare translation files run: | diff --git a/.lintstagedrc.json b/.lintstagedrc.json index 36195c0491..d48c67cd6a 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,5 +1,5 @@ { "**/*.{ts,tsx,yml}": "eslint --fix", "**/*.{ts,tsx,json,scss,css,yml}": "prettier --write", - "**/*.{ts,tsx}": "node scripts/githooks/check-localstorage-usage.js" + "**/*.{ts,tsx}": "npx tsx scripts/githooks/check-localstorage-usage.ts" } diff --git a/package.json b/package.json index ef7a3ee4a9..b8d97c8d67 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "update:toc": "node scripts/githooks/update-toc.js", "lint-staged": "lint-staged --concurrent false", "setup": "tsx setup.ts", - "check-localstorage": "node scripts/githooks/check-localstorage-usage.js", + "check-localstorage": "tsx scripts/githooks/check-localstorage-usage.ts", "postgenerate-docs": "find docs/docs/auto-docs -name 'README.md' -delete && node fix-repo-url.js", "generate-docs": "typedoc && npm run postgenerate-docs && node fix-readme-links.js" }, diff --git a/scripts/githooks/check-localstorage-usage.js b/scripts/githooks/check-localstorage-usage.ts similarity index 60% rename from scripts/githooks/check-localstorage-usage.js rename to scripts/githooks/check-localstorage-usage.ts index 0a811df307..74e36fa5a2 100755 --- a/scripts/githooks/check-localstorage-usage.js +++ b/scripts/githooks/check-localstorage-usage.ts @@ -3,44 +3,49 @@ import { readFileSync, existsSync } from 'fs'; import path from 'path'; import { execSync } from 'child_process'; +import type { ExecSyncOptionsWithStringEncoding } from 'child_process'; -const args = process.argv.slice(2); -const scanEntireRepo = args.includes('--scan-entire-repo'); +const args: string[] = process.argv.slice(2); +const scanEntireRepo: boolean = args.includes('--scan-entire-repo'); -const containsSkipComment = (file) => { +const containsSkipComment = (file: string): boolean => { try { const content = readFileSync(file, 'utf-8'); return content.includes('// SKIP_LOCALSTORAGE_CHECK'); } catch (error) { - console.error(`Error reading file ${file}:`, error.message); + console.error( + `Error reading file ${file}:`, + error instanceof Error ? error.message : error, + ); return false; } }; -const getModifiedFiles = () => { +const getModifiedFiles = (): string[] => { try { + const options: ExecSyncOptionsWithStringEncoding = { encoding: 'utf-8' }; + if (scanEntireRepo) { - const result = execSync('git ls-files | grep ".tsx\\?$"', { - encoding: 'utf-8', - }); + const result = execSync('git ls-files | grep ".tsx\\?$"', options); return result.trim().split('\n'); } - const result = execSync('git diff --cached --name-only', { - encoding: 'utf-8', - }); + const result = execSync('git diff --cached --name-only', options); return result.trim().split('\n'); } catch (error) { - console.error('Error fetching modified files:', error.message); + console.error( + 'Error fetching modified files:', + error instanceof Error ? error.message : error, + ); process.exit(1); } + return []; }; -const files = getModifiedFiles(); - -const filesWithLocalStorage = []; +const files: string[] = getModifiedFiles(); +const filesWithLocalStorage: string[] = []; -const checkLocalStorageUsage = (file) => { +const checkLocalStorageUsage = (file: string): void => { if (!file) { return; } @@ -49,7 +54,7 @@ const checkLocalStorageUsage = (file) => { // Skip files with specific names or containing a skip comment if ( - fileName === 'check-localstorage-usage.js' || + fileName === 'check-localstorage-usage.ts' || // Updated extension fileName === 'useLocalstorage.test.ts' || fileName === 'useLocalstorage.ts' || containsSkipComment(file) @@ -73,7 +78,10 @@ const checkLocalStorageUsage = (file) => { console.log(`File ${file} does not exist.`); } } catch (error) { - console.error(`Error reading file ${file}:`, error.message); + console.error( + `Error reading file ${file}:`, + error instanceof Error ? error.message : error, + ); } }; @@ -86,10 +94,10 @@ if (filesWithLocalStorage.length > 0) { console.info( '\x1b[34m%s\x1b[0m', - '\nInfo: Consider using custom hook functions.' + '\nInfo: Consider using custom hook functions.', ); console.info( - 'Please use the getItem, setItem, and removeItem functions provided by the custom hook useLocalStorage.\n' + 'Please use the getItem, setItem, and removeItem functions provided by the custom hook useLocalStorage.\n', ); process.exit(1);