Skip to content

Commit f9fdea1

Browse files
authored
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
1 parent bdee2c7 commit f9fdea1

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

.github/workflows/pull-request.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ jobs:
6565
run: npm run check-tsdoc # Run the TSDoc check script
6666

6767
- name: Check for localStorage Usage
68-
run: |
69-
chmod +x scripts/githooks/check-localstorage-usage.js
70-
node scripts/githooks/check-localstorage-usage.js --scan-entire-repo
68+
run: npx tsx scripts/githooks/check-localstorage-usage.ts --scan-entire-repo
7169

7270
- name: Compare translation files
7371
run: |

.lintstagedrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"**/*.{ts,tsx,yml}": "eslint --fix",
33
"**/*.{ts,tsx,json,scss,css,yml}": "prettier --write",
4-
"**/*.{ts,tsx}": "node scripts/githooks/check-localstorage-usage.js"
4+
"**/*.{ts,tsx}": "npx tsx scripts/githooks/check-localstorage-usage.ts"
55
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"update:toc": "node scripts/githooks/update-toc.js",
9191
"lint-staged": "lint-staged --concurrent false",
9292
"setup": "tsx setup.ts",
93-
"check-localstorage": "node scripts/githooks/check-localstorage-usage.js",
93+
"check-localstorage": "tsx scripts/githooks/check-localstorage-usage.ts",
9494
"postgenerate-docs": "find docs/docs/auto-docs -name 'README.md' -delete && node fix-repo-url.js",
9595
"generate-docs": "typedoc && npm run postgenerate-docs && node fix-readme-links.js"
9696
},

scripts/githooks/check-localstorage-usage.js renamed to scripts/githooks/check-localstorage-usage.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,49 @@
33
import { readFileSync, existsSync } from 'fs';
44
import path from 'path';
55
import { execSync } from 'child_process';
6+
import type { ExecSyncOptionsWithStringEncoding } from 'child_process';
67

7-
const args = process.argv.slice(2);
8-
const scanEntireRepo = args.includes('--scan-entire-repo');
8+
const args: string[] = process.argv.slice(2);
9+
const scanEntireRepo: boolean = args.includes('--scan-entire-repo');
910

10-
const containsSkipComment = (file) => {
11+
const containsSkipComment = (file: string): boolean => {
1112
try {
1213
const content = readFileSync(file, 'utf-8');
1314
return content.includes('// SKIP_LOCALSTORAGE_CHECK');
1415
} catch (error) {
15-
console.error(`Error reading file ${file}:`, error.message);
16+
console.error(
17+
`Error reading file ${file}:`,
18+
error instanceof Error ? error.message : error,
19+
);
1620
return false;
1721
}
1822
};
1923

20-
const getModifiedFiles = () => {
24+
const getModifiedFiles = (): string[] => {
2125
try {
26+
const options: ExecSyncOptionsWithStringEncoding = { encoding: 'utf-8' };
27+
2228
if (scanEntireRepo) {
23-
const result = execSync('git ls-files | grep ".tsx\\?$"', {
24-
encoding: 'utf-8',
25-
});
29+
const result = execSync('git ls-files | grep ".tsx\\?$"', options);
2630
return result.trim().split('\n');
2731
}
2832

29-
const result = execSync('git diff --cached --name-only', {
30-
encoding: 'utf-8',
31-
});
33+
const result = execSync('git diff --cached --name-only', options);
3234
return result.trim().split('\n');
3335
} catch (error) {
34-
console.error('Error fetching modified files:', error.message);
36+
console.error(
37+
'Error fetching modified files:',
38+
error instanceof Error ? error.message : error,
39+
);
3540
process.exit(1);
3641
}
42+
return [];
3743
};
3844

39-
const files = getModifiedFiles();
40-
41-
const filesWithLocalStorage = [];
45+
const files: string[] = getModifiedFiles();
46+
const filesWithLocalStorage: string[] = [];
4247

43-
const checkLocalStorageUsage = (file) => {
48+
const checkLocalStorageUsage = (file: string): void => {
4449
if (!file) {
4550
return;
4651
}
@@ -49,7 +54,7 @@ const checkLocalStorageUsage = (file) => {
4954

5055
// Skip files with specific names or containing a skip comment
5156
if (
52-
fileName === 'check-localstorage-usage.js' ||
57+
fileName === 'check-localstorage-usage.ts' || // Updated extension
5358
fileName === 'useLocalstorage.test.ts' ||
5459
fileName === 'useLocalstorage.ts' ||
5560
containsSkipComment(file)
@@ -73,7 +78,10 @@ const checkLocalStorageUsage = (file) => {
7378
console.log(`File ${file} does not exist.`);
7479
}
7580
} catch (error) {
76-
console.error(`Error reading file ${file}:`, error.message);
81+
console.error(
82+
`Error reading file ${file}:`,
83+
error instanceof Error ? error.message : error,
84+
);
7785
}
7886
};
7987

@@ -86,10 +94,10 @@ if (filesWithLocalStorage.length > 0) {
8694

8795
console.info(
8896
'\x1b[34m%s\x1b[0m',
89-
'\nInfo: Consider using custom hook functions.'
97+
'\nInfo: Consider using custom hook functions.',
9098
);
9199
console.info(
92-
'Please use the getItem, setItem, and removeItem functions provided by the custom hook useLocalStorage.\n'
100+
'Please use the getItem, setItem, and removeItem functions provided by the custom hook useLocalStorage.\n',
93101
);
94102

95103
process.exit(1);

0 commit comments

Comments
 (0)