Skip to content

Commit

Permalink
Fixed ESLint bugs that required ts-specific rules for js functions an…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
yugal07 authored Jan 22, 2025
1 parent bdee2c7 commit f9fdea1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand All @@ -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,
);
}
};

Expand All @@ -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);
Expand Down

0 comments on commit f9fdea1

Please sign in to comment.