Skip to content

Commit 512ad28

Browse files
committed
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.
1 parent 78507f4 commit 512ad28

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ async function expectTransformation(input: string, expected: string): Promise<vo
1616
const reporter = new RefactorReporter(logger);
1717
const transformed = transformJasmineToVitest('spec.ts', input, reporter);
1818
const formattedTransformed = await format(transformed, { parser: 'typescript' });
19-
let formattedExpected = await format(expected, { parser: 'typescript' });
20-
// Strip blank lines to avoid test failures due to cosmetic differences.
21-
formattedExpected = formattedExpected.replace(/\n\s*\n/g, '\n');
19+
const formattedExpected = await format(expected, { parser: 'typescript' });
2220

2321
expect(formattedTransformed).toBe(formattedExpected);
2422
}

packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ import {
4141
import { RefactorContext } from './utils/refactor-context';
4242
import { RefactorReporter } from './utils/refactor-reporter';
4343

44+
const BLANK_LINE_PLACEHOLDER = '// __PRESERVE_BLANK_LINE__';
45+
46+
function preserveBlankLines(content: string): string {
47+
return content
48+
.split('\n')
49+
.map((line) => (line.trim() === '' ? BLANK_LINE_PLACEHOLDER : line))
50+
.join('\n');
51+
}
52+
53+
function restoreBlankLines(content: string): string {
54+
const regex = new RegExp(`^\\s*${BLANK_LINE_PLACEHOLDER.replace(/\//g, '\\/')}\\s*$`, 'gm');
55+
56+
return content.replace(regex, '');
57+
}
58+
4459
/**
4560
* Transforms a string of Jasmine test code to Vitest test code.
4661
* This is the main entry point for the transformation.
@@ -53,9 +68,11 @@ export function transformJasmineToVitest(
5368
content: string,
5469
reporter: RefactorReporter,
5570
): string {
71+
const contentWithPlaceholders = preserveBlankLines(content);
72+
5673
const sourceFile = ts.createSourceFile(
5774
filePath,
58-
content,
75+
contentWithPlaceholders,
5976
ts.ScriptTarget.Latest,
6077
true,
6178
ts.ScriptKind.TS,
@@ -151,6 +168,7 @@ export function transformJasmineToVitest(
151168
}
152169

153170
const printer = ts.createPrinter();
171+
const transformedContentWithPlaceholders = printer.printFile(result.transformed[0]);
154172

155-
return printer.printFile(result.transformed[0]);
173+
return restoreBlankLines(transformedContentWithPlaceholders);
156174
}

0 commit comments

Comments
 (0)