Ts-morph: SourceFile.formatText() seems to remove all blank lines

Created on 5 Dec 2017  路  14Comments  路  Source: dsherret/ts-morph

If I add some blank lines using this.sourceFile.addStatements(writer => writer.newLine()); (no matter if at the end of the file or somewhere in the middle), and then call SourceFile.formatText(), all the blank lines get removed.

It feels like it should leave the blank lines.

bug

All 14 comments

Hmmm... it must be the typescript compiler doing this. Internally I'm basically just using the code outlined in my answer here. There might not be an easy solution to this at the moment.

Strangely enough, but when I run this code:

import { createPrinter, createSourceFile, ScriptTarget, NewLineKind } from 'typescript';
import * as fs from 'fs';

const printer = createPrinter({
    newLine: NewLineKind.LineFeed,
    removeComments: false
});

const file = createSourceFile('./aaa.ts', `
    import    {smth} from 'dfsdf';
    import {smth2} from 'dfsdf2';

    foo();

`, ScriptTarget.ES2017);

printer.printFile(file);
fs.writeFileSync(file.fileName, file.getFullText());

I get the following output:


    import    {smth} from 'dfsdf';
    import {smth2} from 'dfsdf2';

    foo();

I.e., nothing gets formatted at all! Am I missing something?

I might be mistaken, but it seems like this is the line responsible for removing the lines: https://github.com/dsherret/ts-simple-ast/blob/master/src/compiler/file/SourceFile.ts#L499

Try replacing:

printer.printFile(file);
fs.writeFileSync(file.fileName, file.getFullText());

With:

fs.writeFileSync(file.fileName, printer.printFile(file));

Indeed, you're right. printer.printFile() is responsible for this

I've opened a bug at TypeScript page: https://github.com/Microsoft/TypeScript/issues/20506

I'm not sure it is a bug. The printer essentially rewrites the entire file from the nodes in the AST (it doesn't seem to look at the whitespace) so maybe it's by design that it ignores blank lines. It will still be interesting to see what they say though.

By the way, I'm thinking to change this method name from .formatText() to .prettyPrint() and then sometime in the future implement a proper .formatText() method.

It seems I've found an even worse problem with this method :)
When I add any statements after calling formatText(), they don't get added to the output text file:

    exportsFile.formatText();
    exportsFile.addStatements(writer => writer.newLine().newLine().writeLine('something();'));
    this.ast.saveUnsavedSourceFilesSync();

No newlines, no something(); statement. If I remove the exportsFile.formatText();, everything is ok.

How do I modify the file contents after calling formatText()?

That's a bug. Not sure why that's happening. Can you open a new issue? (or I'll do it tonight)

Hey, I've received a hint at the TypeScript repo that in order to format a file one should rather use the formatting API, not the printer. I guess this is indeed what we need? https://github.com/vvakame/typescript-formatter/blob/master/lib/formatter.ts#L21

Oh, never knew about that formatting api. Yeah, that looks like what we need here. I'll try to get something in tonight or tomorrow night.

@Maximaximum btw, it's the languageService.getFormattingEditsForDocument that does the job (so it is part of the compiler)

Fixed in v2.0.0.

Was this page helpful?
0 / 5 - 0 ratings