Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ async function expectTransformation(input: string, expected: string): Promise<vo
const reporter = new RefactorReporter(logger);
const transformed = transformJasmineToVitest('spec.ts', input, reporter);
const formattedTransformed = await format(transformed, { parser: 'typescript' });
let formattedExpected = await format(expected, { parser: 'typescript' });
// Strip blank lines to avoid test failures due to cosmetic differences.
formattedExpected = formattedExpected.replace(/\n\s*\n/g, '\n');
const formattedExpected = await format(expected, { parser: 'typescript' });

expect(formattedTransformed).toBe(formattedExpected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ import {
import { RefactorContext } from './utils/refactor-context';
import { RefactorReporter } from './utils/refactor-reporter';

const BLANK_LINE_PLACEHOLDER = '// __PRESERVE_BLANK_LINE__';

function preserveBlankLines(content: string): string {
return content
.split('\n')
.map((line) => (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.
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}