Skip to content

Commit e62e159

Browse files
michaelfaithIanVSfbartho
authored
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 <ian.vanschooten@gmail.com> Co-authored-by: Frederic Barthelemy <github@fbartho.com>
1 parent 16d8a5a commit e62e159

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/preprocessors/preprocessor.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ export function preprocessor(code: string, options: PrettierOptions): string {
2222
parserOptions.allowReturnOutsideFunction = true;
2323
}
2424

25-
const ast = babelParser(code, parserOptions);
25+
let ast: ReturnType<typeof babelParser>;
26+
try {
27+
ast = babelParser(code, parserOptions);
28+
} catch (_) {
29+
console.error(
30+
' [error] [prettier-plugin-sort-imports]: import sorting aborted due to babel parsing error.',
31+
);
32+
return code;
33+
}
2634

2735
const directives = ast.program.directives;
2836
const interpreter = ast.program.interpreter;

0 commit comments

Comments
 (0)