Skip to content

createPrinter.printFile() should not remove empty lines #20506

@Maximaximum

Description

@Maximaximum

I'd expect createPrinter.printFile() to format the file, but not remove the empty lines inside the file. Empty lines are meaningful, because the developers often use an empty line to structure the code into identifiable blocks. For example, to separate the import declarations from the rest of the file.

TypeScript Version: 2.7.0-dev.20171206

Code

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

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

foo();

`, ScriptTarget.Latest);

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

Expected behavior:
output.ts should contain given text:

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

foo();

Actual behavior:
output.ts contains given text:

import { smth } from "dfsdf";
import { smth2 } from "dfsdf2";
foo();

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