Skip to content

Way to use transformations API while preserving whitespace in emitted TS? #25801

@cyungmann

Description

@cyungmann

Is there a way to use the transformations API to transform a TS file and then emit it, all while preserving existing whitespace/formatting in the file? If I do something like the following, all blank lines get removed (I believe the pretty printer does this?):

const result = ts.transform(file, [transformerFactory]);
const newFile = result.transformed[0];

if (ts.isSourceFile(newFile)) {
    const newContent = printer.printFile(newFile);
    ts.sys.writeFile(newFile.fileName, newContent);
}

If I do newFile.getText() or newFile.getFullText(), the resulting string does not include any of the changes from my transformation.

My ultimate goal is to rewrite a bunch of imports of the form import { foo } from '../../Enum/foo'; to import { foo } from '../../Enum/enums'. It'd be nice to coalesce all the imports from the same file as well, but first things first :D

P.S. I hope this is an appropriate forum for this question. It looked like questions are disallowed with the exception of questions about the compiler API.

Thanks,
Chris

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions