Skip to content

Commit 8f63f70

Browse files
committed
Don't add final empty line if file has no contents
Fix prettier#60. Use same logic as prettier's printer-estree.js "Program" case.
1 parent d6499b9 commit 8f63f70

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

src/printer/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,14 @@ function genericPrint(path, options, print) {
472472

473473
switch (n.ast_type) {
474474
case "Module": {
475-
return concat([printBody(path, print, true /*isTopLevel*/), hardline]);
475+
const parts = [printBody(path, print, true /*isTopLevel*/)];
476+
477+
// Only force a trailing newline if there were any contents.
478+
if (n.body.length || n.comments) {
479+
parts.push(hardline);
480+
}
481+
482+
return concat(parts);
476483
}
477484

478485
case "AsyncFunctionDef":
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`empty_file.py 1`] = `
4+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
6+
`;
7+
8+
exports[`empty_file.py 2`] = `
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
11+
`;
12+
13+
exports[`single_space.py 1`] = `
14+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
16+
`;
17+
18+
exports[`single_space.py 2`] = `
19+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
21+
`;

tests/python_empty/empty_file.py

Whitespace-only changes.

tests/python_empty/jsfmt.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run_spec(__dirname, ["python"], { pythonVersion: "2" });
2+
run_spec(__dirname, ["python"], { pythonVersion: "3" });

tests/python_empty/single_space.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)