TypeScript AST transformation removes all blank lines

Workaround:

  • replace empty lines with comment
  • transform
  • replace comments with empty line

    import {decodeEmptyLines, encodeEmptyLines} from 'ts-empty-line-encoder';
    
    let sourceCode = editor.document.getText();
    //encode empty lines
    sourceCode = encodeEmptyLines(sourceCode);
    const sourceFile = ts.createSourceFile(editor.document.fileName, sourceCode, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
    const transformation = ts.transform(sourceFile, [organizeTransformer]);
    sourceCode = transformation.transformed[0].getFullText();
    //decode empty lines
    sourceCode = decodeEmptyLines(sourceCode);