From e62e15946c57ba110d1c4f967304610d2cf9fa31 Mon Sep 17 00:00:00 2001 From: michael faith Date: Mon, 13 Jan 2025 08:17:08 -0600 Subject: [PATCH] fix: catch babel parse errors (#198) This change updates the plugin to catch babel parsing errors, so that we can give prettier a chance to throw. We're still writing an error to console, so that in the event that prettier doesn't end up throwing, we're still indicating that the import sorting failed. ![screenshot of new behavior](https://github.com/user-attachments/assets/8ce5daaf-e496-4260-85d0-6753b137f9eb) Closes #197 --------- Co-authored-by: Ian VanSchooten Co-authored-by: Frederic Barthelemy --- src/preprocessors/preprocessor.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/preprocessors/preprocessor.ts b/src/preprocessors/preprocessor.ts index 63f8a0d..5f5d2f5 100644 --- a/src/preprocessors/preprocessor.ts +++ b/src/preprocessors/preprocessor.ts @@ -22,7 +22,15 @@ export function preprocessor(code: string, options: PrettierOptions): string { parserOptions.allowReturnOutsideFunction = true; } - const ast = babelParser(code, parserOptions); + let ast: ReturnType; + try { + ast = babelParser(code, parserOptions); + } catch (_) { + console.error( + ' [error] [prettier-plugin-sort-imports]: import sorting aborted due to babel parsing error.', + ); + return code; + } const directives = ast.program.directives; const interpreter = ast.program.interpreter;