Skip to content

Commit b65e6fc

Browse files
authored
Revert [eprh] Remove hermes-parser (facebook#34747)
Adds back HermesParser to eslint-plugin-react-hooks. There are still [external users of Flow](facebook#34719 (comment)) using the plugin, so we shouldn't break the plugin for them. However, we still have the problem of double parsing: once from eslint (which we discard) and then another via babel/hermes parser. In the long run we should investigate a translation layer from estree to babel (or alternatively, update the compiler to take estree as input). But for now, I am reverting the PR. This does mean that [Sandpack in react.dev](https://github.com/reactjs/react.dev/blob/11cb6b591571caf5fa2a192117b6a6445c3f2027/src/components/MDX/Sandpack/runESLint.tsx#L31) cannot update to the latest eprh as HermesParser does not appear to be able to be run in a browser. I discovered this while trying to update eprh on react.dev last week, but didn't investigate deeply. I'll need to double check that again to find out more.
1 parent c786258 commit b65e6fc

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/eslint-plugin-react-hooks/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"dependencies": {
4242
"@babel/core": "^7.24.4",
4343
"@babel/parser": "^7.24.4",
44+
"hermes-parser": "^0.25.1",
4445
"zod": "^3.22.4 || ^4.0.0",
4546
"zod-validation-error": "^3.0.3 || ^4.0.0"
4647
},

packages/eslint-plugin-react-hooks/src/shared/RunReactCompiler.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import BabelPluginReactCompiler, {
1717
LoggerEvent,
1818
} from 'babel-plugin-react-compiler';
1919
import type {SourceCode} from 'eslint';
20+
import * as HermesParser from 'hermes-parser';
2021
import {isDeepStrictEqual} from 'util';
2122
import type {ParseResult} from '@babel/parser';
2223

@@ -113,14 +114,28 @@ function runReactCompilerImpl({
113114
}
114115

115116
let babelAST: ParseResult<File> | null = null;
116-
try {
117-
babelAST = babelParse(sourceCode.text, {
118-
sourceFilename: filename,
119-
sourceType: 'unambiguous',
120-
plugins: ['typescript', 'jsx'],
121-
});
122-
} catch (err: unknown) {
123-
/* empty */
117+
118+
if (filename.endsWith('.tsx') || filename.endsWith('.ts')) {
119+
try {
120+
babelAST = babelParse(sourceCode.text, {
121+
sourceFilename: filename,
122+
sourceType: 'unambiguous',
123+
plugins: ['typescript', 'jsx'],
124+
});
125+
} catch {
126+
/* empty */
127+
}
128+
} else {
129+
try {
130+
babelAST = HermesParser.parse(sourceCode.text, {
131+
babel: true,
132+
enableExperimentalComponentSyntax: true,
133+
sourceFilename: filename,
134+
sourceType: 'module',
135+
});
136+
} catch {
137+
/* empty */
138+
}
124139
}
125140

126141
if (babelAST != null) {

0 commit comments

Comments
 (0)