elm-optimize-level-2/test/helpers/transformCode.ts

31 lines
665 B
TypeScript

import ts from 'typescript';
export function transformCode(
initialCode: string,
expectedCode: string,
transformer: ts.TransformerFactory<ts.SourceFile>
): {
actual: string;
expected: string;
} {
const source = ts.createSourceFile(
'elm.js',
initialCode,
ts.ScriptTarget.ES2018
);
const printer = ts.createPrinter();
const [output] = ts.transform(source, [transformer]).transformed;
const expectedOutput = printer.printFile(
ts.createSourceFile('elm.js', expectedCode, ts.ScriptTarget.ES2018)
);
const printedOutput = printer.printFile(output);
return {
actual: printedOutput,
expected: expectedOutput,
};
}