Skip to content

Commit 7f70a87

Browse files
Refactor ESLint integration for improved readability and error handling
1 parent 310d5dc commit 7f70a87

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/components/MDX/Sandpack/runESLint.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,34 @@ const getCodeMirrorPosition = (
2121

2222
const linter = new Linter();
2323

24-
const reactRules = require('eslint-plugin-react-hooks').rules;
25-
linter.defineRules({
26-
'react-hooks/rules-of-hooks': reactRules['rules-of-hooks'],
27-
'react-hooks/exhaustive-deps': reactRules['exhaustive-deps'],
28-
});
24+
let reactRules;
25+
try {
26+
reactRules = require('eslint-plugin-react-hooks').rules;
27+
if (reactRules) {
28+
linter.defineRules({
29+
'react-hooks/rules-of-hooks': reactRules['rules-of-hooks'],
30+
'react-hooks/exhaustive-deps': reactRules['exhaustive-deps'],
31+
});
32+
}
33+
} catch (e) {
34+
console.warn('Could not load react-hooks rules:', e);
35+
// Define empty rules as fallback
36+
reactRules = {};
37+
}
2938

3039
const options = {
3140
parserOptions: {
3241
ecmaVersion: 12,
3342
sourceType: 'module',
3443
ecmaFeatures: {jsx: true},
3544
},
36-
rules: {
37-
'react-hooks/rules-of-hooks': 'error',
38-
'react-hooks/exhaustive-deps': 'error',
39-
},
45+
rules:
46+
reactRules['rules-of-hooks'] && reactRules['exhaustive-deps']
47+
? {
48+
'react-hooks/rules-of-hooks': 'error',
49+
'react-hooks/exhaustive-deps': 'error',
50+
}
51+
: {},
4052
};
4153

4254
export const runESLint = (

0 commit comments

Comments
 (0)