Skip to content

Commit

Permalink
[compiler:eslint] Don't crash if hermes parser fails to parse
Browse files Browse the repository at this point in the history
Eslint rules should never throw, so if we fail to parse with Babel or
Hermes, we should just ignore the error. This should fix issues such as
trying to run the eslint rule on non tsx|ts|jsx|js files, Hermes parser
not supporting certain JS syntax, etc.

I didn't add a test for this as our eslint-rule-tester config uses
hermes-eslint parser, so it wasn't possible to add a top level await as
it would crash hermes-eslint before our rule was triggered. Similarly I
couldn't add a test for non-JS files as it would not be parseable by
hermes-eslint.

Fixes #29107

ghstack-source-id: 60afcdb89ab4a8d2e4697cc50c5490803e7cbeac
Pull Request resolved: #29631
  • Loading branch information
poteto committed May 31, 2024
1 parent adbec0c commit 113c8e7
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ const rule: Rule.RuleModule = {
});
} catch {}
} else {
babelAST = HermesParser.parse(sourceCode, {
babel: true,
enableExperimentalComponentSyntax: true,
sourceFilename: filename,
sourceType: "module",
});
try {
babelAST = HermesParser.parse(sourceCode, {
babel: true,
enableExperimentalComponentSyntax: true,
sourceFilename: filename,
sourceType: "module",
});
} catch {}
}

if (babelAST != null) {
Expand Down

0 comments on commit 113c8e7

Please sign in to comment.