From e2012f5ed7d352a1f2726056a09f0208357df914 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 22 Oct 2025 16:20:38 -0400 Subject: [PATCH] fix(@schematics/angular): preserve blank lines in jasmine-to-vitest schematic The TypeScript printer, by design, does not retain blank lines. This caused the jasmine-to-vitest refactoring schematic to produce code that lost the original file's vertical spacing, harming readability. This commit introduces a pre- and post-processing step within the main transformation function. Before parsing, blank lines are converted into unique placeholder comments. After the TypeScript printer generates the new code, these placeholders are converted back into blank lines, ensuring that the vertical formatting of the original file is preserved in the transformed output. --- .../test-file-transformer.integration_spec.ts | 4 +--- .../jasmine-vitest/test-file-transformer.ts | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts index a440eadbbf48..60e2675b7572 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts @@ -16,9 +16,7 @@ async function expectTransformation(input: string, expected: string): Promise (line.trim() === '' ? BLANK_LINE_PLACEHOLDER : line)) + .join('\n'); +} + +function restoreBlankLines(content: string): string { + const regex = new RegExp(`^\\s*${BLANK_LINE_PLACEHOLDER.replace(/\//g, '\\/')}\\s*$`, 'gm'); + + return content.replace(regex, ''); +} + /** * Transforms a string of Jasmine test code to Vitest test code. * This is the main entry point for the transformation. @@ -53,9 +68,11 @@ export function transformJasmineToVitest( content: string, reporter: RefactorReporter, ): string { + const contentWithPlaceholders = preserveBlankLines(content); + const sourceFile = ts.createSourceFile( filePath, - content, + contentWithPlaceholders, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS, @@ -151,6 +168,7 @@ export function transformJasmineToVitest( } const printer = ts.createPrinter(); + const transformedContentWithPlaceholders = printer.printFile(result.transformed[0]); - return printer.printFile(result.transformed[0]); + return restoreBlankLines(transformedContentWithPlaceholders); }