Skip to content

Commit

Permalink
fix: catch babel parse errors (#198)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Frederic Barthelemy <[email protected]>
  • Loading branch information
3 people authored Jan 13, 2025
1 parent 16d8a5a commit e62e159
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export function preprocessor(code: string, options: PrettierOptions): string {
parserOptions.allowReturnOutsideFunction = true;
}

const ast = babelParser(code, parserOptions);
let ast: ReturnType<typeof babelParser>;
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;
Expand Down

0 comments on commit e62e159

Please sign in to comment.